@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/esm/testing.js CHANGED
@@ -1,6 +1,6 @@
1
- import { c as invalidArgument, d as API_ERROR_DEFINITIONS, o as CalmLensError, s as SDK_ERROR_CODES, u as API_ERROR_CODES } from "./CalmLensVersion-DPnL_Ebv.js";
2
- import { c as hmacSha256Hex, i as WEBHOOK_TIMESTAMP_HEADER, l as stringifyJsonCanonical, n as WEBHOOK_SIGNATURE_HEADER, s as signUploadTokenWithApiKey, t as WEBHOOK_DELIVERY_HEADER } from "./CalmLensWebhooks-CWlD-f_P.js";
3
- import { b as getRejectedUploadMimeTypeError, f as sleep, g as validateAssetPayload, h as getAssetPayloadByteLength, m as assetToData, n as encodePathSegment, o as toTextAssetOptions, p as assetDataToUint8Array, r as pollForDecision, s as transformSelector, t as appendCdnSelector, u as validateServerClientOptions, x as isRejectedUploadMimeType } from "./CalmLensUtils-BQo79TA1.js";
1
+ import { c as invalidArgument, d as API_ERROR_DEFINITIONS, o as CalmLensError, s as SDK_ERROR_CODES, u as API_ERROR_CODES } from "./CalmLensVersion-CchG7NWy.js";
2
+ import { c as hmacSha256Hex, i as WEBHOOK_TIMESTAMP_HEADER, l as stringifyJsonCanonical, n as WEBHOOK_SIGNATURE_HEADER, s as signUploadTokenWithApiKey, t as WEBHOOK_DELIVERY_HEADER } from "./CalmLensWebhooks-B7nQMCHx.js";
3
+ import { b as getRejectedUploadMimeTypeError, f as sleep, g as validateAssetPayload, h as getAssetPayloadByteLength, m as assetToData, n as encodePathSegment, o as toTextAssetOptions, p as assetDataToUint8Array, r as pollForDecision, s as transformSelector, t as appendCdnSelector, u as validateServerClientOptions, x as isRejectedUploadMimeType } from "./CalmLensUtils-BDtpOfho.js";
4
4
  import * as zod from "zod";
5
5
  import "zod/v4";
6
6
  //#region ../shared/src/ModerationCategoryDefinitions.ts
@@ -355,10 +355,10 @@ const HEX_WORDS = [
355
355
  "foxtrot"
356
356
  ];
357
357
  /** Read this aloud, or render it as large, high-contrast text in a video frame. */
358
- function generateMediaTestCode(signal, projectId) {
358
+ function generateMediaTestCode(signal, testInputKey) {
359
359
  if (!MEDIA_TEST_SIGNALS.includes(signal)) throw new Error("Unsupported media test signal");
360
- const suffix = projectId.slice(-8).toLowerCase();
361
- if (!/^[a-f0-9]{8}$/.test(suffix)) throw new Error("Expected a project ID with an eight-character hexadecimal suffix");
360
+ const suffix = testInputKey.toLowerCase();
361
+ if (!/^[a-f0-9]{8}$/.test(suffix)) throw new Error("Expected an eight-character hexadecimal test input key");
362
362
  return `Calm Lens test ${[...suffix].map((character) => HEX_WORDS[Number.parseInt(character, 16)]).join(" ")} ${signal.replace(/[.-]/g, " ")} end test`;
363
363
  }
364
364
  //#endregion
@@ -557,18 +557,43 @@ const inferAssetType = (options) => {
557
557
  return "file";
558
558
  };
559
559
  const createPage = (items, query) => {
560
- const pageIndex = query.pageIndex ?? 0;
561
560
  const pageSize = query.pageSize ?? 10;
562
- if (!Number.isInteger(pageIndex) || pageIndex < 0) throw invalidArgument("pageIndex must be a non-negative integer");
563
- if (!Number.isInteger(pageSize) || pageSize < 1 || pageSize > 100) throw invalidArgument("pageSize must be an integer from 1 to 100");
564
- const start = pageIndex * pageSize;
561
+ if (!Number.isInteger(pageSize) || pageSize < 1 || pageSize > 100) throw invalidArgument("Invalid page size");
562
+ const ordering = query.ordering?.length ? [...query.ordering] : [{
563
+ field: "createdAt",
564
+ direction: "DESC"
565
+ }];
566
+ if (ordering.some(({ field }) => field !== "createdAt" && field !== "id")) throw invalidArgument("Invalid ordering");
567
+ if (!ordering.some(({ field }) => field === "id")) ordering.push({
568
+ field: "id",
569
+ direction: ordering[0].direction
570
+ });
571
+ let remaining = orderItems(items, ordering);
572
+ if (query.cursor) {
573
+ let values;
574
+ try {
575
+ values = JSON.parse(atob(query.cursor));
576
+ 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();
577
+ } catch {
578
+ throw invalidArgument("Invalid cursor");
579
+ }
580
+ remaining = remaining.filter((item) => {
581
+ for (const [i, rule] of ordering.entries()) {
582
+ const result = compareValues(item[rule.field], values[i]);
583
+ if (result) return rule.direction === "DESC" ? result < 0 : result > 0;
584
+ }
585
+ return false;
586
+ });
587
+ }
588
+ const pageItems = remaining.slice(0, pageSize);
589
+ const hasMore = remaining.length > pageSize;
590
+ const last = pageItems.at(-1);
565
591
  return {
566
- items: items.slice(start, start + pageSize),
567
- total: items.length,
568
- pageIndex,
592
+ items: pageItems,
569
593
  pageSize,
570
- ordering: query.ordering ?? [],
571
- hasMore: start + pageSize < items.length
594
+ hasMore,
595
+ ordering,
596
+ nextCursor: hasMore && last ? btoa(JSON.stringify(ordering.map(({ field }) => last[field]))) : null
572
597
  };
573
598
  };
574
599
  const compareValues = (left, right) => {
@@ -906,10 +931,12 @@ var MockCalmLensServerClient = class {
906
931
  }
907
932
  }
908
933
  async *iteratePages(listPage) {
909
- for (let pageIndex = 0;; pageIndex++) {
910
- const page = await listPage(pageIndex);
934
+ let cursor;
935
+ for (;;) {
936
+ const page = await listPage(cursor);
911
937
  yield* page.items;
912
- if (!page.hasMore || page.items.length === 0) return;
938
+ if (!page.nextCursor || !page.hasMore || page.items.length === 0) return;
939
+ cursor = page.nextCursor;
913
940
  }
914
941
  }
915
942
  get text() {
@@ -958,14 +985,11 @@ var MockCalmLensServerClient = class {
958
985
  let assets = await this.databaseStorage.listAssets(this.projectId);
959
986
  if (options.folderId !== void 0) assets = assets.filter((asset) => (asset.folderId ?? null) === options.folderId);
960
987
  assets = filterAssets(assets, options.filter);
961
- return createPage(orderItems(assets, options.ordering), {
962
- ...options,
963
- pageIndex: options.pageIndex ?? 0
964
- });
988
+ return createPage(assets, options);
965
989
  },
966
- iterate: (options = {}) => this.iteratePages((pageIndex) => this.assets.list({
990
+ iterate: (options = {}) => this.iteratePages((cursor) => this.assets.list({
967
991
  ...options,
968
- pageIndex
992
+ cursor
969
993
  })),
970
994
  retrieve: async (options) => {
971
995
  this.begin("assets.retrieve", options);
@@ -976,16 +1000,13 @@ var MockCalmLensServerClient = class {
976
1000
  const { assetId, ...options } = childrenOptions;
977
1001
  encodePathSegment(assetId, "Asset ID");
978
1002
  const assets = await this.databaseStorage.listAssets(this.projectId);
979
- return createPage(orderItems(assets.filter((asset) => asset.parentId === assetId), options.ordering), {
980
- ...options,
981
- pageIndex: options.pageIndex ?? 0
982
- });
1003
+ return createPage(assets.filter((asset) => asset.parentId === assetId), options);
983
1004
  },
984
1005
  iterateChildren: (options) => {
985
1006
  encodePathSegment(options?.assetId, "Asset ID");
986
- return this.iteratePages((pageIndex) => this.assets.listChildren({
1007
+ return this.iteratePages((cursor) => this.assets.listChildren({
987
1008
  ...options,
988
- pageIndex
1009
+ cursor
989
1010
  }));
990
1011
  },
991
1012
  update: async (options) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@calmlens/sdk",
3
- "version": "0.14.0",
3
+ "version": "0.16.0",
4
4
  "description": "Official CalmLens SDK for JavaScript and TypeScript: content moderation, asset storage and CDN delivery.",
5
5
  "license": "MIT",
6
6
  "homepage": "https://calmlens.com/docs/sdk/javascript",
@@ -105,7 +105,18 @@
105
105
  },
106
106
  "default": "./esm/testing.js"
107
107
  },
108
- "./package.json": "./package.json"
108
+ "./package.json": "./package.json",
109
+ "./test-inputs": {
110
+ "import": {
111
+ "types": "./esm/test-inputs.d.ts",
112
+ "default": "./esm/test-inputs.js"
113
+ },
114
+ "require": {
115
+ "types": "./cjs/test-inputs.d.ts",
116
+ "default": "./cjs/test-inputs.js"
117
+ },
118
+ "default": "./esm/test-inputs.js"
119
+ }
109
120
  },
110
121
  "bugs": {
111
122
  "url": "https://github.com/CalmLens/CalmLens/issues"