mirror of
https://github.com/supabase/supabase.git
synced 2026-07-07 07:30:21 +08:00
46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
import { useQuery, UseQueryOptions } from '@tanstack/react-query'
|
|
import { get } from 'lib/common/fetch'
|
|
import { API_ADMIN_URL, IS_PLATFORM } from 'lib/constants'
|
|
import { edgeFunctionsKeys } from './keys'
|
|
import type { ResponseError } from 'types'
|
|
|
|
export type EdgeFunctionsVariables = { projectRef?: string }
|
|
|
|
export type EdgeFunctionsResponse = {
|
|
id: string
|
|
name: string
|
|
slug: string
|
|
status: string
|
|
version: number
|
|
created_at: number
|
|
updated_at: number
|
|
verify_jwt: boolean
|
|
import_map: boolean
|
|
}
|
|
|
|
export async function getEdgeFunctions(
|
|
{ projectRef }: EdgeFunctionsVariables,
|
|
signal?: AbortSignal
|
|
) {
|
|
if (!projectRef) throw new Error('projectRef is required')
|
|
|
|
const response = await get(`${API_ADMIN_URL}/projects/${projectRef}/functions`, {
|
|
signal,
|
|
})
|
|
if (response.error) throw response.error
|
|
return response as EdgeFunctionsResponse[]
|
|
}
|
|
|
|
export type EdgeFunctionsData = Awaited<ReturnType<typeof getEdgeFunctions>>
|
|
export type EdgeFunctionsError = ResponseError
|
|
|
|
export const useEdgeFunctionsQuery = <TData = EdgeFunctionsData>(
|
|
{ projectRef }: EdgeFunctionsVariables,
|
|
{ enabled = true, ...options }: UseQueryOptions<EdgeFunctionsData, EdgeFunctionsError, TData> = {}
|
|
) =>
|
|
useQuery<EdgeFunctionsData, EdgeFunctionsError, TData>(
|
|
edgeFunctionsKeys.list(projectRef),
|
|
({ signal }) => getEdgeFunctions({ projectRef }, signal),
|
|
{ enabled: IS_PLATFORM && enabled && typeof projectRef !== 'undefined', ...options }
|
|
)
|