@calmlens/sdk 0.14.0 → 0.16.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.
Files changed (38) hide show
  1. package/README.md +30 -10
  2. package/cjs/{CalmLensConnection-Bth5fJyp.js → CalmLensConnection-CPdx_acA.js} +2 -2
  3. package/cjs/{CalmLensUtils-DNdmYeX6.js → CalmLensUtils-DvhY8-cU.js} +1 -1
  4. package/cjs/{CalmLensVersion-Bs0DreBV.js → CalmLensVersion-CrlGk2gX.js} +6 -1
  5. package/cjs/{CalmLensWebhooks-6thkvr_F.js → CalmLensWebhooks-C2VrleZm.js} +4 -4
  6. package/cjs/Classification-DJU_DfOO.d.ts +4565 -0
  7. package/cjs/TestTriggers-CxFZ-3uz.d.ts +43 -0
  8. package/cjs/{WebhookPayload-vy8HNMaK.d.ts → WebhookPayload-BfUmVu2y.d.ts} +77 -4622
  9. package/cjs/app.d.ts +4 -3
  10. package/cjs/app.js +3 -3
  11. package/cjs/{index-Dz340UQ9.d.ts → index-VxklV9OQ.d.ts} +4 -1
  12. package/cjs/index.d.ts +4 -3
  13. package/cjs/index.js +1 -1
  14. package/cjs/server.d.ts +8 -7
  15. package/cjs/server.js +17 -21
  16. package/cjs/test-inputs.d.ts +2 -0
  17. package/cjs/test-inputs.js +265 -0
  18. package/cjs/testing.d.ts +3 -5
  19. package/cjs/testing.js +51 -30
  20. package/esm/{CalmLensConnection-DrCOL9h9.js → CalmLensConnection-DYo5U2L0.js} +2 -2
  21. package/esm/{CalmLensUtils-BQo79TA1.js → CalmLensUtils-BDtpOfho.js} +1 -1
  22. package/esm/{CalmLensVersion-DPnL_Ebv.js → CalmLensVersion-CchG7NWy.js} +6 -1
  23. package/esm/{CalmLensWebhooks-CWlD-f_P.js → CalmLensWebhooks-B7nQMCHx.js} +4 -4
  24. package/esm/Classification-DJU_DfOO.d.ts +4565 -0
  25. package/esm/TestTriggers-CxFZ-3uz.d.ts +43 -0
  26. package/esm/{WebhookPayload-vy8HNMaK.d.ts → WebhookPayload-BfUmVu2y.d.ts} +77 -4622
  27. package/esm/app.d.ts +4 -3
  28. package/esm/app.js +3 -3
  29. package/esm/{index-Dz340UQ9.d.ts → index-VxklV9OQ.d.ts} +4 -1
  30. package/esm/index.d.ts +4 -3
  31. package/esm/index.js +1 -1
  32. package/esm/server.d.ts +8 -7
  33. package/esm/server.js +17 -21
  34. package/esm/test-inputs.d.ts +2 -0
  35. package/esm/test-inputs.js +256 -0
  36. package/esm/testing.d.ts +3 -5
  37. package/esm/testing.js +51 -30
  38. package/package.json +13 -2
package/cjs/testing.js CHANGED
@@ -21,9 +21,9 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
21
21
  enumerable: true
22
22
  }) : target, mod));
23
23
  //#endregion
24
- const require_CalmLensVersion = require("./CalmLensVersion-Bs0DreBV.js");
25
- const require_CalmLensWebhooks = require("./CalmLensWebhooks-6thkvr_F.js");
26
- const require_CalmLensUtils = require("./CalmLensUtils-DNdmYeX6.js");
24
+ const require_CalmLensVersion = require("./CalmLensVersion-CrlGk2gX.js");
25
+ const require_CalmLensWebhooks = require("./CalmLensWebhooks-C2VrleZm.js");
26
+ const require_CalmLensUtils = require("./CalmLensUtils-DvhY8-cU.js");
27
27
  let zod = require("zod");
28
28
  zod = __toESM(zod);
29
29
  require("zod/v4");
@@ -379,10 +379,10 @@ const HEX_WORDS = [
379
379
  "foxtrot"
380
380
  ];
381
381
  /** Read this aloud, or render it as large, high-contrast text in a video frame. */
382
- function generateMediaTestCode(signal, projectId) {
382
+ function generateMediaTestCode(signal, testInputKey) {
383
383
  if (!MEDIA_TEST_SIGNALS.includes(signal)) throw new Error("Unsupported media test signal");
384
- const suffix = projectId.slice(-8).toLowerCase();
385
- if (!/^[a-f0-9]{8}$/.test(suffix)) throw new Error("Expected a project ID with an eight-character hexadecimal suffix");
384
+ const suffix = testInputKey.toLowerCase();
385
+ if (!/^[a-f0-9]{8}$/.test(suffix)) throw new Error("Expected an eight-character hexadecimal test input key");
386
386
  return `Calm Lens test ${[...suffix].map((character) => HEX_WORDS[Number.parseInt(character, 16)]).join(" ")} ${signal.replace(/[.-]/g, " ")} end test`;
387
387
  }
388
388
  //#endregion
@@ -581,18 +581,43 @@ const inferAssetType = (options) => {
581
581
  return "file";
582
582
  };
583
583
  const createPage = (items, query) => {
584
- const pageIndex = query.pageIndex ?? 0;
585
584
  const pageSize = query.pageSize ?? 10;
586
- if (!Number.isInteger(pageIndex) || pageIndex < 0) throw require_CalmLensVersion.invalidArgument("pageIndex must be a non-negative integer");
587
- if (!Number.isInteger(pageSize) || pageSize < 1 || pageSize > 100) throw require_CalmLensVersion.invalidArgument("pageSize must be an integer from 1 to 100");
588
- const start = pageIndex * pageSize;
585
+ if (!Number.isInteger(pageSize) || pageSize < 1 || pageSize > 100) throw require_CalmLensVersion.invalidArgument("Invalid page size");
586
+ const ordering = query.ordering?.length ? [...query.ordering] : [{
587
+ field: "createdAt",
588
+ direction: "DESC"
589
+ }];
590
+ if (ordering.some(({ field }) => field !== "createdAt" && field !== "id")) throw require_CalmLensVersion.invalidArgument("Invalid ordering");
591
+ if (!ordering.some(({ field }) => field === "id")) ordering.push({
592
+ field: "id",
593
+ direction: ordering[0].direction
594
+ });
595
+ let remaining = orderItems(items, ordering);
596
+ if (query.cursor) {
597
+ let values;
598
+ try {
599
+ values = JSON.parse(atob(query.cursor));
600
+ if (!Array.isArray(values) || values.length !== ordering.length || values.some((value, i) => ordering[i].field === "id" ? typeof value !== "string" : typeof value !== "number" || !Number.isFinite(value))) throw new Error();
601
+ } catch {
602
+ throw require_CalmLensVersion.invalidArgument("Invalid cursor");
603
+ }
604
+ remaining = remaining.filter((item) => {
605
+ for (const [i, rule] of ordering.entries()) {
606
+ const result = compareValues(item[rule.field], values[i]);
607
+ if (result) return rule.direction === "DESC" ? result < 0 : result > 0;
608
+ }
609
+ return false;
610
+ });
611
+ }
612
+ const pageItems = remaining.slice(0, pageSize);
613
+ const hasMore = remaining.length > pageSize;
614
+ const last = pageItems.at(-1);
589
615
  return {
590
- items: items.slice(start, start + pageSize),
591
- total: items.length,
592
- pageIndex,
616
+ items: pageItems,
593
617
  pageSize,
594
- ordering: query.ordering ?? [],
595
- hasMore: start + pageSize < items.length
618
+ hasMore,
619
+ ordering,
620
+ nextCursor: hasMore && last ? btoa(JSON.stringify(ordering.map(({ field }) => last[field]))) : null
596
621
  };
597
622
  };
598
623
  const compareValues = (left, right) => {
@@ -930,10 +955,12 @@ var MockCalmLensServerClient = class {
930
955
  }
931
956
  }
932
957
  async *iteratePages(listPage) {
933
- for (let pageIndex = 0;; pageIndex++) {
934
- const page = await listPage(pageIndex);
958
+ let cursor;
959
+ for (;;) {
960
+ const page = await listPage(cursor);
935
961
  yield* page.items;
936
- if (!page.hasMore || page.items.length === 0) return;
962
+ if (!page.nextCursor || !page.hasMore || page.items.length === 0) return;
963
+ cursor = page.nextCursor;
937
964
  }
938
965
  }
939
966
  get text() {
@@ -982,14 +1009,11 @@ var MockCalmLensServerClient = class {
982
1009
  let assets = await this.databaseStorage.listAssets(this.projectId);
983
1010
  if (options.folderId !== void 0) assets = assets.filter((asset) => (asset.folderId ?? null) === options.folderId);
984
1011
  assets = filterAssets(assets, options.filter);
985
- return createPage(orderItems(assets, options.ordering), {
986
- ...options,
987
- pageIndex: options.pageIndex ?? 0
988
- });
1012
+ return createPage(assets, options);
989
1013
  },
990
- iterate: (options = {}) => this.iteratePages((pageIndex) => this.assets.list({
1014
+ iterate: (options = {}) => this.iteratePages((cursor) => this.assets.list({
991
1015
  ...options,
992
- pageIndex
1016
+ cursor
993
1017
  })),
994
1018
  retrieve: async (options) => {
995
1019
  this.begin("assets.retrieve", options);
@@ -1000,16 +1024,13 @@ var MockCalmLensServerClient = class {
1000
1024
  const { assetId, ...options } = childrenOptions;
1001
1025
  require_CalmLensUtils.encodePathSegment(assetId, "Asset ID");
1002
1026
  const assets = await this.databaseStorage.listAssets(this.projectId);
1003
- return createPage(orderItems(assets.filter((asset) => asset.parentId === assetId), options.ordering), {
1004
- ...options,
1005
- pageIndex: options.pageIndex ?? 0
1006
- });
1027
+ return createPage(assets.filter((asset) => asset.parentId === assetId), options);
1007
1028
  },
1008
1029
  iterateChildren: (options) => {
1009
1030
  require_CalmLensUtils.encodePathSegment(options?.assetId, "Asset ID");
1010
- return this.iteratePages((pageIndex) => this.assets.listChildren({
1031
+ return this.iteratePages((cursor) => this.assets.listChildren({
1011
1032
  ...options,
1012
- pageIndex
1033
+ cursor
1013
1034
  }));
1014
1035
  },
1015
1036
  update: async (options) => {
@@ -1,5 +1,5 @@
1
- import { c as invalidArgument, o as CalmLensError, s as SDK_ERROR_CODES, u as API_ERROR_CODES } from "./CalmLensVersion-DPnL_Ebv.js";
2
- import { a as stringifyQuery, b as getRejectedUploadMimeTypeError, d as CalmLensTransport, g as validateAssetPayload, h as getAssetPayloadByteLength, i as randomUuid, m as assetToData, v as MAX_IMAGE_UPLOAD_BYTES, x as isRejectedUploadMimeType, y as MIN_MULTIPART_IMAGE_UPLOAD_BYTES } from "./CalmLensUtils-BQo79TA1.js";
1
+ import { c as invalidArgument, o as CalmLensError, s as SDK_ERROR_CODES, u as API_ERROR_CODES } from "./CalmLensVersion-CchG7NWy.js";
2
+ import { a as stringifyQuery, b as getRejectedUploadMimeTypeError, d as CalmLensTransport, g as validateAssetPayload, h as getAssetPayloadByteLength, i as randomUuid, m as assetToData, v as MAX_IMAGE_UPLOAD_BYTES, x as isRejectedUploadMimeType, y as MIN_MULTIPART_IMAGE_UPLOAD_BYTES } from "./CalmLensUtils-BDtpOfho.js";
3
3
  //#region src/client/CalmLensConnection.ts
4
4
  const DEFAULT_TIMEOUT_MS = 12e4;
5
5
  const DEFAULT_MAX_RETRIES = 2;
@@ -1,4 +1,4 @@
1
- import { c as invalidArgument, l as invalidConfig, n as getUserAgent, o as CalmLensError, r as isBrowserRuntime, s as SDK_ERROR_CODES, u as API_ERROR_CODES } from "./CalmLensVersion-DPnL_Ebv.js";
1
+ import { c as invalidArgument, l as invalidConfig, n as getUserAgent, o as CalmLensError, r as isBrowserRuntime, s as SDK_ERROR_CODES, u as API_ERROR_CODES } from "./CalmLensVersion-CchG7NWy.js";
2
2
  const MAX_IMAGE_UPLOAD_BYTES = /* @__PURE__ */ (() => 20971520)();
3
3
  const MIN_MULTIPART_IMAGE_UPLOAD_BYTES = /* @__PURE__ */ (() => 10485760)();
4
4
  const REJECTED_UPLOAD_MIME_TYPE_SET = /* @__PURE__ */ new Set([
@@ -1,6 +1,7 @@
1
1
  //#region src/client/CalmLensErrorCodes.ts
2
2
  /** Error codes the CalmLens API documents, by name. */
3
3
  const API_ERROR_CODES = {
4
+ INVALID_CURSOR: "validation/invalid-cursor",
4
5
  MISSING_TOKEN: "auth/missing-token",
5
6
  INVALID_API_KEY: "auth/invalid-api-key",
6
7
  API_KEY_PROJECT_MISMATCH: "auth/project-mismatch",
@@ -63,6 +64,10 @@ const API_ERROR_CODES = {
63
64
  };
64
65
  /** The HTTP status and default message the API returns with each documented error code. */
65
66
  const API_ERROR_DEFINITIONS = {
67
+ "validation/invalid-cursor": {
68
+ status: 400,
69
+ message: "The cursor is invalid or belongs to a different query."
70
+ },
66
71
  "auth/missing-token": {
67
72
  status: 401,
68
73
  message: "Authentication required. Send your project API key in the X-Api-Key header."
@@ -390,7 +395,7 @@ const invalidConfig = (message) => new CalmLensError(SDK_ERROR_CODES.INVALID_CON
390
395
  //#endregion
391
396
  //#region src/client/CalmLensVersion.ts
392
397
  /** This SDK's version, from its package.json. */
393
- const SDK_VERSION = "0.14.0";
398
+ const SDK_VERSION = "0.16.0";
394
399
  const runtimeName = () => {
395
400
  const runtime = globalThis;
396
401
  if (isReactNativeRuntime()) return "react-native";
@@ -1,5 +1,5 @@
1
- import { c as invalidArgument, o as CalmLensError, s as SDK_ERROR_CODES } from "./CalmLensVersion-DPnL_Ebv.js";
2
- import { n as encodePathSegment } from "./CalmLensUtils-BQo79TA1.js";
1
+ import { c as invalidArgument, o as CalmLensError, s as SDK_ERROR_CODES } from "./CalmLensVersion-CchG7NWy.js";
2
+ import { n as encodePathSegment } from "./CalmLensUtils-BDtpOfho.js";
3
3
  //#region ../shared/src/JsonUtils.ts
4
4
  const stringifyJsonCanonical = (value) => JSON.stringify(value, (_key, nestedValue) => {
5
5
  if (!nestedValue || typeof nestedValue !== "object" || Array.isArray(nestedValue)) return nestedValue;
@@ -124,7 +124,7 @@ async function verifyWebhookSignature(options) {
124
124
  /**
125
125
  * Verifies a webhook delivery and parses its body. Throws `sdk/invalid-webhook-signature`
126
126
  * when the signature or timestamp does not verify, and `sdk/invalid-webhook-payload` when a
127
- * verified body is not a webhook event. Narrow the result on `type` for typed `data.facts`.
127
+ * verified body is not a webhook event. Unknown event types, fact values and fields remain available.
128
128
  *
129
129
  * Event types and fact keys added to CalmLens after this SDK version are returned as they are,
130
130
  * so keep a default branch when you switch on `type`.
@@ -141,7 +141,7 @@ async function parseWebhookEvent(options) {
141
141
  });
142
142
  }
143
143
  const candidate = event;
144
- if (!candidate || typeof candidate !== "object" || typeof candidate.id !== "string" || typeof candidate.type !== "string" || typeof candidate.projectId !== "string" || !candidate.data || typeof candidate.data !== "object") throw new CalmLensError(SDK_ERROR_CODES.INVALID_WEBHOOK_PAYLOAD, { message: "Webhook body is not a CalmLens event" });
144
+ if (!candidate || typeof candidate !== "object" || typeof candidate.id !== "string" || typeof candidate.type !== "string" || typeof candidate.projectId !== "string" || !candidate.data || typeof candidate.data !== "object" || Array.isArray(candidate.data)) throw new CalmLensError(SDK_ERROR_CODES.INVALID_WEBHOOK_PAYLOAD, { message: "Webhook body is not a CalmLens event" });
145
145
  return event;
146
146
  }
147
147
  //#endregion