tooot/src/utils/queryHooks/instance.ts

35 lines
852 B
TypeScript
Raw Normal View History

2021-01-07 19:13:09 +01:00
import client from '@api/client'
import { AxiosError } from 'axios'
import { useQuery, UseQueryOptions } from 'react-query'
2021-02-09 01:16:12 +01:00
export type QueryKey = ['Instance', { instanceDomain?: string }]
2021-01-07 19:13:09 +01:00
2021-02-09 01:16:12 +01:00
const queryFunction = ({ queryKey }: { queryKey: QueryKey }) => {
const { instanceDomain } = queryKey[1]
2021-01-07 19:13:09 +01:00
2021-02-09 01:16:12 +01:00
return client<Mastodon.Instance>({
2021-01-07 19:13:09 +01:00
method: 'get',
instance: 'remote',
instanceDomain,
url: `instance`
2021-02-11 01:33:31 +01:00
}).then(res => 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 }