calmlens 0.2.1 → 0.3.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/bin/calmlens.js CHANGED
@@ -100,6 +100,11 @@ const PUBLIC_ERROR_CODES = {
100
100
  defaultMessage: "This API key belongs to a different project. Use a key created for the project that owns this resource.",
101
101
  status: 403
102
102
  }),
103
+ API_KEY_NOT_ACCEPTED: new sendable_error.ErrorCode({
104
+ id: "auth/api-key-not-accepted",
105
+ defaultMessage: "API keys are only accepted by the public API at /v1.",
106
+ status: 403
107
+ }),
103
108
  FILE_TOO_LARGE: new sendable_error.ErrorCode({
104
109
  id: "upload/file-too-large",
105
110
  defaultMessage: "File size exceeds limit. Maximum size for images is 10MB, for other files is 5GB.",
@@ -135,6 +135,11 @@ const PUBLIC_ERROR_CODES = {
135
135
  defaultMessage: "This API key belongs to a different project. Use a key created for the project that owns this resource.",
136
136
  status: 403
137
137
  }),
138
+ API_KEY_NOT_ACCEPTED: new sendable_error.ErrorCode({
139
+ id: "auth/api-key-not-accepted",
140
+ defaultMessage: "API keys are only accepted by the public API at /v1.",
141
+ status: 403
142
+ }),
138
143
  FILE_TOO_LARGE: new sendable_error.ErrorCode({
139
144
  id: "upload/file-too-large",
140
145
  defaultMessage: "File size exceeds limit. Maximum size for images is 10MB, for other files is 5GB.",
package/cjs/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_CalmLensUtils = require("./CalmLensUtils-DioFUAmI.js");
2
+ const require_CalmLensUtils = require("./CalmLensUtils-DocJkqQj.js");
3
3
  let api_def = require("api-def");
4
4
  //#region src/client/CalmLensClient.ts
5
5
  const _excluded = ["uint8Array"];
package/cjs/testing.d.ts CHANGED
@@ -1,18 +1,28 @@
1
1
  import { A as AssetBinaryPayloadInput, D as GetEventsResponse, E as GetAssetsPageResponse, M as Folder, N as Asset, O as PostAssetSignedUrlResponse, P as AssetStatus, S as UploadAssetPartOptions, T as GetAssetResponse, a as CreateFolderOptions, c as EnsureLensesOptions, d as ListAssetsOptions, f as ListEventsOptions, h as MoveFolderOptions, i as CreateAssetSignedUrlOptions, j as Lens, l as FolderIdOptions, m as MoveAssetOptions, n as CdnAssetUrlOptions, p as ModerateTextOptions, r as CdnServeOptions, s as EnsureLensOptions, t as CalmLensClientOptions, u as ListAssetChildrenOptions, v as RetrieveAssetOptions, w as GetAssetChildrenResponse, x as UpdateFolderOptions, y as SubmitAssetOptions } from "./CalmLensTypes-Bnq4u8HJ.js";
2
2
  import * as zod from "zod";
3
- //#region ../shared/src/Roles.d.ts
4
- declare const ROLE_SCHEMA: zod.ZodEnum<{
5
- "assets:view-private": "assets:view-private";
6
- "system:assets:view-all": "system:assets:view-all";
7
- "system:banned": "system:banned";
8
- "system:super-admin": "system:super-admin";
9
- "system:uploads:unlimited": "system:uploads:unlimited";
10
- }>;
11
- type Role = zod.infer<typeof ROLE_SCHEMA>;
3
+ //#region ../shared/src/Permissions.d.ts
4
+ declare const GRANT_SCHEMA: zod.ZodObject<{
5
+ actions: zod.ZodArray<zod.ZodEnum<{
6
+ "assets:create": "assets:create";
7
+ "assets:delete": "assets:delete";
8
+ "assets:download": "assets:download";
9
+ "assets:read": "assets:read";
10
+ "assets:share": "assets:share";
11
+ "assets:update": "assets:update";
12
+ "folders:create": "folders:create";
13
+ "folders:delete": "folders:delete";
14
+ "folders:read": "folders:read";
15
+ "folders:update": "folders:update";
16
+ "project:manage": "project:manage";
17
+ }>>;
18
+ folderId: zod.ZodNullable<zod.ZodString>;
19
+ includeSubfolders: zod.ZodBoolean;
20
+ }, zod.core.$strict>;
21
+ type PermissionGrant = zod.infer<typeof GRANT_SCHEMA>;
12
22
  //#endregion
13
23
  //#region src/testing/MockApiKey.d.ts
14
24
  export declare const MOCK_API_KEY_PREFIX = "sk_test_";
15
- export declare const createMockApiKey: (roles?: Role[]) => string;
25
+ export declare const createMockApiKey: (grants?: PermissionGrant[]) => string;
16
26
  //#endregion
17
27
  //#region src/testing/MockStorage.d.ts
18
28
  interface MockStoredContent {
@@ -93,7 +103,7 @@ export declare class MockCalmLensClient {
93
103
  private readonly now;
94
104
  private readonly queuedStatus;
95
105
  private readonly analyzedStatus;
96
- private readonly apiKeyRoles;
106
+ private readonly apiKeyGrants;
97
107
  private readonly signedUrlBaseUrl;
98
108
  private readonly cdnBaseUrl;
99
109
  private readonly lenses;
package/cjs/testing.js CHANGED
@@ -21,17 +21,56 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
21
21
  enumerable: true
22
22
  }) : target, mod));
23
23
  //#endregion
24
- const require_CalmLensUtils = require("./CalmLensUtils-DioFUAmI.js");
24
+ const require_CalmLensUtils = require("./CalmLensUtils-DocJkqQj.js");
25
25
  let zod = require("zod");
26
26
  zod = __toESM(zod);
27
- //#region ../shared/src/Roles.ts
28
- const ROLE_SCHEMA = zod.enum([
29
- "system:super-admin",
30
- "system:banned",
31
- "system:assets:view-all",
32
- "assets:view-private",
27
+ //#region ../shared/src/Permissions.ts
28
+ const ACTION_SCHEMA = zod.enum([
29
+ "assets:read",
30
+ "assets:download",
31
+ "assets:create",
32
+ "assets:update",
33
+ "assets:delete",
34
+ "assets:share",
35
+ "folders:read",
36
+ "folders:create",
37
+ "folders:update",
38
+ "folders:delete",
39
+ "project:manage"
40
+ ]);
41
+ const GRANT_SCHEMA = zod.object({
42
+ actions: zod.array(ACTION_SCHEMA),
43
+ folderId: zod.string().uuid().nullable(),
44
+ includeSubfolders: zod.boolean()
45
+ }).strict().refine((g) => g.folderId === null || !g.actions.includes("project:manage"), "Project management requires project scope");
46
+ const GRANTS_SCHEMA = zod.array(GRANT_SCHEMA).max(100);
47
+ zod.enum([
48
+ "admin",
49
+ "member",
50
+ "viewer",
51
+ "custom"
52
+ ]);
53
+ const permits = (grants, action, folderId, folders) => grants.some((grant) => {
54
+ if (!grant.actions.includes(action)) return false;
55
+ if (grant.folderId === null) return true;
56
+ let current = folderId;
57
+ const visited = /* @__PURE__ */ new Set();
58
+ while (current && !visited.has(current)) {
59
+ var _folders$find$parentI, _folders$find;
60
+ if (current === grant.folderId) return true;
61
+ if (!grant.includeSubfolders) return false;
62
+ visited.add(current);
63
+ current = (_folders$find$parentI = (_folders$find = folders.find((folder) => folder.id === current)) === null || _folders$find === void 0 ? void 0 : _folders$find.parentId) !== null && _folders$find$parentI !== void 0 ? _folders$find$parentI : null;
64
+ }
65
+ return false;
66
+ });
67
+ const SYSTEM_PERMISSION_SCHEMA = zod.enum([
68
+ "system:admin",
69
+ "system:assets:read-all",
33
70
  "system:uploads:unlimited"
34
71
  ]);
72
+ zod.array(SYSTEM_PERMISSION_SCHEMA);
73
+ zod.enum(["active", "banned"]);
35
74
  //#endregion
36
75
  //#region src/testing/MockApiKey.ts
37
76
  const MOCK_API_KEY_PREFIX = "sk_test_";
@@ -40,16 +79,11 @@ const fromBase64Url = (value) => {
40
79
  const padded = value.padEnd(value.length + (4 - value.length % 4) % 4, "=");
41
80
  return atob(padded.replaceAll("-", "+").replaceAll("_", "/"));
42
81
  };
43
- const isRole = (value) => typeof value === "string" && ROLE_SCHEMA.options.includes(value);
44
- const createMockApiKey = (roles = []) => {
45
- const encodedRoles = toBase64Url(JSON.stringify([...new Set(roles)].sort()));
46
- return `${MOCK_API_KEY_PREFIX}${encodedRoles}`;
47
- };
48
- const getMockApiKeyRoles = (apiKey) => {
82
+ const createMockApiKey = (grants = []) => `${MOCK_API_KEY_PREFIX}${toBase64Url(JSON.stringify(GRANTS_SCHEMA.parse(grants)))}`;
83
+ const getMockApiKeyGrants = (apiKey) => {
49
84
  if (!apiKey.startsWith("sk_test_")) return [];
50
85
  try {
51
- const roles = JSON.parse(fromBase64Url(apiKey.slice(8)));
52
- return Array.isArray(roles) ? roles.filter(isRole) : [];
86
+ return GRANTS_SCHEMA.parse(JSON.parse(fromBase64Url(apiKey.slice(8))));
53
87
  } catch (_unused) {
54
88
  return [];
55
89
  }
@@ -245,7 +279,7 @@ var MockCalmLensClient = class {
245
279
  this.now = (_options$mockOptions$4 = (_options$mockOptions4 = options.mockOptions) === null || _options$mockOptions4 === void 0 ? void 0 : _options$mockOptions4.now) !== null && _options$mockOptions$4 !== void 0 ? _options$mockOptions$4 : Date.now;
246
280
  this.queuedStatus = (_options$mockOptions$5 = (_options$mockOptions5 = options.mockOptions) === null || _options$mockOptions5 === void 0 ? void 0 : _options$mockOptions5.queuedStatus) !== null && _options$mockOptions$5 !== void 0 ? _options$mockOptions$5 : "pending";
247
281
  this.analyzedStatus = (_options$mockOptions$6 = (_options$mockOptions6 = options.mockOptions) === null || _options$mockOptions6 === void 0 ? void 0 : _options$mockOptions6.analyzedStatus) !== null && _options$mockOptions$6 !== void 0 ? _options$mockOptions$6 : "approved";
248
- this.apiKeyRoles = getMockApiKeyRoles(options.apiKey);
282
+ this.apiKeyGrants = getMockApiKeyGrants(options.apiKey);
249
283
  this.signedUrlBaseUrl = (_options$mockOptions$7 = (_options$mockOptions7 = options.mockOptions) === null || _options$mockOptions7 === void 0 ? void 0 : _options$mockOptions7.signedUrlBaseUrl) !== null && _options$mockOptions$7 !== void 0 ? _options$mockOptions$7 : "mock://calmlens";
250
284
  this.cdnBaseUrl = (_options$mockOptions$8 = (_options$mockOptions8 = options.mockOptions) === null || _options$mockOptions8 === void 0 ? void 0 : _options$mockOptions8.cdnBaseUrl) !== null && _options$mockOptions$8 !== void 0 ? _options$mockOptions$8 : "mock://calmlens/v1";
251
285
  }
@@ -262,14 +296,14 @@ var MockCalmLensClient = class {
262
296
  },
263
297
  serve: function() {
264
298
  var _ref3 = require_CalmLensUtils._asyncToGenerator(function* (_ref2) {
265
- var _storedContent$type;
299
+ var _asset$folderId, _await$this$databaseS, _this$databaseStorage, _this$databaseStorage2, _storedContent$type;
266
300
  let { assetIdentifier } = _ref2, options = require_CalmLensUtils._objectWithoutProperties(_ref2, _excluded2);
267
301
  if (!assetIdentifier) throw new Error("Asset identifier is required");
268
302
  const asset = (yield _this.databaseStorage.listAssets(_this.options.projectId)).find((asset) => asset.id === assetIdentifier || asset.externalId === assetIdentifier);
269
303
  if (!asset) return new Response("Asset not found", { status: 404 });
270
- if (asset.visibility !== "public" && !_this.apiKeyRoles.includes("assets:view-private") && !_this.apiKeyRoles.includes("system:assets:view-all") && !_this.apiKeyRoles.includes("system:super-admin")) return new Response(JSON.stringify({
271
- code: "asset/api-key-missing-role",
272
- message: "This API key needs assets:view-private to read private assets. Use a signed asset URL or enable the permission."
304
+ if (asset.visibility !== "public" && !permits(_this.apiKeyGrants, "assets:download", (_asset$folderId = asset.folderId) !== null && _asset$folderId !== void 0 ? _asset$folderId : null, (_await$this$databaseS = yield (_this$databaseStorage = (_this$databaseStorage2 = _this.databaseStorage).listFolders) === null || _this$databaseStorage === void 0 ? void 0 : _this$databaseStorage.call(_this$databaseStorage2, _this.options.projectId)) !== null && _await$this$databaseS !== void 0 ? _await$this$databaseS : [])) return new Response(JSON.stringify({
305
+ code: "asset/api-key-missing-permission",
306
+ message: "This API key needs assets:download permission for this asset’s folder."
273
307
  }), {
274
308
  status: 403,
275
309
  headers: { "Content-Type": "application/json" }
@@ -135,6 +135,11 @@ const PUBLIC_ERROR_CODES = {
135
135
  defaultMessage: "This API key belongs to a different project. Use a key created for the project that owns this resource.",
136
136
  status: 403
137
137
  }),
138
+ API_KEY_NOT_ACCEPTED: new ErrorCode({
139
+ id: "auth/api-key-not-accepted",
140
+ defaultMessage: "API keys are only accepted by the public API at /v1.",
141
+ status: 403
142
+ }),
138
143
  FILE_TOO_LARGE: new ErrorCode({
139
144
  id: "upload/file-too-large",
140
145
  defaultMessage: "File size exceeds limit. Maximum size for images is 10MB, for other files is 5GB.",
package/esm/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { _ as getAssetPayloadByteLength, a as CalmLensError, b as _asyncToGenerator, c as DOMAIN, d as getRejectedUploadMimeTypeError, f as isRejectedUploadMimeType, g as assetToData, i as _objectWithoutProperties, m as stringifyJsonCanonical, n as verifyWebhookSignature, o as SDK_ERRORS, p as PUBLIC_ERROR_CODES, r as _objectSpread2, s as calmLensError, t as toTextAssetOptions, v as getRawBase64, y as validateAssetPayload } from "./CalmLensUtils-Dixed1Ox.js";
1
+ import { _ as getAssetPayloadByteLength, a as CalmLensError, b as _asyncToGenerator, c as DOMAIN, d as getRejectedUploadMimeTypeError, f as isRejectedUploadMimeType, g as assetToData, i as _objectWithoutProperties, m as stringifyJsonCanonical, n as verifyWebhookSignature, o as SDK_ERRORS, p as PUBLIC_ERROR_CODES, r as _objectSpread2, s as calmLensError, t as toTextAssetOptions, v as getRawBase64, y as validateAssetPayload } from "./CalmLensUtils--N_91gHC.js";
2
2
  import { Api, RequestCoalescingMiddleware, RequestEvent, getErrorResponse, isRequestError } from "api-def";
3
3
  //#region src/client/CalmLensClient.ts
4
4
  const _excluded = ["uint8Array"];
package/esm/testing.d.ts CHANGED
@@ -1,18 +1,28 @@
1
1
  import { A as AssetBinaryPayloadInput, D as GetEventsResponse, E as GetAssetsPageResponse, M as Folder, N as Asset, O as PostAssetSignedUrlResponse, P as AssetStatus, S as UploadAssetPartOptions, T as GetAssetResponse, a as CreateFolderOptions, c as EnsureLensesOptions, d as ListAssetsOptions, f as ListEventsOptions, h as MoveFolderOptions, i as CreateAssetSignedUrlOptions, j as Lens, l as FolderIdOptions, m as MoveAssetOptions, n as CdnAssetUrlOptions, p as ModerateTextOptions, r as CdnServeOptions, s as EnsureLensOptions, t as CalmLensClientOptions, u as ListAssetChildrenOptions, v as RetrieveAssetOptions, w as GetAssetChildrenResponse, x as UpdateFolderOptions, y as SubmitAssetOptions } from "./CalmLensTypes-Bnq4u8HJ.js";
2
2
  import * as zod from "zod";
3
- //#region ../shared/src/Roles.d.ts
4
- declare const ROLE_SCHEMA: zod.ZodEnum<{
5
- "assets:view-private": "assets:view-private";
6
- "system:assets:view-all": "system:assets:view-all";
7
- "system:banned": "system:banned";
8
- "system:super-admin": "system:super-admin";
9
- "system:uploads:unlimited": "system:uploads:unlimited";
10
- }>;
11
- type Role = zod.infer<typeof ROLE_SCHEMA>;
3
+ //#region ../shared/src/Permissions.d.ts
4
+ declare const GRANT_SCHEMA: zod.ZodObject<{
5
+ actions: zod.ZodArray<zod.ZodEnum<{
6
+ "assets:create": "assets:create";
7
+ "assets:delete": "assets:delete";
8
+ "assets:download": "assets:download";
9
+ "assets:read": "assets:read";
10
+ "assets:share": "assets:share";
11
+ "assets:update": "assets:update";
12
+ "folders:create": "folders:create";
13
+ "folders:delete": "folders:delete";
14
+ "folders:read": "folders:read";
15
+ "folders:update": "folders:update";
16
+ "project:manage": "project:manage";
17
+ }>>;
18
+ folderId: zod.ZodNullable<zod.ZodString>;
19
+ includeSubfolders: zod.ZodBoolean;
20
+ }, zod.core.$strict>;
21
+ type PermissionGrant = zod.infer<typeof GRANT_SCHEMA>;
12
22
  //#endregion
13
23
  //#region src/testing/MockApiKey.d.ts
14
24
  export declare const MOCK_API_KEY_PREFIX = "sk_test_";
15
- export declare const createMockApiKey: (roles?: Role[]) => string;
25
+ export declare const createMockApiKey: (grants?: PermissionGrant[]) => string;
16
26
  //#endregion
17
27
  //#region src/testing/MockStorage.d.ts
18
28
  interface MockStoredContent {
@@ -93,7 +103,7 @@ export declare class MockCalmLensClient {
93
103
  private readonly now;
94
104
  private readonly queuedStatus;
95
105
  private readonly analyzedStatus;
96
- private readonly apiKeyRoles;
106
+ private readonly apiKeyGrants;
97
107
  private readonly signedUrlBaseUrl;
98
108
  private readonly cdnBaseUrl;
99
109
  private readonly lenses;
package/esm/testing.js CHANGED
@@ -1,13 +1,52 @@
1
- import { _ as getAssetPayloadByteLength, b as _asyncToGenerator, d as getRejectedUploadMimeTypeError, f as isRejectedUploadMimeType, g as assetToData, h as assetDataToUint8Array, i as _objectWithoutProperties, m as stringifyJsonCanonical, p as PUBLIC_ERROR_CODES, r as _objectSpread2, s as calmLensError, t as toTextAssetOptions, y as validateAssetPayload } from "./CalmLensUtils-Dixed1Ox.js";
1
+ import { _ as getAssetPayloadByteLength, b as _asyncToGenerator, d as getRejectedUploadMimeTypeError, f as isRejectedUploadMimeType, g as assetToData, h as assetDataToUint8Array, i as _objectWithoutProperties, m as stringifyJsonCanonical, p as PUBLIC_ERROR_CODES, r as _objectSpread2, s as calmLensError, t as toTextAssetOptions, y as validateAssetPayload } from "./CalmLensUtils--N_91gHC.js";
2
2
  import * as zod from "zod";
3
- //#region ../shared/src/Roles.ts
4
- const ROLE_SCHEMA = zod.enum([
5
- "system:super-admin",
6
- "system:banned",
7
- "system:assets:view-all",
8
- "assets:view-private",
3
+ //#region ../shared/src/Permissions.ts
4
+ const ACTION_SCHEMA = zod.enum([
5
+ "assets:read",
6
+ "assets:download",
7
+ "assets:create",
8
+ "assets:update",
9
+ "assets:delete",
10
+ "assets:share",
11
+ "folders:read",
12
+ "folders:create",
13
+ "folders:update",
14
+ "folders:delete",
15
+ "project:manage"
16
+ ]);
17
+ const GRANT_SCHEMA = zod.object({
18
+ actions: zod.array(ACTION_SCHEMA),
19
+ folderId: zod.string().uuid().nullable(),
20
+ includeSubfolders: zod.boolean()
21
+ }).strict().refine((g) => g.folderId === null || !g.actions.includes("project:manage"), "Project management requires project scope");
22
+ const GRANTS_SCHEMA = zod.array(GRANT_SCHEMA).max(100);
23
+ zod.enum([
24
+ "admin",
25
+ "member",
26
+ "viewer",
27
+ "custom"
28
+ ]);
29
+ const permits = (grants, action, folderId, folders) => grants.some((grant) => {
30
+ if (!grant.actions.includes(action)) return false;
31
+ if (grant.folderId === null) return true;
32
+ let current = folderId;
33
+ const visited = /* @__PURE__ */ new Set();
34
+ while (current && !visited.has(current)) {
35
+ var _folders$find$parentI, _folders$find;
36
+ if (current === grant.folderId) return true;
37
+ if (!grant.includeSubfolders) return false;
38
+ visited.add(current);
39
+ current = (_folders$find$parentI = (_folders$find = folders.find((folder) => folder.id === current)) === null || _folders$find === void 0 ? void 0 : _folders$find.parentId) !== null && _folders$find$parentI !== void 0 ? _folders$find$parentI : null;
40
+ }
41
+ return false;
42
+ });
43
+ const SYSTEM_PERMISSION_SCHEMA = zod.enum([
44
+ "system:admin",
45
+ "system:assets:read-all",
9
46
  "system:uploads:unlimited"
10
47
  ]);
48
+ zod.array(SYSTEM_PERMISSION_SCHEMA);
49
+ zod.enum(["active", "banned"]);
11
50
  //#endregion
12
51
  //#region src/testing/MockApiKey.ts
13
52
  const MOCK_API_KEY_PREFIX = "sk_test_";
@@ -16,16 +55,11 @@ const fromBase64Url = (value) => {
16
55
  const padded = value.padEnd(value.length + (4 - value.length % 4) % 4, "=");
17
56
  return atob(padded.replaceAll("-", "+").replaceAll("_", "/"));
18
57
  };
19
- const isRole = (value) => typeof value === "string" && ROLE_SCHEMA.options.includes(value);
20
- const createMockApiKey = (roles = []) => {
21
- const encodedRoles = toBase64Url(JSON.stringify([...new Set(roles)].sort()));
22
- return `${MOCK_API_KEY_PREFIX}${encodedRoles}`;
23
- };
24
- const getMockApiKeyRoles = (apiKey) => {
58
+ const createMockApiKey = (grants = []) => `${MOCK_API_KEY_PREFIX}${toBase64Url(JSON.stringify(GRANTS_SCHEMA.parse(grants)))}`;
59
+ const getMockApiKeyGrants = (apiKey) => {
25
60
  if (!apiKey.startsWith("sk_test_")) return [];
26
61
  try {
27
- const roles = JSON.parse(fromBase64Url(apiKey.slice(8)));
28
- return Array.isArray(roles) ? roles.filter(isRole) : [];
62
+ return GRANTS_SCHEMA.parse(JSON.parse(fromBase64Url(apiKey.slice(8))));
29
63
  } catch (_unused) {
30
64
  return [];
31
65
  }
@@ -221,7 +255,7 @@ var MockCalmLensClient = class {
221
255
  this.now = (_options$mockOptions$4 = (_options$mockOptions4 = options.mockOptions) === null || _options$mockOptions4 === void 0 ? void 0 : _options$mockOptions4.now) !== null && _options$mockOptions$4 !== void 0 ? _options$mockOptions$4 : Date.now;
222
256
  this.queuedStatus = (_options$mockOptions$5 = (_options$mockOptions5 = options.mockOptions) === null || _options$mockOptions5 === void 0 ? void 0 : _options$mockOptions5.queuedStatus) !== null && _options$mockOptions$5 !== void 0 ? _options$mockOptions$5 : "pending";
223
257
  this.analyzedStatus = (_options$mockOptions$6 = (_options$mockOptions6 = options.mockOptions) === null || _options$mockOptions6 === void 0 ? void 0 : _options$mockOptions6.analyzedStatus) !== null && _options$mockOptions$6 !== void 0 ? _options$mockOptions$6 : "approved";
224
- this.apiKeyRoles = getMockApiKeyRoles(options.apiKey);
258
+ this.apiKeyGrants = getMockApiKeyGrants(options.apiKey);
225
259
  this.signedUrlBaseUrl = (_options$mockOptions$7 = (_options$mockOptions7 = options.mockOptions) === null || _options$mockOptions7 === void 0 ? void 0 : _options$mockOptions7.signedUrlBaseUrl) !== null && _options$mockOptions$7 !== void 0 ? _options$mockOptions$7 : "mock://calmlens";
226
260
  this.cdnBaseUrl = (_options$mockOptions$8 = (_options$mockOptions8 = options.mockOptions) === null || _options$mockOptions8 === void 0 ? void 0 : _options$mockOptions8.cdnBaseUrl) !== null && _options$mockOptions$8 !== void 0 ? _options$mockOptions$8 : "mock://calmlens/v1";
227
261
  }
@@ -238,14 +272,14 @@ var MockCalmLensClient = class {
238
272
  },
239
273
  serve: function() {
240
274
  var _ref3 = _asyncToGenerator(function* (_ref2) {
241
- var _storedContent$type;
275
+ var _asset$folderId, _await$this$databaseS, _this$databaseStorage, _this$databaseStorage2, _storedContent$type;
242
276
  let { assetIdentifier } = _ref2, options = _objectWithoutProperties(_ref2, _excluded2);
243
277
  if (!assetIdentifier) throw new Error("Asset identifier is required");
244
278
  const asset = (yield _this.databaseStorage.listAssets(_this.options.projectId)).find((asset) => asset.id === assetIdentifier || asset.externalId === assetIdentifier);
245
279
  if (!asset) return new Response("Asset not found", { status: 404 });
246
- if (asset.visibility !== "public" && !_this.apiKeyRoles.includes("assets:view-private") && !_this.apiKeyRoles.includes("system:assets:view-all") && !_this.apiKeyRoles.includes("system:super-admin")) return new Response(JSON.stringify({
247
- code: "asset/api-key-missing-role",
248
- message: "This API key needs assets:view-private to read private assets. Use a signed asset URL or enable the permission."
280
+ if (asset.visibility !== "public" && !permits(_this.apiKeyGrants, "assets:download", (_asset$folderId = asset.folderId) !== null && _asset$folderId !== void 0 ? _asset$folderId : null, (_await$this$databaseS = yield (_this$databaseStorage = (_this$databaseStorage2 = _this.databaseStorage).listFolders) === null || _this$databaseStorage === void 0 ? void 0 : _this$databaseStorage.call(_this$databaseStorage2, _this.options.projectId)) !== null && _await$this$databaseS !== void 0 ? _await$this$databaseS : [])) return new Response(JSON.stringify({
281
+ code: "asset/api-key-missing-permission",
282
+ message: "This API key needs assets:download permission for this asset’s folder."
249
283
  }), {
250
284
  status: 403,
251
285
  headers: { "Content-Type": "application/json" }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "calmlens",
3
- "version": "0.2.1",
3
+ "version": "0.3.1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/Censkh/CalmLens.git",