2021-01-07 19:13:09 +01:00
|
|
|
import client from '@api/client'
|
|
|
|
import { AxiosError } from 'axios'
|
2021-01-27 00:35:34 +01:00
|
|
|
import * as AuthSession from 'expo-auth-session'
|
2021-01-07 19:13:09 +01:00
|
|
|
import { useQuery, UseQueryOptions } from 'react-query'
|
|
|
|
|
|
|
|
export type QueryKey = ['Apps', { instanceDomain?: string }]
|
|
|
|
|
|
|
|
const queryFunction = ({ queryKey }: { queryKey: QueryKey }) => {
|
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-01-07 19:13:09 +01:00
|
|
|
const { instanceDomain } = queryKey[1]
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
|
|
|
return client<Mastodon.Apps>({
|
|
|
|
method: 'post',
|
|
|
|
instance: 'remote',
|
|
|
|
instanceDomain,
|
|
|
|
url: `apps`,
|
|
|
|
body: formData
|
2021-02-11 01:33:31 +01:00
|
|
|
}).then(res => res.body)
|
2021-01-07 19:13:09 +01:00
|
|
|
}
|
|
|
|
|
2021-01-11 21:36:57 +01:00
|
|
|
const useAppsQuery = <TData = Mastodon.Apps>({
|
2021-01-07 19:13:09 +01:00
|
|
|
options,
|
|
|
|
...queryKeyParams
|
|
|
|
}: QueryKey[1] & {
|
|
|
|
options?: UseQueryOptions<Mastodon.Apps, AxiosError, TData>
|
|
|
|
}) => {
|
|
|
|
const queryKey: QueryKey = ['Apps', { ...queryKeyParams }]
|
|
|
|
return useQuery(queryKey, queryFunction, options)
|
|
|
|
}
|
|
|
|
|
2021-01-11 21:36:57 +01:00
|
|
|
export { useAppsQuery }
|