This commit is contained in:
xmflsct 2023-03-12 19:45:09 +01:00
parent 1df73b070a
commit 63900c4338
1 changed files with 11 additions and 2 deletions

View File

@ -9,13 +9,22 @@ export type QueryKeyInstance = ['Instance'] | ['Instance', { domain?: string }]
const queryFunction = async ({ queryKey }: QueryFunctionContext<QueryKeyInstance>) => { const queryFunction = async ({ queryKey }: QueryFunctionContext<QueryKeyInstance>) => {
const domain = queryKey[1]?.domain const domain = queryKey[1]?.domain
const checkV2Format = (body: Mastodon.Instance_V2) => {
if (body.version) {
return body
} else {
throw new Error('Instance v2 format error')
}
}
if (domain) { if (domain) {
return await apiGeneral<Mastodon.Instance<'v2'>>({ return await apiGeneral<Mastodon.Instance<'v2'>>({
method: 'get', method: 'get',
domain, domain,
url: 'api/v2/instance' url: 'api/v2/instance'
}) })
.then(res => res.body) .then(res => checkV2Format(res.body))
.catch( .catch(
async () => async () =>
await apiGeneral<Mastodon.Instance<'v1'>>({ await apiGeneral<Mastodon.Instance<'v1'>>({
@ -32,7 +41,7 @@ const queryFunction = async ({ queryKey }: QueryFunctionContext<QueryKeyInstance
version: 'v2', version: 'v2',
url: 'instance' url: 'instance'
}) })
.then(res => res.body) .then(res => checkV2Format(res.body))
.catch( .catch(
async () => async () =>
await apiInstance<Mastodon.Instance<'v1'>>({ await apiInstance<Mastodon.Instance<'v1'>>({