@meistrari/vault-sdk 0.0.8 → 0.0.9
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.d.mts +4 -2
- package/dist/index.d.ts +4 -2
- package/dist/index.mjs +14 -3
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -23,6 +23,8 @@ type VaultParams = {
|
|
|
23
23
|
authStrategy: AuthStrategy;
|
|
24
24
|
vaultUrl: string;
|
|
25
25
|
};
|
|
26
|
+
type DownloadResponseType = 'blob' | 'base64';
|
|
27
|
+
type DownloadResponse<T extends DownloadResponseType> = T extends 'blob' ? Blob : string;
|
|
26
28
|
declare class VaultFile {
|
|
27
29
|
readonly name: string;
|
|
28
30
|
readonly vaultUrl: string;
|
|
@@ -30,12 +32,12 @@ declare class VaultFile {
|
|
|
30
32
|
constructor(params: VaultParams);
|
|
31
33
|
getVaultUrl(): string;
|
|
32
34
|
removeVaultPrefix(url: string): string;
|
|
33
|
-
getUploadUrl(): Promise<
|
|
35
|
+
getUploadUrl(): Promise<URL>;
|
|
34
36
|
getDownloadUrl(): Promise<URL>;
|
|
35
37
|
refreshAuth(authStrategy: AuthStrategy): void;
|
|
36
38
|
_fetch(params: FetchParams): Promise<any>;
|
|
37
39
|
upload(file: Blob): Promise<void>;
|
|
38
|
-
download(): Promise<
|
|
40
|
+
download<T extends DownloadResponseType = 'blob'>(responseType?: T): Promise<DownloadResponse<T>>;
|
|
39
41
|
}
|
|
40
42
|
|
|
41
43
|
declare class FetchError extends Error {
|
package/dist/index.d.ts
CHANGED
|
@@ -23,6 +23,8 @@ type VaultParams = {
|
|
|
23
23
|
authStrategy: AuthStrategy;
|
|
24
24
|
vaultUrl: string;
|
|
25
25
|
};
|
|
26
|
+
type DownloadResponseType = 'blob' | 'base64';
|
|
27
|
+
type DownloadResponse<T extends DownloadResponseType> = T extends 'blob' ? Blob : string;
|
|
26
28
|
declare class VaultFile {
|
|
27
29
|
readonly name: string;
|
|
28
30
|
readonly vaultUrl: string;
|
|
@@ -30,12 +32,12 @@ declare class VaultFile {
|
|
|
30
32
|
constructor(params: VaultParams);
|
|
31
33
|
getVaultUrl(): string;
|
|
32
34
|
removeVaultPrefix(url: string): string;
|
|
33
|
-
getUploadUrl(): Promise<
|
|
35
|
+
getUploadUrl(): Promise<URL>;
|
|
34
36
|
getDownloadUrl(): Promise<URL>;
|
|
35
37
|
refreshAuth(authStrategy: AuthStrategy): void;
|
|
36
38
|
_fetch(params: FetchParams): Promise<any>;
|
|
37
39
|
upload(file: Blob): Promise<void>;
|
|
38
|
-
download(): Promise<
|
|
40
|
+
download<T extends DownloadResponseType = 'blob'>(responseType?: T): Promise<DownloadResponse<T>>;
|
|
39
41
|
}
|
|
40
42
|
|
|
41
43
|
declare class FetchError extends Error {
|
package/dist/index.mjs
CHANGED
|
@@ -6,6 +6,14 @@ class FetchError extends Error {
|
|
|
6
6
|
}
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
+
async function blobToBase64(blob) {
|
|
10
|
+
const fileContent = new Uint8Array(await blob.arrayBuffer());
|
|
11
|
+
let content = "";
|
|
12
|
+
for (const part of fileContent)
|
|
13
|
+
content += String.fromCharCode(part);
|
|
14
|
+
return btoa(content);
|
|
15
|
+
}
|
|
16
|
+
|
|
9
17
|
var __defProp$1 = Object.defineProperty;
|
|
10
18
|
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
11
19
|
var __publicField$1 = (obj, key, value) => {
|
|
@@ -35,7 +43,7 @@ class VaultFile {
|
|
|
35
43
|
method: "POST",
|
|
36
44
|
path: `/v2/files/${this.removeVaultPrefix(this.name)}`
|
|
37
45
|
});
|
|
38
|
-
return response.url;
|
|
46
|
+
return new URL(response.url);
|
|
39
47
|
}
|
|
40
48
|
async getDownloadUrl() {
|
|
41
49
|
const response = await this._fetch({
|
|
@@ -71,7 +79,7 @@ class VaultFile {
|
|
|
71
79
|
throw new FetchError(`Error uploading file ${this.name}: ${response.status} ${response.statusText}`, response);
|
|
72
80
|
}
|
|
73
81
|
}
|
|
74
|
-
async download() {
|
|
82
|
+
async download(responseType = "blob") {
|
|
75
83
|
const downloadUrl = await this.getDownloadUrl();
|
|
76
84
|
const response = await fetch(downloadUrl, {
|
|
77
85
|
method: "GET"
|
|
@@ -79,7 +87,10 @@ class VaultFile {
|
|
|
79
87
|
if (!response.ok) {
|
|
80
88
|
throw new FetchError(`Error downloading file ${this.name}: ${response.status} ${response.statusText}`, response);
|
|
81
89
|
}
|
|
82
|
-
|
|
90
|
+
const blob = await response.blob();
|
|
91
|
+
if (responseType === "blob")
|
|
92
|
+
return blob;
|
|
93
|
+
return await blobToBase64(blob);
|
|
83
94
|
}
|
|
84
95
|
}
|
|
85
96
|
|