Files
supabase/apps/studio/data/config/project-api-query.ts
Joshen Lim 21890675e5 Chore/sentry fixes 050624 (#27025)
* 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
2024-06-05 16:32:24 +07:00

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,
}
)