mirror of
https://github.com/supabase/supabase.git
synced 2026-05-24 20:58:45 +08:00
* First round of wrapping RQ errors with handleError * Remove the throw before the handleError usage. * Make the handling of an API error more versatile. Add logging in Sentry if the error is of unknown type. * Remove throwing of the handleError function. * Add return type to the handleError function to be never so that we're sure it always throws. --------- Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
67 lines
1.8 KiB
TypeScript
67 lines
1.8 KiB
TypeScript
import { useQuery, UseQueryOptions } from '@tanstack/react-query'
|
|
|
|
import { get, handleError } from 'data/fetchers'
|
|
import type { ResponseError } from 'types'
|
|
import { openApiKeys } from './keys'
|
|
|
|
export type OpenAPISpecVariables = {
|
|
projectRef?: string
|
|
}
|
|
|
|
export type OpenAPISpecResponse = {
|
|
data: any
|
|
tables: any[]
|
|
functions: any[]
|
|
}
|
|
|
|
export async function getOpenAPISpec({ projectRef }: OpenAPISpecVariables, signal?: AbortSignal) {
|
|
if (!projectRef) throw new Error('projectRef is required')
|
|
|
|
const { data, error } = await get(`/platform/projects/{ref}/api/rest`, {
|
|
params: { path: { ref: projectRef } },
|
|
signal,
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
|
|
const tables = data.definitions
|
|
? Object.entries(data.definitions).map(([key, table]: any) => ({
|
|
...table,
|
|
name: key,
|
|
fields: Object.entries(table.properties || {}).map(([key, field]: any) => ({
|
|
...field,
|
|
name: key,
|
|
})),
|
|
}))
|
|
: []
|
|
|
|
const functions = data.paths
|
|
? Object.entries(data.paths)
|
|
.map(([path, value]: any) => ({
|
|
...value,
|
|
path,
|
|
name: path.replace('/rpc/', ''),
|
|
}))
|
|
.filter((x) => x.path.includes('/rpc'))
|
|
.sort((a, b) => a.name.localeCompare(b.name))
|
|
: []
|
|
|
|
return { data: data, tables, functions }
|
|
}
|
|
|
|
export type OpenAPISpecData = Awaited<OpenAPISpecResponse>
|
|
export type OpenAPISpecError = ResponseError
|
|
|
|
export const useOpenAPISpecQuery = <TData = OpenAPISpecData>(
|
|
{ projectRef }: OpenAPISpecVariables,
|
|
{ enabled = true, ...options }: UseQueryOptions<OpenAPISpecData, OpenAPISpecError, TData> = {}
|
|
) =>
|
|
useQuery<OpenAPISpecData, OpenAPISpecError, TData>(
|
|
openApiKeys.apiSpec(projectRef),
|
|
({ signal }) => getOpenAPISpec({ projectRef }, signal),
|
|
{
|
|
enabled: enabled && typeof projectRef !== 'undefined',
|
|
...options,
|
|
}
|
|
)
|