@meistrari/vault-sdk 1.4.1 → 1.4.3
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 +48 -22
- package/dist/index.d.cts +6 -4
- package/dist/index.d.mts +6 -4
- package/dist/index.d.ts +6 -4
- package/dist/index.mjs +48 -22
- package/package.json +47 -47
package/dist/index.cjs
CHANGED
|
@@ -50,6 +50,19 @@ ${text}`, url, method, response);
|
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
+
function resolveBaseUrl(baseUrl) {
|
|
54
|
+
if (baseUrl.endsWith("/")) {
|
|
55
|
+
return baseUrl;
|
|
56
|
+
}
|
|
57
|
+
return `${baseUrl}/`;
|
|
58
|
+
}
|
|
59
|
+
function resolveConfig(config) {
|
|
60
|
+
return {
|
|
61
|
+
...config,
|
|
62
|
+
vaultUrl: resolveBaseUrl(config.vaultUrl)
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
53
66
|
var __defProp$1 = Object.defineProperty;
|
|
54
67
|
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
55
68
|
var __publicField$1 = (obj, key, value) => {
|
|
@@ -57,7 +70,7 @@ var __publicField$1 = (obj, key, value) => {
|
|
|
57
70
|
return value;
|
|
58
71
|
};
|
|
59
72
|
class Permalink {
|
|
60
|
-
constructor(
|
|
73
|
+
constructor(vaultConfig, params) {
|
|
61
74
|
__publicField$1(this, "config");
|
|
62
75
|
__publicField$1(this, "id");
|
|
63
76
|
__publicField$1(this, "workspaceId");
|
|
@@ -65,6 +78,8 @@ class Permalink {
|
|
|
65
78
|
__publicField$1(this, "createdBy");
|
|
66
79
|
__publicField$1(this, "expiresAt");
|
|
67
80
|
__publicField$1(this, "fileId");
|
|
81
|
+
__publicField$1(this, "baseUrl");
|
|
82
|
+
const config = resolveConfig(vaultConfig);
|
|
68
83
|
this.config = config;
|
|
69
84
|
this.id = params.id;
|
|
70
85
|
this.workspaceId = params.workspaceId;
|
|
@@ -72,12 +87,13 @@ class Permalink {
|
|
|
72
87
|
this.createdBy = params.createdBy;
|
|
73
88
|
this.expiresAt = params.expiresAt ? new Date(params.expiresAt) : null;
|
|
74
89
|
this.fileId = params.fileId;
|
|
90
|
+
this.baseUrl = config.vaultUrl;
|
|
75
91
|
}
|
|
76
92
|
/**
|
|
77
93
|
* The URL for the permalink.
|
|
78
94
|
*/
|
|
79
95
|
get url() {
|
|
80
|
-
return new URL(
|
|
96
|
+
return new URL(`permalinks/${this.id}`, this.baseUrl);
|
|
81
97
|
}
|
|
82
98
|
/**
|
|
83
99
|
* Get a new permalink instance from its ID.
|
|
@@ -86,8 +102,9 @@ class Permalink {
|
|
|
86
102
|
* @param id - The permalink ID.
|
|
87
103
|
* @returns The permalink.
|
|
88
104
|
*/
|
|
89
|
-
static async fromId(
|
|
90
|
-
const
|
|
105
|
+
static async fromId(vaultConfig, id) {
|
|
106
|
+
const config = resolveConfig(vaultConfig);
|
|
107
|
+
const response = await fetch(new URL(`permalinks/${id}/metadata`, config.vaultUrl), {
|
|
91
108
|
headers: config.authStrategy.getHeaders()
|
|
92
109
|
});
|
|
93
110
|
if (!response.ok) {
|
|
@@ -110,14 +127,15 @@ class Permalink {
|
|
|
110
127
|
*
|
|
111
128
|
* @returns The permalink.
|
|
112
129
|
*/
|
|
113
|
-
static async create(
|
|
130
|
+
static async create(vaultConfig, params, options) {
|
|
131
|
+
const config = resolveConfig(vaultConfig);
|
|
114
132
|
const { expiresIn } = params;
|
|
115
133
|
const expiresAt = expiresIn ? new Date(Date.now() + expiresIn * 1e3) : void 0;
|
|
116
134
|
const headers = config.authStrategy.getHeaders();
|
|
117
135
|
headers.append("x-compatibility-date", "2025-07-29");
|
|
118
136
|
headers.append("content-type", "application/json");
|
|
119
137
|
const body = expiresAt ? JSON.stringify({ expiresAt: expiresAt.toISOString() }) : "{}";
|
|
120
|
-
const response = await fetch(
|
|
138
|
+
const response = await fetch(new URL(`files/${params.fileId}/permalinks`, config.vaultUrl), {
|
|
121
139
|
method: "POST",
|
|
122
140
|
signal: options?.signal,
|
|
123
141
|
headers,
|
|
@@ -136,7 +154,7 @@ class Permalink {
|
|
|
136
154
|
* @param options.signal - The signal to abort the request.
|
|
137
155
|
*/
|
|
138
156
|
async delete(options) {
|
|
139
|
-
const response = await fetch(
|
|
157
|
+
const response = await fetch(new URL(`permalinks/${this.id}`, this.baseUrl), {
|
|
140
158
|
method: "DELETE",
|
|
141
159
|
signal: options?.signal,
|
|
142
160
|
headers: this.config.authStrategy.getHeaders()
|
|
@@ -163,9 +181,8 @@ async function getFileHash(blob) {
|
|
|
163
181
|
return hashHex;
|
|
164
182
|
}
|
|
165
183
|
async function detectFileMimeType(blob) {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
return result.mime;
|
|
184
|
+
if (blob instanceof Blob && blob.type) {
|
|
185
|
+
return blob.type;
|
|
169
186
|
}
|
|
170
187
|
if ("name" in blob && typeof blob.name === "string") {
|
|
171
188
|
const extension = blob.name.split(".").pop()?.toLowerCase();
|
|
@@ -176,8 +193,9 @@ async function detectFileMimeType(blob) {
|
|
|
176
193
|
}
|
|
177
194
|
}
|
|
178
195
|
}
|
|
179
|
-
|
|
180
|
-
|
|
196
|
+
const result = await fileType.fileTypeFromBlob(blob).catch(() => void 0);
|
|
197
|
+
if (result?.mime) {
|
|
198
|
+
return result.mime;
|
|
181
199
|
}
|
|
182
200
|
return void 0;
|
|
183
201
|
}
|
|
@@ -221,11 +239,13 @@ class VaultFile {
|
|
|
221
239
|
__publicField(this, "content");
|
|
222
240
|
__publicField(this, "lastDownloadUrl");
|
|
223
241
|
__publicField(this, "lastUploadUrl");
|
|
224
|
-
this
|
|
242
|
+
__publicField(this, "baseUrl");
|
|
243
|
+
this.config = resolveConfig(config);
|
|
225
244
|
this.content = content;
|
|
226
245
|
this.id = id;
|
|
227
246
|
this.name = name;
|
|
228
247
|
this.metadata = metadata;
|
|
248
|
+
this.baseUrl = this.config.vaultUrl;
|
|
229
249
|
}
|
|
230
250
|
/**
|
|
231
251
|
* Gets the headers for the request based on the auth strategy.
|
|
@@ -248,7 +268,7 @@ class VaultFile {
|
|
|
248
268
|
*/
|
|
249
269
|
async _fetch(params) {
|
|
250
270
|
const { method, path, body, signal, query } = params;
|
|
251
|
-
const url = new URL(path, this.
|
|
271
|
+
const url = new URL(path, this.baseUrl);
|
|
252
272
|
const headers = new Headers(this.headers);
|
|
253
273
|
headers.set("x-compatibility-date", compatibilityDate);
|
|
254
274
|
if (query) {
|
|
@@ -262,6 +282,9 @@ class VaultFile {
|
|
|
262
282
|
headers,
|
|
263
283
|
signal
|
|
264
284
|
});
|
|
285
|
+
if (response.status === 204 || response.headers.get("content-length") === "0") {
|
|
286
|
+
return null;
|
|
287
|
+
}
|
|
265
288
|
const content = await response.json();
|
|
266
289
|
return content;
|
|
267
290
|
}
|
|
@@ -293,7 +316,7 @@ class VaultFile {
|
|
|
293
316
|
}
|
|
294
317
|
this.id = response.data.id;
|
|
295
318
|
this.metadata = response.data.metadata;
|
|
296
|
-
this.name = response.data.metadata?.originalFileName;
|
|
319
|
+
this.name = response.data.metadata?.originalFileName ?? void 0;
|
|
297
320
|
return response.data;
|
|
298
321
|
}
|
|
299
322
|
/**
|
|
@@ -336,10 +359,11 @@ class VaultFile {
|
|
|
336
359
|
* ```
|
|
337
360
|
*/
|
|
338
361
|
static async fromVaultReference(params, options) {
|
|
339
|
-
const { reference, config, download = false } = params;
|
|
362
|
+
const { reference, config: vaultConfig, download = false } = params;
|
|
363
|
+
const config = resolveConfig(vaultConfig);
|
|
340
364
|
const { vaultUrl, authStrategy } = config;
|
|
341
365
|
const id = removeVaultPrefix(reference);
|
|
342
|
-
const url = new URL(`files/${id}`, vaultUrl);
|
|
366
|
+
const url = new URL(`files/${id}`, config.vaultUrl);
|
|
343
367
|
const response = await wrappedFetch(url, {
|
|
344
368
|
method: "GET",
|
|
345
369
|
headers: authStrategy.getHeaders(),
|
|
@@ -355,7 +379,7 @@ class VaultFile {
|
|
|
355
379
|
vaultUrl,
|
|
356
380
|
authStrategy
|
|
357
381
|
},
|
|
358
|
-
name: response.data.metadata?.originalFileName
|
|
382
|
+
name: response.data.metadata?.originalFileName ?? void 0
|
|
359
383
|
};
|
|
360
384
|
if (download) {
|
|
361
385
|
await wrappedFetch(response.data.url, { method: "GET", signal: options?.signal }).then((response2) => response2.blob()).then((blob) => fileParams.content = blob);
|
|
@@ -406,7 +430,8 @@ class VaultFile {
|
|
|
406
430
|
* ```
|
|
407
431
|
*/
|
|
408
432
|
static async fromContent(params, options) {
|
|
409
|
-
const { name, content, config, upload = false } = params;
|
|
433
|
+
const { name, content, config: vaultConfig, upload = false } = params;
|
|
434
|
+
const config = resolveConfig(vaultConfig);
|
|
410
435
|
const { vaultUrl, authStrategy } = config;
|
|
411
436
|
const sha256sum = await getFileHash(content);
|
|
412
437
|
const mimeType = await detectFileMimeType(content);
|
|
@@ -441,7 +466,7 @@ class VaultFile {
|
|
|
441
466
|
async populateMetadata(options) {
|
|
442
467
|
try {
|
|
443
468
|
this.metadata = await this.getFileMetadata({ signal: options?.signal });
|
|
444
|
-
this.name = this.metadata.originalFileName;
|
|
469
|
+
this.name = this.metadata.originalFileName ?? void 0;
|
|
445
470
|
this.id = this.metadata.id;
|
|
446
471
|
return this;
|
|
447
472
|
} catch (error) {
|
|
@@ -498,7 +523,7 @@ class VaultFile {
|
|
|
498
523
|
const createdFile = await this._createFile();
|
|
499
524
|
this.id = createdFile.id;
|
|
500
525
|
this.metadata = createdFile.metadata;
|
|
501
|
-
this.name = createdFile.metadata?.originalFileName;
|
|
526
|
+
this.name = createdFile.metadata?.originalFileName ?? void 0;
|
|
502
527
|
this.lastUploadUrl = { url: new URL(createdFile.uploadUrl), expiresAt: new Date(createdFile.expiresAt) };
|
|
503
528
|
return this.lastUploadUrl.url;
|
|
504
529
|
}
|
|
@@ -654,7 +679,8 @@ class VaultFile {
|
|
|
654
679
|
}
|
|
655
680
|
}
|
|
656
681
|
|
|
657
|
-
function vaultClient(
|
|
682
|
+
function vaultClient(vaultConfig) {
|
|
683
|
+
const config = resolveConfig(vaultConfig);
|
|
658
684
|
function createFromContent(name, content, options) {
|
|
659
685
|
return VaultFile.fromContent({
|
|
660
686
|
name,
|
package/dist/index.d.cts
CHANGED
|
@@ -36,7 +36,8 @@ declare class Permalink {
|
|
|
36
36
|
readonly createdBy: string | null;
|
|
37
37
|
readonly expiresAt: Date | null;
|
|
38
38
|
readonly fileId: string;
|
|
39
|
-
|
|
39
|
+
private readonly baseUrl;
|
|
40
|
+
constructor(vaultConfig: VaultConfig, params: SerializedPermalink);
|
|
40
41
|
/**
|
|
41
42
|
* The URL for the permalink.
|
|
42
43
|
*/
|
|
@@ -48,7 +49,7 @@ declare class Permalink {
|
|
|
48
49
|
* @param id - The permalink ID.
|
|
49
50
|
* @returns The permalink.
|
|
50
51
|
*/
|
|
51
|
-
static fromId(
|
|
52
|
+
static fromId(vaultConfig: VaultConfig, id: string): Promise<Permalink>;
|
|
52
53
|
/**
|
|
53
54
|
* Create a new permalink.
|
|
54
55
|
*
|
|
@@ -63,7 +64,7 @@ declare class Permalink {
|
|
|
63
64
|
*
|
|
64
65
|
* @returns The permalink.
|
|
65
66
|
*/
|
|
66
|
-
static create(
|
|
67
|
+
static create(vaultConfig: VaultConfig, params: Pick<SerializedPermalink, 'fileId' | 'workspaceId'> & {
|
|
67
68
|
expiresIn?: number;
|
|
68
69
|
}, options?: {
|
|
69
70
|
signal?: AbortSignal;
|
|
@@ -111,6 +112,7 @@ declare class VaultFile {
|
|
|
111
112
|
url: URL;
|
|
112
113
|
expiresAt: Date;
|
|
113
114
|
} | undefined;
|
|
115
|
+
private readonly baseUrl;
|
|
114
116
|
/**
|
|
115
117
|
* Constructs a new VaultFile instance. Direct usage of the constructor is not recommended,
|
|
116
118
|
* instead use the static methods {@link VaultFile.fromVaultReference} when dealing with an existing file in the vault,
|
|
@@ -397,7 +399,7 @@ declare class VaultFile {
|
|
|
397
399
|
}): Promise<Permalink[]>;
|
|
398
400
|
}
|
|
399
401
|
|
|
400
|
-
declare function vaultClient(
|
|
402
|
+
declare function vaultClient(vaultConfig: VaultConfig): {
|
|
401
403
|
createFromContent: (name: string, content: Blob | File, options?: {
|
|
402
404
|
signal?: AbortSignal;
|
|
403
405
|
}) => Promise<VaultFile>;
|
package/dist/index.d.mts
CHANGED
|
@@ -36,7 +36,8 @@ declare class Permalink {
|
|
|
36
36
|
readonly createdBy: string | null;
|
|
37
37
|
readonly expiresAt: Date | null;
|
|
38
38
|
readonly fileId: string;
|
|
39
|
-
|
|
39
|
+
private readonly baseUrl;
|
|
40
|
+
constructor(vaultConfig: VaultConfig, params: SerializedPermalink);
|
|
40
41
|
/**
|
|
41
42
|
* The URL for the permalink.
|
|
42
43
|
*/
|
|
@@ -48,7 +49,7 @@ declare class Permalink {
|
|
|
48
49
|
* @param id - The permalink ID.
|
|
49
50
|
* @returns The permalink.
|
|
50
51
|
*/
|
|
51
|
-
static fromId(
|
|
52
|
+
static fromId(vaultConfig: VaultConfig, id: string): Promise<Permalink>;
|
|
52
53
|
/**
|
|
53
54
|
* Create a new permalink.
|
|
54
55
|
*
|
|
@@ -63,7 +64,7 @@ declare class Permalink {
|
|
|
63
64
|
*
|
|
64
65
|
* @returns The permalink.
|
|
65
66
|
*/
|
|
66
|
-
static create(
|
|
67
|
+
static create(vaultConfig: VaultConfig, params: Pick<SerializedPermalink, 'fileId' | 'workspaceId'> & {
|
|
67
68
|
expiresIn?: number;
|
|
68
69
|
}, options?: {
|
|
69
70
|
signal?: AbortSignal;
|
|
@@ -111,6 +112,7 @@ declare class VaultFile {
|
|
|
111
112
|
url: URL;
|
|
112
113
|
expiresAt: Date;
|
|
113
114
|
} | undefined;
|
|
115
|
+
private readonly baseUrl;
|
|
114
116
|
/**
|
|
115
117
|
* Constructs a new VaultFile instance. Direct usage of the constructor is not recommended,
|
|
116
118
|
* instead use the static methods {@link VaultFile.fromVaultReference} when dealing with an existing file in the vault,
|
|
@@ -397,7 +399,7 @@ declare class VaultFile {
|
|
|
397
399
|
}): Promise<Permalink[]>;
|
|
398
400
|
}
|
|
399
401
|
|
|
400
|
-
declare function vaultClient(
|
|
402
|
+
declare function vaultClient(vaultConfig: VaultConfig): {
|
|
401
403
|
createFromContent: (name: string, content: Blob | File, options?: {
|
|
402
404
|
signal?: AbortSignal;
|
|
403
405
|
}) => Promise<VaultFile>;
|
package/dist/index.d.ts
CHANGED
|
@@ -36,7 +36,8 @@ declare class Permalink {
|
|
|
36
36
|
readonly createdBy: string | null;
|
|
37
37
|
readonly expiresAt: Date | null;
|
|
38
38
|
readonly fileId: string;
|
|
39
|
-
|
|
39
|
+
private readonly baseUrl;
|
|
40
|
+
constructor(vaultConfig: VaultConfig, params: SerializedPermalink);
|
|
40
41
|
/**
|
|
41
42
|
* The URL for the permalink.
|
|
42
43
|
*/
|
|
@@ -48,7 +49,7 @@ declare class Permalink {
|
|
|
48
49
|
* @param id - The permalink ID.
|
|
49
50
|
* @returns The permalink.
|
|
50
51
|
*/
|
|
51
|
-
static fromId(
|
|
52
|
+
static fromId(vaultConfig: VaultConfig, id: string): Promise<Permalink>;
|
|
52
53
|
/**
|
|
53
54
|
* Create a new permalink.
|
|
54
55
|
*
|
|
@@ -63,7 +64,7 @@ declare class Permalink {
|
|
|
63
64
|
*
|
|
64
65
|
* @returns The permalink.
|
|
65
66
|
*/
|
|
66
|
-
static create(
|
|
67
|
+
static create(vaultConfig: VaultConfig, params: Pick<SerializedPermalink, 'fileId' | 'workspaceId'> & {
|
|
67
68
|
expiresIn?: number;
|
|
68
69
|
}, options?: {
|
|
69
70
|
signal?: AbortSignal;
|
|
@@ -111,6 +112,7 @@ declare class VaultFile {
|
|
|
111
112
|
url: URL;
|
|
112
113
|
expiresAt: Date;
|
|
113
114
|
} | undefined;
|
|
115
|
+
private readonly baseUrl;
|
|
114
116
|
/**
|
|
115
117
|
* Constructs a new VaultFile instance. Direct usage of the constructor is not recommended,
|
|
116
118
|
* instead use the static methods {@link VaultFile.fromVaultReference} when dealing with an existing file in the vault,
|
|
@@ -397,7 +399,7 @@ declare class VaultFile {
|
|
|
397
399
|
}): Promise<Permalink[]>;
|
|
398
400
|
}
|
|
399
401
|
|
|
400
|
-
declare function vaultClient(
|
|
402
|
+
declare function vaultClient(vaultConfig: VaultConfig): {
|
|
401
403
|
createFromContent: (name: string, content: Blob | File, options?: {
|
|
402
404
|
signal?: AbortSignal;
|
|
403
405
|
}) => Promise<VaultFile>;
|
package/dist/index.mjs
CHANGED
|
@@ -48,6 +48,19 @@ ${text}`, url, method, response);
|
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
+
function resolveBaseUrl(baseUrl) {
|
|
52
|
+
if (baseUrl.endsWith("/")) {
|
|
53
|
+
return baseUrl;
|
|
54
|
+
}
|
|
55
|
+
return `${baseUrl}/`;
|
|
56
|
+
}
|
|
57
|
+
function resolveConfig(config) {
|
|
58
|
+
return {
|
|
59
|
+
...config,
|
|
60
|
+
vaultUrl: resolveBaseUrl(config.vaultUrl)
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
51
64
|
var __defProp$1 = Object.defineProperty;
|
|
52
65
|
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
53
66
|
var __publicField$1 = (obj, key, value) => {
|
|
@@ -55,7 +68,7 @@ var __publicField$1 = (obj, key, value) => {
|
|
|
55
68
|
return value;
|
|
56
69
|
};
|
|
57
70
|
class Permalink {
|
|
58
|
-
constructor(
|
|
71
|
+
constructor(vaultConfig, params) {
|
|
59
72
|
__publicField$1(this, "config");
|
|
60
73
|
__publicField$1(this, "id");
|
|
61
74
|
__publicField$1(this, "workspaceId");
|
|
@@ -63,6 +76,8 @@ class Permalink {
|
|
|
63
76
|
__publicField$1(this, "createdBy");
|
|
64
77
|
__publicField$1(this, "expiresAt");
|
|
65
78
|
__publicField$1(this, "fileId");
|
|
79
|
+
__publicField$1(this, "baseUrl");
|
|
80
|
+
const config = resolveConfig(vaultConfig);
|
|
66
81
|
this.config = config;
|
|
67
82
|
this.id = params.id;
|
|
68
83
|
this.workspaceId = params.workspaceId;
|
|
@@ -70,12 +85,13 @@ class Permalink {
|
|
|
70
85
|
this.createdBy = params.createdBy;
|
|
71
86
|
this.expiresAt = params.expiresAt ? new Date(params.expiresAt) : null;
|
|
72
87
|
this.fileId = params.fileId;
|
|
88
|
+
this.baseUrl = config.vaultUrl;
|
|
73
89
|
}
|
|
74
90
|
/**
|
|
75
91
|
* The URL for the permalink.
|
|
76
92
|
*/
|
|
77
93
|
get url() {
|
|
78
|
-
return new URL(
|
|
94
|
+
return new URL(`permalinks/${this.id}`, this.baseUrl);
|
|
79
95
|
}
|
|
80
96
|
/**
|
|
81
97
|
* Get a new permalink instance from its ID.
|
|
@@ -84,8 +100,9 @@ class Permalink {
|
|
|
84
100
|
* @param id - The permalink ID.
|
|
85
101
|
* @returns The permalink.
|
|
86
102
|
*/
|
|
87
|
-
static async fromId(
|
|
88
|
-
const
|
|
103
|
+
static async fromId(vaultConfig, id) {
|
|
104
|
+
const config = resolveConfig(vaultConfig);
|
|
105
|
+
const response = await fetch(new URL(`permalinks/${id}/metadata`, config.vaultUrl), {
|
|
89
106
|
headers: config.authStrategy.getHeaders()
|
|
90
107
|
});
|
|
91
108
|
if (!response.ok) {
|
|
@@ -108,14 +125,15 @@ class Permalink {
|
|
|
108
125
|
*
|
|
109
126
|
* @returns The permalink.
|
|
110
127
|
*/
|
|
111
|
-
static async create(
|
|
128
|
+
static async create(vaultConfig, params, options) {
|
|
129
|
+
const config = resolveConfig(vaultConfig);
|
|
112
130
|
const { expiresIn } = params;
|
|
113
131
|
const expiresAt = expiresIn ? new Date(Date.now() + expiresIn * 1e3) : void 0;
|
|
114
132
|
const headers = config.authStrategy.getHeaders();
|
|
115
133
|
headers.append("x-compatibility-date", "2025-07-29");
|
|
116
134
|
headers.append("content-type", "application/json");
|
|
117
135
|
const body = expiresAt ? JSON.stringify({ expiresAt: expiresAt.toISOString() }) : "{}";
|
|
118
|
-
const response = await fetch(
|
|
136
|
+
const response = await fetch(new URL(`files/${params.fileId}/permalinks`, config.vaultUrl), {
|
|
119
137
|
method: "POST",
|
|
120
138
|
signal: options?.signal,
|
|
121
139
|
headers,
|
|
@@ -134,7 +152,7 @@ class Permalink {
|
|
|
134
152
|
* @param options.signal - The signal to abort the request.
|
|
135
153
|
*/
|
|
136
154
|
async delete(options) {
|
|
137
|
-
const response = await fetch(
|
|
155
|
+
const response = await fetch(new URL(`permalinks/${this.id}`, this.baseUrl), {
|
|
138
156
|
method: "DELETE",
|
|
139
157
|
signal: options?.signal,
|
|
140
158
|
headers: this.config.authStrategy.getHeaders()
|
|
@@ -161,9 +179,8 @@ async function getFileHash(blob) {
|
|
|
161
179
|
return hashHex;
|
|
162
180
|
}
|
|
163
181
|
async function detectFileMimeType(blob) {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
return result.mime;
|
|
182
|
+
if (blob instanceof Blob && blob.type) {
|
|
183
|
+
return blob.type;
|
|
167
184
|
}
|
|
168
185
|
if ("name" in blob && typeof blob.name === "string") {
|
|
169
186
|
const extension = blob.name.split(".").pop()?.toLowerCase();
|
|
@@ -174,8 +191,9 @@ async function detectFileMimeType(blob) {
|
|
|
174
191
|
}
|
|
175
192
|
}
|
|
176
193
|
}
|
|
177
|
-
|
|
178
|
-
|
|
194
|
+
const result = await fileTypeFromBlob(blob).catch(() => void 0);
|
|
195
|
+
if (result?.mime) {
|
|
196
|
+
return result.mime;
|
|
179
197
|
}
|
|
180
198
|
return void 0;
|
|
181
199
|
}
|
|
@@ -219,11 +237,13 @@ class VaultFile {
|
|
|
219
237
|
__publicField(this, "content");
|
|
220
238
|
__publicField(this, "lastDownloadUrl");
|
|
221
239
|
__publicField(this, "lastUploadUrl");
|
|
222
|
-
this
|
|
240
|
+
__publicField(this, "baseUrl");
|
|
241
|
+
this.config = resolveConfig(config);
|
|
223
242
|
this.content = content;
|
|
224
243
|
this.id = id;
|
|
225
244
|
this.name = name;
|
|
226
245
|
this.metadata = metadata;
|
|
246
|
+
this.baseUrl = this.config.vaultUrl;
|
|
227
247
|
}
|
|
228
248
|
/**
|
|
229
249
|
* Gets the headers for the request based on the auth strategy.
|
|
@@ -246,7 +266,7 @@ class VaultFile {
|
|
|
246
266
|
*/
|
|
247
267
|
async _fetch(params) {
|
|
248
268
|
const { method, path, body, signal, query } = params;
|
|
249
|
-
const url = new URL(path, this.
|
|
269
|
+
const url = new URL(path, this.baseUrl);
|
|
250
270
|
const headers = new Headers(this.headers);
|
|
251
271
|
headers.set("x-compatibility-date", compatibilityDate);
|
|
252
272
|
if (query) {
|
|
@@ -260,6 +280,9 @@ class VaultFile {
|
|
|
260
280
|
headers,
|
|
261
281
|
signal
|
|
262
282
|
});
|
|
283
|
+
if (response.status === 204 || response.headers.get("content-length") === "0") {
|
|
284
|
+
return null;
|
|
285
|
+
}
|
|
263
286
|
const content = await response.json();
|
|
264
287
|
return content;
|
|
265
288
|
}
|
|
@@ -291,7 +314,7 @@ class VaultFile {
|
|
|
291
314
|
}
|
|
292
315
|
this.id = response.data.id;
|
|
293
316
|
this.metadata = response.data.metadata;
|
|
294
|
-
this.name = response.data.metadata?.originalFileName;
|
|
317
|
+
this.name = response.data.metadata?.originalFileName ?? void 0;
|
|
295
318
|
return response.data;
|
|
296
319
|
}
|
|
297
320
|
/**
|
|
@@ -334,10 +357,11 @@ class VaultFile {
|
|
|
334
357
|
* ```
|
|
335
358
|
*/
|
|
336
359
|
static async fromVaultReference(params, options) {
|
|
337
|
-
const { reference, config, download = false } = params;
|
|
360
|
+
const { reference, config: vaultConfig, download = false } = params;
|
|
361
|
+
const config = resolveConfig(vaultConfig);
|
|
338
362
|
const { vaultUrl, authStrategy } = config;
|
|
339
363
|
const id = removeVaultPrefix(reference);
|
|
340
|
-
const url = new URL(`files/${id}`, vaultUrl);
|
|
364
|
+
const url = new URL(`files/${id}`, config.vaultUrl);
|
|
341
365
|
const response = await wrappedFetch(url, {
|
|
342
366
|
method: "GET",
|
|
343
367
|
headers: authStrategy.getHeaders(),
|
|
@@ -353,7 +377,7 @@ class VaultFile {
|
|
|
353
377
|
vaultUrl,
|
|
354
378
|
authStrategy
|
|
355
379
|
},
|
|
356
|
-
name: response.data.metadata?.originalFileName
|
|
380
|
+
name: response.data.metadata?.originalFileName ?? void 0
|
|
357
381
|
};
|
|
358
382
|
if (download) {
|
|
359
383
|
await wrappedFetch(response.data.url, { method: "GET", signal: options?.signal }).then((response2) => response2.blob()).then((blob) => fileParams.content = blob);
|
|
@@ -404,7 +428,8 @@ class VaultFile {
|
|
|
404
428
|
* ```
|
|
405
429
|
*/
|
|
406
430
|
static async fromContent(params, options) {
|
|
407
|
-
const { name, content, config, upload = false } = params;
|
|
431
|
+
const { name, content, config: vaultConfig, upload = false } = params;
|
|
432
|
+
const config = resolveConfig(vaultConfig);
|
|
408
433
|
const { vaultUrl, authStrategy } = config;
|
|
409
434
|
const sha256sum = await getFileHash(content);
|
|
410
435
|
const mimeType = await detectFileMimeType(content);
|
|
@@ -439,7 +464,7 @@ class VaultFile {
|
|
|
439
464
|
async populateMetadata(options) {
|
|
440
465
|
try {
|
|
441
466
|
this.metadata = await this.getFileMetadata({ signal: options?.signal });
|
|
442
|
-
this.name = this.metadata.originalFileName;
|
|
467
|
+
this.name = this.metadata.originalFileName ?? void 0;
|
|
443
468
|
this.id = this.metadata.id;
|
|
444
469
|
return this;
|
|
445
470
|
} catch (error) {
|
|
@@ -496,7 +521,7 @@ class VaultFile {
|
|
|
496
521
|
const createdFile = await this._createFile();
|
|
497
522
|
this.id = createdFile.id;
|
|
498
523
|
this.metadata = createdFile.metadata;
|
|
499
|
-
this.name = createdFile.metadata?.originalFileName;
|
|
524
|
+
this.name = createdFile.metadata?.originalFileName ?? void 0;
|
|
500
525
|
this.lastUploadUrl = { url: new URL(createdFile.uploadUrl), expiresAt: new Date(createdFile.expiresAt) };
|
|
501
526
|
return this.lastUploadUrl.url;
|
|
502
527
|
}
|
|
@@ -652,7 +677,8 @@ class VaultFile {
|
|
|
652
677
|
}
|
|
653
678
|
}
|
|
654
679
|
|
|
655
|
-
function vaultClient(
|
|
680
|
+
function vaultClient(vaultConfig) {
|
|
681
|
+
const config = resolveConfig(vaultConfig);
|
|
656
682
|
function createFromContent(name, content, options) {
|
|
657
683
|
return VaultFile.fromContent({
|
|
658
684
|
name,
|
package/package.json
CHANGED
|
@@ -1,49 +1,49 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
2
|
+
"name": "@meistrari/vault-sdk",
|
|
3
|
+
"version": "1.4.3",
|
|
4
|
+
"license": "UNLICENSED",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/meistrari/vault.git"
|
|
8
|
+
},
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.mjs",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"main": "dist/index.mjs",
|
|
17
|
+
"types": "dist/index.d.ts",
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"test": "vitest --no-watch",
|
|
23
|
+
"test:watch": "vitest",
|
|
24
|
+
"build": "unbuild",
|
|
25
|
+
"lint": "eslint .",
|
|
26
|
+
"lint:fix": "eslint . --fix",
|
|
27
|
+
"check": "bun run lint && bun tsc --noEmit"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@meistrari/vault-shared": "0.0.5",
|
|
31
|
+
"file-type": "21.0.0",
|
|
32
|
+
"mime-types": "3.0.1",
|
|
33
|
+
"ofetch": "1.4.1",
|
|
34
|
+
"zod": "3.23.8"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@types/bun": "latest",
|
|
38
|
+
"@types/mime-types": "3.0.1",
|
|
39
|
+
"msw": "2.6.8",
|
|
40
|
+
"unbuild": "2.0.0",
|
|
41
|
+
"vitest": "2.1.9"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"typescript": "^5.0.0"
|
|
45
|
+
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public"
|
|
14
48
|
}
|
|
15
|
-
|
|
16
|
-
"main": "dist/index.mjs",
|
|
17
|
-
"types": "dist/index.d.ts",
|
|
18
|
-
"files": [
|
|
19
|
-
"dist"
|
|
20
|
-
],
|
|
21
|
-
"scripts": {
|
|
22
|
-
"test": "vitest --no-watch",
|
|
23
|
-
"test:watch": "vitest",
|
|
24
|
-
"build": "unbuild",
|
|
25
|
-
"lint": "eslint .",
|
|
26
|
-
"lint:fix": "eslint . --fix",
|
|
27
|
-
"check": "bun run lint && bun tsc --noEmit"
|
|
28
|
-
},
|
|
29
|
-
"dependencies": {
|
|
30
|
-
"@meistrari/vault-shared": "0.0.3",
|
|
31
|
-
"file-type": "21.0.0",
|
|
32
|
-
"mime-types": "3.0.1",
|
|
33
|
-
"ofetch": "1.4.1",
|
|
34
|
-
"zod": "3.23.8"
|
|
35
|
-
},
|
|
36
|
-
"devDependencies": {
|
|
37
|
-
"@types/bun": "latest",
|
|
38
|
-
"@types/mime-types": "3.0.1",
|
|
39
|
-
"msw": "2.6.8",
|
|
40
|
-
"unbuild": "2.0.0",
|
|
41
|
-
"vitest": "2.1.9"
|
|
42
|
-
},
|
|
43
|
-
"peerDependencies": {
|
|
44
|
-
"typescript": "^5.0.0"
|
|
45
|
-
},
|
|
46
|
-
"publishConfig": {
|
|
47
|
-
"access": "public"
|
|
48
|
-
}
|
|
49
|
-
}
|
|
49
|
+
}
|