Files
supabase/studio/data/storage/bucket-delete-mutation.ts
T

57 lines
1.7 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'react-hot-toast'
import { delete_, post } from 'lib/common/fetch'
import { API_URL } from 'lib/constants'
import { ResponseError } from 'types'
import { storageKeys } from './keys'
export type BucketDeleteVariables = {
projectRef: string
id: string
}
export async function deleteBucket({ projectRef, id }: BucketDeleteVariables) {
if (!projectRef) throw new Error('projectRef is required')
if (!id) throw new Error('Bucket name is requried')
const emptyBucketRes = await post(`${API_URL}/storage/${projectRef}/buckets/${id}/empty`, {})
if (emptyBucketRes.error) throw emptyBucketRes.error
const response = await delete_(`${API_URL}/storage/${projectRef}/buckets/${id}`)
if (response.error) throw response.error
return response
}
type BucketDeleteData = Awaited<ReturnType<typeof deleteBucket>>
export const useBucketDeleteMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<BucketDeleteData, ResponseError, BucketDeleteVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<BucketDeleteData, ResponseError, BucketDeleteVariables>(
(vars) => deleteBucket(vars),
{
async onSuccess(data, variables, context) {
const { projectRef } = variables
await queryClient.invalidateQueries(storageKeys.buckets(projectRef))
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to delete bucket: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
}
)
}