Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

chore: move edge functions to new fetchers (#26812)

Alaister Young6f6bcb9b08e3
diff --git a/apps/studio/data/edge-functions/edge-function-query.ts b/apps/studio/data/edge-functions/edge-function-query.ts
index fe8d0c0d83d..63f1b03470b 100644
--- a/apps/studio/data/edge-functions/edge-function-query.ts
+++ b/apps/studio/data/edge-functions/edge-function-query.ts
@@ -1,6 +1,6 @@
import { useQuery, UseQueryOptions } from '@tanstack/react-query'
-import { get } from 'lib/common/fetch'
-import { API_ADMIN_URL, IS_PLATFORM } from 'lib/constants'
+import { get, handleError } from 'data/fetchers'
+import { IS_PLATFORM } from 'lib/constants'
import { edgeFunctionsKeys } from './keys'
export type EdgeFunctionVariables = {
@@ -8,19 +8,6 @@ export type EdgeFunctionVariables = {
slug?: string
}
-export type EdgeFunctionResponse = {
- id: string
- slug: string
- name: string
- status: string
- created_at: number
- updated_at: number
- verify_jwt: boolean
- import_map: boolean
- version: number
- import_map_path: string
-}
-
export async function getEdgeFunction(
{ projectRef, slug }: EdgeFunctionVariables,
signal?: AbortSignal
@@ -28,11 +15,13 @@ export async function getEdgeFunction(
if (!projectRef) throw new Error('projectRef is required')
if (!slug) throw new Error('slug is required')
- const response = await get(`${API_ADMIN_URL}/projects/${projectRef}/functions/${slug}`, {
+ const { data, error } = await get(`/v1/projects/{ref}/functions/{function_slug}`, {
+ params: { path: { ref: projectRef, function_slug: slug } },
signal,
})
- if (response.error) throw response.error
- return response as EdgeFunctionResponse
+
+ if (error) handleError(error)
+ return data
}
export type EdgeFunctionData = Awaited<ReturnType<typeof getEdgeFunction>>
diff --git a/apps/studio/data/edge-functions/edge-functions-delete-mutation.ts b/apps/studio/data/edge-functions/edge-functions-delete-mutation.ts
index eb5c2cb5d49..bb0741c5167 100644
--- a/apps/studio/data/edge-functions/edge-functions-delete-mutation.ts
+++ b/apps/studio/data/edge-functions/edge-functions-delete-mutation.ts
@@ -1,8 +1,7 @@
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'react-hot-toast'
-import { delete_ } from 'lib/common/fetch'
-import { API_ADMIN_URL } from 'lib/constants'
+import { del, handleError } from 'data/fetchers'
import type { ResponseError } from 'types'
import { edgeFunctionsKeys } from './keys'
@@ -14,12 +13,14 @@ export type EdgeFunctionsDeleteVariables = {
export async function deleteEdgeFunction({ projectRef, slug }: EdgeFunctionsDeleteVariables) {
if (!projectRef) throw new Error('projectRef is required')
- const response = await delete_(`${API_ADMIN_URL}/projects/${projectRef}/functions/${slug}`, {})
- if (response.error) {
- throw response.error
- }
+ const { data, error } = await del(`/v1/projects/{ref}/functions/{function_slug}`, {
+ params: {
+ path: { ref: projectRef, function_slug: slug },
+ },
+ })
- return response
+ if (error) handleError(error)
+ return data
}
type EdgeFunctionsDeleteData = Awaited<ReturnType<typeof deleteEdgeFunction>>
diff --git a/apps/studio/data/edge-functions/edge-functions-query.ts b/apps/studio/data/edge-functions/edge-functions-query.ts
index 6792b10fe92..3df3688111e 100644
--- a/apps/studio/data/edge-functions/edge-functions-query.ts
+++ b/apps/studio/data/edge-functions/edge-functions-query.ts
@@ -1,22 +1,13 @@
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 { components } from 'api-types'
+import { get, handleError } from 'data/fetchers'
+import { IS_PLATFORM } from 'lib/constants'
import type { ResponseError } from 'types'
+import { edgeFunctionsKeys } from './keys'
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 type EdgeFunctionsResponse = components['schemas']['FunctionResponse']
export async function getEdgeFunctions(
{ projectRef }: EdgeFunctionsVariables,
@@ -24,11 +15,13 @@ export async function getEdgeFunctions(
) {
if (!projectRef) throw new Error('projectRef is required')
- const response = await get(`${API_ADMIN_URL}/projects/${projectRef}/functions`, {
+ const { data, error } = await get(`/v1/projects/{ref}/functions`, {
+ params: { path: { ref: projectRef } },
signal,
})
- if (response.error) throw response.error
- return response as EdgeFunctionsResponse[]
+
+ if (error) handleError(error)
+ return data
}
export type EdgeFunctionsData = Awaited<ReturnType<typeof getEdgeFunctions>>
diff --git a/apps/studio/data/edge-functions/edge-functions-update-mutation.ts b/apps/studio/data/edge-functions/edge-functions-update-mutation.ts
index 2bd6c09e0b6..d45f1a14838 100644
--- a/apps/studio/data/edge-functions/edge-functions-update-mutation.ts
+++ b/apps/studio/data/edge-functions/edge-functions-update-mutation.ts
@@ -1,11 +1,9 @@
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'react-hot-toast'
-import { patch } from 'lib/common/fetch'
-import { API_ADMIN_URL } from 'lib/constants'
+import { handleError, patch } from 'data/fetchers'
import type { ResponseError } from 'types'
import { edgeFunctionsKeys } from './keys'
-
export type EdgeFunctionsUpdateVariables = {
projectRef: string
slug: string
@@ -23,10 +21,15 @@ export async function updateEdgeFunction({
}: EdgeFunctionsUpdateVariables) {
if (!projectRef) throw new Error('projectRef is required')
- const response = await patch(`${API_ADMIN_URL}/projects/${projectRef}/functions/${slug}`, payload)
- if (response.error) throw response.error
+ const { data, error } = await patch(`/v1/projects/{ref}/functions/{function_slug}`, {
+ params: {
+ path: { ref: projectRef, function_slug: slug },
+ },
+ body: payload,
+ })
- return response
+ if (error) handleError(error)
+ return data
}
type EdgeFunctionsUpdateData = Awaited<ReturnType<typeof updateEdgeFunction>>
@@ -46,7 +49,10 @@ export const useEdgeFunctionUpdateMutation = ({
{
async onSuccess(data, variables, context) {
const { projectRef, slug } = variables
- await queryClient.invalidateQueries(edgeFunctionsKeys.detail(projectRef, slug))
+ await Promise.all([
+ queryClient.invalidateQueries(edgeFunctionsKeys.detail(projectRef, slug)),
+ queryClient.invalidateQueries(edgeFunctionsKeys.list(projectRef)),
+ ])
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {