diff --git a/src/routes/_api/search.js b/src/routes/_api/search.js index 40f00044..290f71b5 100644 --- a/src/routes/_api/search.js +++ b/src/routes/_api/search.js @@ -1,8 +1,8 @@ import { get, paramsString, DEFAULT_TIMEOUT } from '../_utils/ajax' import { auth, basename } from './utils' -export function search (instanceName, accessToken, query, resolve = true, limit = 5, signal = null) { - const url = `${basename(instanceName)}/api/v1/search?` + paramsString({ +function doSearch (version, instanceName, accessToken, query, resolve, limit, signal) { + const url = `${basename(instanceName)}/api/${version}/search?` + paramsString({ q: query, resolve, limit @@ -12,3 +12,29 @@ export function search (instanceName, accessToken, query, resolve = true, limit signal }) } + +async function doSearchV1 (instanceName, accessToken, query, resolve, limit, signal) { + const resp = await doSearch('v1', instanceName, accessToken, query, resolve, limit, signal) + resp.hashtags = resp.hashtags && resp.hashtags.map(tag => ({ + name: tag, + url: `${basename(instanceName)}/tags/${tag.toLowerCase()}`, + history: [] + })) + return resp +} + +async function doSearchV2 (instanceName, accessToken, query, resolve, limit, signal) { + return doSearch('v2', instanceName, accessToken, query, resolve, limit, signal) +} + +export async function search (instanceName, accessToken, query, resolve = true, limit = 5, signal = null) { + try { + return (await doSearchV2(instanceName, accessToken, query, resolve, limit, signal)) + } catch (err) { + if (err && err.status === 404) { // fall back to old search API + return doSearchV1(instanceName, accessToken, query, resolve, limit, signal) + } else { + throw err + } + } +} diff --git a/src/routes/_components/search/HashtagSearchResult.html b/src/routes/_components/search/HashtagSearchResult.html index 846da9f8..fb6423b1 100644 --- a/src/routes/_components/search/HashtagSearchResult.html +++ b/src/routes/_components/search/HashtagSearchResult.html @@ -1,5 +1,5 @@ - - {'#' + hashtag} + + {'#' + hashtag.name} @@ -10,4 +10,4 @@ SearchResult } } - \ No newline at end of file + diff --git a/src/routes/_utils/ajax.js b/src/routes/_utils/ajax.js index 82116a50..81e678db 100644 --- a/src/routes/_utils/ajax.js +++ b/src/routes/_utils/ajax.js @@ -23,6 +23,11 @@ function makeFetchOptions (method, headers, options) { } async function throwErrorIfInvalidResponse (response) { + if (response.status >= 300) { + const err = new Error('Request failed: ' + response.status) + err.status = response.status + throw err + } const json = await response.json() if (response.status >= 200 && response.status < 300) { return json