Skip to content

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

Update bucket methods to use data fetchers (#33551)

Joshen Lim561e39ddc4c1
diff --git a/apps/studio/data/storage/bucket-create-mutation.ts b/apps/studio/data/storage/bucket-create-mutation.ts
index 352e585fc29..444dfdb125b 100644
--- a/apps/studio/data/storage/bucket-create-mutation.ts
+++ b/apps/studio/data/storage/bucket-create-mutation.ts
@@ -1,8 +1,8 @@
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
-import { post } from 'lib/common/fetch'
-import { API_URL } from 'lib/constants'
+import { components } from 'api-types'
+import { handleError, post } from 'data/fetchers'
import type { ResponseError } from 'types'
import { storageKeys } from './keys'
@@ -14,6 +14,8 @@ export type BucketCreateVariables = {
allowed_mime_types: string[] | null
}
+type CreateStorageBucketBody = components['schemas']['CreateStorageBucketBody']
+
export async function createBucket({
projectRef,
id,
@@ -22,16 +24,21 @@ export async function createBucket({
allowed_mime_types,
}: BucketCreateVariables) {
if (!projectRef) throw new Error('projectRef is required')
- if (!id) throw new Error('Bucket name is requried')
+ if (!id) throw new Error('Bucket name is required')
+
+ const payload: Partial<CreateStorageBucketBody> = { id, public: isPublic }
+ if (file_size_limit) payload.file_size_limit = file_size_limit
+ if (allowed_mime_types) payload.allowed_mime_types
- const response = await post(`${API_URL}/storage/${projectRef}/buckets`, {
- id,
- public: isPublic,
- file_size_limit,
- allowed_mime_types,
+ const { data, error } = await post('/platform/storage/{ref}/buckets', {
+ params: { path: { ref: projectRef } },
+ body: payload as CreateStorageBucketBody,
})
- if (response.error) throw response.error
- return response
+
+ if (error) handleError(error)
+ // @ts-expect-error API type is wrong here
+ // https://github.com/supabase/infrastructure/blob/develop/api/src/routes/platform/storage/ref/buckets/buckets.controller.ts#L55
+ return data as { name: string }
}
type BucketCreateData = Awaited<ReturnType<typeof createBucket>>
diff --git a/apps/studio/data/storage/bucket-delete-mutation.ts b/apps/studio/data/storage/bucket-delete-mutation.ts
index b71cea5a3d5..2333e5839a9 100644
--- a/apps/studio/data/storage/bucket-delete-mutation.ts
+++ b/apps/studio/data/storage/bucket-delete-mutation.ts
@@ -1,8 +1,7 @@
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
-import { delete_, post } from 'lib/common/fetch'
-import { API_URL } from 'lib/constants'
+import { del, handleError, post } from 'data/fetchers'
import type { ResponseError } from 'types'
import { storageKeys } from './keys'
@@ -15,12 +14,16 @@ 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 { error: emptyBucketError } = await post('/platform/storage/{ref}/buckets/{id}/empty', {
+ params: { path: { ref: projectRef, id } },
+ })
+ if (emptyBucketError) handleError(emptyBucketError)
- const response = await delete_(`${API_URL}/storage/${projectRef}/buckets/${id}`)
- if (response.error) throw response.error
- return response
+ const { data, error: deleteBucketError } = await del('/platform/storage/{ref}/buckets/{id}', {
+ params: { path: { ref: projectRef, id } },
+ })
+ if (deleteBucketError) handleError(deleteBucketError)
+ return data
}
type BucketDeleteData = Awaited<ReturnType<typeof deleteBucket>>
diff --git a/apps/studio/data/storage/bucket-update-mutation.ts b/apps/studio/data/storage/bucket-update-mutation.ts
index 98cb6117e40..7fb4ac94b1e 100644
--- a/apps/studio/data/storage/bucket-update-mutation.ts
+++ b/apps/studio/data/storage/bucket-update-mutation.ts
@@ -1,8 +1,8 @@
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
-import { patch } from 'lib/common/fetch'
-import { API_URL } from 'lib/constants'
+import { components } from 'api-types'
+import { handleError, patch } from 'data/fetchers'
import type { ResponseError } from 'types'
import { storageKeys } from './keys'
@@ -14,6 +14,8 @@ export type BucketUpdateVariables = {
allowed_mime_types: string[] | null
}
+type UpdateStorageBucketBody = components['schemas']['UpdateStorageBucketBody']
+
export async function updateBucket({
projectRef,
id,
@@ -24,13 +26,17 @@ export async function updateBucket({
if (!projectRef) throw new Error('projectRef is required')
if (!id) throw new Error('Bucket name is requried')
- const response = await patch(`${API_URL}/storage/${projectRef}/buckets/${id}`, {
- public: isPublic,
- file_size_limit,
- allowed_mime_types,
+ const payload: Partial<UpdateStorageBucketBody> = { public: isPublic }
+ if (file_size_limit) payload.file_size_limit = file_size_limit
+ if (allowed_mime_types) payload.allowed_mime_types = allowed_mime_types
+
+ const { data, error } = await patch('/platform/storage/{ref}/buckets/{id}', {
+ params: { path: { id, ref: projectRef } },
+ body: payload as UpdateStorageBucketBody,
})
- if (response.error) throw response.error
- return response
+
+ if (error) handleError(error)
+ return data
}
type BucketUpdateData = Awaited<ReturnType<typeof updateBucket>>
diff --git a/apps/studio/data/storage/buckets-query.ts b/apps/studio/data/storage/buckets-query.ts
index e4efcfad08c..1684a4ca86b 100644
--- a/apps/studio/data/storage/buckets-query.ts
+++ b/apps/studio/data/storage/buckets-query.ts
@@ -1,4 +1,5 @@
import { useQuery, UseQueryOptions } from '@tanstack/react-query'
+
import { get, handleError } from 'data/fetchers'
import { useSelectedProject } from 'hooks/misc/useSelectedProject'
import { PROJECT_STATUS } from 'lib/constants'