@meistrari/vault-sdk 1.1.0 → 1.2.0

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/index.cjs CHANGED
@@ -4,6 +4,35 @@ const schemas = require('@meistrari/vault-shared/schemas');
4
4
  const fileType = require('file-type');
5
5
  const mimeTypes = require('mime-types');
6
6
 
7
+ var __defProp$1 = Object.defineProperty;
8
+ var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
+ var __publicField$1 = (obj, key, value) => {
10
+ __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
11
+ return value;
12
+ };
13
+ class DataTokenAuthStrategy {
14
+ constructor(dataToken) {
15
+ __publicField$1(this, "dataToken");
16
+ this.dataToken = dataToken;
17
+ }
18
+ getHeaders() {
19
+ return new Headers({
20
+ "x-data-token": this.dataToken
21
+ });
22
+ }
23
+ }
24
+ class APIKeyAuthStrategy {
25
+ constructor(apiKey) {
26
+ __publicField$1(this, "apiKey");
27
+ this.apiKey = apiKey;
28
+ }
29
+ getHeaders() {
30
+ return new Headers({
31
+ Authorization: this.apiKey
32
+ });
33
+ }
34
+ }
35
+
7
36
  class FetchError extends Error {
8
37
  constructor(message, url, method, response) {
9
38
  super(message);
@@ -56,10 +85,10 @@ async function detectFileMimeType(blob) {
56
85
  return void 0;
57
86
  }
58
87
 
59
- var __defProp$1 = Object.defineProperty;
60
- var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
61
- var __publicField$1 = (obj, key, value) => {
62
- __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
88
+ var __defProp = Object.defineProperty;
89
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
90
+ var __publicField = (obj, key, value) => {
91
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
63
92
  return value;
64
93
  };
65
94
  const compatibilityDate = "2025-05-19";
@@ -88,13 +117,13 @@ class VaultFile {
88
117
  * @param params.metadata - The metadata of the file
89
118
  */
90
119
  constructor({ config, content, id, name, metadata }) {
91
- __publicField$1(this, "id");
92
- __publicField$1(this, "name");
93
- __publicField$1(this, "metadata");
94
- __publicField$1(this, "config");
95
- __publicField$1(this, "content");
96
- __publicField$1(this, "lastDownloadUrl");
97
- __publicField$1(this, "lastUploadUrl");
120
+ __publicField(this, "id");
121
+ __publicField(this, "name");
122
+ __publicField(this, "metadata");
123
+ __publicField(this, "config");
124
+ __publicField(this, "content");
125
+ __publicField(this, "lastDownloadUrl");
126
+ __publicField(this, "lastUploadUrl");
98
127
  this.config = config;
99
128
  this.content = content;
100
129
  this.id = id;
@@ -116,38 +145,46 @@ class VaultFile {
116
145
  * @param params.method - The method to use for the fetch
117
146
  * @param params.path - The path to fetch
118
147
  * @param params.body - The body of the request
148
+ * @param params.signal - The signal to abort the request
119
149
  * @returns The response from the vault
120
150
  * @throws {FetchError} If the fetch fails
121
151
  */
122
152
  async _fetch(params) {
123
- const { method, path, body } = params;
153
+ const { method, path, body, signal } = params;
124
154
  const url = new URL(this.config.vaultUrl + path).toString();
125
155
  const headers = new Headers(this.headers);
126
156
  headers.set("x-compatibility-date", compatibilityDate);
127
157
  const response = await wrappedFetch(url, {
128
158
  method,
129
159
  body,
130
- headers
160
+ headers,
161
+ signal
131
162
  });
132
163
  const content = await response.json();
133
164
  return content;
134
165
  }
135
166
  /**
136
167
  * Creates a new file in the vault.
168
+ * @param metadata - The metadata for creating a file
169
+ * @param metadata.size - The size of the file
170
+ * @param metadata.mimeType - The mime type of the file
171
+ * @param options - The options for the request
172
+ * @param options.signal - The signal to abort the request
137
173
  *
138
174
  * @returns The metadata of the file
139
175
  * @throws {Error} If the file ID is not set
140
176
  * @throws {FetchError} If the metadata fetch fails
141
177
  */
142
- async _createFile(params = {}) {
178
+ async _createFile(metadata = {}, options) {
143
179
  const response = await this._fetch({
144
180
  method: "POST",
145
181
  path: `/v2/files`,
146
182
  body: JSON.stringify({
147
- ...params,
183
+ ...metadata,
148
184
  fileName: this.name,
149
185
  sha256sum: this.id ?? this.metadata?.id ?? (this.content ? await getFileHash(this.content) : void 0)
150
- })
186
+ }),
187
+ signal: options?.signal
151
188
  }).then((data) => schemas.GetUploadUrlResponseV2.safeParse(data));
152
189
  if (!response.success) {
153
190
  throw new Error(`Invalid response from vault service. ${JSON.stringify(response.error)}`);
@@ -164,6 +201,9 @@ class VaultFile {
164
201
  * @param params.reference - The reference to the file in the vault
165
202
  * @param params.config - The configuration for the VaultFile
166
203
  * @param params.download - Whether to download the file content (default: false)
204
+ * @param options - The options for the request
205
+ * @param options.signal - The signal to abort the request
206
+ *
167
207
  * @returns A new VaultFile instance
168
208
  *
169
209
  * @example
@@ -193,13 +233,14 @@ class VaultFile {
193
233
  * const content = vaultFile.content
194
234
  * ```
195
235
  */
196
- static async fromVaultReference(params) {
236
+ static async fromVaultReference(params, options) {
197
237
  const { reference, config, download = false } = params;
198
238
  const { vaultUrl, authStrategy } = config;
199
239
  const id = removeVaultPrefix(reference);
200
240
  const response = await wrappedFetch(`${vaultUrl}/v2/files/${id}`, {
201
241
  method: "GET",
202
- headers: authStrategy.getHeaders()
242
+ headers: authStrategy.getHeaders(),
243
+ signal: options?.signal
203
244
  }).then((response2) => response2.json()).then((data) => schemas.GetDownloadUrlResponse.safeParse(data));
204
245
  if (!response.success) {
205
246
  throw new Error("Invalid response from vault service");
@@ -214,7 +255,7 @@ class VaultFile {
214
255
  name: response.data.metadata?.originalFileName
215
256
  };
216
257
  if (download) {
217
- await wrappedFetch(response.data.url, { method: "GET" }).then((response2) => response2.blob()).then((blob) => fileParams.content = blob);
258
+ await wrappedFetch(response.data.url, { method: "GET", signal: options?.signal }).then((response2) => response2.blob()).then((blob) => fileParams.content = blob);
218
259
  }
219
260
  return new VaultFile(fileParams);
220
261
  }
@@ -226,6 +267,9 @@ class VaultFile {
226
267
  * @param params.content - The content of the file
227
268
  * @param params.config - The configuration for the VaultFile
228
269
  * @param params.upload - Whether to upload the file (default: false)
270
+ * @param options - The options for the request
271
+ * @param options.signal - The signal to abort the request
272
+ *
229
273
  * @returns A new VaultFile instance
230
274
  *
231
275
  * @example
@@ -258,7 +302,7 @@ class VaultFile {
258
302
  * })
259
303
  * ```
260
304
  */
261
- static async fromContent(params) {
305
+ static async fromContent(params, options) {
262
306
  const { name, content, config, upload = false } = params;
263
307
  const { vaultUrl, authStrategy } = config;
264
308
  const sha256sum = await getFileHash(content);
@@ -276,22 +320,24 @@ class VaultFile {
276
320
  const createdFile = await file._createFile({
277
321
  size,
278
322
  mimeType
279
- });
323
+ }, { signal: options?.signal });
280
324
  if (upload) {
281
- await file.upload(file.content, createdFile.uploadUrl);
325
+ await file.upload(file.content, createdFile.uploadUrl, { signal: options?.signal });
282
326
  }
283
327
  return file;
284
328
  }
285
329
  /**
286
330
  * Populates the metadata of the file instance.
331
+ * @param options - The options for the request
332
+ * @param options.signal - The signal to abort the request
287
333
  *
288
334
  * @returns The file instance
289
335
  * @throws {Error} If the file ID is not set
290
336
  * @throws {FetchError} If the metadata fetch fails
291
337
  */
292
- async populateMetadata() {
338
+ async populateMetadata(options) {
293
339
  try {
294
- this.metadata = await this.getFileMetadata();
340
+ this.metadata = await this.getFileMetadata({ signal: options?.signal });
295
341
  this.name = this.metadata.originalFileName;
296
342
  this.id = this.metadata.id;
297
343
  return this;
@@ -313,29 +359,34 @@ class VaultFile {
313
359
  }
314
360
  /**
315
361
  * Fetches the metadata of the file.
362
+ * @param options - The options for the request
363
+ * @param options.signal - The signal to abort the request
316
364
  *
317
365
  * @returns The metadata of the file
318
366
  * @throws {Error} If the file ID is not set
319
367
  * @throws {FetchError} If the metadata fetch fails
320
368
  */
321
- async getFileMetadata() {
369
+ async getFileMetadata(options) {
322
370
  if (!this.id) {
323
371
  throw new Error("File ID is not set");
324
372
  }
325
373
  const response = await this._fetch({
326
374
  method: "GET",
327
- path: `/v2/files/${this.id}/metadata`
375
+ path: `/v2/files/${this.id}/metadata`,
376
+ signal: options?.signal
328
377
  });
329
378
  return response;
330
379
  }
331
380
  /**
332
381
  * Fetches a upload URL for the file.
382
+ * @param options - The options for the request
383
+ * @param options.signal - The signal to abort the request
333
384
  *
334
385
  * @returns The upload URL for the file
335
386
  * @throws {Error} If the vault service returns an invalid response
336
387
  * @throws {FetchError} If the upload URL fetch fails
337
388
  */
338
- async getUploadUrl() {
389
+ async getUploadUrl(options) {
339
390
  if (this.lastUploadUrl && this.lastUploadUrl.expiresAt > /* @__PURE__ */ new Date()) {
340
391
  return this.lastUploadUrl.url;
341
392
  }
@@ -349,7 +400,8 @@ class VaultFile {
349
400
  }
350
401
  const response = await this._fetch({
351
402
  method: "PUT",
352
- path: `/v2/files/${this.id}`
403
+ path: `/v2/files/${this.id}`,
404
+ signal: options?.signal
353
405
  }).then(schemas.GetUploadUrlResponseV2.safeParse);
354
406
  if (!response.success) {
355
407
  throw new Error(`Invalid response from vault service. ${JSON.stringify(response.error)}`);
@@ -359,13 +411,15 @@ class VaultFile {
359
411
  }
360
412
  /**
361
413
  * Fetches a download URL for the file.
414
+ * @param options - The options for the request
415
+ * @param options.signal - The signal to abort the request
362
416
  *
363
417
  * @returns The download URL for the file
364
418
  * @throws {Error} If the vault service returns an invalid response
365
419
  * @throws {Error} If not file ID, name or content is set
366
420
  * @throws {FetchError} If the download URL fetch fails
367
421
  */
368
- async getDownloadUrl() {
422
+ async getDownloadUrl(options) {
369
423
  if (this.lastDownloadUrl && this.lastDownloadUrl.expiresAt > /* @__PURE__ */ new Date()) {
370
424
  return this.lastDownloadUrl.url;
371
425
  }
@@ -375,7 +429,8 @@ class VaultFile {
375
429
  const id = this.id ?? this.metadata?.id ?? (this.content ? await getFileHash(this.content) : this.name);
376
430
  const response = await this._fetch({
377
431
  method: "GET",
378
- path: `/v2/files/${id}`
432
+ path: `/v2/files/${id}`,
433
+ signal: options?.signal
379
434
  });
380
435
  this.lastDownloadUrl = { url: new URL(response.url), expiresAt: new Date(response.expiresAt) };
381
436
  return this.lastDownloadUrl.url;
@@ -398,11 +453,15 @@ class VaultFile {
398
453
  * ```
399
454
  *
400
455
  * @param file - The file to upload to the vault. If not provided, the file content will be taken from the `content` property.
456
+ * @param url - The URL to upload the file to. If not provided, the upload URL will be fetched from the vault.
457
+ * @param options - The options for the request
458
+ * @param options.signal - The signal to abort the request
459
+ *
401
460
  * @throws {FetchError} If the upload fails
402
461
  * @throws {Error} If the file content is not set and no file is provided
403
462
  * @returns Promise that resolves when upload is complete
404
463
  */
405
- async upload(file, url) {
464
+ async upload(file, url, options) {
406
465
  const content = file ?? this.content;
407
466
  if (!content) {
408
467
  throw new Error("Missing file content. Use fromContent() to create a file with content, or provide a file to upload.");
@@ -415,13 +474,15 @@ class VaultFile {
415
474
  await wrappedFetch(uploadUrl, {
416
475
  method: "PUT",
417
476
  body: content,
418
- headers
477
+ headers,
478
+ signal: options?.signal
419
479
  });
420
480
  }
421
- async download(responseType = "blob") {
422
- const downloadUrl = await this.getDownloadUrl();
481
+ async download(responseType = "blob", options) {
482
+ const downloadUrl = await this.getDownloadUrl({ signal: options?.signal });
423
483
  const response = await wrappedFetch(downloadUrl, {
424
- method: "GET"
484
+ method: "GET",
485
+ signal: options?.signal
425
486
  });
426
487
  const blob = await response.blob();
427
488
  if (responseType === "blob")
@@ -430,48 +491,19 @@ class VaultFile {
430
491
  }
431
492
  }
432
493
 
433
- var __defProp = Object.defineProperty;
434
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
435
- var __publicField = (obj, key, value) => {
436
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
437
- return value;
438
- };
439
- class DataTokenAuthStrategy {
440
- constructor(dataToken) {
441
- __publicField(this, "dataToken");
442
- this.dataToken = dataToken;
443
- }
444
- getHeaders() {
445
- return new Headers({
446
- "x-data-token": this.dataToken
447
- });
448
- }
449
- }
450
- class APIKeyAuthStrategy {
451
- constructor(apiKey) {
452
- __publicField(this, "apiKey");
453
- this.apiKey = apiKey;
454
- }
455
- getHeaders() {
456
- return new Headers({
457
- Authorization: this.apiKey
458
- });
459
- }
460
- }
461
-
462
494
  function vaultClient(config) {
463
- function createFromContent(name, content) {
495
+ function createFromContent(name, content, options) {
464
496
  return VaultFile.fromContent({
465
497
  name,
466
498
  content,
467
499
  config
468
- });
500
+ }, { signal: options?.signal });
469
501
  }
470
- function createFromReference(reference) {
502
+ function createFromReference(reference, options) {
471
503
  return VaultFile.fromVaultReference({
472
504
  reference,
473
505
  config
474
- });
506
+ }, { signal: options?.signal });
475
507
  }
476
508
  return { createFromContent, createFromReference };
477
509
  }
package/dist/index.d.cts CHANGED
@@ -14,6 +14,15 @@ declare class APIKeyAuthStrategy implements AuthStrategy {
14
14
  constructor(apiKey: string);
15
15
  }
16
16
 
17
+ declare class FetchError extends Error {
18
+ readonly message: string;
19
+ readonly url: string;
20
+ readonly method: string;
21
+ readonly response: Response;
22
+ constructor(message: string, url: string, method: string, response: Response);
23
+ static from(url: string, method: string, response: Response): Promise<FetchError>;
24
+ }
25
+
17
26
  type VaultConfig = {
18
27
  vaultUrl: string;
19
28
  authStrategy: AuthStrategy;
@@ -76,12 +85,18 @@ declare class VaultFile {
76
85
  * @param params.method - The method to use for the fetch
77
86
  * @param params.path - The path to fetch
78
87
  * @param params.body - The body of the request
88
+ * @param params.signal - The signal to abort the request
79
89
  * @returns The response from the vault
80
90
  * @throws {FetchError} If the fetch fails
81
91
  */
82
92
  private _fetch;
83
93
  /**
84
94
  * Creates a new file in the vault.
95
+ * @param metadata - The metadata for creating a file
96
+ * @param metadata.size - The size of the file
97
+ * @param metadata.mimeType - The mime type of the file
98
+ * @param options - The options for the request
99
+ * @param options.signal - The signal to abort the request
85
100
  *
86
101
  * @returns The metadata of the file
87
102
  * @throws {Error} If the file ID is not set
@@ -95,6 +110,9 @@ declare class VaultFile {
95
110
  * @param params.reference - The reference to the file in the vault
96
111
  * @param params.config - The configuration for the VaultFile
97
112
  * @param params.download - Whether to download the file content (default: false)
113
+ * @param options - The options for the request
114
+ * @param options.signal - The signal to abort the request
115
+ *
98
116
  * @returns A new VaultFile instance
99
117
  *
100
118
  * @example
@@ -128,6 +146,8 @@ declare class VaultFile {
128
146
  reference: string;
129
147
  config: VaultConfig;
130
148
  download?: boolean;
149
+ }, options?: {
150
+ signal?: AbortSignal;
131
151
  }): Promise<VaultFile>;
132
152
  /**
133
153
  * Creates a new VaultFile instance from given content.
@@ -137,6 +157,9 @@ declare class VaultFile {
137
157
  * @param params.content - The content of the file
138
158
  * @param params.config - The configuration for the VaultFile
139
159
  * @param params.upload - Whether to upload the file (default: false)
160
+ * @param options - The options for the request
161
+ * @param options.signal - The signal to abort the request
162
+ *
140
163
  * @returns A new VaultFile instance
141
164
  *
142
165
  * @example
@@ -174,15 +197,21 @@ declare class VaultFile {
174
197
  content: Blob | File;
175
198
  config: VaultConfig;
176
199
  upload?: boolean;
200
+ }, options?: {
201
+ signal?: AbortSignal;
177
202
  }): Promise<VaultFile>;
178
203
  /**
179
204
  * Populates the metadata of the file instance.
205
+ * @param options - The options for the request
206
+ * @param options.signal - The signal to abort the request
180
207
  *
181
208
  * @returns The file instance
182
209
  * @throws {Error} If the file ID is not set
183
210
  * @throws {FetchError} If the metadata fetch fails
184
211
  */
185
- populateMetadata(): Promise<this | undefined>;
212
+ populateMetadata(options?: {
213
+ signal?: AbortSignal;
214
+ }): Promise<this | undefined>;
186
215
  /**
187
216
  * Gets the vault reference for this file.
188
217
  *
@@ -192,29 +221,41 @@ declare class VaultFile {
192
221
  getVaultReference(): string;
193
222
  /**
194
223
  * Fetches the metadata of the file.
224
+ * @param options - The options for the request
225
+ * @param options.signal - The signal to abort the request
195
226
  *
196
227
  * @returns The metadata of the file
197
228
  * @throws {Error} If the file ID is not set
198
229
  * @throws {FetchError} If the metadata fetch fails
199
230
  */
200
- getFileMetadata(): Promise<FileMetadata>;
231
+ getFileMetadata(options?: {
232
+ signal?: AbortSignal;
233
+ }): Promise<FileMetadata>;
201
234
  /**
202
235
  * Fetches a upload URL for the file.
236
+ * @param options - The options for the request
237
+ * @param options.signal - The signal to abort the request
203
238
  *
204
239
  * @returns The upload URL for the file
205
240
  * @throws {Error} If the vault service returns an invalid response
206
241
  * @throws {FetchError} If the upload URL fetch fails
207
242
  */
208
- getUploadUrl(): Promise<URL>;
243
+ getUploadUrl(options?: {
244
+ signal?: AbortSignal;
245
+ }): Promise<URL>;
209
246
  /**
210
247
  * Fetches a download URL for the file.
248
+ * @param options - The options for the request
249
+ * @param options.signal - The signal to abort the request
211
250
  *
212
251
  * @returns The download URL for the file
213
252
  * @throws {Error} If the vault service returns an invalid response
214
253
  * @throws {Error} If not file ID, name or content is set
215
254
  * @throws {FetchError} If the download URL fetch fails
216
255
  */
217
- getDownloadUrl(): Promise<URL>;
256
+ getDownloadUrl(options?: {
257
+ signal?: AbortSignal;
258
+ }): Promise<URL>;
218
259
  /**
219
260
  * Uploads a file to the vault.
220
261
  *
@@ -233,15 +274,24 @@ declare class VaultFile {
233
274
  * ```
234
275
  *
235
276
  * @param file - The file to upload to the vault. If not provided, the file content will be taken from the `content` property.
277
+ * @param url - The URL to upload the file to. If not provided, the upload URL will be fetched from the vault.
278
+ * @param options - The options for the request
279
+ * @param options.signal - The signal to abort the request
280
+ *
236
281
  * @throws {FetchError} If the upload fails
237
282
  * @throws {Error} If the file content is not set and no file is provided
238
283
  * @returns Promise that resolves when upload is complete
239
284
  */
240
- upload(file?: Blob, url?: string): Promise<void>;
285
+ upload(file?: Blob, url?: string, options?: {
286
+ signal?: AbortSignal;
287
+ }): Promise<void>;
241
288
  /**
242
289
  * Downloads a file from the vault.
243
290
  *
244
291
  * @param responseType - The type of the response
292
+ * @param options - The options for the request
293
+ * @param options.signal - The signal to abort the request
294
+ *
245
295
  * @returns The response from the vault
246
296
  *
247
297
  * @example
@@ -250,22 +300,21 @@ declare class VaultFile {
250
300
  * const content = await vaultFile.download()
251
301
  * ```
252
302
  */
253
- download(responseType?: 'blob'): Promise<Blob>;
254
- download(responseType: 'base64'): Promise<string>;
255
- }
256
-
257
- declare class FetchError extends Error {
258
- readonly message: string;
259
- readonly url: string;
260
- readonly method: string;
261
- readonly response: Response;
262
- constructor(message: string, url: string, method: string, response: Response);
263
- static from(url: string, method: string, response: Response): Promise<FetchError>;
303
+ download(responseType?: 'blob', options?: {
304
+ signal?: AbortSignal;
305
+ }): Promise<Blob>;
306
+ download(responseType: 'base64', options?: {
307
+ signal?: AbortSignal;
308
+ }): Promise<string>;
264
309
  }
265
310
 
266
311
  declare function vaultClient(config: VaultConfig): {
267
- createFromContent: (name: string, content: Blob | File) => Promise<VaultFile>;
268
- createFromReference: (reference: string) => Promise<VaultFile>;
312
+ createFromContent: (name: string, content: Blob | File, options?: {
313
+ signal?: AbortSignal;
314
+ }) => Promise<VaultFile>;
315
+ createFromReference: (reference: string, options?: {
316
+ signal?: AbortSignal;
317
+ }) => Promise<VaultFile>;
269
318
  };
270
319
 
271
320
  export { APIKeyAuthStrategy, DataTokenAuthStrategy, FetchError, VaultFile, vaultClient };
package/dist/index.d.mts CHANGED
@@ -14,6 +14,15 @@ declare class APIKeyAuthStrategy implements AuthStrategy {
14
14
  constructor(apiKey: string);
15
15
  }
16
16
 
17
+ declare class FetchError extends Error {
18
+ readonly message: string;
19
+ readonly url: string;
20
+ readonly method: string;
21
+ readonly response: Response;
22
+ constructor(message: string, url: string, method: string, response: Response);
23
+ static from(url: string, method: string, response: Response): Promise<FetchError>;
24
+ }
25
+
17
26
  type VaultConfig = {
18
27
  vaultUrl: string;
19
28
  authStrategy: AuthStrategy;
@@ -76,12 +85,18 @@ declare class VaultFile {
76
85
  * @param params.method - The method to use for the fetch
77
86
  * @param params.path - The path to fetch
78
87
  * @param params.body - The body of the request
88
+ * @param params.signal - The signal to abort the request
79
89
  * @returns The response from the vault
80
90
  * @throws {FetchError} If the fetch fails
81
91
  */
82
92
  private _fetch;
83
93
  /**
84
94
  * Creates a new file in the vault.
95
+ * @param metadata - The metadata for creating a file
96
+ * @param metadata.size - The size of the file
97
+ * @param metadata.mimeType - The mime type of the file
98
+ * @param options - The options for the request
99
+ * @param options.signal - The signal to abort the request
85
100
  *
86
101
  * @returns The metadata of the file
87
102
  * @throws {Error} If the file ID is not set
@@ -95,6 +110,9 @@ declare class VaultFile {
95
110
  * @param params.reference - The reference to the file in the vault
96
111
  * @param params.config - The configuration for the VaultFile
97
112
  * @param params.download - Whether to download the file content (default: false)
113
+ * @param options - The options for the request
114
+ * @param options.signal - The signal to abort the request
115
+ *
98
116
  * @returns A new VaultFile instance
99
117
  *
100
118
  * @example
@@ -128,6 +146,8 @@ declare class VaultFile {
128
146
  reference: string;
129
147
  config: VaultConfig;
130
148
  download?: boolean;
149
+ }, options?: {
150
+ signal?: AbortSignal;
131
151
  }): Promise<VaultFile>;
132
152
  /**
133
153
  * Creates a new VaultFile instance from given content.
@@ -137,6 +157,9 @@ declare class VaultFile {
137
157
  * @param params.content - The content of the file
138
158
  * @param params.config - The configuration for the VaultFile
139
159
  * @param params.upload - Whether to upload the file (default: false)
160
+ * @param options - The options for the request
161
+ * @param options.signal - The signal to abort the request
162
+ *
140
163
  * @returns A new VaultFile instance
141
164
  *
142
165
  * @example
@@ -174,15 +197,21 @@ declare class VaultFile {
174
197
  content: Blob | File;
175
198
  config: VaultConfig;
176
199
  upload?: boolean;
200
+ }, options?: {
201
+ signal?: AbortSignal;
177
202
  }): Promise<VaultFile>;
178
203
  /**
179
204
  * Populates the metadata of the file instance.
205
+ * @param options - The options for the request
206
+ * @param options.signal - The signal to abort the request
180
207
  *
181
208
  * @returns The file instance
182
209
  * @throws {Error} If the file ID is not set
183
210
  * @throws {FetchError} If the metadata fetch fails
184
211
  */
185
- populateMetadata(): Promise<this | undefined>;
212
+ populateMetadata(options?: {
213
+ signal?: AbortSignal;
214
+ }): Promise<this | undefined>;
186
215
  /**
187
216
  * Gets the vault reference for this file.
188
217
  *
@@ -192,29 +221,41 @@ declare class VaultFile {
192
221
  getVaultReference(): string;
193
222
  /**
194
223
  * Fetches the metadata of the file.
224
+ * @param options - The options for the request
225
+ * @param options.signal - The signal to abort the request
195
226
  *
196
227
  * @returns The metadata of the file
197
228
  * @throws {Error} If the file ID is not set
198
229
  * @throws {FetchError} If the metadata fetch fails
199
230
  */
200
- getFileMetadata(): Promise<FileMetadata>;
231
+ getFileMetadata(options?: {
232
+ signal?: AbortSignal;
233
+ }): Promise<FileMetadata>;
201
234
  /**
202
235
  * Fetches a upload URL for the file.
236
+ * @param options - The options for the request
237
+ * @param options.signal - The signal to abort the request
203
238
  *
204
239
  * @returns The upload URL for the file
205
240
  * @throws {Error} If the vault service returns an invalid response
206
241
  * @throws {FetchError} If the upload URL fetch fails
207
242
  */
208
- getUploadUrl(): Promise<URL>;
243
+ getUploadUrl(options?: {
244
+ signal?: AbortSignal;
245
+ }): Promise<URL>;
209
246
  /**
210
247
  * Fetches a download URL for the file.
248
+ * @param options - The options for the request
249
+ * @param options.signal - The signal to abort the request
211
250
  *
212
251
  * @returns The download URL for the file
213
252
  * @throws {Error} If the vault service returns an invalid response
214
253
  * @throws {Error} If not file ID, name or content is set
215
254
  * @throws {FetchError} If the download URL fetch fails
216
255
  */
217
- getDownloadUrl(): Promise<URL>;
256
+ getDownloadUrl(options?: {
257
+ signal?: AbortSignal;
258
+ }): Promise<URL>;
218
259
  /**
219
260
  * Uploads a file to the vault.
220
261
  *
@@ -233,15 +274,24 @@ declare class VaultFile {
233
274
  * ```
234
275
  *
235
276
  * @param file - The file to upload to the vault. If not provided, the file content will be taken from the `content` property.
277
+ * @param url - The URL to upload the file to. If not provided, the upload URL will be fetched from the vault.
278
+ * @param options - The options for the request
279
+ * @param options.signal - The signal to abort the request
280
+ *
236
281
  * @throws {FetchError} If the upload fails
237
282
  * @throws {Error} If the file content is not set and no file is provided
238
283
  * @returns Promise that resolves when upload is complete
239
284
  */
240
- upload(file?: Blob, url?: string): Promise<void>;
285
+ upload(file?: Blob, url?: string, options?: {
286
+ signal?: AbortSignal;
287
+ }): Promise<void>;
241
288
  /**
242
289
  * Downloads a file from the vault.
243
290
  *
244
291
  * @param responseType - The type of the response
292
+ * @param options - The options for the request
293
+ * @param options.signal - The signal to abort the request
294
+ *
245
295
  * @returns The response from the vault
246
296
  *
247
297
  * @example
@@ -250,22 +300,21 @@ declare class VaultFile {
250
300
  * const content = await vaultFile.download()
251
301
  * ```
252
302
  */
253
- download(responseType?: 'blob'): Promise<Blob>;
254
- download(responseType: 'base64'): Promise<string>;
255
- }
256
-
257
- declare class FetchError extends Error {
258
- readonly message: string;
259
- readonly url: string;
260
- readonly method: string;
261
- readonly response: Response;
262
- constructor(message: string, url: string, method: string, response: Response);
263
- static from(url: string, method: string, response: Response): Promise<FetchError>;
303
+ download(responseType?: 'blob', options?: {
304
+ signal?: AbortSignal;
305
+ }): Promise<Blob>;
306
+ download(responseType: 'base64', options?: {
307
+ signal?: AbortSignal;
308
+ }): Promise<string>;
264
309
  }
265
310
 
266
311
  declare function vaultClient(config: VaultConfig): {
267
- createFromContent: (name: string, content: Blob | File) => Promise<VaultFile>;
268
- createFromReference: (reference: string) => Promise<VaultFile>;
312
+ createFromContent: (name: string, content: Blob | File, options?: {
313
+ signal?: AbortSignal;
314
+ }) => Promise<VaultFile>;
315
+ createFromReference: (reference: string, options?: {
316
+ signal?: AbortSignal;
317
+ }) => Promise<VaultFile>;
269
318
  };
270
319
 
271
320
  export { APIKeyAuthStrategy, DataTokenAuthStrategy, FetchError, VaultFile, vaultClient };
package/dist/index.d.ts CHANGED
@@ -14,6 +14,15 @@ declare class APIKeyAuthStrategy implements AuthStrategy {
14
14
  constructor(apiKey: string);
15
15
  }
16
16
 
17
+ declare class FetchError extends Error {
18
+ readonly message: string;
19
+ readonly url: string;
20
+ readonly method: string;
21
+ readonly response: Response;
22
+ constructor(message: string, url: string, method: string, response: Response);
23
+ static from(url: string, method: string, response: Response): Promise<FetchError>;
24
+ }
25
+
17
26
  type VaultConfig = {
18
27
  vaultUrl: string;
19
28
  authStrategy: AuthStrategy;
@@ -76,12 +85,18 @@ declare class VaultFile {
76
85
  * @param params.method - The method to use for the fetch
77
86
  * @param params.path - The path to fetch
78
87
  * @param params.body - The body of the request
88
+ * @param params.signal - The signal to abort the request
79
89
  * @returns The response from the vault
80
90
  * @throws {FetchError} If the fetch fails
81
91
  */
82
92
  private _fetch;
83
93
  /**
84
94
  * Creates a new file in the vault.
95
+ * @param metadata - The metadata for creating a file
96
+ * @param metadata.size - The size of the file
97
+ * @param metadata.mimeType - The mime type of the file
98
+ * @param options - The options for the request
99
+ * @param options.signal - The signal to abort the request
85
100
  *
86
101
  * @returns The metadata of the file
87
102
  * @throws {Error} If the file ID is not set
@@ -95,6 +110,9 @@ declare class VaultFile {
95
110
  * @param params.reference - The reference to the file in the vault
96
111
  * @param params.config - The configuration for the VaultFile
97
112
  * @param params.download - Whether to download the file content (default: false)
113
+ * @param options - The options for the request
114
+ * @param options.signal - The signal to abort the request
115
+ *
98
116
  * @returns A new VaultFile instance
99
117
  *
100
118
  * @example
@@ -128,6 +146,8 @@ declare class VaultFile {
128
146
  reference: string;
129
147
  config: VaultConfig;
130
148
  download?: boolean;
149
+ }, options?: {
150
+ signal?: AbortSignal;
131
151
  }): Promise<VaultFile>;
132
152
  /**
133
153
  * Creates a new VaultFile instance from given content.
@@ -137,6 +157,9 @@ declare class VaultFile {
137
157
  * @param params.content - The content of the file
138
158
  * @param params.config - The configuration for the VaultFile
139
159
  * @param params.upload - Whether to upload the file (default: false)
160
+ * @param options - The options for the request
161
+ * @param options.signal - The signal to abort the request
162
+ *
140
163
  * @returns A new VaultFile instance
141
164
  *
142
165
  * @example
@@ -174,15 +197,21 @@ declare class VaultFile {
174
197
  content: Blob | File;
175
198
  config: VaultConfig;
176
199
  upload?: boolean;
200
+ }, options?: {
201
+ signal?: AbortSignal;
177
202
  }): Promise<VaultFile>;
178
203
  /**
179
204
  * Populates the metadata of the file instance.
205
+ * @param options - The options for the request
206
+ * @param options.signal - The signal to abort the request
180
207
  *
181
208
  * @returns The file instance
182
209
  * @throws {Error} If the file ID is not set
183
210
  * @throws {FetchError} If the metadata fetch fails
184
211
  */
185
- populateMetadata(): Promise<this | undefined>;
212
+ populateMetadata(options?: {
213
+ signal?: AbortSignal;
214
+ }): Promise<this | undefined>;
186
215
  /**
187
216
  * Gets the vault reference for this file.
188
217
  *
@@ -192,29 +221,41 @@ declare class VaultFile {
192
221
  getVaultReference(): string;
193
222
  /**
194
223
  * Fetches the metadata of the file.
224
+ * @param options - The options for the request
225
+ * @param options.signal - The signal to abort the request
195
226
  *
196
227
  * @returns The metadata of the file
197
228
  * @throws {Error} If the file ID is not set
198
229
  * @throws {FetchError} If the metadata fetch fails
199
230
  */
200
- getFileMetadata(): Promise<FileMetadata>;
231
+ getFileMetadata(options?: {
232
+ signal?: AbortSignal;
233
+ }): Promise<FileMetadata>;
201
234
  /**
202
235
  * Fetches a upload URL for the file.
236
+ * @param options - The options for the request
237
+ * @param options.signal - The signal to abort the request
203
238
  *
204
239
  * @returns The upload URL for the file
205
240
  * @throws {Error} If the vault service returns an invalid response
206
241
  * @throws {FetchError} If the upload URL fetch fails
207
242
  */
208
- getUploadUrl(): Promise<URL>;
243
+ getUploadUrl(options?: {
244
+ signal?: AbortSignal;
245
+ }): Promise<URL>;
209
246
  /**
210
247
  * Fetches a download URL for the file.
248
+ * @param options - The options for the request
249
+ * @param options.signal - The signal to abort the request
211
250
  *
212
251
  * @returns The download URL for the file
213
252
  * @throws {Error} If the vault service returns an invalid response
214
253
  * @throws {Error} If not file ID, name or content is set
215
254
  * @throws {FetchError} If the download URL fetch fails
216
255
  */
217
- getDownloadUrl(): Promise<URL>;
256
+ getDownloadUrl(options?: {
257
+ signal?: AbortSignal;
258
+ }): Promise<URL>;
218
259
  /**
219
260
  * Uploads a file to the vault.
220
261
  *
@@ -233,15 +274,24 @@ declare class VaultFile {
233
274
  * ```
234
275
  *
235
276
  * @param file - The file to upload to the vault. If not provided, the file content will be taken from the `content` property.
277
+ * @param url - The URL to upload the file to. If not provided, the upload URL will be fetched from the vault.
278
+ * @param options - The options for the request
279
+ * @param options.signal - The signal to abort the request
280
+ *
236
281
  * @throws {FetchError} If the upload fails
237
282
  * @throws {Error} If the file content is not set and no file is provided
238
283
  * @returns Promise that resolves when upload is complete
239
284
  */
240
- upload(file?: Blob, url?: string): Promise<void>;
285
+ upload(file?: Blob, url?: string, options?: {
286
+ signal?: AbortSignal;
287
+ }): Promise<void>;
241
288
  /**
242
289
  * Downloads a file from the vault.
243
290
  *
244
291
  * @param responseType - The type of the response
292
+ * @param options - The options for the request
293
+ * @param options.signal - The signal to abort the request
294
+ *
245
295
  * @returns The response from the vault
246
296
  *
247
297
  * @example
@@ -250,22 +300,21 @@ declare class VaultFile {
250
300
  * const content = await vaultFile.download()
251
301
  * ```
252
302
  */
253
- download(responseType?: 'blob'): Promise<Blob>;
254
- download(responseType: 'base64'): Promise<string>;
255
- }
256
-
257
- declare class FetchError extends Error {
258
- readonly message: string;
259
- readonly url: string;
260
- readonly method: string;
261
- readonly response: Response;
262
- constructor(message: string, url: string, method: string, response: Response);
263
- static from(url: string, method: string, response: Response): Promise<FetchError>;
303
+ download(responseType?: 'blob', options?: {
304
+ signal?: AbortSignal;
305
+ }): Promise<Blob>;
306
+ download(responseType: 'base64', options?: {
307
+ signal?: AbortSignal;
308
+ }): Promise<string>;
264
309
  }
265
310
 
266
311
  declare function vaultClient(config: VaultConfig): {
267
- createFromContent: (name: string, content: Blob | File) => Promise<VaultFile>;
268
- createFromReference: (reference: string) => Promise<VaultFile>;
312
+ createFromContent: (name: string, content: Blob | File, options?: {
313
+ signal?: AbortSignal;
314
+ }) => Promise<VaultFile>;
315
+ createFromReference: (reference: string, options?: {
316
+ signal?: AbortSignal;
317
+ }) => Promise<VaultFile>;
269
318
  };
270
319
 
271
320
  export { APIKeyAuthStrategy, DataTokenAuthStrategy, FetchError, VaultFile, vaultClient };
package/dist/index.mjs CHANGED
@@ -2,6 +2,35 @@ import { GetUploadUrlResponseV2, GetDownloadUrlResponse } from '@meistrari/vault
2
2
  import { fileTypeFromBlob } from 'file-type';
3
3
  import { lookup } from 'mime-types';
4
4
 
5
+ var __defProp$1 = Object.defineProperty;
6
+ var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
7
+ var __publicField$1 = (obj, key, value) => {
8
+ __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
9
+ return value;
10
+ };
11
+ class DataTokenAuthStrategy {
12
+ constructor(dataToken) {
13
+ __publicField$1(this, "dataToken");
14
+ this.dataToken = dataToken;
15
+ }
16
+ getHeaders() {
17
+ return new Headers({
18
+ "x-data-token": this.dataToken
19
+ });
20
+ }
21
+ }
22
+ class APIKeyAuthStrategy {
23
+ constructor(apiKey) {
24
+ __publicField$1(this, "apiKey");
25
+ this.apiKey = apiKey;
26
+ }
27
+ getHeaders() {
28
+ return new Headers({
29
+ Authorization: this.apiKey
30
+ });
31
+ }
32
+ }
33
+
5
34
  class FetchError extends Error {
6
35
  constructor(message, url, method, response) {
7
36
  super(message);
@@ -54,10 +83,10 @@ async function detectFileMimeType(blob) {
54
83
  return void 0;
55
84
  }
56
85
 
57
- var __defProp$1 = Object.defineProperty;
58
- var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
59
- var __publicField$1 = (obj, key, value) => {
60
- __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
86
+ var __defProp = Object.defineProperty;
87
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
88
+ var __publicField = (obj, key, value) => {
89
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
61
90
  return value;
62
91
  };
63
92
  const compatibilityDate = "2025-05-19";
@@ -86,13 +115,13 @@ class VaultFile {
86
115
  * @param params.metadata - The metadata of the file
87
116
  */
88
117
  constructor({ config, content, id, name, metadata }) {
89
- __publicField$1(this, "id");
90
- __publicField$1(this, "name");
91
- __publicField$1(this, "metadata");
92
- __publicField$1(this, "config");
93
- __publicField$1(this, "content");
94
- __publicField$1(this, "lastDownloadUrl");
95
- __publicField$1(this, "lastUploadUrl");
118
+ __publicField(this, "id");
119
+ __publicField(this, "name");
120
+ __publicField(this, "metadata");
121
+ __publicField(this, "config");
122
+ __publicField(this, "content");
123
+ __publicField(this, "lastDownloadUrl");
124
+ __publicField(this, "lastUploadUrl");
96
125
  this.config = config;
97
126
  this.content = content;
98
127
  this.id = id;
@@ -114,38 +143,46 @@ class VaultFile {
114
143
  * @param params.method - The method to use for the fetch
115
144
  * @param params.path - The path to fetch
116
145
  * @param params.body - The body of the request
146
+ * @param params.signal - The signal to abort the request
117
147
  * @returns The response from the vault
118
148
  * @throws {FetchError} If the fetch fails
119
149
  */
120
150
  async _fetch(params) {
121
- const { method, path, body } = params;
151
+ const { method, path, body, signal } = params;
122
152
  const url = new URL(this.config.vaultUrl + path).toString();
123
153
  const headers = new Headers(this.headers);
124
154
  headers.set("x-compatibility-date", compatibilityDate);
125
155
  const response = await wrappedFetch(url, {
126
156
  method,
127
157
  body,
128
- headers
158
+ headers,
159
+ signal
129
160
  });
130
161
  const content = await response.json();
131
162
  return content;
132
163
  }
133
164
  /**
134
165
  * Creates a new file in the vault.
166
+ * @param metadata - The metadata for creating a file
167
+ * @param metadata.size - The size of the file
168
+ * @param metadata.mimeType - The mime type of the file
169
+ * @param options - The options for the request
170
+ * @param options.signal - The signal to abort the request
135
171
  *
136
172
  * @returns The metadata of the file
137
173
  * @throws {Error} If the file ID is not set
138
174
  * @throws {FetchError} If the metadata fetch fails
139
175
  */
140
- async _createFile(params = {}) {
176
+ async _createFile(metadata = {}, options) {
141
177
  const response = await this._fetch({
142
178
  method: "POST",
143
179
  path: `/v2/files`,
144
180
  body: JSON.stringify({
145
- ...params,
181
+ ...metadata,
146
182
  fileName: this.name,
147
183
  sha256sum: this.id ?? this.metadata?.id ?? (this.content ? await getFileHash(this.content) : void 0)
148
- })
184
+ }),
185
+ signal: options?.signal
149
186
  }).then((data) => GetUploadUrlResponseV2.safeParse(data));
150
187
  if (!response.success) {
151
188
  throw new Error(`Invalid response from vault service. ${JSON.stringify(response.error)}`);
@@ -162,6 +199,9 @@ class VaultFile {
162
199
  * @param params.reference - The reference to the file in the vault
163
200
  * @param params.config - The configuration for the VaultFile
164
201
  * @param params.download - Whether to download the file content (default: false)
202
+ * @param options - The options for the request
203
+ * @param options.signal - The signal to abort the request
204
+ *
165
205
  * @returns A new VaultFile instance
166
206
  *
167
207
  * @example
@@ -191,13 +231,14 @@ class VaultFile {
191
231
  * const content = vaultFile.content
192
232
  * ```
193
233
  */
194
- static async fromVaultReference(params) {
234
+ static async fromVaultReference(params, options) {
195
235
  const { reference, config, download = false } = params;
196
236
  const { vaultUrl, authStrategy } = config;
197
237
  const id = removeVaultPrefix(reference);
198
238
  const response = await wrappedFetch(`${vaultUrl}/v2/files/${id}`, {
199
239
  method: "GET",
200
- headers: authStrategy.getHeaders()
240
+ headers: authStrategy.getHeaders(),
241
+ signal: options?.signal
201
242
  }).then((response2) => response2.json()).then((data) => GetDownloadUrlResponse.safeParse(data));
202
243
  if (!response.success) {
203
244
  throw new Error("Invalid response from vault service");
@@ -212,7 +253,7 @@ class VaultFile {
212
253
  name: response.data.metadata?.originalFileName
213
254
  };
214
255
  if (download) {
215
- await wrappedFetch(response.data.url, { method: "GET" }).then((response2) => response2.blob()).then((blob) => fileParams.content = blob);
256
+ await wrappedFetch(response.data.url, { method: "GET", signal: options?.signal }).then((response2) => response2.blob()).then((blob) => fileParams.content = blob);
216
257
  }
217
258
  return new VaultFile(fileParams);
218
259
  }
@@ -224,6 +265,9 @@ class VaultFile {
224
265
  * @param params.content - The content of the file
225
266
  * @param params.config - The configuration for the VaultFile
226
267
  * @param params.upload - Whether to upload the file (default: false)
268
+ * @param options - The options for the request
269
+ * @param options.signal - The signal to abort the request
270
+ *
227
271
  * @returns A new VaultFile instance
228
272
  *
229
273
  * @example
@@ -256,7 +300,7 @@ class VaultFile {
256
300
  * })
257
301
  * ```
258
302
  */
259
- static async fromContent(params) {
303
+ static async fromContent(params, options) {
260
304
  const { name, content, config, upload = false } = params;
261
305
  const { vaultUrl, authStrategy } = config;
262
306
  const sha256sum = await getFileHash(content);
@@ -274,22 +318,24 @@ class VaultFile {
274
318
  const createdFile = await file._createFile({
275
319
  size,
276
320
  mimeType
277
- });
321
+ }, { signal: options?.signal });
278
322
  if (upload) {
279
- await file.upload(file.content, createdFile.uploadUrl);
323
+ await file.upload(file.content, createdFile.uploadUrl, { signal: options?.signal });
280
324
  }
281
325
  return file;
282
326
  }
283
327
  /**
284
328
  * Populates the metadata of the file instance.
329
+ * @param options - The options for the request
330
+ * @param options.signal - The signal to abort the request
285
331
  *
286
332
  * @returns The file instance
287
333
  * @throws {Error} If the file ID is not set
288
334
  * @throws {FetchError} If the metadata fetch fails
289
335
  */
290
- async populateMetadata() {
336
+ async populateMetadata(options) {
291
337
  try {
292
- this.metadata = await this.getFileMetadata();
338
+ this.metadata = await this.getFileMetadata({ signal: options?.signal });
293
339
  this.name = this.metadata.originalFileName;
294
340
  this.id = this.metadata.id;
295
341
  return this;
@@ -311,29 +357,34 @@ class VaultFile {
311
357
  }
312
358
  /**
313
359
  * Fetches the metadata of the file.
360
+ * @param options - The options for the request
361
+ * @param options.signal - The signal to abort the request
314
362
  *
315
363
  * @returns The metadata of the file
316
364
  * @throws {Error} If the file ID is not set
317
365
  * @throws {FetchError} If the metadata fetch fails
318
366
  */
319
- async getFileMetadata() {
367
+ async getFileMetadata(options) {
320
368
  if (!this.id) {
321
369
  throw new Error("File ID is not set");
322
370
  }
323
371
  const response = await this._fetch({
324
372
  method: "GET",
325
- path: `/v2/files/${this.id}/metadata`
373
+ path: `/v2/files/${this.id}/metadata`,
374
+ signal: options?.signal
326
375
  });
327
376
  return response;
328
377
  }
329
378
  /**
330
379
  * Fetches a upload URL for the file.
380
+ * @param options - The options for the request
381
+ * @param options.signal - The signal to abort the request
331
382
  *
332
383
  * @returns The upload URL for the file
333
384
  * @throws {Error} If the vault service returns an invalid response
334
385
  * @throws {FetchError} If the upload URL fetch fails
335
386
  */
336
- async getUploadUrl() {
387
+ async getUploadUrl(options) {
337
388
  if (this.lastUploadUrl && this.lastUploadUrl.expiresAt > /* @__PURE__ */ new Date()) {
338
389
  return this.lastUploadUrl.url;
339
390
  }
@@ -347,7 +398,8 @@ class VaultFile {
347
398
  }
348
399
  const response = await this._fetch({
349
400
  method: "PUT",
350
- path: `/v2/files/${this.id}`
401
+ path: `/v2/files/${this.id}`,
402
+ signal: options?.signal
351
403
  }).then(GetUploadUrlResponseV2.safeParse);
352
404
  if (!response.success) {
353
405
  throw new Error(`Invalid response from vault service. ${JSON.stringify(response.error)}`);
@@ -357,13 +409,15 @@ class VaultFile {
357
409
  }
358
410
  /**
359
411
  * Fetches a download URL for the file.
412
+ * @param options - The options for the request
413
+ * @param options.signal - The signal to abort the request
360
414
  *
361
415
  * @returns The download URL for the file
362
416
  * @throws {Error} If the vault service returns an invalid response
363
417
  * @throws {Error} If not file ID, name or content is set
364
418
  * @throws {FetchError} If the download URL fetch fails
365
419
  */
366
- async getDownloadUrl() {
420
+ async getDownloadUrl(options) {
367
421
  if (this.lastDownloadUrl && this.lastDownloadUrl.expiresAt > /* @__PURE__ */ new Date()) {
368
422
  return this.lastDownloadUrl.url;
369
423
  }
@@ -373,7 +427,8 @@ class VaultFile {
373
427
  const id = this.id ?? this.metadata?.id ?? (this.content ? await getFileHash(this.content) : this.name);
374
428
  const response = await this._fetch({
375
429
  method: "GET",
376
- path: `/v2/files/${id}`
430
+ path: `/v2/files/${id}`,
431
+ signal: options?.signal
377
432
  });
378
433
  this.lastDownloadUrl = { url: new URL(response.url), expiresAt: new Date(response.expiresAt) };
379
434
  return this.lastDownloadUrl.url;
@@ -396,11 +451,15 @@ class VaultFile {
396
451
  * ```
397
452
  *
398
453
  * @param file - The file to upload to the vault. If not provided, the file content will be taken from the `content` property.
454
+ * @param url - The URL to upload the file to. If not provided, the upload URL will be fetched from the vault.
455
+ * @param options - The options for the request
456
+ * @param options.signal - The signal to abort the request
457
+ *
399
458
  * @throws {FetchError} If the upload fails
400
459
  * @throws {Error} If the file content is not set and no file is provided
401
460
  * @returns Promise that resolves when upload is complete
402
461
  */
403
- async upload(file, url) {
462
+ async upload(file, url, options) {
404
463
  const content = file ?? this.content;
405
464
  if (!content) {
406
465
  throw new Error("Missing file content. Use fromContent() to create a file with content, or provide a file to upload.");
@@ -413,13 +472,15 @@ class VaultFile {
413
472
  await wrappedFetch(uploadUrl, {
414
473
  method: "PUT",
415
474
  body: content,
416
- headers
475
+ headers,
476
+ signal: options?.signal
417
477
  });
418
478
  }
419
- async download(responseType = "blob") {
420
- const downloadUrl = await this.getDownloadUrl();
479
+ async download(responseType = "blob", options) {
480
+ const downloadUrl = await this.getDownloadUrl({ signal: options?.signal });
421
481
  const response = await wrappedFetch(downloadUrl, {
422
- method: "GET"
482
+ method: "GET",
483
+ signal: options?.signal
423
484
  });
424
485
  const blob = await response.blob();
425
486
  if (responseType === "blob")
@@ -428,48 +489,19 @@ class VaultFile {
428
489
  }
429
490
  }
430
491
 
431
- var __defProp = Object.defineProperty;
432
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
433
- var __publicField = (obj, key, value) => {
434
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
435
- return value;
436
- };
437
- class DataTokenAuthStrategy {
438
- constructor(dataToken) {
439
- __publicField(this, "dataToken");
440
- this.dataToken = dataToken;
441
- }
442
- getHeaders() {
443
- return new Headers({
444
- "x-data-token": this.dataToken
445
- });
446
- }
447
- }
448
- class APIKeyAuthStrategy {
449
- constructor(apiKey) {
450
- __publicField(this, "apiKey");
451
- this.apiKey = apiKey;
452
- }
453
- getHeaders() {
454
- return new Headers({
455
- Authorization: this.apiKey
456
- });
457
- }
458
- }
459
-
460
492
  function vaultClient(config) {
461
- function createFromContent(name, content) {
493
+ function createFromContent(name, content, options) {
462
494
  return VaultFile.fromContent({
463
495
  name,
464
496
  content,
465
497
  config
466
- });
498
+ }, { signal: options?.signal });
467
499
  }
468
- function createFromReference(reference) {
500
+ function createFromReference(reference, options) {
469
501
  return VaultFile.fromVaultReference({
470
502
  reference,
471
503
  config
472
- });
504
+ }, { signal: options?.signal });
473
505
  }
474
506
  return { createFromContent, createFromReference };
475
507
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meistrari/vault-sdk",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "license": "UNLICENSED",
5
5
  "repository": {
6
6
  "type": "git",