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> export type ProjectJsonSchemaError = unknown export const useProjectJsonSchemaQuery = ( { projectRef }: ProjectJsonSchemaVariables, { enabled = true, ...options }: UseQueryOptions = {} ) => useQuery( docsKeys.jsonSchema(projectRef), ({ signal }) => getProjectJsonSchema({ projectRef }, signal), { enabled: enabled && typeof projectRef !== 'undefined', ...options, } )