@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 +114 -66
- package/dist/index.d.cts +76 -18
- package/dist/index.d.mts +76 -18
- package/dist/index.d.ts +76 -18
- package/dist/index.mjs +114 -66
- package/package.json +1 -1
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
|
|
58
|
-
var __defNormalProp
|
|
59
|
-
var __publicField
|
|
60
|
-
__defNormalProp
|
|
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
|
|
90
|
-
__publicField
|
|
91
|
-
__publicField
|
|
92
|
-
__publicField
|
|
93
|
-
__publicField
|
|
94
|
-
__publicField
|
|
95
|
-
__publicField
|
|
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(
|
|
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
|
-
...
|
|
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,63 +472,52 @@ 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")
|
|
426
487
|
return blob;
|
|
427
488
|
return await blobToBase64(blob);
|
|
428
489
|
}
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
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
|
|
490
|
+
/**
|
|
491
|
+
* Deletes the file from the vault.
|
|
492
|
+
* @param options - The options for the request
|
|
493
|
+
* @param options.signal - The signal to abort the request
|
|
494
|
+
*
|
|
495
|
+
*/
|
|
496
|
+
async delete(options) {
|
|
497
|
+
if (!this.id) {
|
|
498
|
+
throw new Error("File ID is not set");
|
|
499
|
+
}
|
|
500
|
+
await this._fetch({
|
|
501
|
+
method: "DELETE",
|
|
502
|
+
path: `/v2/files/${this.id}`,
|
|
503
|
+
signal: options?.signal
|
|
456
504
|
});
|
|
457
505
|
}
|
|
458
506
|
}
|
|
459
507
|
|
|
460
508
|
function vaultClient(config) {
|
|
461
|
-
function createFromContent(name, content) {
|
|
509
|
+
function createFromContent(name, content, options) {
|
|
462
510
|
return VaultFile.fromContent({
|
|
463
511
|
name,
|
|
464
512
|
content,
|
|
465
513
|
config
|
|
466
|
-
});
|
|
514
|
+
}, { signal: options?.signal });
|
|
467
515
|
}
|
|
468
|
-
function createFromReference(reference) {
|
|
516
|
+
function createFromReference(reference, options) {
|
|
469
517
|
return VaultFile.fromVaultReference({
|
|
470
518
|
reference,
|
|
471
519
|
config
|
|
472
|
-
});
|
|
520
|
+
}, { signal: options?.signal });
|
|
473
521
|
}
|
|
474
522
|
return { createFromContent, createFromReference };
|
|
475
523
|
}
|