tooot/src/utils/queryHooks/instance.ts

38 lines
890 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'
import { useQuery, UseQueryOptions } from 'react-query'
2021-02-20 19:12:44 +01:00
export type QueryKey = ['Instance', { domain?: string }]
2021-01-07 19:13:09 +01:00
2021-02-20 19:12:44 +01:00
const queryFunction = async ({ queryKey }: { queryKey: QueryKey }) => {
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-01-22 01:34:20 +01:00
const useInstanceQuery = <
TData = Mastodon.Instance & { publicAllow?: boolean }
>({
2021-01-07 19:13:09 +01:00
options,
...queryKeyParams
}: QueryKey[1] & {
2021-01-22 01:34:20 +01:00
options?: UseQueryOptions<
Mastodon.Instance & { publicAllow?: boolean },
AxiosError,
TData
>
2021-01-07 19:13:09 +01:00
}) => {
const queryKey: QueryKey = ['Instance', { ...queryKeyParams }]
return useQuery(queryKey, queryFunction, options)
}
2021-01-11 21:36:57 +01:00
export { useInstanceQuery }