tooot/src/utils/queryHooks/instance.ts

37 lines
884 B
TypeScript
Raw Normal View History

2021-02-20 19:12:44 +01:00
import apiGeneral from '@api/general'
2021-01-07 19:13:09 +01:00
import { AxiosError } from 'axios'
2021-12-18 19:59:38 +01:00
import { QueryFunctionContext, useQuery, UseQueryOptions } from 'react-query'
2021-01-07 19:13:09 +01:00
2021-12-18 19:59:38 +01:00
export type QueryKeyInstance = ['Instance', { domain?: string }]
2021-01-07 19:13:09 +01:00
2021-12-18 19:59:38 +01:00
const queryFunction = async ({
queryKey
}: QueryFunctionContext<QueryKeyInstance>) => {
2021-02-20 19:12:44 +01:00
const { domain } = queryKey[1]
if (!domain) {
return Promise.reject()
}
2021-01-07 19:13:09 +01:00
2021-02-20 19:12:44 +01:00
const res = await apiGeneral<Mastodon.Instance>({
2021-01-07 19:13:09 +01:00
method: 'get',
2021-02-20 19:12:44 +01:00
domain: domain,
url: `api/v1/instance`
})
return res.body
2021-01-07 19:13:09 +01:00
}
2021-12-18 19:59:38 +01:00
const useInstanceQuery = ({
2021-01-07 19:13:09 +01:00
options,
...queryKeyParams
2021-12-18 19:59:38 +01:00
}: QueryKeyInstance[1] & {
2021-01-22 01:34:20 +01:00
options?: UseQueryOptions<
Mastodon.Instance & { publicAllow?: boolean },
2021-12-18 19:59:38 +01:00
AxiosError
2021-01-22 01:34:20 +01:00
>
2021-01-07 19:13:09 +01:00
}) => {
2021-12-18 19:59:38 +01:00
const queryKey: QueryKeyInstance = ['Instance', { ...queryKeyParams }]
2021-01-07 19:13:09 +01:00
return useQuery(queryKey, queryFunction, options)
}
2021-01-11 21:36:57 +01:00
export { useInstanceQuery }