Skip to content

Commit 714c67e

Browse files
authored
feat(types): export HubBlob interface for external use (#658)
1 parent 3d374d9 commit 714c67e

File tree

2 files changed

+120
-119
lines changed

2 files changed

+120
-119
lines changed

src/runtime/blob/server/utils/blob.ts

Lines changed: 1 addition & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { joinURL } from 'ufo'
1111
import { streamToArrayBuffer } from '../../../utils/stream'
1212
import { requireNuxtHubFeature } from '../../../utils/features'
1313
import { getCloudflareAccessHeaders } from '../../../utils/cloudflareAccess'
14-
import type { BlobType, FileSizeUnit, BlobUploadedPart, BlobListResult, BlobMultipartUpload, HandleMPUResponse, BlobMultipartOptions, BlobUploadOptions, BlobPutOptions, BlobEnsureOptions, BlobObject, BlobListOptions, BlobCredentialsOptions, BlobCredentials } from '@nuxthub/core'
14+
import type { BlobType, FileSizeUnit, BlobUploadedPart, BlobListResult, BlobMultipartUpload, BlobMultipartOptions, BlobUploadOptions, BlobPutOptions, BlobEnsureOptions, BlobObject, BlobListOptions, BlobCredentialsOptions, BlobCredentials, HubBlob } from '@nuxthub/core'
1515
import { useRuntimeConfig } from '#imports'
1616

1717
const _r2_buckets: Record<string, R2Bucket> = {}
@@ -33,124 +33,6 @@ function _useBucket(name: string = 'BLOB') {
3333
throw createError(`Missing Cloudflare ${name} binding (R2)`)
3434
}
3535

36-
interface HubBlob {
37-
/**
38-
* List all the blobs in the bucket (metadata only).
39-
*
40-
* @param options The list options
41-
*
42-
* @example ```ts
43-
* const { blobs } = await hubBlob().list({ limit: 10 })
44-
* ```
45-
*/
46-
list(options?: BlobListOptions): Promise<BlobListResult>
47-
/**
48-
* Serve the blob from the bucket.
49-
*
50-
* @param event The H3 event (needed to set headers for the response)
51-
* @param pathname The pathname of the blob
52-
*
53-
* @example ```ts
54-
* export default eventHandler(async (event) => {
55-
* return hubBlob().serve(event, '/my-image.jpg')
56-
* })
57-
* ```
58-
*/
59-
serve(event: H3Event, pathname: string): Promise<ReadableStream<any>>
60-
/**
61-
* Put a new blob into the bucket.
62-
*
63-
* @param pathname The pathname of the blob
64-
* @param body The blob content
65-
* @param options The put options
66-
*
67-
* @example ```ts
68-
* const blob = await hubBlob().put('/my-image.jpg', file)
69-
* ```
70-
*/
71-
put(pathname: string, body: string | ReadableStream<any> | ArrayBuffer | ArrayBufferView | Blob, options?: BlobPutOptions): Promise<BlobObject>
72-
/**
73-
* Get the blob metadata from the bucket.
74-
*
75-
* @param pathname The pathname of the blob
76-
*
77-
* @example ```ts
78-
* const blobMetadata = await hubBlob().head('/my-image.jpg')
79-
* ```
80-
*/
81-
head(pathname: string): Promise<BlobObject>
82-
/**
83-
* Get the blob body from the bucket.
84-
*
85-
* @param pathname The pathname of the blob
86-
*
87-
* @example ```ts
88-
* const blob = await hubBlob().get('/my-image.jpg')
89-
* ```
90-
*/
91-
get(pathname: string): Promise<Blob | null>
92-
/**
93-
* Delete the blob from the bucket.
94-
*
95-
* @param pathnames The pathname of the blob
96-
*
97-
* @example ```ts
98-
* await hubBlob().del('/my-image.jpg')
99-
* ```
100-
*/
101-
del(pathnames: string | string[]): Promise<void>
102-
/**
103-
* Delete the blob from the bucket.
104-
*
105-
* @param pathnames The pathname of the blob
106-
*
107-
* @example ```ts
108-
* await hubBlob().delete('/my-image.jpg')
109-
* ```
110-
*/
111-
delete(pathnames: string | string[]): Promise<void>
112-
/**
113-
* Create a multipart upload.
114-
*
115-
* @see https://hub.nuxt.com/docs/features/blob#createmultipartupload
116-
*/
117-
createMultipartUpload(pathname: string, options?: BlobMultipartOptions): Promise<BlobMultipartUpload>
118-
/**
119-
* Get the specified multipart upload.
120-
*
121-
* @see https://hub.nuxt.com/docs/features/blob#resumemultipartupload
122-
*/
123-
resumeMultipartUpload(pathname: string, uploadId: string): BlobMultipartUpload
124-
/**
125-
* Handle the multipart upload request.
126-
* Make sure your route includes `[action]` and `[...pathname]` params.
127-
*
128-
* @see https://hub.nuxt.com/docs/features/blob#handlemultipartupload
129-
*/
130-
handleMultipartUpload(event: H3Event, options?: BlobMultipartOptions): Promise<HandleMPUResponse>
131-
/**
132-
* Handle a file upload.
133-
*
134-
* @param event The H3 event (needed to set headers for the response)
135-
* @param options The upload options
136-
*
137-
* @see https://hub.nuxt.com/docs/features/blob#handleupload
138-
*/
139-
handleUpload(event: H3Event, options?: BlobUploadOptions): Promise<BlobObject[]>
140-
/**
141-
* Creates temporary access credentials that can be optionally scoped to prefixes or objects.
142-
*
143-
* Useful to create a signed url to upload directory to R2 from client-side.
144-
*
145-
* Only available in production or in development with `--remote` flag.
146-
*
147-
* @example ```ts
148-
* const { accountId, bucketName, accessKeyId, secretAccessKey, sessionToken } = await hubBlob().createCredentials()
149-
* ```
150-
*/
151-
createCredentials(options?: BlobCredentialsOptions): Promise<BlobCredentials>
152-
}
153-
15436
/**
15537
* Access the Blob storage.
15638
*

src/types/blob.ts

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { ReadableStream, R2HTTPMetadata } from '@cloudflare/workers-types/experimental'
22
import type { MimeType } from '@uploadthing/mime-types'
3+
import type { H3Event } from 'h3'
34

45
// Credits from shared utils of https://github.com/pingdotgg/uploadthing
56
export type PowOf2 = 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128 | 256 | 512 | 1024
@@ -262,3 +263,121 @@ export interface BlobCredentials {
262263
*/
263264
sessionToken: string
264265
}
266+
267+
export interface HubBlob {
268+
/**
269+
* List all the blobs in the bucket (metadata only).
270+
*
271+
* @param options The list options
272+
*
273+
* @example ```ts
274+
* const { blobs } = await hubBlob().list({ limit: 10 })
275+
* ```
276+
*/
277+
list(options?: BlobListOptions): Promise<BlobListResult>
278+
/**
279+
* Serve the blob from the bucket.
280+
*
281+
* @param event The H3 event (needed to set headers for the response)
282+
* @param pathname The pathname of the blob
283+
*
284+
* @example ```ts
285+
* export default eventHandler(async (event) => {
286+
* return hubBlob().serve(event, '/my-image.jpg')
287+
* })
288+
* ```
289+
*/
290+
serve(event: H3Event, pathname: string): Promise<ReadableStream<any>>
291+
/**
292+
* Put a new blob into the bucket.
293+
*
294+
* @param pathname The pathname of the blob
295+
* @param body The blob content
296+
* @param options The put options
297+
*
298+
* @example ```ts
299+
* const blob = await hubBlob().put('/my-image.jpg', file)
300+
* ```
301+
*/
302+
put(pathname: string, body: string | ReadableStream<any> | ArrayBuffer | ArrayBufferView | Blob, options?: BlobPutOptions): Promise<BlobObject>
303+
/**
304+
* Get the blob metadata from the bucket.
305+
*
306+
* @param pathname The pathname of the blob
307+
*
308+
* @example ```ts
309+
* const blobMetadata = await hubBlob().head('/my-image.jpg')
310+
* ```
311+
*/
312+
head(pathname: string): Promise<BlobObject>
313+
/**
314+
* Get the blob body from the bucket.
315+
*
316+
* @param pathname The pathname of the blob
317+
*
318+
* @example ```ts
319+
* const blob = await hubBlob().get('/my-image.jpg')
320+
* ```
321+
*/
322+
get(pathname: string): Promise<Blob | null>
323+
/**
324+
* Delete the blob from the bucket.
325+
*
326+
* @param pathnames The pathname of the blob
327+
*
328+
* @example ```ts
329+
* await hubBlob().del('/my-image.jpg')
330+
* ```
331+
*/
332+
del(pathnames: string | string[]): Promise<void>
333+
/**
334+
* Delete the blob from the bucket.
335+
*
336+
* @param pathnames The pathname of the blob
337+
*
338+
* @example ```ts
339+
* await hubBlob().delete('/my-image.jpg')
340+
* ```
341+
*/
342+
delete(pathnames: string | string[]): Promise<void>
343+
/**
344+
* Create a multipart upload.
345+
*
346+
* @see https://hub.nuxt.com/docs/features/blob#createmultipartupload
347+
*/
348+
createMultipartUpload(pathname: string, options?: BlobMultipartOptions): Promise<BlobMultipartUpload>
349+
/**
350+
* Get the specified multipart upload.
351+
*
352+
* @see https://hub.nuxt.com/docs/features/blob#resumemultipartupload
353+
*/
354+
resumeMultipartUpload(pathname: string, uploadId: string): BlobMultipartUpload
355+
/**
356+
* Handle the multipart upload request.
357+
* Make sure your route includes `[action]` and `[...pathname]` params.
358+
*
359+
* @see https://hub.nuxt.com/docs/features/blob#handlemultipartupload
360+
*/
361+
handleMultipartUpload(event: H3Event, options?: BlobMultipartOptions): Promise<HandleMPUResponse>
362+
/**
363+
* Handle a file upload.
364+
*
365+
* @param event The H3 event (needed to set headers for the response)
366+
* @param options The upload options
367+
*
368+
* @see https://hub.nuxt.com/docs/features/blob#handleupload
369+
*/
370+
handleUpload(event: H3Event, options?: BlobUploadOptions): Promise<BlobObject[]>
371+
/**
372+
* Creates temporary access credentials that can be optionally scoped to prefixes or objects.
373+
*
374+
* Useful to create a signed url to upload directory to R2 from client-side.
375+
*
376+
* Only available in production or in development with `--remote` flag.
377+
*
378+
* @example ```ts
379+
* const { accountId, bucketName, accessKeyId, secretAccessKey, sessionToken } = await hubBlob().createCredentials()
380+
* ```
381+
*/
382+
createCredentials(options?: BlobCredentialsOptions): Promise<BlobCredentials>
383+
}

0 commit comments

Comments
 (0)