Skip to content

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

deploy edge function mutaton (#33371)

Saxon Fletcherd6245f4c0821
diff --git a/apps/studio/data/edge-functions/edge-functions-deploy-mutation.ts b/apps/studio/data/edge-functions/edge-functions-deploy-mutation.ts
new file mode 100644
index 00000000000..0f1b5bcaa57
--- /dev/null
+++ b/apps/studio/data/edge-functions/edge-functions-deploy-mutation.ts
@@ -0,0 +1,82 @@
+import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
+import { toast } from 'sonner'
+
+import { handleError, post } from 'data/fetchers'
+import type { ResponseError } from 'types'
+import { edgeFunctionsKeys } from './keys'
+
+export type EdgeFunctionsDeployVariables = {
+ projectRef: string
+ metadata: {
+ entrypoint_path?: string
+ import_map_path?: string
+ name?: string
+ static_patterns?: string[]
+ verify_jwt?: boolean
+ }
+ files: { name: string; content: string }[]
+}
+
+export async function deployEdgeFunction({
+ projectRef,
+ metadata,
+ files,
+}: EdgeFunctionsDeployVariables) {
+ if (!projectRef) throw new Error('projectRef is required')
+
+ const { data, error } = await post(`/v1/projects/{ref}/functions/deploy`, {
+ params: { path: { ref: projectRef }, query: { slug: metadata.name } },
+ body: {
+ file: files as any,
+ metadata: metadata,
+ },
+ bodySerializer(body) {
+ const formData = new FormData()
+
+ formData.append('metadata', JSON.stringify(body.metadata))
+
+ body.file.forEach((f: any) => {
+ const file = f as { name: string; content: string }
+ const blob = new Blob([file.content], { type: 'text/plain' })
+ formData.append('file', blob, file.name)
+ })
+
+ return formData
+ },
+ })
+
+ if (error) handleError(error)
+ return data
+}
+
+type EdgeFunctionsDeployData = Awaited<ReturnType<typeof deployEdgeFunction>>
+
+export const useEdgeFunctionDeployMutation = ({
+ onSuccess,
+ onError,
+ ...options
+}: Omit<
+ UseMutationOptions<EdgeFunctionsDeployData, ResponseError, EdgeFunctionsDeployVariables>,
+ 'mutationFn'
+> = {}) => {
+ const queryClient = useQueryClient()
+
+ return useMutation<EdgeFunctionsDeployData, ResponseError, EdgeFunctionsDeployVariables>(
+ (vars) => deployEdgeFunction(vars),
+ {
+ async onSuccess(data, variables, context) {
+ const { projectRef } = variables
+ await Promise.all([queryClient.invalidateQueries(edgeFunctionsKeys.list(projectRef))])
+ await onSuccess?.(data, variables, context)
+ },
+ async onError(data, variables, context) {
+ if (onError === undefined) {
+ toast.error(`Failed to deploy edge function: ${data.message}`)
+ } else {
+ onError(data, variables, context)
+ }
+ },
+ ...options,
+ }
+ )
+}
diff --git a/apps/studio/data/fetchers.ts b/apps/studio/data/fetchers.ts
index 5d472a1cc71..b9e42e1097c 100644
--- a/apps/studio/data/fetchers.ts
+++ b/apps/studio/data/fetchers.ts
@@ -7,7 +7,6 @@ import { ResponseError } from 'types'
import type { paths } from './api' // generated from openapi-typescript
const DEFAULT_HEADERS = {
- 'Content-Type': 'application/json',
Accept: 'application/json',
}