Files
supabase/apps/studio/data/storage/versioning/object-version-delete-mutation.ts
T
Claude ea3f383d60 feat(storage): fetch and mutate object versions against Storage
The object list endpoint now takes `noncurrentVersions`, `deleteMarkers`
and `exactMatch`, and returns `version`, `archived_at` and
`is_delete_marker` per row, so the version history has a real source.

- `objectVersionsQueryOptions` lists one object's history, with
  `exactMatch` so the path isn't read as a folder prefix
- Restoring is a move onto the object's own path with `sourceVersionId`,
  which consumes the source version rather than duplicating it
- Deleting one version addresses it as `{ path, versionId }`, which is a
  hard delete even on a versioned bucket
- Purging reads the history first and deletes every version by id, since
  a bare path would only hide whatever is current

Variables take the object's full bucket-relative `path` rather than its
leaf name, which is what these endpoints address.

`ObjectVersionAction` drops `restore`: the list endpoint has no
provenance field, so a restore is indistinguishable from an overwrite.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N3euXsz25sTybsGTcxCFfn
2026-10-07 16:38:40 +02:00

69 lines
2.1 KiB
TypeScript

import { useMutation, useQueryClient, type UseMutationOptions } from '@tanstack/react-query'
import { toast } from 'sonner'
import { storageKeys } from '../keys'
import { del, handleError } from '@/data/fetchers'
import type { ResponseError } from '@/types'
export type ObjectVersionDeleteVariables = {
projectRef: string
bucketId: string
/** The object's full path within the bucket, not just its leaf name. */
path: string
/** The single version to remove. Other versions of the object are untouched. */
versionId: string
}
/**
* Addressing a version explicitly is a hard delete even on a versioned bucket —
* a bare path would only hide whatever is currently at it.
*/
async function deleteObjectVersion({
projectRef,
bucketId,
path,
versionId,
}: ObjectVersionDeleteVariables) {
if (!projectRef) throw new Error('projectRef is required')
if (!bucketId) throw new Error('bucketId is required')
if (!path) throw new Error('path is required')
if (!versionId) throw new Error('versionId is required')
const { error } = await del('/platform/storage/{ref}/buckets/{id}/objects', {
params: { path: { ref: projectRef, id: bucketId } },
body: { paths: [{ path, versionId }] },
})
if (error) handleError(error)
}
export const useObjectVersionDeleteMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<void, ResponseError, ObjectVersionDeleteVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<void, ResponseError, ObjectVersionDeleteVariables>({
mutationFn: deleteObjectVersion,
async onSuccess(data, variables, context) {
await queryClient.invalidateQueries({
queryKey: storageKeys.objectVersions(
variables.projectRef,
variables.bucketId,
variables.path
),
})
await onSuccess?.(data, variables, context)
},
async onError(error, variables, context) {
if (onError === undefined) toast.error(`Failed to delete version: ${error.message}`)
else onError(error, variables, context)
},
...options,
})
}