mirror of
https://github.com/supabase/supabase.git
synced 2026-07-04 02:54:28 +08:00
* chore: move edge functions to new fetchers * invalidate edge functions list query cache on update mutation
43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
import { useQuery, UseQueryOptions } from '@tanstack/react-query'
|
|
import { get, handleError } from 'data/fetchers'
|
|
import { IS_PLATFORM } from 'lib/constants'
|
|
import { edgeFunctionsKeys } from './keys'
|
|
|
|
export type EdgeFunctionVariables = {
|
|
projectRef?: string
|
|
slug?: string
|
|
}
|
|
|
|
export async function getEdgeFunction(
|
|
{ projectRef, slug }: EdgeFunctionVariables,
|
|
signal?: AbortSignal
|
|
) {
|
|
if (!projectRef) throw new Error('projectRef is required')
|
|
if (!slug) throw new Error('slug is required')
|
|
|
|
const { data, error } = await get(`/v1/projects/{ref}/functions/{function_slug}`, {
|
|
params: { path: { ref: projectRef, function_slug: slug } },
|
|
signal,
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
return data
|
|
}
|
|
|
|
export type EdgeFunctionData = Awaited<ReturnType<typeof getEdgeFunction>>
|
|
export type EdgeFunctionError = unknown
|
|
|
|
export const useEdgeFunctionQuery = <TData = EdgeFunctionData>(
|
|
{ projectRef, slug }: EdgeFunctionVariables,
|
|
{ enabled = true, ...options }: UseQueryOptions<EdgeFunctionData, EdgeFunctionError, TData> = {}
|
|
) =>
|
|
useQuery<EdgeFunctionData, EdgeFunctionError, TData>(
|
|
edgeFunctionsKeys.detail(projectRef, slug),
|
|
({ signal }) => getEdgeFunction({ projectRef, slug }, signal),
|
|
{
|
|
enabled:
|
|
IS_PLATFORM && enabled && typeof projectRef !== 'undefined' && typeof slug !== 'undefined',
|
|
...options,
|
|
}
|
|
)
|