@meistrari/vault-sdk 0.0.12 → 1.0.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.
@@ -0,0 +1,264 @@
1
+ import { FileMetadata } from '@meistrari/vault-shared/schemas';
2
+
3
+ interface AuthStrategy {
4
+ getHeaders: () => Headers;
5
+ }
6
+ declare class DataTokenAuthStrategy implements AuthStrategy {
7
+ dataToken: string;
8
+ getHeaders(): Headers;
9
+ constructor(dataToken: string);
10
+ }
11
+ declare class APIKeyAuthStrategy implements AuthStrategy {
12
+ apiKey: string;
13
+ getHeaders(): Headers;
14
+ constructor(apiKey: string);
15
+ }
16
+
17
+ type VaultConfig = {
18
+ vaultUrl: string;
19
+ authStrategy: AuthStrategy;
20
+ };
21
+ type VaultFileParams = {
22
+ id?: string;
23
+ name?: string;
24
+ content?: Blob;
25
+ metadata?: FileMetadata | null;
26
+ config: VaultConfig;
27
+ };
28
+ /**
29
+ * Represents a file in the vault and allows interacting with it.
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * const vaultFile = await VaultFile.fromVaultReference('vault://1234567890', {
34
+ * vaultUrl,
35
+ * authStrategy,
36
+ * })
37
+ * ```
38
+ */
39
+ declare class VaultFile {
40
+ id: string | undefined;
41
+ name: string | undefined;
42
+ metadata: FileMetadata | undefined | null;
43
+ config: VaultConfig;
44
+ content: Blob | undefined;
45
+ lastDownloadUrl: {
46
+ url: URL;
47
+ expiresAt: Date;
48
+ } | undefined;
49
+ lastUploadUrl: {
50
+ url: URL;
51
+ expiresAt: Date;
52
+ } | undefined;
53
+ /**
54
+ * Constructs a new VaultFile instance. Direct usage of the constructor is not recommended,
55
+ * instead use the static methods {@link VaultFile.fromVaultReference} when dealing with an existing file in the vault,
56
+ * or {@link VaultFile.fromContent} when preparing a new file for upload.
57
+ *
58
+ * @param params - The parameters for the VaultFile constructor
59
+ * @param params.config - The configuration for the VaultFile
60
+ * @param params.content - The content of the file
61
+ * @param params.id - The ID of the file
62
+ * @param params.name - The name of the file
63
+ * @param params.metadata - The metadata of the file
64
+ */
65
+ constructor({ config, content, id, name, metadata }: VaultFileParams);
66
+ /**
67
+ * Gets the headers for the request based on the auth strategy.
68
+ *
69
+ * @returns The headers for the request
70
+ */
71
+ private get headers();
72
+ /**
73
+ * Performs a request to the vault service and handles the response or errors.
74
+ *
75
+ * @param params - The parameters for the fetch
76
+ * @param params.method - The method to use for the fetch
77
+ * @param params.path - The path to fetch
78
+ * @param params.body - The body of the request
79
+ * @returns The response from the vault
80
+ * @throws {FetchError} If the fetch fails
81
+ */
82
+ private _fetch;
83
+ /**
84
+ * Creates a new file in the vault.
85
+ *
86
+ * @returns The metadata of the file
87
+ * @throws {Error} If the file ID is not set
88
+ * @throws {FetchError} If the metadata fetch fails
89
+ */
90
+ private _createFile;
91
+ /**
92
+ * Creates a new VaultFile instance from a vault reference.
93
+ *
94
+ * @param params - The parameters for creating a VaultFile from a vault reference
95
+ * @param params.reference - The reference to the file in the vault
96
+ * @param params.config - The configuration for the VaultFile
97
+ * @param params.download - Whether to download the file content (default: false)
98
+ * @returns A new VaultFile instance
99
+ *
100
+ * @example
101
+ * ```ts
102
+ * // Lazily download the file content
103
+ * const vaultFile = await VaultFile.fromVaultReference({
104
+ * reference: 'vault://1234567890',
105
+ * config: {
106
+ * vaultUrl,
107
+ * authStrategy,
108
+ * }
109
+ * })
110
+ * const content = await vaultFile.download()
111
+ * ```
112
+ *
113
+ * @example
114
+ * ```ts
115
+ * // Download the file content while creating the instance
116
+ * const vaultFile = await VaultFile.fromVaultReference({
117
+ * reference: 'vault://1234567890',
118
+ * config: {
119
+ * vaultUrl,
120
+ * authStrategy,
121
+ * },
122
+ * download: true
123
+ * })
124
+ * const content = vaultFile.content
125
+ * ```
126
+ */
127
+ static fromVaultReference(params: {
128
+ reference: string;
129
+ config: VaultConfig;
130
+ download?: boolean;
131
+ }): Promise<VaultFile>;
132
+ /**
133
+ * Creates a new VaultFile instance from given content.
134
+ *
135
+ * @param params - The parameters for creating a VaultFile from content
136
+ * @param params.name - The name of the file
137
+ * @param params.content - The content of the file
138
+ * @param params.config - The configuration for the VaultFile
139
+ * @param params.upload - Whether to upload the file (default: false)
140
+ * @returns A new VaultFile instance
141
+ *
142
+ * @example
143
+ * ```ts
144
+ * // Lazily upload the file content
145
+ * const file = new File(['content'], 'document.txt')
146
+ * const vaultFile = await VaultFile.fromContent({
147
+ * name: 'document.txt',
148
+ * content: file,
149
+ * config: {
150
+ * vaultUrl,
151
+ * authStrategy,
152
+ * }
153
+ * })
154
+ * await vaultFile.upload()
155
+ * ```
156
+ *
157
+ * @example
158
+ * ```ts
159
+ * // Upload the file content while creating the instance
160
+ * const file = new File(['content'], 'document.txt')
161
+ * const vaultFile = await VaultFile.fromContent({
162
+ * name: 'document.txt',
163
+ * content: file,
164
+ * config: {
165
+ * vaultUrl,
166
+ * authStrategy,
167
+ * },
168
+ * upload: true
169
+ * })
170
+ * ```
171
+ */
172
+ static fromContent(params: {
173
+ name: string;
174
+ content: Blob | File;
175
+ config: VaultConfig;
176
+ upload?: boolean;
177
+ }): Promise<VaultFile>;
178
+ /**
179
+ * Populates the metadata of the file instance.
180
+ *
181
+ * @returns The file instance
182
+ * @throws {Error} If the file ID is not set
183
+ * @throws {FetchError} If the metadata fetch fails
184
+ */
185
+ populateMetadata(): Promise<this | undefined>;
186
+ /**
187
+ * Gets the vault reference for this file.
188
+ *
189
+ * @returns The vault reference in the format `vault://{fileId}`
190
+ * @throws {Error} If the file ID is not set
191
+ */
192
+ getVaultReference(): string;
193
+ /**
194
+ * Fetches the metadata of the file.
195
+ *
196
+ * @returns The metadata of the file
197
+ * @throws {Error} If the file ID is not set
198
+ * @throws {FetchError} If the metadata fetch fails
199
+ */
200
+ getFileMetadata(): Promise<FileMetadata>;
201
+ /**
202
+ * Fetches a upload URL for the file.
203
+ *
204
+ * @returns The upload URL for the file
205
+ * @throws {Error} If the vault service returns an invalid response
206
+ * @throws {FetchError} If the upload URL fetch fails
207
+ */
208
+ getUploadUrl(): Promise<URL>;
209
+ /**
210
+ * Fetches a download URL for the file.
211
+ *
212
+ * @returns The download URL for the file
213
+ * @throws {Error} If the vault service returns an invalid response
214
+ * @throws {Error} If not file ID, name or content is set
215
+ * @throws {FetchError} If the download URL fetch fails
216
+ */
217
+ getDownloadUrl(): Promise<URL>;
218
+ /**
219
+ * Uploads a file to the vault.
220
+ *
221
+ * @example
222
+ * ```ts
223
+ * const file = new File(['content'], 'document.txt')
224
+ * const vaultFile = await VaultFile.fromBlob('document.txt', file, { vaultUrl, authStrategy })
225
+ * await vaultFile.upload(file)
226
+ * ```
227
+ *
228
+ * @param file - The file to upload to the vault. If not provided, the file content will be taken from the `content` property.
229
+ * @throws {FetchError} If the upload fails
230
+ * @throws {Error} If the file content is not set and no file is provided
231
+ * @returns Promise that resolves when upload is complete
232
+ */
233
+ upload(file?: Blob, url?: string): Promise<void>;
234
+ /**
235
+ * Downloads a file from the vault.
236
+ *
237
+ * @param responseType - The type of the response
238
+ * @returns The response from the vault
239
+ *
240
+ * @example
241
+ * ```ts
242
+ * const vaultFile = await VaultFile.fromVaultReference('vault://1234567890', { vaultUrl, authStrategy })
243
+ * const content = await vaultFile.download()
244
+ * ```
245
+ */
246
+ download(responseType?: 'blob'): Promise<Blob>;
247
+ download(responseType: 'base64'): Promise<string>;
248
+ }
249
+
250
+ declare class FetchError extends Error {
251
+ readonly message: string;
252
+ readonly url: string;
253
+ readonly method: string;
254
+ readonly response: Response;
255
+ constructor(message: string, url: string, method: string, response: Response);
256
+ static from(url: string, method: string, response: Response): Promise<FetchError>;
257
+ }
258
+
259
+ declare function vaultClient(config: VaultConfig): {
260
+ createFromContent: (name: string, content: Blob | File) => Promise<VaultFile>;
261
+ createFromReference: (reference: string) => Promise<VaultFile>;
262
+ };
263
+
264
+ export { APIKeyAuthStrategy, type AuthStrategy, DataTokenAuthStrategy, FetchError, type VaultConfig, VaultFile, vaultClient };
package/dist/index.d.mts CHANGED
@@ -1,3 +1,5 @@
1
+ import { FileMetadata } from '@meistrari/vault-shared/schemas';
2
+
1
3
  interface AuthStrategy {
2
4
  getHeaders: () => Headers;
3
5
  }
@@ -12,76 +14,251 @@ declare class APIKeyAuthStrategy implements AuthStrategy {
12
14
  constructor(apiKey: string);
13
15
  }
14
16
 
15
- type FetchParams = {
16
- method: 'GET' | 'POST' | 'PUT';
17
- path: string;
18
- body?: any;
19
- ignoreHeaders?: boolean;
20
- };
21
- type VaultParams = {
22
- name: string;
23
- authStrategy: AuthStrategy;
17
+ type VaultConfig = {
24
18
  vaultUrl: string;
19
+ authStrategy: AuthStrategy;
25
20
  };
21
+ type VaultFileParams = {
22
+ id?: string;
23
+ name?: string;
24
+ content?: Blob;
25
+ metadata?: FileMetadata | null;
26
+ config: VaultConfig;
27
+ };
28
+ /**
29
+ * Represents a file in the vault and allows interacting with it.
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * const vaultFile = await VaultFile.fromVaultReference('vault://1234567890', {
34
+ * vaultUrl,
35
+ * authStrategy,
36
+ * })
37
+ * ```
38
+ */
26
39
  declare class VaultFile {
27
- readonly vaultUrl: string;
28
- name: string;
29
- headers: Headers;
30
- constructor(params: VaultParams);
31
- getVaultUrl(): string;
32
- removeVaultPrefix(url: string): string;
33
- getUploadUrl(): Promise<URL>;
34
- getDownloadUrl(): Promise<URL>;
35
- refreshAuth(authStrategy: AuthStrategy): void;
36
- _fetch(params: FetchParams): Promise<any>;
40
+ id: string | undefined;
41
+ name: string | undefined;
42
+ metadata: FileMetadata | undefined | null;
43
+ config: VaultConfig;
44
+ content: Blob | undefined;
45
+ lastDownloadUrl: {
46
+ url: URL;
47
+ expiresAt: Date;
48
+ } | undefined;
49
+ lastUploadUrl: {
50
+ url: URL;
51
+ expiresAt: Date;
52
+ } | undefined;
53
+ /**
54
+ * Constructs a new VaultFile instance. Direct usage of the constructor is not recommended,
55
+ * instead use the static methods {@link VaultFile.fromVaultReference} when dealing with an existing file in the vault,
56
+ * or {@link VaultFile.fromContent} when preparing a new file for upload.
57
+ *
58
+ * @param params - The parameters for the VaultFile constructor
59
+ * @param params.config - The configuration for the VaultFile
60
+ * @param params.content - The content of the file
61
+ * @param params.id - The ID of the file
62
+ * @param params.name - The name of the file
63
+ * @param params.metadata - The metadata of the file
64
+ */
65
+ constructor({ config, content, id, name, metadata }: VaultFileParams);
66
+ /**
67
+ * Gets the headers for the request based on the auth strategy.
68
+ *
69
+ * @returns The headers for the request
70
+ */
71
+ private get headers();
72
+ /**
73
+ * Performs a request to the vault service and handles the response or errors.
74
+ *
75
+ * @param params - The parameters for the fetch
76
+ * @param params.method - The method to use for the fetch
77
+ * @param params.path - The path to fetch
78
+ * @param params.body - The body of the request
79
+ * @returns The response from the vault
80
+ * @throws {FetchError} If the fetch fails
81
+ */
82
+ private _fetch;
83
+ /**
84
+ * Creates a new file in the vault.
85
+ *
86
+ * @returns The metadata of the file
87
+ * @throws {Error} If the file ID is not set
88
+ * @throws {FetchError} If the metadata fetch fails
89
+ */
90
+ private _createFile;
91
+ /**
92
+ * Creates a new VaultFile instance from a vault reference.
93
+ *
94
+ * @param params - The parameters for creating a VaultFile from a vault reference
95
+ * @param params.reference - The reference to the file in the vault
96
+ * @param params.config - The configuration for the VaultFile
97
+ * @param params.download - Whether to download the file content (default: false)
98
+ * @returns A new VaultFile instance
99
+ *
100
+ * @example
101
+ * ```ts
102
+ * // Lazily download the file content
103
+ * const vaultFile = await VaultFile.fromVaultReference({
104
+ * reference: 'vault://1234567890',
105
+ * config: {
106
+ * vaultUrl,
107
+ * authStrategy,
108
+ * }
109
+ * })
110
+ * const content = await vaultFile.download()
111
+ * ```
112
+ *
113
+ * @example
114
+ * ```ts
115
+ * // Download the file content while creating the instance
116
+ * const vaultFile = await VaultFile.fromVaultReference({
117
+ * reference: 'vault://1234567890',
118
+ * config: {
119
+ * vaultUrl,
120
+ * authStrategy,
121
+ * },
122
+ * download: true
123
+ * })
124
+ * const content = vaultFile.content
125
+ * ```
126
+ */
127
+ static fromVaultReference(params: {
128
+ reference: string;
129
+ config: VaultConfig;
130
+ download?: boolean;
131
+ }): Promise<VaultFile>;
37
132
  /**
38
- * Adds a SHA-256 hash of the file content as a prefix to the filename. This ensures uniqueness and prevents
39
- * files with identical names but different content from overwriting each other in the vault.
133
+ * Creates a new VaultFile instance from given content.
40
134
  *
41
- * The resulting filename format is: "{hash}-{originalName}"
135
+ * @param params - The parameters for creating a VaultFile from content
136
+ * @param params.name - The name of the file
137
+ * @param params.content - The content of the file
138
+ * @param params.config - The configuration for the VaultFile
139
+ * @param params.upload - Whether to upload the file (default: false)
140
+ * @returns A new VaultFile instance
42
141
  *
43
- * IMPORTANT: The modified filename must be stored and used for all future operations with this file in the vault,
44
- * as it becomes the file's unique identifier. The original filename alone will not be sufficient to retrieve
45
- * the file later.
142
+ * @example
143
+ * ```ts
144
+ * // Lazily upload the file content
145
+ * const file = new File(['content'], 'document.txt')
146
+ * const vaultFile = await VaultFile.fromContent({
147
+ * name: 'document.txt',
148
+ * content: file,
149
+ * config: {
150
+ * vaultUrl,
151
+ * authStrategy,
152
+ * }
153
+ * })
154
+ * await vaultFile.upload()
155
+ * ```
46
156
  *
47
157
  * @example
158
+ * ```ts
159
+ * // Upload the file content while creating the instance
48
160
  * const file = new File(['content'], 'document.txt')
49
- * await vaultFile.addHashToName(file)
50
- * // vaultFile.name becomes: "a1b2c3...xyz-document.txt"
161
+ * const vaultFile = await VaultFile.fromContent({
162
+ * name: 'document.txt',
163
+ * content: file,
164
+ * config: {
165
+ * vaultUrl,
166
+ * authStrategy,
167
+ * },
168
+ * upload: true
169
+ * })
170
+ * ```
171
+ */
172
+ static fromContent(params: {
173
+ name: string;
174
+ content: Blob | File;
175
+ config: VaultConfig;
176
+ upload?: boolean;
177
+ }): Promise<VaultFile>;
178
+ /**
179
+ * Populates the metadata of the file instance.
51
180
  *
52
- * @param file - The file to generate a hash for
53
- * @returns The new filename with the hash prefix
181
+ * @returns The file instance
182
+ * @throws {Error} If the file ID is not set
183
+ * @throws {FetchError} If the metadata fetch fails
54
184
  */
55
- addHashToName(file: Blob): Promise<string>;
185
+ populateMetadata(): Promise<this | undefined>;
56
186
  /**
57
- * Uploads a file to the vault.
187
+ * Gets the vault reference for this file.
58
188
  *
59
- * Files are saved with the given file names, so files with the same name within the same workspace
60
- * will overwrite each other. To prevent accidental overwrites and support multiple files with the
61
- * same original name, you should call addHashToName() before uploading to add a unique content-based
62
- * hash to the filename.
189
+ * @returns The vault reference in the format `vault://{fileId}`
190
+ * @throws {Error} If the file ID is not set
191
+ */
192
+ getVaultReference(): string;
193
+ /**
194
+ * Fetches the metadata of the file.
195
+ *
196
+ * @returns The metadata of the file
197
+ * @throws {Error} If the file ID is not set
198
+ * @throws {FetchError} If the metadata fetch fails
199
+ */
200
+ getFileMetadata(): Promise<FileMetadata>;
201
+ /**
202
+ * Fetches a upload URL for the file.
203
+ *
204
+ * @returns The upload URL for the file
205
+ * @throws {Error} If the vault service returns an invalid response
206
+ * @throws {FetchError} If the upload URL fetch fails
207
+ */
208
+ getUploadUrl(): Promise<URL>;
209
+ /**
210
+ * Fetches a download URL for the file.
211
+ *
212
+ * @returns The download URL for the file
213
+ * @throws {Error} If the vault service returns an invalid response
214
+ * @throws {Error} If not file ID, name or content is set
215
+ * @throws {FetchError} If the download URL fetch fails
216
+ */
217
+ getDownloadUrl(): Promise<URL>;
218
+ /**
219
+ * Uploads a file to the vault.
63
220
  *
64
221
  * @example
222
+ * ```ts
65
223
  * const file = new File(['content'], 'document.txt')
66
- * await vaultFile.addHashToName(file) // Adds hash prefix to filename
224
+ * const vaultFile = await VaultFile.fromBlob('document.txt', file, { vaultUrl, authStrategy })
67
225
  * await vaultFile.upload(file)
226
+ * ```
68
227
  *
69
- * @param file - The file to upload to the vault
228
+ * @param file - The file to upload to the vault. If not provided, the file content will be taken from the `content` property.
70
229
  * @throws {FetchError} If the upload fails
230
+ * @throws {Error} If the file content is not set and no file is provided
71
231
  * @returns Promise that resolves when upload is complete
72
232
  */
73
- upload(file: Blob): Promise<void>;
233
+ upload(file?: Blob, url?: string): Promise<void>;
234
+ /**
235
+ * Downloads a file from the vault.
236
+ *
237
+ * @param responseType - The type of the response
238
+ * @returns The response from the vault
239
+ *
240
+ * @example
241
+ * ```ts
242
+ * const vaultFile = await VaultFile.fromVaultReference('vault://1234567890', { vaultUrl, authStrategy })
243
+ * const content = await vaultFile.download()
244
+ * ```
245
+ */
74
246
  download(responseType?: 'blob'): Promise<Blob>;
75
247
  download(responseType: 'base64'): Promise<string>;
76
248
  }
77
249
 
78
250
  declare class FetchError extends Error {
251
+ readonly message: string;
252
+ readonly url: string;
253
+ readonly method: string;
79
254
  readonly response: Response;
80
- constructor(message: string, response: Response);
255
+ constructor(message: string, url: string, method: string, response: Response);
256
+ static from(url: string, method: string, response: Response): Promise<FetchError>;
81
257
  }
82
258
 
83
- declare function useVault(vaultUrl: string, authStrategy: AuthStrategy): {
84
- createVaultFile: (name: string) => VaultFile;
259
+ declare function vaultClient(config: VaultConfig): {
260
+ createFromContent: (name: string, content: Blob | File) => Promise<VaultFile>;
261
+ createFromReference: (reference: string) => Promise<VaultFile>;
85
262
  };
86
263
 
87
- export { APIKeyAuthStrategy, type AuthStrategy, DataTokenAuthStrategy, FetchError, VaultFile, useVault };
264
+ export { APIKeyAuthStrategy, type AuthStrategy, DataTokenAuthStrategy, FetchError, type VaultConfig, VaultFile, vaultClient };