@meistrari/vault-sdk 3.3.0 → 3.4.1

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.1";
136
135
  const license = "UNLICENSED";
137
136
  const repository = {
138
137
  type: "git",
@@ -150,17 +149,9 @@ const types = "dist/index.d.ts";
150
149
  const files = [
151
150
  "dist"
152
151
  ];
153
- const scripts = {
154
- test: "vitest --no-watch",
155
- "test:watch": "vitest",
156
- build: "unbuild",
157
- lint: "eslint .",
158
- "lint:fix": "eslint . --fix",
159
- check: "bun run lint && bun tsc --noEmit"
160
- };
161
152
  const dependencies = {
162
- "@meistrari/file-type": "22.0.0",
163
- "@meistrari/vault-shared": "0.1.2",
153
+ "@meistrari/file-type": "^22.0.0",
154
+ "@meistrari/vault-shared": "0.2.1",
164
155
  "mime-types": "3.0.1",
165
156
  ofetch: "1.4.1",
166
157
  zod: "4.3.6"
@@ -187,7 +178,6 @@ const packageJson = {
187
178
  main: main,
188
179
  types: types,
189
180
  files: files,
190
- scripts: scripts,
191
181
  dependencies: dependencies,
192
182
  devDependencies: devDependencies,
193
183
  peerDependencies: peerDependencies,
@@ -204,13 +194,6 @@ var __publicField$2 = (obj, key, value) => {
204
194
  return value;
205
195
  };
206
196
  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
197
  async function wrappedFetch$1(url, requestInit) {
215
198
  const request = new Request(url, requestInit);
216
199
  request.headers.set("User-Agent", userAgent);
@@ -222,18 +205,58 @@ async function wrappedFetch$1(url, requestInit) {
222
205
  return response;
223
206
  }
224
207
  class VaultAsset {
225
- constructor({ assetId, assetUrl, uploadUrl, expiresAt, uploadHeaders }) {
208
+ /**
209
+ * Constructs a VaultAsset instance from Vault response data.
210
+ *
211
+ * Direct usage of the constructor is not usually needed. Prefer
212
+ * {@link VaultAsset.create} or {@link VaultAsset.fromContent}.
213
+ *
214
+ * @param params - Values returned by Vault when creating an asset upload.
215
+ * @param params.assetId - Vault-assigned asset ID.
216
+ * @param params.assetUrl - Stable URL used to resolve or download the asset.
217
+ * @param params.uploadUrl - Presigned URL used to upload the asset content.
218
+ * @param params.expiresAt - Expiration time for the presigned upload URL.
219
+ * @param params.uploadHeaders - Headers required by the presigned upload URL.
220
+ * @param params.asset - Stored asset metadata returned by Vault.
221
+ */
222
+ constructor({ assetId, assetUrl, uploadUrl, expiresAt, uploadHeaders, asset }) {
223
+ /** Vault-assigned asset ID. */
226
224
  __publicField$2(this, "assetId");
225
+ /** Stable URL used to resolve or download the asset. */
227
226
  __publicField$2(this, "assetUrl");
227
+ /** Presigned URL used to upload asset content. */
228
228
  __publicField$2(this, "uploadUrl");
229
+ /** Expiration time for the presigned upload URL. */
229
230
  __publicField$2(this, "expiresAt");
231
+ /** Headers that must be sent when uploading to {@link VaultAsset.uploadUrl}. */
230
232
  __publicField$2(this, "uploadHeaders");
233
+ /** Stored asset metadata returned by Vault. */
234
+ __publicField$2(this, "asset");
231
235
  this.assetId = assetId;
232
236
  this.assetUrl = new URL(assetUrl);
233
237
  this.uploadUrl = new URL(uploadUrl);
234
238
  this.expiresAt = new Date(expiresAt);
235
239
  this.uploadHeaders = uploadHeaders;
240
+ this.asset = asset;
236
241
  }
242
+ /**
243
+ * Creates an asset upload URL without uploading content.
244
+ *
245
+ * Use {@link VaultAsset.upload} to upload content later, or use
246
+ * {@link VaultAsset.fromContent} with `upload: true` to create and upload in one call.
247
+ *
248
+ * @param vaultConfig - Vault client configuration.
249
+ * @param params - Parameters for the asset upload.
250
+ * @param params.assetClass - Application-defined class for grouping assets.
251
+ * @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.
252
+ * @param params.mimeType - MIME type of the content that will be uploaded.
253
+ * @param params.size - Size of the content in bytes, when known.
254
+ * @param options - Additional options for the request.
255
+ * @param options.signal - Signal used to abort the request.
256
+ * @returns A VaultAsset containing the asset URL and presigned upload URL.
257
+ * @throws {FetchError} If Vault rejects the create request.
258
+ * @throws {Error} If Vault returns a response that does not match the SDK schema.
259
+ */
237
260
  static async create(vaultConfig, params, options) {
238
261
  const config = resolveConfig(vaultConfig);
239
262
  const headers = config.authStrategy.getHeaders();
@@ -244,19 +267,40 @@ class VaultAsset {
244
267
  headers,
245
268
  body: JSON.stringify({
246
269
  assetClass: params.assetClass,
270
+ subject: params.subject,
247
271
  mimeType: params.mimeType,
248
272
  size: params.size
249
273
  })
250
- }).then(async (response2) => await response2.json()).then((data) => PublicAssetUploadResponse.safeParse(data));
274
+ }).then(async (response2) => await response2.json()).then((data) => CreateAssetUploadResponse.safeParse(data));
251
275
  if (!response.success) {
252
276
  throw new Error(`Invalid response from vault service. ${JSON.stringify(response.error)}`);
253
277
  }
254
278
  return new VaultAsset(response.data);
255
279
  }
280
+ /**
281
+ * Creates an asset from Blob or File content.
282
+ *
283
+ * The SDK detects the MIME type when `params.mimeType` is omitted. Set
284
+ * `params.upload` to `true` to upload the content immediately.
285
+ *
286
+ * @param vaultConfig - Vault client configuration.
287
+ * @param content - Content to associate with the asset.
288
+ * @param params - Parameters for the asset.
289
+ * @param params.assetClass - Application-defined class for grouping assets.
290
+ * @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.
291
+ * @param params.mimeType - MIME type override for the content.
292
+ * @param params.upload - Whether to upload the content immediately after creating the asset.
293
+ * @param options - Additional options for the create and upload requests.
294
+ * @param options.signal - Signal used to abort either request.
295
+ * @returns A VaultAsset containing the asset URL and presigned upload URL.
296
+ * @throws {FetchError} If Vault rejects the create request or the upload request fails.
297
+ * @throws {Error} If Vault returns a response that does not match the SDK schema.
298
+ */
256
299
  static async fromContent(vaultConfig, content, params, options) {
257
300
  const mimeType = params.mimeType ?? await detectFileMimeType(content) ?? "application/octet-stream";
258
301
  const asset = await VaultAsset.create(vaultConfig, {
259
302
  assetClass: params.assetClass,
303
+ subject: params.subject,
260
304
  mimeType,
261
305
  size: content.size
262
306
  }, options);
@@ -265,6 +309,16 @@ class VaultAsset {
265
309
  }
266
310
  return asset;
267
311
  }
312
+ /**
313
+ * Uploads content to this asset's presigned upload URL.
314
+ *
315
+ * The upload request sends the headers returned by Vault when the asset was created.
316
+ *
317
+ * @param content - Content to upload.
318
+ * @param options - Additional options for the upload request.
319
+ * @param options.signal - Signal used to abort the upload.
320
+ * @throws {FetchError} If the upload endpoint returns an error response.
321
+ */
268
322
  async upload(content, options) {
269
323
  await wrappedFetch$1(this.uploadUrl, {
270
324
  method: "PUT",
@@ -2098,9 +2152,32 @@ function vaultClient(vaultConfig) {
2098
2152
  }, { signal: options?.signal });
2099
2153
  }
2100
2154
  const assets = {
2155
+ /**
2156
+ * Creates an asset upload URL without uploading content.
2157
+ *
2158
+ * `params.subject` should be a stable application-owned entity reference such as
2159
+ * `user:user_123` or `organization:org_123`. Vault stores it for lookup and cleanup,
2160
+ * but does not interpret it, enforce authorization from it, or include it in the asset URL.
2161
+ *
2162
+ * @param params - Parameters for the asset upload.
2163
+ * @param options - Additional options for the request.
2164
+ * @returns A VaultAsset with the stable asset URL and presigned upload URL.
2165
+ */
2101
2166
  async create(params, options) {
2102
2167
  return await VaultAsset.create(config, params, { signal: options?.signal });
2103
2168
  },
2169
+ /**
2170
+ * Creates an asset from File or Blob content and optionally uploads it immediately.
2171
+ *
2172
+ * `params.subject` should be a stable application-owned entity reference such as
2173
+ * `user:user_123` or `organization:org_123`. Vault stores it for lookup and cleanup,
2174
+ * but does not interpret it, enforce authorization from it, or include it in the asset URL.
2175
+ *
2176
+ * @param content - File or Blob content to associate with the asset.
2177
+ * @param params - Parameters for the asset.
2178
+ * @param options - Additional options for the create and upload requests.
2179
+ * @returns A VaultAsset with the stable asset URL and presigned upload URL.
2180
+ */
2104
2181
  async createFromContent(content, params, options) {
2105
2182
  return await VaultAsset.fromContent(config, content, params, { signal: options?.signal });
2106
2183
  }
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.1",
4
4
  "license": "UNLICENSED",
5
5
  "repository": {
6
6
  "type": "git",
@@ -18,17 +18,9 @@
18
18
  "files": [
19
19
  "dist"
20
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
21
  "dependencies": {
30
- "@meistrari/file-type": "22.0.0",
31
- "@meistrari/vault-shared": "0.1.2",
22
+ "@meistrari/file-type": "^22.0.0",
23
+ "@meistrari/vault-shared": "0.2.1",
32
24
  "mime-types": "3.0.1",
33
25
  "ofetch": "1.4.1",
34
26
  "zod": "4.3.6"