mirror of
https://github.com/supabase/supabase.git
synced 2026-07-07 11:40:20 +08:00
59 lines
1.7 KiB
TypeScript
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) throw 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])
|
|
}
|