@tokenoftrust/cli 8.4.0 → 8.5.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tokenoftrust/cli",
3
- "version": "8.4.0",
3
+ "version": "8.5.0",
4
4
  "description": "Token of Trust developer CLI — clone a tenant store, run it locally with save→reload, and submit it for preview. Installs the `tot` command.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Token of Trust",
@@ -23,6 +23,13 @@ import { missingGrantsOf, refusalHint } from "../../admission-hint.mjs";
23
23
  import { validateManifest } from "../../vendor/private-apps-devkit.mjs";
24
24
  import { generateCandidateKeys } from "./candidate-keys.mjs";
25
25
 
26
+ /** @typedef {import("../../vendor/private-apps-v1.8/admin.js").AdminInstallRequest} AdminInstallRequest */
27
+ /** @typedef {import("../../vendor/private-apps-v1.8/admin.js").AdminUpdateRequest} AdminUpdateRequest */
28
+ /** @typedef {import("../../vendor/private-apps-v1.8/admin.js").AdminInstallList} AdminInstallList */
29
+ /** @typedef {import("../../vendor/private-apps-v1.8/admin.js").AdminAppCandidateStage} AdminAppCandidateStage */
30
+ /** @typedef {import("../../vendor/private-apps-v1.8/admin.js").AdminAppCandidateList["candidates"][number]} AdminAppCandidate */
31
+ /** @typedef {{ id: string, version: string, scopes: string[], webhooks?: { endpoint: string }, widgets?: object[], clientAuth?: object }} ValidatedManifest */
32
+
26
33
  export const DEFAULT_APP_ENV = "test";
27
34
  const OPTION_VERBS = new Set(["install", "update", "rotate", "list", "replay", "test-order", "candidate", "suspend", "resume", "uninstall", "validate"]);
28
35
  const TEST_ORDER_TRANSITIONS = new Map([["release", "released"], ["hold", "held"], ["cancel", "canceled"]]);
@@ -253,11 +260,11 @@ export async function runAdmin(argv, ctx, deps = {}) {
253
260
  const webhookEndpoint = args.webhookEndpoint === undefined ? m.webhooks?.endpoint : args.webhookEndpoint;
254
261
  request = {
255
262
  method: "POST", path: "/api/apps/admin/install",
256
- body: {
263
+ body: /** @satisfies {AdminInstallRequest} */ ({
257
264
  appId: m.id, appVersion: m.version, env: appEnv, scopes: m.scopes || [], manifest: m,
258
265
  ...(webhookEndpoint ? { webhookEndpoint } : {}),
259
266
  ...(Array.isArray(m.widgets) && m.widgets.length ? { widgetPlacements: m.widgets } : {}),
260
- },
267
+ }),
261
268
  };
262
269
  } else if (verb === "update") {
263
270
  const installId = args.positional[0];
@@ -267,13 +274,13 @@ export async function runAdmin(argv, ctx, deps = {}) {
267
274
  const m = loaded.manifest;
268
275
  request = {
269
276
  method: "POST", path: "/api/apps/admin/update",
270
- body: {
277
+ body: /** @satisfies {AdminUpdateRequest} */ ({
271
278
  installId, appVersion: m.version, scopes: m.scopes || [], manifest: m,
272
279
  ...(args.webhookEndpoint === undefined
273
280
  ? (m.webhooks?.endpoint ? { webhookEndpoint: m.webhooks.endpoint } : {})
274
281
  : { webhookEndpoint: args.webhookEndpoint }),
275
282
  ...(Array.isArray(m.widgets) ? { widgetPlacements: m.widgets } : {}),
276
- },
283
+ }),
277
284
  };
278
285
  } else if (verb === "rotate") {
279
286
  const installId = args.positional[0];
@@ -348,14 +355,14 @@ export async function runAdmin(argv, ctx, deps = {}) {
348
355
  const webhookEndpoint = args.webhookEndpoint === undefined ? m.webhooks?.endpoint : args.webhookEndpoint;
349
356
  request = {
350
357
  method: "POST", path: "/api/apps/admin/candidates",
351
- body: {
358
+ body: /** @satisfies {AdminAppCandidateStage} */ ({
352
359
  appId: m.id, appVersion: m.version, scopes: m.scopes || [], manifest: m,
353
360
  ...(webhookEndpoint ? { webhookEndpoint } : {}),
354
361
  ...(Array.isArray(m.widgets) && m.widgets.length ? { widgetPlacements: m.widgets } : {}),
355
362
  ...(args.jwksUri ? { jwksUri: args.jwksUri } : {}),
356
363
  ...(jwks ? { jwks } : {}),
357
364
  ...(args.requestConsent ? { requestConsent: true } : {}),
358
- },
365
+ }),
359
366
  };
360
367
  } else {
361
368
  const deliveryId = args.positional[0];
@@ -491,6 +498,7 @@ function runValidateManifest(args, ctx, out, err) {
491
498
  return 1;
492
499
  }
493
500
 
501
+ /** @param {{ candidates?: Array<Partial<AdminAppCandidate> & { id?: string }> } | null} data */
494
502
  function renderCandidates(data, json, out) {
495
503
  const rows = (Array.isArray(data?.candidates) ? data.candidates : []).map((c) => ({
496
504
  candidateId: c?.candidateId ?? c?.id, appId: c?.appId, appVersion: c?.appVersion, status: c?.status,
@@ -517,7 +525,7 @@ function parseJsonBody(text) {
517
525
 
518
526
  /**
519
527
  * @param {string|null} appDir @param {any} ctx
520
- * @returns {{ ok: true, manifest: any, dir: string } | { ok: false, message: string, next: string }}
528
+ * @returns {{ ok: true, manifest: ValidatedManifest, dir: string } | { ok: false, message: string, next: string }}
521
529
  */
522
530
  function loadManifest(appDir, ctx) {
523
531
  const dir = resolve(appDir || ctx?.cwd || process.cwd());
@@ -576,6 +584,7 @@ function renderTestOrder(data, args, out, err) {
576
584
  return 0;
577
585
  }
578
586
 
587
+ /** @param {Partial<AdminInstallList> | null} data */
579
588
  function renderList(data, json, out) {
580
589
  const installs = Array.isArray(data?.installs) ? data.installs : [];
581
590
  if (json) {
@@ -596,7 +605,7 @@ function renderList(data, json, out) {
596
605
  return 0;
597
606
  }
598
607
 
599
- /** Only these fields ever leave a list response — a secret or hash can never ride along. */
608
+ /** Only these fields ever leave a list response. @param {AdminInstallList["installs"][number]} i */
600
609
  function installRow(i) {
601
610
  return {
602
611
  installId: i?.installId, appId: i?.appId, appVersion: i?.appVersion, env: i?.env, status: i?.status,
@@ -34,6 +34,14 @@ export function shippingDeclarationProblems(shipping: unknown): string[];
34
34
  export const COMMERCE_KEYS: readonly string[];
35
35
  export const ORDER_NUMBERING_KEYS: readonly string[];
36
36
  export function commerceDeclarationProblems(commerce: unknown): string[];
37
+ export const DEFAULT_SALES_CHANNEL_ID: "default";
38
+ export const SALES_CHANNEL_ID_PATTERN: RegExp;
39
+ export const SALES_CHANNEL_COMPLIANCE_MODES: readonly string[];
40
+ export const SALES_CHANNEL_KEYS: readonly string[];
41
+ export const SALES_CHANNEL_CATALOG_FILTER_KEYS: readonly string[];
42
+ export function isValidSalesChannelId(value: unknown): value is string;
43
+ export function isValidChannelDomain(value: unknown): value is string;
44
+ export function channelsDeclarationProblems(channels: unknown): string[];
37
45
  export const COMPLIANCE_NOTICES: Record<string, string>;
38
46
  export const MANDATED_NOTICE_KEYS: string[];
39
47
  export function nearestKey(key: string, known: readonly string[]): string | null;
@@ -19,7 +19,7 @@
19
19
 
20
20
  export const TOT_CONFIG_KEYS = [
21
21
  "$schema", "tenant", "scope", "mappings", "hostPlatform", "sample", "seed",
22
- "displayName", "siteType", "compliance", "capabilities", "features", "embeds", "seo", "currency", "timezone", "shipping", "catalog", "commerce", "accessPolicy", "verification",
22
+ "displayName", "siteType", "compliance", "capabilities", "features", "embeds", "seo", "currency", "timezone", "shipping", "catalog", "commerce", "accessPolicy", "verification", "channels",
23
23
  ];
24
24
  // Keys other tooling has written that the platform does NOT read, with the key it does.
25
25
  const RENAMED_CONFIG_KEYS = { appDomain: "scope", tenantId: "tenant" };
@@ -67,6 +67,194 @@ export function verificationDeclarationProblems(value) {
67
67
  return problems;
68
68
  }
69
69
 
70
+ export const DEFAULT_SALES_CHANNEL_ID = "default";
71
+ export const SALES_CHANNEL_ID_PATTERN = /^[a-z0-9][a-z0-9-]{0,62}$/;
72
+ export const SALES_CHANNEL_COMPLIANCE_MODES = Object.freeze(["b2c", "b2b"]);
73
+ export const SALES_CHANNEL_KEYS = Object.freeze([
74
+ "id",
75
+ "displayName",
76
+ "domains",
77
+ "canonicalDomain",
78
+ "default",
79
+ "salesChannel",
80
+ "accessPolicy",
81
+ "catalogFilter",
82
+ "theme_tokens",
83
+ "seo",
84
+ ]);
85
+ export const SALES_CHANNEL_CATALOG_FILTER_KEYS = Object.freeze([
86
+ "includeCollections",
87
+ "excludeCollections",
88
+ "includeTags",
89
+ "excludeTags",
90
+ "includeProductHandles",
91
+ "excludeProductHandles",
92
+ ]);
93
+
94
+ const CHANNEL_THEME_SECTION_KEYS = Object.freeze(["color", "type", "space", "shape", "brand"]);
95
+ const CHANNEL_DOMAIN_RE = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)+$/;
96
+ const CHANNEL_SECRET_KEY_RE = /(?:secret|token|password|apikey|api_key|private|credential|kms|auth)/i;
97
+ const MAX_SALES_CHANNELS = 50;
98
+ const MAX_CHANNEL_DOMAINS = 50;
99
+ const MAX_CHANNEL_FILTER_ITEMS = 500;
100
+
101
+ /** @param {unknown} value @returns {value is string} */
102
+ export function isValidSalesChannelId(value) {
103
+ return typeof value === "string" && SALES_CHANNEL_ID_PATTERN.test(value);
104
+ }
105
+
106
+ /** @param {unknown} value @returns {value is string} */
107
+ export function isValidChannelDomain(value) {
108
+ return (
109
+ typeof value === "string" &&
110
+ value.length >= 3 &&
111
+ value.length <= 253 &&
112
+ value === value.trim() &&
113
+ value === value.toLowerCase() &&
114
+ CHANNEL_DOMAIN_RE.test(value)
115
+ );
116
+ }
117
+
118
+ /** @param {unknown} value @param {string} at @returns {string[]} */
119
+ function channelThemeTokensProblems(value, at) {
120
+ if (!isObject(value)) return [`\`${at}\` must be an object`];
121
+ const problems = [];
122
+ for (const [section, tokens] of Object.entries(/** @type {Record<string, unknown>} */ (value))) {
123
+ if (!CHANNEL_THEME_SECTION_KEYS.includes(section)) {
124
+ problems.push(`\`${at}.${section}\` is not a theme_tokens section (${CHANNEL_THEME_SECTION_KEYS.join(", ")})`);
125
+ continue;
126
+ }
127
+ if (!isObject(tokens)) {
128
+ problems.push(`\`${at}.${section}\` must be an object`);
129
+ continue;
130
+ }
131
+ for (const [key, val] of Object.entries(/** @type {Record<string, unknown>} */ (tokens))) {
132
+ if (CHANNEL_SECRET_KEY_RE.test(key)) {
133
+ problems.push(`\`${at}.${section}.${key}\` is not a permitted theme token key`);
134
+ } else if (typeof val !== "string" && typeof val !== "number" && typeof val !== "boolean") {
135
+ problems.push(`\`${at}.${section}.${key}\` must be a scalar theme value`);
136
+ }
137
+ }
138
+ }
139
+ return problems;
140
+ }
141
+
142
+ /** @param {unknown} value @param {string} at @returns {string[]} */
143
+ function channelCatalogFilterProblems(value, at) {
144
+ if (!isObject(value)) return [`\`${at}\` must be an object`];
145
+ const problems = [];
146
+ const filter = /** @type {Record<string, unknown>} */ (value);
147
+ for (const key of Object.keys(filter)) {
148
+ if (!SALES_CHANNEL_CATALOG_FILTER_KEYS.includes(key)) {
149
+ problems.push(`\`${at}.${key}\` is not a catalogFilter key (${SALES_CHANNEL_CATALOG_FILTER_KEYS.join(", ")})`);
150
+ continue;
151
+ }
152
+ const list = filter[key];
153
+ if (
154
+ !Array.isArray(list) ||
155
+ list.length > MAX_CHANNEL_FILTER_ITEMS ||
156
+ !list.every((item) => typeof item === "string" && item.trim().length > 0 && item.length <= 120 && !/[\u0000-\u001f\u007f]/.test(item))
157
+ ) {
158
+ problems.push(`\`${at}.${key}\` must be an array of at most ${MAX_CHANNEL_FILTER_ITEMS} non-empty strings`);
159
+ }
160
+ }
161
+ return problems;
162
+ }
163
+
164
+ /** @param {unknown} value @returns {string[]} */
165
+ export function channelsDeclarationProblems(value) {
166
+ if (!Array.isArray(value) || value.length === 0 || value.length > MAX_SALES_CHANNELS) {
167
+ return [`\`channels\` must be a non-empty array of at most ${MAX_SALES_CHANNELS} channel declarations`];
168
+ }
169
+ const problems = [];
170
+ const seenIds = new Set();
171
+ const seenDomains = new Set();
172
+ let defaultCount = 0;
173
+
174
+ value.forEach((raw, index) => {
175
+ const at = `channels[${index}]`;
176
+ if (!isObject(raw)) {
177
+ problems.push(`\`${at}\` must be an object`);
178
+ return;
179
+ }
180
+ const ch = /** @type {Record<string, unknown>} */ (raw);
181
+ for (const key of Object.keys(ch)) {
182
+ if (!SALES_CHANNEL_KEYS.includes(key)) {
183
+ problems.push(`\`${at}.${key}\` is not a sales channel key (${SALES_CHANNEL_KEYS.join(", ")})`);
184
+ }
185
+ }
186
+ if (!isValidSalesChannelId(ch.id)) {
187
+ problems.push(`\`${at}.id\` must match ${SALES_CHANNEL_ID_PATTERN}`);
188
+ } else if (seenIds.has(ch.id)) {
189
+ problems.push(`\`${at}.id\` duplicates channel id ${JSON.stringify(ch.id)}`);
190
+ } else {
191
+ seenIds.add(ch.id);
192
+ }
193
+ if (typeof ch.salesChannel !== "string" || !SALES_CHANNEL_COMPLIANCE_MODES.includes(ch.salesChannel)) {
194
+ problems.push(`\`${at}.salesChannel\` must be one of ${SALES_CHANNEL_COMPLIANCE_MODES.join(", ")}`);
195
+ }
196
+ if (
197
+ ch.displayName !== undefined &&
198
+ (typeof ch.displayName !== "string" || ch.displayName.trim() !== ch.displayName || ch.displayName.length === 0 || ch.displayName.length > 120 || /[\u0000-\u001f\u007f]/.test(ch.displayName))
199
+ ) {
200
+ problems.push(`\`${at}.displayName\` must be a non-empty trimmed string (max 120 chars)`);
201
+ }
202
+ if (ch.default !== undefined) {
203
+ if (typeof ch.default !== "boolean") {
204
+ problems.push(`\`${at}.default\` must be true or false`);
205
+ } else if (ch.default) {
206
+ defaultCount += 1;
207
+ if (defaultCount > 1) problems.push(`\`${at}.default\`: only one channel may be marked default`);
208
+ }
209
+ }
210
+ if (ch.domains !== undefined) {
211
+ if (!Array.isArray(ch.domains) || ch.domains.length > MAX_CHANNEL_DOMAINS) {
212
+ problems.push(`\`${at}.domains\` must be an array of at most ${MAX_CHANNEL_DOMAINS} hostnames`);
213
+ } else {
214
+ for (let dIdx = 0; dIdx < ch.domains.length; dIdx += 1) {
215
+ const domain = ch.domains[dIdx];
216
+ if (!isValidChannelDomain(domain)) {
217
+ problems.push(`\`${at}.domains[${dIdx}]\` must be a normalized lowercase hostname with no scheme, port, path, userinfo, or wildcard`);
218
+ } else if (seenDomains.has(domain)) {
219
+ problems.push(`\`${at}.domains[${dIdx}]\` duplicates domain ${JSON.stringify(domain)}`);
220
+ } else {
221
+ seenDomains.add(domain);
222
+ }
223
+ }
224
+ }
225
+ }
226
+ if (ch.canonicalDomain !== undefined) {
227
+ if (!isValidChannelDomain(ch.canonicalDomain)) {
228
+ problems.push(`\`${at}.canonicalDomain\` must be a normalized lowercase hostname`);
229
+ } else if (!(Array.isArray(ch.domains) && ch.domains.includes(ch.canonicalDomain))) {
230
+ if (seenDomains.has(ch.canonicalDomain)) {
231
+ problems.push(`\`${at}.canonicalDomain\` duplicates a domain bound to another channel`);
232
+ } else {
233
+ seenDomains.add(ch.canonicalDomain);
234
+ }
235
+ }
236
+ }
237
+ if (ch.accessPolicy !== undefined) {
238
+ for (const p of accessPolicyDeclarationProblems(ch.accessPolicy)) {
239
+ problems.push(`\`${at}.accessPolicy\`: ${p}`);
240
+ }
241
+ }
242
+ if (ch.catalogFilter !== undefined) {
243
+ problems.push(...channelCatalogFilterProblems(ch.catalogFilter, `${at}.catalogFilter`));
244
+ }
245
+ if (ch.theme_tokens !== undefined) {
246
+ problems.push(...channelThemeTokensProblems(ch.theme_tokens, `${at}.theme_tokens`));
247
+ }
248
+ if (ch.seo !== undefined) {
249
+ for (const p of seoDeclarationProblems(ch.seo)) {
250
+ problems.push(`\`${at}.seo\`: ${p}`);
251
+ }
252
+ }
253
+ });
254
+
255
+ return problems;
256
+ }
257
+
70
258
  /** The currency a store sells in when `.tot/config.json` declares none. */
71
259
  export const DEFAULT_CURRENCY = "USD";
72
260
 
@@ -978,6 +1166,11 @@ export function validateDeclaredConfig(config, file = ".tot/config.json") {
978
1166
  out.push(mk(ERROR, "config-shopper-verification", file, problem));
979
1167
  }
980
1168
  }
1169
+ if (config.channels !== undefined) {
1170
+ for (const problem of channelsDeclarationProblems(config.channels)) {
1171
+ out.push(mk(ERROR, "config-channels", `${file} channels`, problem));
1172
+ }
1173
+ }
981
1174
  if (config.catalog !== undefined) {
982
1175
  const problems = catalogDeclarationProblems(config.catalog);
983
1176
  if (problems.length > 0) {
@@ -0,0 +1,103 @@
1
+ // Storefront Devkit Private Apps OpenAPI 1.8.0, commit 86ebe41a9785b177fb6dedf7f387bd4fc3b20787.
2
+ // apps/private/contract/openapi.yaml, SHA-256 00cd2b2665c4fc3183f48cac1ea2c30a6fa3a1b024f02bf0fc2774f60ec54b3e.
3
+
4
+ export interface AdminInstallRequest {
5
+ appId: string;
6
+ appVersion: string;
7
+ env: string;
8
+ scopes: string[];
9
+ manifest: object;
10
+ webhookEndpoint?: string | null;
11
+ widgetPlacements?: object[];
12
+ }
13
+
14
+ export interface AdminUpdateRequest {
15
+ installId: string;
16
+ appVersion?: string;
17
+ scopes?: string[];
18
+ manifest?: object;
19
+ webhookEndpoint?: string | null;
20
+ widgetPlacements?: object[];
21
+ }
22
+
23
+ export interface AdminInstallList {
24
+ installs: AdminInstall[];
25
+ }
26
+
27
+ export interface AdminInstall {
28
+ installId: string;
29
+ appId: string;
30
+ appVersion: string;
31
+ tenantId: string;
32
+ env: string;
33
+ status: "active" | "suspended" | "uninstalled";
34
+ scopes: string[];
35
+ manifest?: object;
36
+ webhookEndpoint?: string | null;
37
+ widgetPlacements?: object[];
38
+ installActor?: string;
39
+ createdAt: string;
40
+ updatedAt: string;
41
+ }
42
+
43
+ export interface AdminErrorBody {
44
+ error: string;
45
+ code?: string;
46
+ denials?: string[];
47
+ reasons?: ("install-inactive" | "webhook-endpoint-missing" | "processor-scope-missing")[];
48
+ }
49
+
50
+ export interface AdminAppCandidateList {
51
+ candidates: AdminAppCandidate[];
52
+ }
53
+
54
+ export interface AdminAppCandidateStage {
55
+ appId: string;
56
+ appVersion: string;
57
+ scopes: string[];
58
+ manifest: object;
59
+ webhookEndpoint?: string | null;
60
+ widgetPlacements?: object[];
61
+ jwks?: { keys: {
62
+ kty: "RSA" | "EC";
63
+ kid: string;
64
+ alg?: "RS256" | "ES256";
65
+ use?: "sig";
66
+ [parameter: string]: unknown;
67
+ }[] };
68
+ jwksUri?: string;
69
+ requestConsent?: boolean;
70
+ }
71
+
72
+ export interface AdminAppCandidate {
73
+ candidateId: string;
74
+ appId: string;
75
+ appVersion: string;
76
+ env: "production";
77
+ scopes: string[];
78
+ clientId: string;
79
+ keys: { kid: string; alg: "RS256" | "ES256" }[];
80
+ jwksHost: string | null;
81
+ requestConsent: boolean;
82
+ snapshotDigest: string;
83
+ status: "pending_approval" | "approved" | "rejected";
84
+ stagedAt: string;
85
+ decidedAt: string | null;
86
+ installId: string | null;
87
+ }
88
+
89
+ export interface AdminAppCandidateDecision {
90
+ candidateId: string;
91
+ snapshotDigest: string;
92
+ decision: "approve" | "reject";
93
+ grantConsent?: boolean;
94
+ }
95
+
96
+ export interface AdminAppCandidateDecisionResult {
97
+ candidateId: string;
98
+ status: "approved" | "rejected";
99
+ installId: string | null;
100
+ appId: string;
101
+ appVersion: string;
102
+ consentGranted: boolean;
103
+ }