mirror of
https://github.com/supabase/supabase.git
synced 2026-07-03 04:24:20 +08:00
* Add custom types for queries, mutations and infinite queries. * Migrate all queries to use the new type. * Migrate all infinite queries to useCustomInfiniteQueryOptions. * Migrate all mutations to use useCustomMutationOptions. * Add type to all imports in `types` folder.
48 lines
1.5 KiB
TypeScript
48 lines
1.5 KiB
TypeScript
import { useQuery } from '@tanstack/react-query'
|
|
import { components } from 'api-types'
|
|
import { get, handleError } from 'data/fetchers'
|
|
import { IS_PLATFORM } from 'lib/constants'
|
|
import type { ResponseError, UseCustomQueryOptions } from 'types'
|
|
import { edgeFunctionsKeys } from './keys'
|
|
|
|
export type EdgeFunctionVariables = {
|
|
projectRef?: string
|
|
slug?: string
|
|
}
|
|
|
|
export type EdgeFunction = components['schemas']['FunctionSlugResponse']
|
|
|
|
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 = ResponseError
|
|
|
|
export const useEdgeFunctionQuery = <TData = EdgeFunctionData>(
|
|
{ projectRef, slug }: EdgeFunctionVariables,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseCustomQueryOptions<EdgeFunctionData, EdgeFunctionError, TData> = {}
|
|
) =>
|
|
useQuery<EdgeFunctionData, EdgeFunctionError, TData>({
|
|
queryKey: edgeFunctionsKeys.detail(projectRef, slug),
|
|
queryFn: ({ signal }) => getEdgeFunction({ projectRef, slug }, signal),
|
|
enabled:
|
|
IS_PLATFORM && enabled && typeof projectRef !== 'undefined' && typeof slug !== 'undefined',
|
|
...options,
|
|
})
|