tooot/src/utils/queryHooks/account.ts

27 lines
759 B
TypeScript
Raw Normal View History

2021-02-20 19:12:44 +01:00
import apiInstance from '@api/instance'
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-05-09 21:59:03 +02:00
export type QueryKeyAccount = ['Account', { id: Mastodon.Account['id'] }]
2021-01-07 19:13:09 +01:00
2021-12-18 19:59:38 +01:00
const queryFunction = ({ queryKey }: QueryFunctionContext<QueryKeyAccount>) => {
2021-01-07 19:13:09 +01:00
const { id } = queryKey[1]
2021-02-20 19:12:44 +01:00
return apiInstance<Mastodon.Account>({
2021-01-07 19:13:09 +01:00
method: 'get',
url: `accounts/${id}`
2021-02-11 01:33:31 +01:00
}).then(res => res.body)
2021-01-07 19:13:09 +01:00
}
2021-12-18 19:59:38 +01:00
const useAccountQuery = ({
2021-01-07 19:13:09 +01:00
options,
...queryKeyParams
2021-05-09 21:59:03 +02:00
}: QueryKeyAccount[1] & {
2021-12-18 19:59:38 +01:00
options?: UseQueryOptions<Mastodon.Account, AxiosError>
2021-01-07 19:13:09 +01:00
}) => {
2021-05-09 21:59:03 +02:00
const queryKey: QueryKeyAccount = ['Account', { ...queryKeyParams }]
2021-01-07 19:13:09 +01:00
return useQuery(queryKey, queryFunction, options)
}
2021-01-11 21:36:57 +01:00
export { useAccountQuery }