mirror of
https://github.com/supabase/supabase.git
synced 2026-07-04 19:24:53 +08:00
* Add check before generating nodes in settings infrastructure * Fix conditional in ResourceContent and add some typing * Wrap checkIfPrivate with try catch * Add ? in TableEditor.utils generateTableFieldsFromPostgresTable * More typing * Add ternary in RPC * Remove mobx from API docs v1 * Fix renaming folder in storage explorer
57 lines
1.6 KiB
TypeScript
57 lines
1.6 KiB
TypeScript
import { useQuery, UseQueryOptions } from '@tanstack/react-query'
|
|
|
|
import { components } from 'api-types'
|
|
import { get, handleError } from 'data/fetchers'
|
|
import type { ResponseError } from 'types'
|
|
import { configKeys } from './keys'
|
|
|
|
export type ProjectApiVariables = {
|
|
projectRef?: string
|
|
}
|
|
|
|
export type AutoApiService = components['schemas']['AutoApiService']
|
|
|
|
export type ProjectApiResponse = {
|
|
autoApiService: AutoApiService
|
|
}
|
|
|
|
export async function getProjectApi({ projectRef }: ProjectApiVariables, signal?: AbortSignal) {
|
|
if (!projectRef) throw new Error('projectRef is required')
|
|
|
|
const { data, error } = await get('/platform/props/project/{ref}/api', {
|
|
params: { path: { ref: projectRef } },
|
|
signal,
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
return data
|
|
}
|
|
|
|
export type ProjectApiData = Awaited<ReturnType<typeof getProjectApi>>
|
|
export type ProjectApiError = ResponseError
|
|
|
|
export const useProjectApiQuery = <TData = ProjectApiData>(
|
|
{ projectRef }: ProjectApiVariables,
|
|
{ enabled = true, ...options }: UseQueryOptions<ProjectApiData, ProjectApiError, TData> = {}
|
|
) =>
|
|
useQuery<ProjectApiData, ProjectApiError, TData>(
|
|
configKeys.api(projectRef),
|
|
({ signal }) => getProjectApi({ projectRef }, signal),
|
|
{
|
|
enabled: enabled && typeof projectRef !== 'undefined',
|
|
refetchInterval(data, query) {
|
|
if (!data) {
|
|
return false
|
|
}
|
|
|
|
const { autoApiService } = data as unknown as ProjectApiData
|
|
|
|
const apiKeys = autoApiService?.service_api_keys ?? []
|
|
const interval = apiKeys.length === 0 ? 2000 : 0
|
|
|
|
return interval
|
|
},
|
|
...options,
|
|
}
|
|
)
|