@vercel/blob 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/client.d.cts CHANGED
@@ -1,16 +1,21 @@
1
- import { W as WithUploadProgress, P as PutBody, a as PutBlobResult, b as Part, C as CommonMultipartUploadOptions, c as CommonCompleteMultipartUploadOptions, B as BlobCommandOptions } from './create-folder-Bq7XMIcv.cjs';
2
- export { d as createFolder } from './create-folder-Bq7XMIcv.cjs';
1
+ import { W as WithUploadProgress, P as PutBody, a as PutBlobResult, b as Part, C as CommonMultipartUploadOptions, c as CommonCompleteMultipartUploadOptions, B as BlobCommandOptions } from './create-folder-C02EFEPE.cjs';
2
+ export { d as createFolder } from './create-folder-C02EFEPE.cjs';
3
3
  import { IncomingMessage } from 'node:http';
4
4
  import 'stream';
5
5
  import 'undici';
6
6
 
7
+ /**
8
+ * Interface for put, upload and multipart upload operations.
9
+ * This type omits all options that are encoded in the client token.
10
+ */
7
11
  interface ClientCommonCreateBlobOptions {
8
12
  /**
9
13
  * Whether the blob should be publicly accessible.
10
14
  */
11
15
  access: 'public';
12
16
  /**
13
- * Defines the content type of the blob. By default, this value is inferred from the pathname. Sent as the 'content-type' header when downloading a blob.
17
+ * Defines the content type of the blob. By default, this value is inferred from the pathname.
18
+ * Sent as the 'content-type' header when downloading a blob.
14
19
  */
15
20
  contentType?: string;
16
21
  /**
@@ -18,25 +23,82 @@ interface ClientCommonCreateBlobOptions {
18
23
  */
19
24
  abortSignal?: AbortSignal;
20
25
  }
26
+ /**
27
+ * Shared interface for put and multipart operations that use client tokens.
28
+ */
21
29
  interface ClientTokenOptions {
22
30
  /**
23
31
  * A client token that was generated by your server using the `generateClientToken` method.
24
32
  */
25
33
  token: string;
26
34
  }
35
+ /**
36
+ * Shared interface for put and upload operations.
37
+ * @internal This is an internal interface not intended for direct use by consumers.
38
+ */
27
39
  interface ClientCommonPutOptions extends ClientCommonCreateBlobOptions, WithUploadProgress {
28
40
  /**
29
- * Whether to use multipart upload. Use this when uploading large files. It will split the file into multiple parts, upload them in parallel and retry failed parts.
41
+ * Whether to use multipart upload. Use this when uploading large files.
42
+ * It will split the file into multiple parts, upload them in parallel and retry failed parts.
30
43
  */
31
44
  multipart?: boolean;
32
45
  }
46
+ /**
47
+ * Options for the client-side put operation.
48
+ */
33
49
  type ClientPutCommandOptions = ClientCommonPutOptions & ClientTokenOptions;
50
+ /**
51
+ * Uploads a file to the blob store using a client token.
52
+ *
53
+ * @param pathname - The pathname to upload the blob to, including the extension. This will influence the URL of your blob.
54
+ * @param body - The content of your blob. Can be a string, File, Blob, Buffer or ReadableStream.
55
+ * @param options - Configuration options including:
56
+ * - access - (Required) Must be 'public' as blobs are publicly accessible.
57
+ * - token - (Required) A client token generated by your server using the generateClientTokenFromReadWriteToken method.
58
+ * - contentType - (Optional) The media type for the blob. By default, it's derived from the pathname.
59
+ * - multipart - (Optional) Whether to use multipart upload for large files. It will split the file into multiple parts, upload them in parallel and retry failed parts.
60
+ * - abortSignal - (Optional) AbortSignal to cancel the operation.
61
+ * - onUploadProgress - (Optional) Callback to track upload progress: onUploadProgress(\{loaded: number, total: number, percentage: number\})
62
+ * @returns A promise that resolves to the blob information, including pathname, contentType, contentDisposition, url, and downloadUrl.
63
+ */
34
64
  declare const put: (pathname: string, body: PutBody, optionsInput: ClientPutCommandOptions) => Promise<PutBlobResult>;
65
+ /**
66
+ * Options for creating a multipart upload from the client side.
67
+ */
35
68
  type ClientCreateMultipartUploadCommandOptions = ClientCommonCreateBlobOptions & ClientTokenOptions;
69
+ /**
70
+ * Creates a multipart upload. This is the first step in the manual multipart upload process.
71
+ *
72
+ * @param pathname - A string specifying the path inside the blob store. This will be the base value of the return URL and includes the filename and extension.
73
+ * @param options - Configuration options including:
74
+ * - access - (Required) Must be 'public' as blobs are publicly accessible.
75
+ * - token - (Required) A client token generated by your server using the generateClientTokenFromReadWriteToken method.
76
+ * - contentType - (Optional) The media type for the file. If not specified, it's derived from the file extension.
77
+ * - abortSignal - (Optional) AbortSignal to cancel the operation.
78
+ * @returns A promise that resolves to an object containing:
79
+ * - key: A string that identifies the blob object.
80
+ * - uploadId: A string that identifies the multipart upload. Both are needed for subsequent uploadPart calls.
81
+ */
36
82
  declare const createMultipartUpload: (pathname: string, optionsInput: ClientCreateMultipartUploadCommandOptions) => Promise<{
37
83
  key: string;
38
84
  uploadId: string;
39
85
  }>;
86
+ /**
87
+ * Creates a multipart uploader that simplifies the multipart upload process.
88
+ * This is a wrapper around the manual multipart upload process that provides a more convenient API.
89
+ *
90
+ * @param pathname - A string specifying the path inside the blob store. This will be the base value of the return URL and includes the filename and extension.
91
+ * @param options - Configuration options including:
92
+ * - access - (Required) Must be 'public' as blobs are publicly accessible.
93
+ * - token - (Required) A client token generated by your server using the generateClientTokenFromReadWriteToken method.
94
+ * - contentType - (Optional) The media type for the file. If not specified, it's derived from the file extension.
95
+ * - abortSignal - (Optional) AbortSignal to cancel the operation.
96
+ * @returns A promise that resolves to an uploader object with the following properties and methods:
97
+ * - key: A string that identifies the blob object.
98
+ * - uploadId: A string that identifies the multipart upload.
99
+ * - uploadPart: A method to upload a part of the file.
100
+ * - complete: A method to complete the multipart upload process.
101
+ */
40
102
  declare const createMultipartUploader: (pathname: string, optionsInput: ClientCreateMultipartUploadCommandOptions) => Promise<{
41
103
  key: string;
42
104
  uploadId: string;
@@ -46,10 +108,51 @@ declare const createMultipartUploader: (pathname: string, optionsInput: ClientCr
46
108
  }>;
47
109
  complete(parts: Part[]): Promise<PutBlobResult>;
48
110
  }>;
111
+ /**
112
+ * @internal Internal type for multipart upload options.
113
+ */
49
114
  type ClientMultipartUploadCommandOptions = ClientCommonCreateBlobOptions & ClientTokenOptions & CommonMultipartUploadOptions & WithUploadProgress;
115
+ /**
116
+ * Uploads a part of a multipart upload.
117
+ * Used as part of the manual multipart upload process.
118
+ *
119
+ * @param pathname - Same value as the pathname parameter passed to createMultipartUpload. This will influence the final URL of your blob.
120
+ * @param body - A blob object as ReadableStream, String, ArrayBuffer or Blob based on these supported body types. Each part must be a minimum of 5MB, except the last one which can be smaller.
121
+ * @param options - Configuration options including:
122
+ * - access - (Required) Must be 'public' as blobs are publicly accessible.
123
+ * - token - (Required) A client token generated by your server using the generateClientTokenFromReadWriteToken method.
124
+ * - uploadId - (Required) A string returned from createMultipartUpload which identifies the multipart upload.
125
+ * - key - (Required) A string returned from createMultipartUpload which identifies the blob object.
126
+ * - partNumber - (Required) A number identifying which part is uploaded (1-based index).
127
+ * - contentType - (Optional) The media type for the blob. By default, it's derived from the pathname.
128
+ * - abortSignal - (Optional) AbortSignal to cancel the running request.
129
+ * - onUploadProgress - (Optional) Callback to track upload progress: onUploadProgress(\{loaded: number, total: number, percentage: number\})
130
+ * @returns A promise that resolves to the uploaded part information containing etag and partNumber, which will be needed for the completeMultipartUpload call.
131
+ */
50
132
  declare const uploadPart: (pathname: string, body: PutBody, optionsInput: ClientMultipartUploadCommandOptions) => Promise<Part>;
133
+ /**
134
+ * @internal Internal type for completing multipart uploads.
135
+ */
51
136
  type ClientCompleteMultipartUploadCommandOptions = ClientCommonCreateBlobOptions & ClientTokenOptions & CommonCompleteMultipartUploadOptions;
137
+ /**
138
+ * Completes a multipart upload by combining all uploaded parts.
139
+ * This is the final step in the manual multipart upload process.
140
+ *
141
+ * @param pathname - Same value as the pathname parameter passed to createMultipartUpload.
142
+ * @param parts - An array containing all the uploaded parts information from previous uploadPart calls. Each part must have properties etag and partNumber.
143
+ * @param options - Configuration options including:
144
+ * - access - (Required) Must be 'public' as blobs are publicly accessible.
145
+ * - token - (Required) A client token generated by your server using the generateClientTokenFromReadWriteToken method.
146
+ * - uploadId - (Required) A string returned from createMultipartUpload which identifies the multipart upload.
147
+ * - key - (Required) A string returned from createMultipartUpload which identifies the blob object.
148
+ * - contentType - (Optional) The media type for the file. If not specified, it's derived from the file extension.
149
+ * - abortSignal - (Optional) AbortSignal to cancel the operation.
150
+ * @returns A promise that resolves to the finalized blob information, including pathname, contentType, contentDisposition, url, and downloadUrl.
151
+ */
52
152
  declare const completeMultipartUpload: (pathname: string, parts: Part[], optionsInput: ClientCompleteMultipartUploadCommandOptions) => Promise<PutBlobResult>;
153
+ /**
154
+ * Options for client-side upload operations.
155
+ */
53
156
  interface CommonUploadOptions {
54
157
  /**
55
158
  * A route that implements the `handleUpload` function for generating a client token.
@@ -60,6 +163,9 @@ interface CommonUploadOptions {
60
163
  */
61
164
  clientPayload?: string;
62
165
  }
166
+ /**
167
+ * Options for the upload method, which handles client-side uploads.
168
+ */
63
169
  type UploadOptions = ClientCommonPutOptions & CommonUploadOptions;
64
170
  /**
65
171
  * Uploads a blob into your store from the client.
@@ -67,65 +173,224 @@ type UploadOptions = ClientCommonPutOptions & CommonUploadOptions;
67
173
  *
68
174
  * If you want to upload from your server instead, check out the documentation for the put operation: https://vercel.com/docs/vercel-blob/using-blob-sdk#upload-a-blob
69
175
  *
70
- * @param pathname - The pathname to upload the blob to. This includes the filename.
71
- * @param body - The contents of your blob. This has to be a supported fetch body type https://developer.mozilla.org/en-US/docs/Web/API/fetch#body.
72
- * @param options - Additional options.
176
+ * Unlike the put method, this method does not require a client token as it will fetch one from your server.
177
+ *
178
+ * @param pathname - The pathname to upload the blob to. This includes the filename and extension.
179
+ * @param body - The contents of your blob. This has to be a supported fetch body type (string, Blob, File, ArrayBuffer, etc).
180
+ * @param options - Configuration options including:
181
+ * - access - (Required) Must be 'public' as blobs are publicly accessible.
182
+ * - handleUploadUrl - (Required) A string specifying the route to call for generating client tokens for client uploads.
183
+ * - clientPayload - (Optional) A string to be sent to your handleUpload server code. Example use-case: attaching the post id an image relates to.
184
+ * - contentType - (Optional) A string indicating the media type. By default, it's extracted from the pathname's extension.
185
+ * - multipart - (Optional) Whether to use multipart upload for large files. It will split the file into multiple parts, upload them in parallel and retry failed parts.
186
+ * - abortSignal - (Optional) AbortSignal to cancel the operation.
187
+ * - onUploadProgress - (Optional) Callback to track upload progress: onUploadProgress(\{loaded: number, total: number, percentage: number\})
188
+ * @returns A promise that resolves to the blob information, including pathname, contentType, contentDisposition, url, and downloadUrl.
73
189
  */
74
190
  declare const upload: (pathname: string, body: PutBody, optionsInput: UploadOptions) => Promise<PutBlobResult>;
191
+ /**
192
+ * Decoded payload from a client token.
193
+ */
75
194
  type DecodedClientTokenPayload = Omit<GenerateClientTokenOptions, 'token'> & {
195
+ /**
196
+ * Timestamp in milliseconds when the token will expire.
197
+ */
76
198
  validUntil: number;
77
199
  };
200
+ /**
201
+ * Extracts and decodes the payload from a client token.
202
+ *
203
+ * @param clientToken - The client token string to decode
204
+ * @returns The decoded payload containing token options
205
+ */
78
206
  declare function getPayloadFromClientToken(clientToken: string): DecodedClientTokenPayload;
207
+ /**
208
+ * @internal Event type constants for internal use.
209
+ */
79
210
  declare const EventTypes: {
80
211
  readonly generateClientToken: "blob.generate-client-token";
81
212
  readonly uploadCompleted: "blob.upload-completed";
82
213
  };
214
+ /**
215
+ * Event for generating a client token for blob uploads.
216
+ * @internal This is an internal interface used by the SDK.
217
+ */
83
218
  interface GenerateClientTokenEvent {
219
+ /**
220
+ * Type identifier for the generate client token event.
221
+ */
84
222
  type: (typeof EventTypes)['generateClientToken'];
223
+ /**
224
+ * Payload containing information needed to generate a client token.
225
+ */
85
226
  payload: {
227
+ /**
228
+ * The destination path for the blob.
229
+ */
86
230
  pathname: string;
231
+ /**
232
+ * URL where upload completion callbacks will be sent.
233
+ */
87
234
  callbackUrl: string;
235
+ /**
236
+ * Whether the upload will use multipart uploading.
237
+ */
88
238
  multipart: boolean;
239
+ /**
240
+ * Additional data from the client which will be available in onBeforeGenerateToken.
241
+ */
89
242
  clientPayload: string | null;
90
243
  };
91
244
  }
245
+ /**
246
+ * Event that occurs when a client upload has completed.
247
+ * @internal This is an internal interface used by the SDK.
248
+ */
92
249
  interface UploadCompletedEvent {
250
+ /**
251
+ * Type identifier for the upload completed event.
252
+ */
93
253
  type: (typeof EventTypes)['uploadCompleted'];
254
+ /**
255
+ * Payload containing information about the uploaded blob.
256
+ */
94
257
  payload: {
258
+ /**
259
+ * Details about the blob that was uploaded.
260
+ */
95
261
  blob: PutBlobResult;
262
+ /**
263
+ * Optional payload that was defined during token generation.
264
+ */
96
265
  tokenPayload?: string | null;
97
266
  };
98
267
  }
268
+ /**
269
+ * Union type representing either a request to generate a client token or a notification that an upload completed.
270
+ */
99
271
  type HandleUploadBody = GenerateClientTokenEvent | UploadCompletedEvent;
272
+ /**
273
+ * Type representing either a Node.js IncomingMessage or a web standard Request object.
274
+ * @internal This is an internal type used by the SDK.
275
+ */
100
276
  type RequestType = IncomingMessage | Request;
277
+ /**
278
+ * Options for the handleUpload function.
279
+ */
101
280
  interface HandleUploadOptions {
281
+ /**
282
+ * The request body containing upload information.
283
+ */
102
284
  body: HandleUploadBody;
285
+ /**
286
+ * Function called before generating the client token for uploads.
287
+ *
288
+ * @param pathname - The destination path for the blob
289
+ * @param clientPayload - A string payload specified on the client when calling upload()
290
+ * @param multipart - A boolean specifying whether the file is a multipart upload
291
+ *
292
+ * @returns An object with configuration options for the client token
293
+ */
103
294
  onBeforeGenerateToken: (pathname: string, clientPayload: string | null, multipart: boolean) => Promise<Pick<GenerateClientTokenOptions, 'allowedContentTypes' | 'maximumSizeInBytes' | 'validUntil' | 'addRandomSuffix' | 'allowOverwrite' | 'cacheControlMaxAge'> & {
104
295
  tokenPayload?: string | null;
105
296
  }>;
297
+ /**
298
+ * Function called by Vercel Blob when the client upload finishes.
299
+ * This is useful to update your database with the blob URL that was uploaded.
300
+ *
301
+ * @param body - Contains information about the completed upload including the blob details
302
+ */
106
303
  onUploadCompleted: (body: UploadCompletedEvent['payload']) => Promise<void>;
304
+ /**
305
+ * A string specifying the read-write token to use when making requests.
306
+ * It defaults to process.env.BLOB_READ_WRITE_TOKEN when deployed on Vercel.
307
+ */
107
308
  token?: string;
309
+ /**
310
+ * An IncomingMessage or Request object to be used to determine the action to take.
311
+ */
108
312
  request: RequestType;
109
313
  }
314
+ /**
315
+ * A server-side route helper to manage client uploads. It has two responsibilities:
316
+ * 1. Generate tokens for client uploads
317
+ * 2. Listen for completed client uploads, so you can update your database with the URL of the uploaded file
318
+ *
319
+ * @param options - Configuration options for handling uploads
320
+ * - request - (Required) An IncomingMessage or Request object to be used to determine the action to take.
321
+ * - body - (Required) The request body containing upload information.
322
+ * - onBeforeGenerateToken - (Required) Function called before generating the client token for uploads.
323
+ * - onUploadCompleted - (Required) Function called by Vercel Blob when the client upload finishes.
324
+ * - token - (Optional) A string specifying the read-write token to use when making requests. Defaults to process.env.BLOB_READ_WRITE_TOKEN.
325
+ * @returns A promise that resolves to either a client token generation result or an upload completion result
326
+ */
110
327
  declare function handleUpload({ token, request, body, onBeforeGenerateToken, onUploadCompleted, }: HandleUploadOptions): Promise<{
111
- type: GenerateClientTokenEvent['type'];
328
+ type: 'blob.generate-client-token';
112
329
  clientToken: string;
113
330
  } | {
114
- type: UploadCompletedEvent['type'];
331
+ type: 'blob.upload-completed';
115
332
  response: 'ok';
116
333
  }>;
334
+ /**
335
+ * Generates a client token from a read-write token. This function must be called from a server environment.
336
+ * The client token contains permissions and constraints that limit what the client can do.
337
+ *
338
+ * @param options - Options for generating the client token
339
+ * - pathname - (Required) The destination path for the blob.
340
+ * - token - (Optional) A string specifying the read-write token to use. Defaults to process.env.BLOB_READ_WRITE_TOKEN.
341
+ * - onUploadCompleted - (Optional) Configuration for upload completion callback.
342
+ * - maximumSizeInBytes - (Optional) A number specifying the maximum size in bytes that can be uploaded (max 5TB).
343
+ * - allowedContentTypes - (Optional) An array of media types that are allowed to be uploaded. Wildcards are supported (text/*).
344
+ * - validUntil - (Optional) A timestamp in ms when the token will expire. Defaults to one hour from generation.
345
+ * - addRandomSuffix - (Optional) Whether to add a random suffix to the filename. Defaults to false.
346
+ * - allowOverwrite - (Optional) Whether to allow overwriting existing blobs. Defaults to false.
347
+ * - cacheControlMaxAge - (Optional) Number of seconds to configure cache duration. Defaults to one month.
348
+ * @returns A promise that resolves to the generated client token string which can be used in client-side upload operations.
349
+ */
117
350
  declare function generateClientTokenFromReadWriteToken({ token, ...argsWithoutToken }: GenerateClientTokenOptions): Promise<string>;
351
+ /**
352
+ * Options for generating a client token.
353
+ */
118
354
  interface GenerateClientTokenOptions extends BlobCommandOptions {
355
+ /**
356
+ * The destination path for the blob
357
+ */
119
358
  pathname: string;
359
+ /**
360
+ * Configuration for upload completion callback
361
+ */
120
362
  onUploadCompleted?: {
121
363
  callbackUrl: string;
122
364
  tokenPayload?: string | null;
123
365
  };
366
+ /**
367
+ * A number specifying the maximum size in bytes that can be uploaded. The maximum is 5TB.
368
+ */
124
369
  maximumSizeInBytes?: number;
370
+ /**
371
+ * An array of strings specifying the media type that are allowed to be uploaded.
372
+ * By default, it's all content types. Wildcards are supported (text/*)
373
+ */
125
374
  allowedContentTypes?: string[];
375
+ /**
376
+ * A number specifying the timestamp in ms when the token will expire.
377
+ * By default, it's now + 1 hour.
378
+ */
126
379
  validUntil?: number;
380
+ /**
381
+ * Adds a random suffix to the filename.
382
+ * @defaultvalue false
383
+ */
127
384
  addRandomSuffix?: boolean;
385
+ /**
386
+ * Allow overwriting an existing blob. By default this is set to false and will throw an error if the blob already exists.
387
+ * @defaultvalue false
388
+ */
128
389
  allowOverwrite?: boolean;
390
+ /**
391
+ * Number in seconds to configure how long Blobs are cached. Defaults to one month. Cannot be set to a value lower than 1 minute.
392
+ * @defaultvalue 30 * 24 * 60 * 60 (1 Month)
393
+ */
129
394
  cacheControlMaxAge?: number;
130
395
  }
131
396