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 EdgeFunctionsVariables = { projectRef?: string } export type EdgeFunctionsResponse = components['schemas']['FunctionResponse_Output'] export async function getEdgeFunctions( { projectRef }: EdgeFunctionsVariables, signal?: AbortSignal ) { if (!projectRef) throw new Error('projectRef is required') const { data, error } = await get(`/v1/projects/{ref}/functions`, { params: { path: { ref: projectRef } }, signal, }) if (error) handleError(error) return data } export type EdgeFunctionsData = Awaited> export type EdgeFunctionsError = ResponseError export const useEdgeFunctionsQuery = ( { projectRef }: EdgeFunctionsVariables, { enabled = true, ...options }: UseCustomQueryOptions = {} ) => useQuery({ queryKey: edgeFunctionsKeys.list(projectRef), queryFn: ({ signal }) => getEdgeFunctions({ projectRef }, signal), enabled: enabled && typeof projectRef !== 'undefined', ...options, })