Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Refactor the StorageExplorerStore to use API calls from the data folder #23222

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions apps/studio/data/storage/bucket-object-delete-mutation.ts
@@ -0,0 +1,37 @@
import { useMutation } from '@tanstack/react-query'

import { del, handleError } from 'data/fetchers'

type DownloadBucketObjectParams = {
projectRef: string
bucketId?: string
paths: string[]
}
export const deleteBucketObject = async (
{ projectRef, bucketId, paths }: DownloadBucketObjectParams,
signal?: AbortSignal
) => {
if (!bucketId) throw new Error('bucketId is required')

const { data, error } = await del('/platform/storage/{ref}/buckets/{id}/objects', {
params: {
path: {
ref: projectRef,
id: bucketId,
},
},
body: {
paths,
},
signal,
})

if (error) handleError(error)
return data
}

export function useBucketObjectDeleteMutation() {
return useMutation({
mutationFn: deleteBucketObject,
})
}
Comment on lines +33 to +37
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For all these new mutation functions is there a reason why we're not passing through the options like in the other mutations? Makes it easy to pass onSuccess() from outside for example

37 changes: 37 additions & 0 deletions apps/studio/data/storage/bucket-object-download-mutation.ts
@@ -0,0 +1,37 @@
import { useMutation } from '@tanstack/react-query'

import { components } from 'data/api'
import { post } from 'lib/common/fetch'
import { API_URL } from 'lib/constants'

type DownloadBucketObjectParams = {
projectRef: string
bucketId?: string
path: string
options?: components['schemas']['DownloadObjectOptions']
}
export const downloadBucketObject = async (
{ projectRef, bucketId, path, options }: DownloadBucketObjectParams,
signal?: AbortSignal
) => {
if (!bucketId) throw new Error('bucketId is required')

// has to use lib/common/fetch post because the other post doesn't support wrapping blobs
const response = await post(
`${API_URL}/storage/${projectRef}/buckets/${bucketId}/objects/download`,
{
path,
options,
abortSignal: signal,
}
)

if (response.error) throw response.error
return response
}

export function useBucketObjectDownloadMutation() {
return useMutation({
mutationFn: downloadBucketObject,
})
}
@@ -0,0 +1,37 @@
import { useMutation } from '@tanstack/react-query'

import { components } from 'data/api'
import { handleError, post } from 'data/fetchers'

type getPublicUrlForBucketObjectParams = {
projectRef: string
bucketId?: string
path: string
options?: components['schemas']['PublicUrlOptions']
}
export const getPublicUrlForBucketObject = async (
{ projectRef, bucketId, path, options }: getPublicUrlForBucketObjectParams,
signal?: AbortSignal
) => {
if (!bucketId) throw new Error('bucketId is required')

const { data, error } = await post('/platform/storage/{ref}/buckets/{id}/objects/public-url', {
params: {
path: {
ref: projectRef,
id: bucketId,
},
},
body: { path, options },
signal,
})

if (error) handleError(error)
return data
}

export function useBucketObjectGetPublicUrlMutation() {
return useMutation({
mutationFn: getPublicUrlForBucketObject,
})
}
38 changes: 38 additions & 0 deletions apps/studio/data/storage/bucket-object-sign-mutation.ts
@@ -0,0 +1,38 @@
import { useMutation } from '@tanstack/react-query'

import { components } from 'data/api'
import { handleError, post } from 'data/fetchers'

type signBucketObjectParams = {
projectRef: string
bucketId?: string
path: string
expiresIn: number
options?: components['schemas']['SignedUrlOptions']
}
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
}

export function useSignBucketObjectMutation() {
return useMutation({
mutationFn: signBucketObject,
})
}
43 changes: 43 additions & 0 deletions apps/studio/data/storage/bucket-objects-list-mutation.ts
@@ -0,0 +1,43 @@
import { useMutation } from '@tanstack/react-query'

import { components } from 'data/api'
import { handleError, post } from 'data/fetchers'

type ListBucketObjectsParams = {
projectRef: string
bucketId?: string
path: string
options: components['schemas']['StorageObjectSearchOptions']
}

export type StorageObject = components['schemas']['StorageObject']

export const listBucketObjects = async (
{ projectRef, bucketId, path, options }: ListBucketObjectsParams,
signal?: AbortSignal
) => {
if (!bucketId) throw new Error('bucketId is required')

const { data, error } = await post('/platform/storage/{ref}/buckets/{id}/objects/list', {
params: {
path: {
ref: projectRef,
id: bucketId,
},
},
body: {
path,
options,
},
signal,
})

if (error) handleError(error)
return data
}

export function useBucketObjectsListMutation() {
return useMutation({
mutationFn: listBucketObjects,
})
}
40 changes: 40 additions & 0 deletions apps/studio/data/storage/object-move-mutation.ts
@@ -0,0 +1,40 @@
import { useMutation } from '@tanstack/react-query'

import { handleError, post } from 'data/fetchers'

type MoveStorageObjectParams = {
projectRef: string
bucketId?: string
from: string
to: string
}
export const moveStorageObject = async ({
projectRef,
bucketId,
from,
to,
}: MoveStorageObjectParams) => {
if (!bucketId) throw new Error('bucketId is required')

const { data, error } = await post('/platform/storage/{ref}/buckets/{id}/objects/move', {
params: {
path: {
ref: projectRef,
id: bucketId,
},
},
body: {
from,
to,
},
})

if (error) handleError(error)
return data
}

export function useBucketObjectsMoveMutation() {
return useMutation({
mutationFn: moveStorageObject,
})
}