@tokenoftrust/cli 7.4.3 → 7.4.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/package.json +1 -1
- package/src/commands/app/admin.mjs +90 -30
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tokenoftrust/cli",
|
|
3
|
-
"version": "7.4.
|
|
3
|
+
"version": "7.4.4",
|
|
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",
|
|
@@ -24,6 +24,7 @@ import { validateManifest } from "../../vendor/private-apps-devkit.mjs";
|
|
|
24
24
|
|
|
25
25
|
export const DEFAULT_APP_ENV = "test";
|
|
26
26
|
const OPTION_VERBS = new Set(["install", "update", "rotate", "list", "replay", "test-order", "candidate", "suspend", "resume", "uninstall", "validate"]);
|
|
27
|
+
const TEST_ORDER_TRANSITIONS = new Map([["release", "released"], ["hold", "held"], ["cancel", "canceled"]]);
|
|
27
28
|
|
|
28
29
|
export const ADMIN_USAGE = `tot app — manage Private App installs on your store
|
|
29
30
|
|
|
@@ -38,12 +39,17 @@ export const ADMIN_USAGE = `tot app — manage Private App installs on your stor
|
|
|
38
39
|
tot app rotate <installId> issue a new client secret (printed ONCE); the old one stops working
|
|
39
40
|
tot app list your store's installs: id, app, version, env, status, scopes (never secrets)
|
|
40
41
|
tot app replay <deliveryId> re-send a webhook delivery to its install
|
|
41
|
-
tot app test-order bind <installId>
|
|
42
|
+
tot app test-order bind <installId> [--location <id>]
|
|
42
43
|
bind your test install as the store's preview processor
|
|
43
44
|
(it must be active, with a webhook endpoint and fulfillment:process)
|
|
44
|
-
tot app test-order create
|
|
45
|
+
tot app test-order create [--location <id>] [--line <sku>:<qty> ... | --quantity <n>] [--region <state>]
|
|
46
|
+
write one synthetic preview order (--quantity sets the amount of synthetic-sku)
|
|
45
47
|
tot app test-order release <orderId>
|
|
46
48
|
claim and release one synthetic preview order
|
|
49
|
+
tot app test-order hold <orderId>
|
|
50
|
+
put a synthetic preview order on hold
|
|
51
|
+
tot app test-order cancel <orderId>
|
|
52
|
+
cancel a synthetic preview order
|
|
47
53
|
(test orders always act on the store's preview stage, at
|
|
48
54
|
${DEFAULT_STOREFRONT_ORIGIN}/<tenant>; no --url needed)
|
|
49
55
|
tot app candidate status this store's candidates: id, app, version, status
|
|
@@ -66,12 +72,17 @@ Options (all online commands):
|
|
|
66
72
|
/** Parse the online `tot app` argv. Pure — unit-tested. @param {string[]} argv */
|
|
67
73
|
export function parseAdminArgs(argv) {
|
|
68
74
|
/** @type {{ verb: string|null, positional: string[], app: string|null, env: string|null, tenant: string|null,
|
|
69
|
-
* url: string|null, webhookEndpoint: string|null|undefined, jwksUri: string|null, jwks: string|null,
|
|
75
|
+
* url: string|null, webhookEndpoint: string|null|undefined, jwksUri: string|null, jwks: string|null,
|
|
76
|
+
* location: string|null, lines: string[], quantity: string|null, region: string|null,
|
|
77
|
+
* requestConsent: boolean, json: boolean, help: boolean, unknown: string[] }} */
|
|
70
78
|
const a = { verb: null, positional: [], app: null, env: null, tenant: null, url: null,
|
|
71
|
-
webhookEndpoint: undefined, jwksUri: null, jwks: null,
|
|
79
|
+
webhookEndpoint: undefined, jwksUri: null, jwks: null, location: null, lines: [], quantity: null, region: null,
|
|
80
|
+
requestConsent: false, json: false, help: false, unknown: [] };
|
|
72
81
|
const valueOf = (i, t) => {
|
|
73
82
|
const eq = t.indexOf("=");
|
|
74
|
-
|
|
83
|
+
if (eq >= 0) return { value: t.slice(eq + 1), next: i };
|
|
84
|
+
const value = argv[i + 1];
|
|
85
|
+
return value === undefined || value.startsWith("--") ? { value: "", next: i } : { value, next: i + 1 };
|
|
75
86
|
};
|
|
76
87
|
for (let i = 0; i < argv.length; i++) {
|
|
77
88
|
const t = argv[i];
|
|
@@ -80,7 +91,8 @@ export function parseAdminArgs(argv) {
|
|
|
80
91
|
else if (t === "--request-consent") a.requestConsent = true;
|
|
81
92
|
else if (t === "--help" || t === "-h") a.help = true;
|
|
82
93
|
else if (flag === "--app" || flag === "--env" || flag === "--tenant" || flag === "--url" || flag === "--webhook-endpoint"
|
|
83
|
-
|| flag === "--jwks-uri" || flag === "--jwks"
|
|
94
|
+
|| flag === "--jwks-uri" || flag === "--jwks" || flag === "--location" || flag === "--line"
|
|
95
|
+
|| flag === "--quantity" || flag === "--region") {
|
|
84
96
|
const { value, next } = valueOf(i, t);
|
|
85
97
|
i = next;
|
|
86
98
|
const v = value === undefined || value.startsWith("--") ? "" : value;
|
|
@@ -90,6 +102,10 @@ export function parseAdminArgs(argv) {
|
|
|
90
102
|
else if (flag === "--url") a.url = v;
|
|
91
103
|
else if (flag === "--jwks-uri") a.jwksUri = v;
|
|
92
104
|
else if (flag === "--jwks") a.jwks = v;
|
|
105
|
+
else if (flag === "--location") a.location = v;
|
|
106
|
+
else if (flag === "--line") a.lines.push(v);
|
|
107
|
+
else if (flag === "--quantity") a.quantity = v;
|
|
108
|
+
else if (flag === "--region") a.region = v;
|
|
93
109
|
else a.webhookEndpoint = v === "none" ? null : v;
|
|
94
110
|
} else if (t.startsWith("--")) a.unknown.push(t);
|
|
95
111
|
else if (a.verb === null) a.verb = t;
|
|
@@ -112,18 +128,25 @@ function processorReasonsOf(body) {
|
|
|
112
128
|
/** Verbs whose request addresses one install by id, so a bare 404 `not_found` means that install. */
|
|
113
129
|
const INSTALL_VERBS = new Set(["update", "rotate", "suspend", "resume", "uninstall"]);
|
|
114
130
|
|
|
115
|
-
|
|
116
|
-
function plainRefusal(status, body, verb) {
|
|
131
|
+
function refusalCode(status, body, verb, action) {
|
|
117
132
|
const code = typeof body?.code === "string" ? body.code : "";
|
|
118
|
-
|
|
119
|
-
|
|
133
|
+
const installAddressed = INSTALL_VERBS.has(verb) || (verb === "test-order" && action === "bind");
|
|
134
|
+
if (status === 404 && installAddressed && (code === "not_found" || (!code && body?.error === "not_found"))) {
|
|
135
|
+
return "install_not_found";
|
|
120
136
|
}
|
|
137
|
+
if (status === 403 && !code) return "not_entitled";
|
|
138
|
+
return code;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Plain wording for the not-found, processor and entitlement refusals; null when none applies. */
|
|
142
|
+
function plainRefusal(status, body, code) {
|
|
143
|
+
if (status === 404 && code === "install_not_found") return "install_not_found: this store has no install with that id";
|
|
121
144
|
if (status === 404 && code === "order_not_found") return "order_not_found: this store has no synthetic preview order with that id";
|
|
122
145
|
if (code === "install_not_processor") {
|
|
123
146
|
const says = processorReasonsOf(body).map((r) => PROCESSOR_REASONS[r]?.says ?? r);
|
|
124
147
|
return `install_not_processor: that install cannot be the preview processor${says.length ? ` — ${says.join("; ")}` : ""}`;
|
|
125
148
|
}
|
|
126
|
-
if (code === "owner_required_for_production_app" || (status === 403 && missingGrantsOf(body).length)) {
|
|
149
|
+
if (code === "owner_required_for_production_app" || code === "not_entitled" || (status === 403 && missingGrantsOf(body).length)) {
|
|
127
150
|
return "not_entitled: your sign-in is not allowed to do this on this store";
|
|
128
151
|
}
|
|
129
152
|
return null;
|
|
@@ -131,18 +154,18 @@ function plainRefusal(status, body, verb) {
|
|
|
131
154
|
|
|
132
155
|
/**
|
|
133
156
|
* The next step for a refused call: the server's `code` first, then the generic admission hint.
|
|
134
|
-
* @param {number} status @param {any} body @param {
|
|
157
|
+
* @param {number} status @param {any} body @param {string} code
|
|
158
|
+
* @param {{ tenant: string, urlOverridden: boolean, verb: string, action: string }} target
|
|
135
159
|
*/
|
|
136
|
-
function nextForRefusal(status, body, target) {
|
|
137
|
-
const code = typeof body?.code === "string" ? body.code : "";
|
|
160
|
+
function nextForRefusal(status, body, code, target) {
|
|
138
161
|
if (code === "owner_required_for_production_app") {
|
|
139
162
|
return "ask the store owner to install the production app, or install with --env=test";
|
|
140
163
|
}
|
|
141
164
|
if (status === 401) return "run `tot login`, then re-run";
|
|
142
165
|
if (code === "scope_denied") return "remove the refused scopes from tot-app.json, then re-run";
|
|
143
166
|
if (code === "already_installed") return "run `tot app list` for its installId, then `tot app update <installId>`";
|
|
144
|
-
if (code === "
|
|
145
|
-
if (code === "order_not_found") return
|
|
167
|
+
if (code === "install_not_found") return "run `tot app list` to see this store's installs";
|
|
168
|
+
if (code === "order_not_found") return `run \`tot app test-order create\`, then use the orderId it prints with \`tot app test-order ${target.action} <orderId>\``;
|
|
146
169
|
if (code === "install_not_processor") {
|
|
147
170
|
const fixes = processorReasonsOf(body).map((r) => PROCESSOR_REASONS[r]?.fix).filter(Boolean);
|
|
148
171
|
return `${fixes.length ? fixes.join("; then ") : "run `tot app list` to check the install"}; then re-run \`tot app test-order bind <installId>\``;
|
|
@@ -154,7 +177,7 @@ function nextForRefusal(status, body, target) {
|
|
|
154
177
|
: `${preview} has not yet been redeployed with host-free test orders; re-run after its next deploy, or report it with \`tot feedback\``;
|
|
155
178
|
}
|
|
156
179
|
if (code === "test_install_required") return "bind an install with env test: `tot app list`, then `tot app test-order bind <installId>`";
|
|
157
|
-
if (code === "invalid_state") return "run `tot app list` to check the install's status";
|
|
180
|
+
if (code === "invalid_state" && INSTALL_VERBS.has(target.verb)) return "run `tot app list` to check the install's status";
|
|
158
181
|
if (status === 403) return refusalHint(status, missingGrantsOf(body));
|
|
159
182
|
if (status === 429) return "wait a minute, then re-run";
|
|
160
183
|
if (status >= 500) return "retry shortly; if it persists, report it with `tot feedback`";
|
|
@@ -177,6 +200,15 @@ export async function runAdmin(argv, ctx, deps = {}) {
|
|
|
177
200
|
}
|
|
178
201
|
if (args.unknown.length) return fail(`unknown option ${args.unknown[0]}`, "tot app --help");
|
|
179
202
|
const verb = args.verb;
|
|
203
|
+
const testOrderOptions = [
|
|
204
|
+
[args.location !== null, "--location"],
|
|
205
|
+
[args.lines.length > 0, "--line"],
|
|
206
|
+
[args.quantity !== null, "--quantity"],
|
|
207
|
+
[args.region !== null, "--region"],
|
|
208
|
+
].filter(([present]) => present).map(([, flag]) => flag);
|
|
209
|
+
if (verb !== "test-order" && testOrderOptions.length) {
|
|
210
|
+
return fail(`unknown option ${testOrderOptions[0]}`, "tot app --help");
|
|
211
|
+
}
|
|
180
212
|
|
|
181
213
|
if (verb === "validate") return runValidateManifest(args, ctx, out, err);
|
|
182
214
|
|
|
@@ -223,17 +255,41 @@ export async function runAdmin(argv, ctx, deps = {}) {
|
|
|
223
255
|
} else if (verb === "list") {
|
|
224
256
|
request = { method: "GET", path: "/api/apps/admin/list" };
|
|
225
257
|
} else if (verb === "test-order") {
|
|
226
|
-
const action = args.positional
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
return fail("test-order needs an action: bind, create or release", "tot app test-order bind <installId> | create | release <orderId>");
|
|
258
|
+
const [action, id, extra] = args.positional;
|
|
259
|
+
if (action !== "bind" && action !== "create" && !TEST_ORDER_TRANSITIONS.has(action)) {
|
|
260
|
+
return fail("test-order needs an action: bind, create, release, hold or cancel", "tot app test-order --help");
|
|
230
261
|
}
|
|
231
262
|
if (action === "bind" && !id) return fail("no install to bind", "tot app test-order bind <installId> (find it with `tot app list`)");
|
|
232
|
-
if (action
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
263
|
+
if (TEST_ORDER_TRANSITIONS.has(action) && !id) return fail(`no order to ${action}`, `tot app test-order ${action} <orderId>`);
|
|
264
|
+
if (extra || (action === "create" && id)) return fail("unexpected test-order argument", "tot app test-order --help");
|
|
265
|
+
const misplaced = testOrderOptions.find((flag) => action !== "create" && (action !== "bind" || flag !== "--location"));
|
|
266
|
+
if (misplaced) return fail(`test-order ${action} does not accept ${misplaced}`, "tot app test-order --help");
|
|
267
|
+
if (args.location !== null && !args.location.trim()) return fail("--location needs a location id");
|
|
268
|
+
if (args.region !== null && !args.region.trim()) return fail("--region needs a state");
|
|
269
|
+
const location = args.location ? { locationId: args.location.trim() } : {};
|
|
270
|
+
let body;
|
|
271
|
+
if (action === "bind") {
|
|
272
|
+
body = { action, installId: id, ...location };
|
|
273
|
+
} else if (action === "create") {
|
|
274
|
+
if (args.quantity !== null && args.lines.length) return fail("use --quantity or --line, one at a time");
|
|
275
|
+
const lines = [];
|
|
276
|
+
for (const entry of args.lines) {
|
|
277
|
+
const match = /^(.+):(\d+)$/.exec(entry);
|
|
278
|
+
const sku = match?.[1].trim();
|
|
279
|
+
const quantity = Number(match?.[2]);
|
|
280
|
+
if (!sku || !Number.isSafeInteger(quantity) || quantity < 1) return fail("--line needs <sku>:<positive integer>");
|
|
281
|
+
lines.push({ sku, quantity });
|
|
282
|
+
}
|
|
283
|
+
if (args.quantity !== null) {
|
|
284
|
+
const quantity = Number(args.quantity);
|
|
285
|
+
if (!/^\d+$/.test(args.quantity) || !Number.isSafeInteger(quantity) || quantity < 1) return fail("--quantity needs a positive integer");
|
|
286
|
+
lines.push({ sku: "synthetic-sku", quantity });
|
|
287
|
+
}
|
|
288
|
+
body = { action, ...location, ...(lines.length ? { lines } : {}), ...(args.region ? { region: args.region.trim() } : {}) };
|
|
289
|
+
} else {
|
|
290
|
+
body = { action, orderId: id };
|
|
291
|
+
}
|
|
292
|
+
request = { method: "POST", path: "/api/apps/admin/test-orders", body };
|
|
237
293
|
} else if (verb === "suspend" || verb === "resume" || verb === "uninstall") {
|
|
238
294
|
const installId = args.positional[0];
|
|
239
295
|
if (!installId) return fail(`no install to ${verb}`, `tot app ${verb} <installId> (find it with \`tot app list\`)`);
|
|
@@ -304,8 +360,10 @@ export async function runAdmin(argv, ctx, deps = {}) {
|
|
|
304
360
|
? `\n refused scopes: ${data.denials.map((d) => d?.scope ?? d?.id ?? JSON.stringify(d)).join(", ")}` : "";
|
|
305
361
|
const storefrontUrl = String(args.url || env.TOT_STOREFRONT_URL || "").trim().replace(/\/+$/, "");
|
|
306
362
|
const urlOverridden = storefrontUrl !== "" && storefrontUrl !== DEFAULT_STOREFRONT_ORIGIN;
|
|
307
|
-
const
|
|
308
|
-
|
|
363
|
+
const action = args.positional[0];
|
|
364
|
+
const code = refusalCode(res.status, data, verb, action);
|
|
365
|
+
const plain = plainRefusal(res.status, data, code);
|
|
366
|
+
err(`✗ tot app ${verb}: ${plain ? `${plain} (${reason})` : reason}${denials}\n\n → next: ${nextForRefusal(res.status, data, code, { tenant, urlOverridden, verb, action })}`);
|
|
309
367
|
return res.status === 401 || res.status === 403 ? 1 : (res.status >= 500 ? 1 : 2);
|
|
310
368
|
}
|
|
311
369
|
|
|
@@ -432,11 +490,13 @@ function renderTestOrder(data, args, out) {
|
|
|
432
490
|
const action = args.positional[0];
|
|
433
491
|
if (action === "bind") {
|
|
434
492
|
const p = data?.processor;
|
|
435
|
-
|
|
493
|
+
const locationId = p?.locationId ?? args.location?.trim();
|
|
494
|
+
out(`✓ bound ${p?.installId ?? args.positional[1]} as the preview processor${locationId ? ` for ${locationId}` : ""} (notifiable: ${p?.notifiable}, consent granted: ${p?.consentGranted})`);
|
|
436
495
|
} else if (action === "create") {
|
|
437
496
|
out(`✓ created synthetic order ${data?.order?.orderId ?? ""} (${data?.order?.stage ?? "preview"})`);
|
|
438
497
|
} else {
|
|
439
|
-
|
|
498
|
+
const order = data?.order;
|
|
499
|
+
out(`✓ ${TEST_ORDER_TRANSITIONS.get(action)} ${order?.orderId ?? args.positional[1]} (${order?.fulfillment ?? ""}, assigned to ${order?.assignedInstallId ?? "no install"})`);
|
|
440
500
|
}
|
|
441
501
|
return 0;
|
|
442
502
|
}
|