tooot/src/utils/queryHooks/apps.ts

41 lines
1.1 KiB
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-01-27 00:35:34 +01:00
import * as AuthSession from 'expo-auth-session'
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 QueryKeyApps = ['Apps', { domain?: string }]
2021-01-07 19:13:09 +01:00
2021-12-18 19:59:38 +01:00
const queryFunction = ({ queryKey }: QueryFunctionContext<QueryKeyApps>) => {
2021-01-28 00:41:53 +01:00
const redirectUri = AuthSession.makeRedirectUri({
native: 'tooot://instance-auth',
useProxy: false
})
2021-01-16 23:30:25 +01:00
2021-02-20 19:12:44 +01:00
const { domain } = queryKey[1]
2021-01-07 19:13:09 +01:00
const formData = new FormData()
2021-01-22 01:34:20 +01:00
formData.append('client_name', 'tooot')
2021-01-16 23:30:25 +01:00
formData.append('website', 'https://tooot.app')
formData.append('redirect_uris', redirectUri)
2021-01-07 19:13:09 +01:00
formData.append('scopes', 'read write follow push')
2021-02-20 19:12:44 +01:00
return apiGeneral<Mastodon.Apps>({
2021-01-07 19:13:09 +01:00
method: 'post',
2021-02-20 19:12:44 +01:00
domain: domain || '',
url: `api/v1/apps`,
2021-01-07 19:13:09 +01:00
body: formData
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 useAppsQuery = ({
2021-01-07 19:13:09 +01:00
options,
...queryKeyParams
2021-12-18 19:59:38 +01:00
}: QueryKeyApps[1] & {
options?: UseQueryOptions<Mastodon.Apps, AxiosError>
2021-01-07 19:13:09 +01:00
}) => {
2021-12-18 19:59:38 +01:00
const queryKey: QueryKeyApps = ['Apps', { ...queryKeyParams }]
2021-01-07 19:13:09 +01:00
return useQuery(queryKey, queryFunction, options)
}
2021-01-11 21:36:57 +01:00
export { useAppsQuery }