mirror of
https://github.com/supabase/supabase.git
synced 2026-07-03 19:54:38 +08:00
* feat: add IPv4 add on direct conn notice * feat: add session mode warning * fix: remove statement from Supavisor-related types * Revert change in api types file * Revert change in api types file * Update API types file via codegen * Smol feex * Temp lint fix * Fix * fix: include serverless mention --------- Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
53 lines
1.8 KiB
TypeScript
53 lines
1.8 KiB
TypeScript
import { UseQueryOptions, useQuery } from '@tanstack/react-query'
|
|
import type { components } from 'data/api'
|
|
import { get, handleError } from 'data/fetchers'
|
|
import type { ResponseError } from 'types'
|
|
import { databaseFunctionsKeys } from './keys'
|
|
|
|
export type DatabaseFunctionsVariables = {
|
|
projectRef?: string
|
|
connectionString?: string
|
|
}
|
|
|
|
export type DatabaseFunction = components['schemas']['PostgresFunction']
|
|
|
|
export async function getDatabaseFunctions(
|
|
{ projectRef, connectionString }: DatabaseFunctionsVariables,
|
|
signal?: AbortSignal
|
|
) {
|
|
if (!projectRef) throw new Error('projectRef is required')
|
|
|
|
let headers = new Headers()
|
|
if (connectionString) headers.set('x-connection-encrypted', connectionString)
|
|
|
|
const { data, error } = await get('/platform/pg-meta/{ref}/functions', {
|
|
// @ts-ignore [Joshen] Temp, seems like API codegen is wrong
|
|
params: { path: { ref: projectRef } },
|
|
headers,
|
|
signal,
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
// [Joshen] API codegen is wrong, its matching Edge functions type to database functions
|
|
return data as unknown as DatabaseFunction[]
|
|
}
|
|
|
|
export type DatabaseFunctionsData = Awaited<ReturnType<typeof getDatabaseFunctions>>
|
|
export type DatabaseFunctionsError = ResponseError
|
|
|
|
export const useDatabaseFunctionsQuery = <TData = DatabaseFunctionsData>(
|
|
{ projectRef, connectionString }: DatabaseFunctionsVariables,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseQueryOptions<DatabaseFunctionsData, DatabaseFunctionsError, TData> = {}
|
|
) =>
|
|
useQuery<DatabaseFunctionsData, DatabaseFunctionsError, TData>(
|
|
databaseFunctionsKeys.list(projectRef),
|
|
({ signal }) => getDatabaseFunctions({ projectRef, connectionString }, signal),
|
|
{
|
|
enabled: enabled && typeof projectRef !== 'undefined',
|
|
...options,
|
|
}
|
|
)
|