mirror of
https://github.com/supabase/supabase.git
synced 2026-05-31 18:03:33 +08:00
deploy edge function mutaton (#33371)
* deploy edge function mutaton * update url * use openapi-fetch POST in the deploy mutation. --------- Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
This commit is contained in:
@@ -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,
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -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',
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user