mirror of
https://github.com/supabase/supabase.git
synced 2026-10-12 12:55:06 +03:00
* Add custom types for queries, mutations and infinite queries. * Migrate all queries to use the new type. * Migrate all infinite queries to useCustomInfiniteQueryOptions. * Migrate all mutations to use useCustomMutationOptions. * Add type to all imports in `types` folder.
61 lines
1.6 KiB
TypeScript
61 lines
1.6 KiB
TypeScript
import { useMutation } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { components } from 'data/api'
|
|
import { handleError, post } from 'data/fetchers'
|
|
import type { ResponseError, UseCustomMutationOptions } from 'types'
|
|
|
|
type SignBucketObjectParams = {
|
|
projectRef: string
|
|
bucketId?: string
|
|
path: string
|
|
expiresIn: number
|
|
options?: components['schemas']['GetSignedUrlBody']['options']
|
|
}
|
|
export const signBucketObject = async (
|
|
{ projectRef, bucketId, path, expiresIn, options }: SignBucketObjectParams,
|
|
signal?: AbortSignal
|
|
) => {
|
|
if (!bucketId) throw new Error('bucketId is required')
|
|
|
|
const { data, error } = await post('/platform/storage/{ref}/buckets/{id}/objects/sign', {
|
|
params: {
|
|
path: {
|
|
ref: projectRef,
|
|
id: bucketId,
|
|
},
|
|
},
|
|
body: { path, expiresIn, options },
|
|
signal,
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
return data
|
|
}
|
|
|
|
type SignBucketObjectData = Awaited<ReturnType<typeof signBucketObject>>
|
|
|
|
export const useGetSignBucketObjectMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<SignBucketObjectData, ResponseError, SignBucketObjectParams>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
return useMutation<SignBucketObjectData, ResponseError, SignBucketObjectParams>({
|
|
mutationFn: (vars) => signBucketObject(vars),
|
|
async onSuccess(data, variables, context) {
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to get sign bucket object: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|