@inferencebureau/apps-sdk 0.16.3 → 0.16.4

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.d.ts CHANGED
@@ -1601,15 +1601,19 @@ export interface ActionOptions {
1601
1601
  */
1602
1602
  timeoutMS?: number;
1603
1603
  }
1604
+ /** `params` is unknown here: a file's literal without `ActionParams` widens its types to string. */
1604
1605
  export interface ActionModule {
1605
1606
  run?: ActionRun;
1606
1607
  onSuccess?: ActionOnSuccess;
1607
1608
  options?: ActionOptions;
1609
+ params?: unknown;
1608
1610
  }
1609
1611
  /**
1610
1612
  * `export const params` in an action file: what callers pass it, for the app's generated client types
1611
- * (`web/ib-client.d.ts`) to check calls by. Keep it a plain object literal: Inference Bureau reads it from the file.
1612
- * It isn't checked at run time. A create or update action takes the model's fields as well.
1613
+ * (`web/ib-client.d.ts`) to check calls by, and for every run to be checked against before the action runs: a call
1614
+ * whose params don't match is refused with 400 invalid_request, its `problems` naming each param (`note`,
1615
+ * `address.zip`, `lines[2].qty`). Keep it a plain object literal: Inference Bureau reads it from the file. Keys it
1616
+ * doesn't name pass; a create or update action takes the model's fields as well.
1613
1617
  *
1614
1618
  * export const params: ActionParams = { note: { type: "string" }, notify: { type: "boolean", required: true } };
1615
1619
  */
@@ -1633,7 +1637,7 @@ export interface ParamDef {
1633
1637
  * export const params = { note: { type: "string" }, notify: { type: "boolean", required: true } } as const satisfies ActionParams;
1634
1638
  * export const run: ActionRun<DeclaredParams<typeof params>> = async ({ params }) => { params.note; // string | undefined
1635
1639
  *
1636
- * Nothing checks them at run time: a caller can still send anything.
1640
+ * A call whose params don't match is refused before `run` (see ActionParams).
1637
1641
  */
1638
1642
  export type DeclaredParams<D> = Flat<{
1639
1643
  -readonly [K in keyof D as D[K] extends {
package/dist/test.js CHANGED
@@ -28,6 +28,38 @@ const MAX_PAGE = 250;
28
28
  const MAX_SKIP = 10_000;
29
29
  const MAX_RECORD_BYTES = 256 * 1024;
30
30
  const invalid = (problems) => new ApiError(problems.map((p) => `${p.field}: ${p.message}`).join("; "), 400, "invalid_request", problems);
31
+ const PARAM_WORD = { string: "text", dateTime: "text", enum: "text", number: "a number", boolean: "true or false", object: "an object", array: "a list" };
32
+ /** What's wrong with a run's params against the action's `export const params`, as the platform checks them (#206). */
33
+ function paramProblems(def, v, at) {
34
+ const field = at || "params";
35
+ const ok = def.type === "string" || def.type === "dateTime" || def.type === "enum" ? typeof v === "string"
36
+ : def.type === "number" ? typeof v === "number" && Number.isFinite(v)
37
+ : def.type === "boolean" ? typeof v === "boolean"
38
+ : def.type === "object" ? isObj(v)
39
+ : def.type === "array" ? Array.isArray(v)
40
+ : true;
41
+ if (!ok)
42
+ return [{ field, message: `must be ${PARAM_WORD[def.type]}` }];
43
+ if (def.type === "enum" && Array.isArray(def.options) && !def.options.includes(v))
44
+ return [{ field, message: `must be one of ${def.options.join(", ")}` }];
45
+ if (def.type === "dateTime" && Number.isNaN(Date.parse(v)))
46
+ return [{ field, message: "must be a date and time (ISO 8601)" }];
47
+ const out = [];
48
+ if (def.type === "object" && def.properties && isObj(v)) {
49
+ for (const [k, d] of Object.entries(def.properties)) {
50
+ const path = at ? `${at}.${k}` : k;
51
+ if (v[k] === undefined) {
52
+ if (d.required === true)
53
+ out.push({ field: path, message: "is required" });
54
+ }
55
+ else
56
+ out.push(...paramProblems(d, v[k], path));
57
+ }
58
+ }
59
+ if (def.type === "array" && def.items && Array.isArray(v))
60
+ v.forEach((x, i) => out.push(...paramProblems(def.items, x, `${field}[${i}]`)));
61
+ return out;
62
+ }
31
63
  /** A related filter's or sort's problems, named from the relationship: `product.vendor`. */
32
64
  function under(at, f) {
33
65
  try {
@@ -2372,6 +2404,10 @@ export function createTestApp(o = {}) {
2372
2404
  }
2373
2405
  }
2374
2406
  const params = wire(input.params ?? {});
2407
+ // Checked against the file's `export const params` before anything runs, as the platform does; an obligation's payload is Shopify's.
2408
+ const wrong = isObj(mod.params) && !how.obligation ? paramProblems({ type: "object", properties: mod.params }, params, "") : [];
2409
+ if (wrong.length)
2410
+ throw invalid(wrong);
2375
2411
  const shop = input.shop ?? null;
2376
2412
  const given = mod.options?.timeoutMS;
2377
2413
  const total = Math.min(MAX_TIMEOUT_MS, Math.max(1000, typeof given === "number" ? given : how.background ? JOB_TIMEOUT_MS : DEFAULT_CALL_TIMEOUT_MS));
@@ -2901,7 +2937,7 @@ export function createTestApp(o = {}) {
2901
2937
  if (privacy) {
2902
2938
  handled = true;
2903
2939
  if (registry[`$global/${privacy}`])
2904
- runs.push(await run(null, privacy, { params: payload, shop }));
2940
+ runs.push(await run(null, privacy, { params: payload, shop }, { obligation: true }));
2905
2941
  }
2906
2942
  for (const s of o.settings?.webhooks ?? []) {
2907
2943
  if (s.topic !== topic)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inferencebureau/apps-sdk",
3
- "version": "0.16.3",
3
+ "version": "0.16.4",
4
4
  "description": "The SDK an Inference Bureau app's code imports: action helpers, metadata types, the records API client, React hooks with AutoTable and AutoForm, and a test harness for its actions.",
5
5
  "license": "UNLICENSED",
6
6
  "keywords": [