import { useQuery } from '@tanstack/react-query' import { components } from 'api-types' import { edgeFunctionsKeys } from './keys' import { get, handleError } from '@/data/fetchers' import type { ResponseError, UseCustomQueryOptions } from '@/types' 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> export type EdgeFunctionError = ResponseError export const useEdgeFunctionQuery = ( { projectRef, slug }: EdgeFunctionVariables, { enabled = true, ...options }: UseCustomQueryOptions = {} ) => useQuery({ queryKey: edgeFunctionsKeys.detail(projectRef, slug), queryFn: ({ signal }) => getEdgeFunction({ projectRef, slug }, signal), enabled: enabled && typeof projectRef !== 'undefined' && typeof slug !== 'undefined', ...options, })