import { projectAuth } from './setup.ts' import { assertMutationAllowed, readMutationFlags } from './safety.ts' import { projectRequest } from './client.ts' import { optionalBoolean, optionalString, optionalStringArray, requireString, type DryRunResult, type SupabaseAuthInput, } from './types.ts' /** * Delete Storage objects. Requires `confirm: true`, or use `dryRun: true`. * @example * import deleteObject from 'kody:@kody/supabase/delete-object' * const preview = await deleteObject({ * projectRef: 'abcdefghijklmnop', * bucket: 'docs', * paths: ['notes/hello.txt'], * dryRun: true, * }) */ export async function deleteObject( input: SupabaseAuthInput & { bucket: string path?: string paths?: Array dryRun?: boolean confirm?: boolean }, ) { const bucket = requireString(input.bucket, 'bucket') const paths = input.paths ?? (input.path ? [requireString(input.path, 'path')] : undefined) if (!paths || paths.length === 0) { throw new Error('delete-object requires path or paths.') } const allowed = assertMutationAllowed(readMutationFlags(input), 'delete-object') const apiPath = `/storage/v1/object/${encodeURIComponent(bucket)}` if (allowed.dryRun) { const preview: DryRunResult = { dryRun: true, method: 'DELETE', path: apiPath, body: { prefixes: paths }, } return preview } await projectRequest({ auth: projectAuth(input), method: 'DELETE', path: apiPath, body: { prefixes: paths }, }) return { deleted: true, bucket, paths } } /** * Delete Storage objects. Requires `confirm: true`, or use `dryRun: true`. * @example * import deleteObject from 'kody:@kody/supabase/delete-object' * const preview = await deleteObject({ projectRef: 'abcdefghijklmnop', bucket: 'docs', path: 'notes/hello.txt', dryRun: true }) */ export default async function deleteObjectEntrypoint( params: Partial & Record = {}, ) { return await deleteObject({ account: optionalString(params.account, 'account'), secretName: optionalString(params.secretName, 'secretName'), serviceRoleSecretName: optionalString( params.serviceRoleSecretName, 'serviceRoleSecretName', ), projectRef: optionalString(params.projectRef, 'projectRef'), projectUrl: optionalString(params.projectUrl, 'projectUrl'), bucket: requireString(params.bucket, 'bucket'), path: optionalString(params.path, 'path'), paths: optionalStringArray(params.paths, 'paths'), dryRun: optionalBoolean(params.dryRun, 'dryRun'), confirm: optionalBoolean(params.confirm, 'confirm'), }) }