@meistrari/vault-sdk 1.1.0 → 1.3.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,63 +474,52 @@ 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")
428
489
  return blob;
429
490
  return await blobToBase64(blob);
430
491
  }
431
- }
432
-
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
492
+ /**
493
+ * Deletes the file from the vault.
494
+ * @param options - The options for the request
495
+ * @param options.signal - The signal to abort the request
496
+ *
497
+ */
498
+ async delete(options) {
499
+ if (!this.id) {
500
+ throw new Error("File ID is not set");
501
+ }
502
+ await this._fetch({
503
+ method: "DELETE",
504
+ path: `/v2/files/${this.id}`,
505
+ signal: options?.signal
458
506
  });
459
507
  }
460
508
  }
461
509
 
462
510
  function vaultClient(config) {
463
- function createFromContent(name, content) {
511
+ function createFromContent(name, content, options) {
464
512
  return VaultFile.fromContent({
465
513
  name,
466
514
  content,
467
515
  config
468
- });
516
+ }, { signal: options?.signal });
469
517
  }
470
- function createFromReference(reference) {
518
+ function createFromReference(reference, options) {
471
519
  return VaultFile.fromVaultReference({
472
520
  reference,
473
521
  config
474
- });
522
+ }, { signal: options?.signal });
475
523
  }
476
524
  return { createFromContent, createFromReference };
477
525
  }
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,30 @@ 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>;
309
+ /**
310
+ * Deletes the file from the vault.
311
+ * @param options - The options for the request
312
+ * @param options.signal - The signal to abort the request
313
+ *
314
+ */
315
+ delete(options?: {
316
+ signal?: AbortSignal;
317
+ }): Promise<void>;
264
318
  }
265
319
 
266
320
  declare function vaultClient(config: VaultConfig): {
267
- createFromContent: (name: string, content: Blob | File) => Promise<VaultFile>;
268
- createFromReference: (reference: string) => Promise<VaultFile>;
321
+ createFromContent: (name: string, content: Blob | File, options?: {
322
+ signal?: AbortSignal;
323
+ }) => Promise<VaultFile>;
324
+ createFromReference: (reference: string, options?: {
325
+ signal?: AbortSignal;
326
+ }) => Promise<VaultFile>;
269
327
  };
270
328
 
271
329
  export { APIKeyAuthStrategy, DataTokenAuthStrategy, FetchError, VaultFile, vaultClient };