mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
## What
Moves the `ResponseError` class (`types/base.ts`) and the classified
error classes (`types/api-errors.ts`) into
`lib/errors/studio-errors.ts`, and renames `ResponseError` to
`StudioError`.
## Why
Studio's error classification is being expanded to cover errors that
don't come from API responses (thrown network failures, aborted
requests, unexpected client errors). The API-specific name and location
no longer fit. Landing the rename on its own keeps the follow-up PR,
which introduces the classified error model, small and reviewable.
## Notes for reviewers
- Mechanical change, no behavior change. Most of the diff is a one-line
import change per file plus import re-sorting by Prettier.
- Files that only use the type now use `import type { StudioError }`.
- `ResponseError` is no longer re-exported from `@/types`; there are no
compatibility shims at the old paths.
- Unrelated identifiers like `QueryResponseError` and
`parseResponseErrors` are unchanged.
98 lines
3.1 KiB
TypeScript
98 lines
3.1 KiB
TypeScript
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { components } from 'api-types'
|
|
import { toast } from 'sonner'
|
|
|
|
import { storageKeys } from './keys'
|
|
import { patch } from '@/data/fetchers'
|
|
import type { StudioError } from '@/lib/errors/studio-errors'
|
|
import type { UseCustomMutationOptions } from '@/types'
|
|
|
|
type BucketUpdateVariables = {
|
|
projectRef: string
|
|
id: string
|
|
isPublic: boolean
|
|
file_size_limit: number | null
|
|
allowed_mime_types: string[] | null
|
|
/** Omitted when versioning isn't changing. There is no going back to DISABLED. */
|
|
versioning_status?: components['schemas']['UpdateStorageBucketBody']['versioning_status']
|
|
}
|
|
|
|
// [Alaister]: API accept null values for allowed_mime_types and file_size_limit to reset
|
|
type UpdateStorageBucketBody = Omit<
|
|
components['schemas']['UpdateStorageBucketBody'],
|
|
'allowed_mime_types' | 'file_size_limit'
|
|
> & {
|
|
allowed_mime_types: string[] | null
|
|
file_size_limit: number | null
|
|
}
|
|
|
|
async function updateBucket({
|
|
projectRef,
|
|
id,
|
|
isPublic,
|
|
file_size_limit,
|
|
allowed_mime_types,
|
|
versioning_status,
|
|
}: BucketUpdateVariables): Promise<BucketUpdateResult> {
|
|
if (!projectRef) throw new Error('projectRef is required')
|
|
if (!id) throw new Error('Bucket name is required')
|
|
|
|
const payload: Partial<UpdateStorageBucketBody> = { public: isPublic }
|
|
if (file_size_limit !== undefined) payload.file_size_limit = file_size_limit
|
|
if (allowed_mime_types !== undefined) payload.allowed_mime_types = allowed_mime_types
|
|
if (versioning_status !== undefined) payload.versioning_status = versioning_status
|
|
|
|
const { data, error } = await patch('/platform/storage/{ref}/buckets/{id}', {
|
|
params: { path: { id, ref: projectRef } },
|
|
body: payload as any,
|
|
})
|
|
|
|
if (error) {
|
|
// Return the error instead of throwing it, so we can handle it gracefully
|
|
return { data: null, error }
|
|
}
|
|
|
|
return { data, error: null }
|
|
}
|
|
|
|
type BucketUpdateResult = { data: any; error: null } | { data: null; error: any }
|
|
type BucketUpdateData = Awaited<ReturnType<typeof updateBucket>>
|
|
|
|
export const useBucketUpdateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<BucketUpdateData, StudioError, BucketUpdateVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<BucketUpdateData, StudioError, BucketUpdateVariables>({
|
|
mutationFn: async (vars) => {
|
|
const result = await updateBucket(vars)
|
|
if (result.error) {
|
|
throw result.error
|
|
}
|
|
return result.data
|
|
},
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
// `refetchType: 'all'` because the list query is inactive on the bucket page.
|
|
await queryClient.invalidateQueries({
|
|
queryKey: storageKeys.buckets(projectRef),
|
|
refetchType: 'all',
|
|
})
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to update bucket: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|