Files
supabase/apps/studio/data/auth/auth-config-query.ts
T
Joshen LimandIvan Vasilov 163263c3c5 First round of wrapping RQ errors with handleError (#26384)
* First round of wrapping RQ errors with handleError

* Remove the throw before the handleError usage.

* Make the handling of an API error more versatile. Add logging in Sentry if the error is of unknown type.

* Remove throwing of the handleError function.

* Add return type to the handleError function to be never so that we're sure it always throws.

---------

Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
2024-05-17 16:30:55 +08:00

59 lines
1.7 KiB
TypeScript

import { useQuery, useQueryClient, UseQueryOptions } from '@tanstack/react-query'
import type { components } from 'data/api'
import { get, handleError } from 'data/fetchers'
import { useCallback } from 'react'
import type { ResponseError } from 'types'
import { authKeys } from './keys'
export type AuthConfigVariables = {
projectRef?: string
}
export type AuthConfigResponse = components['schemas']['GoTrueConfigResponse']
export async function getProjectAuthConfig(
{ projectRef }: AuthConfigVariables,
signal?: AbortSignal
) {
if (!projectRef) throw new Error('projectRef is required')
const { data, error } = await get('/platform/auth/{ref}/config', {
params: { path: { ref: projectRef } },
signal,
})
if (error) handleError(error)
return data
}
export type ProjectAuthConfigData = Awaited<ReturnType<typeof getProjectAuthConfig>>
export type ProjectAuthConfigError = ResponseError
export const useAuthConfigQuery = <TData = ProjectAuthConfigData>(
{ projectRef }: AuthConfigVariables,
{
enabled = true,
...options
}: UseQueryOptions<ProjectAuthConfigData, ProjectAuthConfigError, TData> = {}
) =>
useQuery<ProjectAuthConfigData, ProjectAuthConfigError, TData>(
authKeys.authConfig(projectRef),
({ signal }) => getProjectAuthConfig({ projectRef }, signal),
{
enabled: enabled && typeof projectRef !== 'undefined',
...options,
}
)
export const useAuthConfigPrefetch = ({ projectRef }: AuthConfigVariables) => {
const client = useQueryClient()
return useCallback(() => {
if (projectRef) {
client.prefetchQuery(authKeys.authConfig(projectRef), ({ signal }) =>
getProjectAuthConfig({ projectRef }, signal)
)
}
}, [client, projectRef])
}