From 444c382e8a3f826ac07f1385dfaa16b4eaf73a7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Thu, 7 Mar 2024 18:13:52 +0100 Subject: [PATCH] feat: add `del` as alias of `delete` for `hubBlob()` --- docs/content/docs/2.storage/3.blob.md | 14 ++++++++++++-- src/server/utils/blob.ts | 17 +++++++++++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/docs/content/docs/2.storage/3.blob.md b/docs/content/docs/2.storage/3.blob.md index 195853f2..b28e1db5 100644 --- a/docs/content/docs/2.storage/3.blob.md +++ b/docs/content/docs/2.storage/3.blob.md @@ -141,9 +141,9 @@ export default eventHandler(async (event) => { Returns a [`BlobObject`](#blobobject). -### `delete()` +### `del()` -Deletes a blob. +Delete a blob with its pathname. ```ts [server/api/files/[...pathname\\].delete.ts] export default eventHandler(async (event) => { @@ -155,6 +155,16 @@ export default eventHandler(async (event) => { }) ``` +You can also delete multiple blobs at once by providing an array of pathnames: + +```ts +await hubBlob().delete(['images/1.jpg', 'images/2.jpg']) +``` + +::note +You can also use the `delete()` method as alias of `del()`. +:: + #### Params ::field-group diff --git a/src/server/utils/blob.ts b/src/server/utils/blob.ts index fb0df4d1..d3ac31e9 100644 --- a/src/server/utils/blob.ts +++ b/src/server/utils/blob.ts @@ -59,7 +59,7 @@ export function hubBlob() { } const bucket = _useBucket() - return { + const blob = { async list(options: BlobListOptions = { limit: 1000 }) { const resolvedOptions = defu(options, { limit: 500, @@ -126,7 +126,7 @@ export function hubBlob() { return mapR2ObjectToBlob(object) }, - async delete(pathnames: string | string[]) { + async del(pathnames: string | string[]) { if (Array.isArray(pathnames)) { return await bucket.delete(pathnames.map((p) => decodeURI(p))) } else { @@ -134,6 +134,10 @@ export function hubBlob() { } } } + return { + ...blob, + delete: blob.del + } } export function proxyHubBlob(projectUrl: string, secretKey?: string) { @@ -144,7 +148,7 @@ export function proxyHubBlob(projectUrl: string, secretKey?: string) { } }) - return { + const blob = { async list(options: BlobListOptions = { limit: 1000 }) { return blobAPI('/', { method: 'GET', @@ -174,7 +178,7 @@ export function proxyHubBlob(projectUrl: string, secretKey?: string) { }) return JSON.parse(headers.get('x-blob') || '{}') as BlobObject }, - async delete(pathnames: string | string[]) { + async del(pathnames: string | string[]) { if (Array.isArray(pathnames)) { await blobAPI('/delete', { method: 'POST', @@ -190,6 +194,11 @@ export function proxyHubBlob(projectUrl: string, secretKey?: string) { return } } + + return { + ...blob, + delete: blob.del + } } function getContentType(pathOrExtension?: string) {