Files
supabase/studio/data/open-api/api-spec-query.ts
Joshen Lim 94f5fc5a61 Feat/api side panel (#17880)
* Demo api side panel

* Add docs for storage and edge functions

* Add content

* fix

* Add scroll to logic

* Add storage docs

* Add docs URLs

* Add connecting to project section in getting started

* Link back to docs site for product + resource specific docs

* Add entry points into API side panel for table editor, auth users and storage

* Add entry points into API side panel for edge functions

* Beef up feature preview modal with content

* Fix asset path

* Address several feedback

* Addressing more feedback

* Shift GraphiQL page to database if new api docs flag enabled

* Spelling

* Wording changes

* Spacing and wording

* Fix small issues

* Fix

* Update key showing toggle button

---------

Co-authored-by: Terry Sutton <saltcod@gmail.com>
2023-10-05 13:30:12 +08:00

66 lines
1.8 KiB
TypeScript

import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import { get } from 'data/fetchers'
import { 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) throw 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,
}
)