@meistrari/vault-sdk 3.3.0 → 3.4.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.mjs CHANGED
@@ -1,7 +1,6 @@
1
- import { z } from 'zod';
1
+ import { CreateAssetUploadResponse, GetUploadUrlResponseV2, GetDownloadUrlResponse } from '@meistrari/vault-shared/schemas';
2
2
  import { fileTypeFromBlob } from '@meistrari/file-type';
3
3
  import { lookup } from 'mime-types';
4
- import { GetUploadUrlResponseV2, GetDownloadUrlResponse } from '@meistrari/vault-shared/schemas';
5
4
  import vaultUtils from '@meistrari/vault-shared/utils';
6
5
 
7
6
  var __defProp$3 = Object.defineProperty;
@@ -72,7 +71,7 @@ async function getFileHash(blob) {
72
71
  }
73
72
  async function detectFileMimeType(blob) {
74
73
  if (blob instanceof Blob && blob.type) {
75
- return blob.type.split(";")[0].trim();
74
+ return blob.type.split(";")[0]?.trim();
76
75
  }
77
76
  if ("name" in blob && typeof blob.name === "string") {
78
77
  const extension = blob.name.split(".").pop()?.toLowerCase();
@@ -100,7 +99,7 @@ async function detectFileMimeType(blob) {
100
99
  if (lines.length > 1) {
101
100
  const commaCounts = lines.map((line) => (line.match(/,/g) || []).length);
102
101
  const allSame = commaCounts.every((count) => count === commaCounts[0]);
103
- if (allSame && commaCounts[0] > 0) {
102
+ if (allSame && (commaCounts[0] ?? 0) > 0) {
104
103
  return "text/csv";
105
104
  }
106
105
  }
@@ -132,7 +131,7 @@ function getFileName(content) {
132
131
  }
133
132
 
134
133
  const name = "@meistrari/vault-sdk";
135
- const version = "3.3.0";
134
+ const version = "3.4.0";
136
135
  const license = "UNLICENSED";
137
136
  const repository = {
138
137
  type: "git",
@@ -160,7 +159,7 @@ const scripts = {
160
159
  };
161
160
  const dependencies = {
162
161
  "@meistrari/file-type": "22.0.0",
163
- "@meistrari/vault-shared": "0.1.2",
162
+ "@meistrari/vault-shared": "0.2.0",
164
163
  "mime-types": "3.0.1",
165
164
  ofetch: "1.4.1",
166
165
  zod: "4.3.6"
@@ -204,13 +203,6 @@ var __publicField$2 = (obj, key, value) => {
204
203
  return value;
205
204
  };
206
205
  const compatibilityDate$1 = "2025-05-19";
207
- const PublicAssetUploadResponse = z.object({
208
- assetId: z.string(),
209
- assetUrl: z.string().url(),
210
- uploadUrl: z.string().url(),
211
- expiresAt: z.string().datetime(),
212
- uploadHeaders: z.record(z.string(), z.string())
213
- });
214
206
  async function wrappedFetch$1(url, requestInit) {
215
207
  const request = new Request(url, requestInit);
216
208
  request.headers.set("User-Agent", userAgent);
@@ -222,18 +214,58 @@ async function wrappedFetch$1(url, requestInit) {
222
214
  return response;
223
215
  }
224
216
  class VaultAsset {
225
- constructor({ assetId, assetUrl, uploadUrl, expiresAt, uploadHeaders }) {
217
+ /**
218
+ * Constructs a VaultAsset instance from Vault response data.
219
+ *
220
+ * Direct usage of the constructor is not usually needed. Prefer
221
+ * {@link VaultAsset.create} or {@link VaultAsset.fromContent}.
222
+ *
223
+ * @param params - Values returned by Vault when creating an asset upload.
224
+ * @param params.assetId - Vault-assigned asset ID.
225
+ * @param params.assetUrl - Stable URL used to resolve or download the asset.
226
+ * @param params.uploadUrl - Presigned URL used to upload the asset content.
227
+ * @param params.expiresAt - Expiration time for the presigned upload URL.
228
+ * @param params.uploadHeaders - Headers required by the presigned upload URL.
229
+ * @param params.asset - Stored asset metadata returned by Vault.
230
+ */
231
+ constructor({ assetId, assetUrl, uploadUrl, expiresAt, uploadHeaders, asset }) {
232
+ /** Vault-assigned asset ID. */
226
233
  __publicField$2(this, "assetId");
234
+ /** Stable URL used to resolve or download the asset. */
227
235
  __publicField$2(this, "assetUrl");
236
+ /** Presigned URL used to upload asset content. */
228
237
  __publicField$2(this, "uploadUrl");
238
+ /** Expiration time for the presigned upload URL. */
229
239
  __publicField$2(this, "expiresAt");
240
+ /** Headers that must be sent when uploading to {@link VaultAsset.uploadUrl}. */
230
241
  __publicField$2(this, "uploadHeaders");
242
+ /** Stored asset metadata returned by Vault. */
243
+ __publicField$2(this, "asset");
231
244
  this.assetId = assetId;
232
245
  this.assetUrl = new URL(assetUrl);
233
246
  this.uploadUrl = new URL(uploadUrl);
234
247
  this.expiresAt = new Date(expiresAt);
235
248
  this.uploadHeaders = uploadHeaders;
249
+ this.asset = asset;
236
250
  }
251
+ /**
252
+ * Creates an asset upload URL without uploading content.
253
+ *
254
+ * Use {@link VaultAsset.upload} to upload content later, or use
255
+ * {@link VaultAsset.fromContent} with `upload: true` to create and upload in one call.
256
+ *
257
+ * @param vaultConfig - Vault client configuration.
258
+ * @param params - Parameters for the asset upload.
259
+ * @param params.assetClass - Application-defined class for grouping assets.
260
+ * @param params.subject - Opaque application-owned entity reference for lookup and cleanup. Use a stable namespaced value such as `user:user_123`; Vault does not interpret it or include it in the asset URL.
261
+ * @param params.mimeType - MIME type of the content that will be uploaded.
262
+ * @param params.size - Size of the content in bytes, when known.
263
+ * @param options - Additional options for the request.
264
+ * @param options.signal - Signal used to abort the request.
265
+ * @returns A VaultAsset containing the asset URL and presigned upload URL.
266
+ * @throws {FetchError} If Vault rejects the create request.
267
+ * @throws {Error} If Vault returns a response that does not match the SDK schema.
268
+ */
237
269
  static async create(vaultConfig, params, options) {
238
270
  const config = resolveConfig(vaultConfig);
239
271
  const headers = config.authStrategy.getHeaders();
@@ -244,19 +276,40 @@ class VaultAsset {
244
276
  headers,
245
277
  body: JSON.stringify({
246
278
  assetClass: params.assetClass,
279
+ subject: params.subject,
247
280
  mimeType: params.mimeType,
248
281
  size: params.size
249
282
  })
250
- }).then(async (response2) => await response2.json()).then((data) => PublicAssetUploadResponse.safeParse(data));
283
+ }).then(async (response2) => await response2.json()).then((data) => CreateAssetUploadResponse.safeParse(data));
251
284
  if (!response.success) {
252
285
  throw new Error(`Invalid response from vault service. ${JSON.stringify(response.error)}`);
253
286
  }
254
287
  return new VaultAsset(response.data);
255
288
  }
289
+ /**
290
+ * Creates an asset from Blob or File content.
291
+ *
292
+ * The SDK detects the MIME type when `params.mimeType` is omitted. Set
293
+ * `params.upload` to `true` to upload the content immediately.
294
+ *
295
+ * @param vaultConfig - Vault client configuration.
296
+ * @param content - Content to associate with the asset.
297
+ * @param params - Parameters for the asset.
298
+ * @param params.assetClass - Application-defined class for grouping assets.
299
+ * @param params.subject - Opaque application-owned entity reference for lookup and cleanup. Use a stable namespaced value such as `user:user_123`; Vault does not interpret it or include it in the asset URL.
300
+ * @param params.mimeType - MIME type override for the content.
301
+ * @param params.upload - Whether to upload the content immediately after creating the asset.
302
+ * @param options - Additional options for the create and upload requests.
303
+ * @param options.signal - Signal used to abort either request.
304
+ * @returns A VaultAsset containing the asset URL and presigned upload URL.
305
+ * @throws {FetchError} If Vault rejects the create request or the upload request fails.
306
+ * @throws {Error} If Vault returns a response that does not match the SDK schema.
307
+ */
256
308
  static async fromContent(vaultConfig, content, params, options) {
257
309
  const mimeType = params.mimeType ?? await detectFileMimeType(content) ?? "application/octet-stream";
258
310
  const asset = await VaultAsset.create(vaultConfig, {
259
311
  assetClass: params.assetClass,
312
+ subject: params.subject,
260
313
  mimeType,
261
314
  size: content.size
262
315
  }, options);
@@ -265,6 +318,16 @@ class VaultAsset {
265
318
  }
266
319
  return asset;
267
320
  }
321
+ /**
322
+ * Uploads content to this asset's presigned upload URL.
323
+ *
324
+ * The upload request sends the headers returned by Vault when the asset was created.
325
+ *
326
+ * @param content - Content to upload.
327
+ * @param options - Additional options for the upload request.
328
+ * @param options.signal - Signal used to abort the upload.
329
+ * @throws {FetchError} If the upload endpoint returns an error response.
330
+ */
268
331
  async upload(content, options) {
269
332
  await wrappedFetch$1(this.uploadUrl, {
270
333
  method: "PUT",
@@ -2098,9 +2161,32 @@ function vaultClient(vaultConfig) {
2098
2161
  }, { signal: options?.signal });
2099
2162
  }
2100
2163
  const assets = {
2164
+ /**
2165
+ * Creates an asset upload URL without uploading content.
2166
+ *
2167
+ * `params.subject` should be a stable application-owned entity reference such as
2168
+ * `user:user_123` or `organization:org_123`. Vault stores it for lookup and cleanup,
2169
+ * but does not interpret it, enforce authorization from it, or include it in the asset URL.
2170
+ *
2171
+ * @param params - Parameters for the asset upload.
2172
+ * @param options - Additional options for the request.
2173
+ * @returns A VaultAsset with the stable asset URL and presigned upload URL.
2174
+ */
2101
2175
  async create(params, options) {
2102
2176
  return await VaultAsset.create(config, params, { signal: options?.signal });
2103
2177
  },
2178
+ /**
2179
+ * Creates an asset from File or Blob content and optionally uploads it immediately.
2180
+ *
2181
+ * `params.subject` should be a stable application-owned entity reference such as
2182
+ * `user:user_123` or `organization:org_123`. Vault stores it for lookup and cleanup,
2183
+ * but does not interpret it, enforce authorization from it, or include it in the asset URL.
2184
+ *
2185
+ * @param content - File or Blob content to associate with the asset.
2186
+ * @param params - Parameters for the asset.
2187
+ * @param options - Additional options for the create and upload requests.
2188
+ * @returns A VaultAsset with the stable asset URL and presigned upload URL.
2189
+ */
2104
2190
  async createFromContent(content, params, options) {
2105
2191
  return await VaultAsset.fromContent(config, content, params, { signal: options?.signal });
2106
2192
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meistrari/vault-sdk",
3
- "version": "3.3.0",
3
+ "version": "3.4.0",
4
4
  "license": "UNLICENSED",
5
5
  "repository": {
6
6
  "type": "git",
@@ -28,7 +28,7 @@
28
28
  },
29
29
  "dependencies": {
30
30
  "@meistrari/file-type": "22.0.0",
31
- "@meistrari/vault-shared": "0.1.2",
31
+ "@meistrari/vault-shared": "0.2.0",
32
32
  "mime-types": "3.0.1",
33
33
  "ofetch": "1.4.1",
34
34
  "zod": "4.3.6"