Files
supabase/apps/studio/data/auth/auth-config-query.ts
T
Joshen LimandAlaister Young b9a0908331 Refactor post calls from lib/common/fetch in auth pages to data/fetchers (#36506)
* Deprecate use of getWithTimeout, refactor BuildingState and RestoringState to use RQ

* Refactor profile-create-mutation to use data/fetchers, and edge-function-status-query to use fetch

* Shift post from lib/common/fetch, refactor bucket-object-download-mutation

* Address feedback

* Minor fix

* Refactor post calls from lib/common/fetch in auth pages to data/fetchers

* Add missing POST users endpoint + small fix when deleting user via context menu

* simplify handleFetchError

* allow handleFetchError to accept unknown

* non-breaking change

---------

Co-authored-by: Alaister Young <a@alaisteryoung.com>
2025-07-21 12:45:53 +08:00

60 lines
1.8 KiB
TypeScript

import { useQuery, useQueryClient, UseQueryOptions } from '@tanstack/react-query'
import type { components } from 'data/api'
import { get, handleError } from 'data/fetchers'
import { IS_PLATFORM } from 'lib/constants'
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 && IS_PLATFORM && 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])
}