Skip to content

Commit

Permalink
feat: add del as alias of delete for hubBlob()
Browse files Browse the repository at this point in the history
  • Loading branch information
atinux committed Mar 7, 2024
1 parent b1e0df7 commit 444c382
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
14 changes: 12 additions & 2 deletions docs/content/docs/2.storage/3.blob.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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
Expand Down
17 changes: 13 additions & 4 deletions src/server/utils/blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -126,14 +126,18 @@ 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 {
return await bucket.delete(decodeURI(pathnames))
}
}
}
return {
...blob,
delete: blob.del
}
}

export function proxyHubBlob(projectUrl: string, secretKey?: string) {
Expand All @@ -144,7 +148,7 @@ export function proxyHubBlob(projectUrl: string, secretKey?: string) {
}
})

return {
const blob = {
async list(options: BlobListOptions = { limit: 1000 }) {
return blobAPI<BlobObject[]>('/', {
method: 'GET',
Expand Down Expand Up @@ -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<void>('/delete', {
method: 'POST',
Expand All @@ -190,6 +194,11 @@ export function proxyHubBlob(projectUrl: string, secretKey?: string) {
return
}
}

return {
...blob,
delete: blob.del
}
}

function getContentType(pathOrExtension?: string) {
Expand Down

0 comments on commit 444c382

Please sign in to comment.