mirror of
https://github.com/supabase/supabase.git
synced 2026-06-22 02:02:51 +08:00
44 lines
1.3 KiB
TypeScript
44 lines
1.3 KiB
TypeScript
import { useQuery, UseQueryOptions } from '@tanstack/react-query'
|
|
import { get } from 'lib/common/fetch'
|
|
import { API_URL } from 'lib/constants'
|
|
import { docsKeys } from './keys'
|
|
|
|
export type ProjectJsonSchemaVariables = {
|
|
projectRef?: string
|
|
}
|
|
|
|
export type ProjectJsonSchemaResponse = any
|
|
|
|
export async function getProjectJsonSchema(
|
|
{ projectRef }: ProjectJsonSchemaVariables,
|
|
signal?: AbortSignal
|
|
) {
|
|
if (!projectRef) {
|
|
throw new Error('projectRef is required')
|
|
}
|
|
|
|
const url = `${API_URL}/projects/${projectRef}/api/rest`
|
|
const response = await get(url, { signal })
|
|
if (response.error) throw response.error
|
|
return response as ProjectJsonSchemaResponse
|
|
}
|
|
|
|
export type ProjectJsonSchemaData = Awaited<ReturnType<typeof getProjectJsonSchema>>
|
|
export type ProjectJsonSchemaError = unknown
|
|
|
|
export const useProjectJsonSchemaQuery = <TData = ProjectJsonSchemaData>(
|
|
{ projectRef }: ProjectJsonSchemaVariables,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseQueryOptions<ProjectJsonSchemaData, ProjectJsonSchemaError, TData> = {}
|
|
) =>
|
|
useQuery<ProjectJsonSchemaData, ProjectJsonSchemaError, TData>(
|
|
docsKeys.jsonSchema(projectRef),
|
|
({ signal }) => getProjectJsonSchema({ projectRef }, signal),
|
|
{
|
|
enabled: enabled && typeof projectRef !== 'undefined',
|
|
...options,
|
|
}
|
|
)
|