primcli 3.0.0-preview.2 → 3.0.0-preview.20

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.
@@ -0,0 +1,109 @@
1
+ import { r as retryIdFields } from "./retry-ids-04ZPJCZY.js";
2
+ import { Errors } from "@oclif/core";
3
+ // src/oclif/required-body-fields.ts
4
+ function object(value) {
5
+ return value !== null && typeof value === "object" && !Array.isArray(value);
6
+ }
7
+ function jsonPath(parts) {
8
+ return parts.reduce((path, part) => typeof part === "number" || !/^[A-Za-z_][A-Za-z0-9_]*$/.test(part) ? `${path}[${JSON.stringify(part)}]` : `${path}.${part}`, "$");
9
+ }
10
+ /** Only exclude a union branch when a supplied literal contradicts it. */
11
+ function compatible(schema, value) {
12
+ if (!object(schema) || !object(value)) return true;
13
+ const properties = object(schema.properties) ? schema.properties : {};
14
+ for (const [name, property] of Object.entries(properties)) {
15
+ if (!Object.hasOwn(value, name) || !object(property)) continue;
16
+ const literal = value[name];
17
+ if (literal === void 0 || literal !== null && typeof literal === "object") continue;
18
+ if (Object.hasOwn(property, "const") && !object(property.const) && !Array.isArray(property.const) && property.const !== literal) return false;
19
+ if (Array.isArray(property.enum) && !property.enum.includes(literal)) return false;
20
+ }
21
+ return true;
22
+ }
23
+ /**
24
+ * Report missing properties, not type or permission failures. Alternative
25
+ * schemas contribute only omissions shared by every still-possible branch.
26
+ */
27
+ function missingBodyFields(schema, body, flags, required) {
28
+ if (body === void 0 && !required) return [];
29
+ function visit(node, value, path) {
30
+ if (!object(node)) return [];
31
+ const missing = [];
32
+ if (object(value)) {
33
+ const names = Array.isArray(node.required) ? node.required : [];
34
+ for (const name of names) {
35
+ if (typeof name !== "string") continue;
36
+ if (Object.hasOwn(value, name) && value[name] !== void 0) continue;
37
+ missing.push({
38
+ field: name,
39
+ flag: path.length === 0 ? flags.get(name) ?? null : null,
40
+ json_path: jsonPath([...path, name])
41
+ });
42
+ }
43
+ if (object(node.properties)) {
44
+ for (const [name, property] of Object.entries(node.properties)) if (Object.hasOwn(value, name) && value[name] !== void 0) missing.push(...visit(property, value[name], [...path, name]));
45
+ }
46
+ } else if (Array.isArray(value)) for (const [index, item] of value.entries()) missing.push(...visit(node.items, item, [...path, index]));
47
+ if (Array.isArray(node.allOf)) for (const branch of node.allOf) missing.push(...visit(branch, value, path));
48
+ for (const union of [node.oneOf, node.anyOf]) {
49
+ if (!Array.isArray(union)) continue;
50
+ const branches = union.filter((branch) => compatible(branch, value)).map((branch) => visit(branch, value, path));
51
+ if (branches.length === 0) continue;
52
+ missing.push(...branches[0].filter((field) => branches.every((branch) => branch.some((other) => other.json_path === field.json_path))));
53
+ }
54
+ return missing;
55
+ }
56
+ const missing = visit(schema, body === void 0 ? {} : body, []);
57
+ return [...new Map(missing.map((field) => [field.json_path, field])).values()];
58
+ }
59
+ function assertRequiredBodyFields(schema, body, flags, required) {
60
+ const missing = missingBodyFields(schema, body, flags, required);
61
+ if (missing.length === 0 && (!required || body !== void 0)) return;
62
+ const fields = missing.length ? missing : [{
63
+ field: "body",
64
+ flag: null,
65
+ json_path: "$"
66
+ }];
67
+ const message = `Missing required fields: ${fields.map((field) => field.flag ?? field.json_path).join(", ")}. Supply the fields as flags where available, or use --raw-body / --body-file JSON.`;
68
+ throw Object.assign(new Errors.CLIError(message, { exit: 2 }), {
69
+ code: "usage_error",
70
+ details: { missing: fields }
71
+ });
72
+ }
73
+ // src/oclif/help-inputs.ts
74
+ const HELP_GROUPS = [
75
+ "required",
76
+ "command",
77
+ "common"
78
+ ];
79
+ const HELP_GROUP_HEADERS = {
80
+ required: "REQUIRED INPUTS",
81
+ command: "COMMAND OPTIONS",
82
+ common: "COMMON FLAGS"
83
+ };
84
+ const COMMON_FLAGS = /* @__PURE__ */ new Set([
85
+ "api-key",
86
+ "api-base-url",
87
+ "json",
88
+ "time",
89
+ "raw-body",
90
+ "envelope",
91
+ "output",
92
+ "idempotency-key",
93
+ "step-up-token"
94
+ ]);
95
+ function helpFlagGroup(name, flag, inputs, flagNames) {
96
+ if (flag.required || flag.exactlyOne?.length || inputs?.requiredFlags.includes(name)) return "required";
97
+ if (COMMON_FLAGS.has(name) || name === "body-file" && flagNames.has("raw-body")) return "common";
98
+ return "command";
99
+ }
100
+ function operationHelpInputs(operation, bodyFlagToProperty, positionalFlag) {
101
+ const propertyFlags = new Map([...bodyFlagToProperty].map(([flag, property]) => [property, `--${flag}`]));
102
+ const generated = new Set(retryIdFields(operation.requestSchema));
103
+ const missing = operation.bodyRequired || generated.size > 0 ? missingBodyFields(operation.requestSchema, {}, propertyFlags, true).filter((field) => !generated.has(field.field)) : [];
104
+ return {
105
+ requiredFlags: [...missing.flatMap((field) => field.flag ? [field.flag.slice(2)] : []), ...positionalFlag ? [positionalFlag] : []],
106
+ requiredJsonFields: operation.bodyRequired && missing.length === 0 && generated.size === 0 ? ["$"] : missing.filter((field) => field.flag === null).map((field) => field.json_path)
107
+ };
108
+ }
109
+ export { assertRequiredBodyFields as a, operationHelpInputs as i, HELP_GROUP_HEADERS as n, helpFlagGroup as r, HELP_GROUPS as t };
@@ -1,3 +1,5 @@
1
+ import { i as takeRetryIds } from "./retry-ids-04ZPJCZY.js";
2
+ import { Flags } from "@oclif/core";
1
3
  import { format } from "node:util";
2
4
  // src/oclif/json-output.ts
3
5
  /**
@@ -22,6 +24,11 @@ import { format } from "node:util";
22
24
  * line per result on stdout, and on failure one more JSON line carrying
23
25
  * `error` and `exit_code`.
24
26
  */
27
+ /**
28
+ * The `--json` flag for a hand-rolled command whose output is already JSON.
29
+ * Declaring it is enough: the registry guards every command that has it.
30
+ */
31
+ const JSON_OUTPUT_FLAG = Flags.boolean({ description: "Print exactly one JSON document on stdout, on success and on failure, and nothing on stderr. A failure prints the error with exit_code." });
25
32
  const GUARDED = Symbol.for("primitive.cli.jsonOutputGuard");
26
33
  /** True when `--json` appears before any `--` separator. */
27
34
  function jsonOutputRequested(argv) {
@@ -139,11 +146,18 @@ function errorExitCode(error) {
139
146
  function isExitSignal(error) {
140
147
  return error?.code === "EEXIT";
141
148
  }
149
+ /**
150
+ * The `error` a `--json` document carries for a thrown error: its message,
151
+ * and its `code` and `details` when it has them (a refusal the CLI makes in
152
+ * an API error's shape, such as a send without mail:send).
153
+ */
142
154
  function thrownErrorBody(error) {
143
155
  if (error instanceof Error) {
144
156
  const body = { message: error.message };
145
157
  const code = error.code;
146
158
  if (typeof code === "string" && code !== "EEXIT") body.code = code;
159
+ const details = error.details;
160
+ if (isPlainObject(details)) body.details = details;
147
161
  return body;
148
162
  }
149
163
  return { message: String(error) };
@@ -153,13 +167,25 @@ function thrownErrorBody(error) {
153
167
  * what it wrote to stdout and stderr and how it ended.
154
168
  */
155
169
  function composeJsonDocument(input) {
170
+ const document = composeResult(input);
171
+ if (!input.retryIds?.length) return document;
172
+ return {
173
+ ...isPlainObject(document) ? document : { data: document },
174
+ cli_retry_ids: input.retryIds
175
+ };
176
+ }
177
+ function composeResult(input) {
156
178
  const { errorPayload, warnings: stderrLines } = summarizeStderr(input.stderr);
157
179
  const failed = input.exitCode !== 0;
158
180
  let warnings = stderrLines;
159
181
  if (!failed && errorPayload !== null) warnings = [...warnings, JSON.stringify(errorPayload)];
160
182
  const failure = () => {
161
183
  if (errorPayload !== null) return errorPayload;
162
- if (input.thrown && !isExitSignal(input.thrown.error)) return thrownErrorBody(input.thrown.error);
184
+ if (input.thrown && !isExitSignal(input.thrown.error)) {
185
+ const body = thrownErrorBody(input.thrown.error);
186
+ if (body.code === void 0) body.code = errorExitCode(input.thrown.error) === 2 ? "usage_error" : "cli_error";
187
+ return body;
188
+ }
163
189
  return { message: warnings.at(-1) ?? `The command failed with exit code ${input.exitCode}.` };
164
190
  };
165
191
  const text = input.stdout.trim();
@@ -195,9 +221,7 @@ function composeJsonDocument(input) {
195
221
  if (merged.exit_code === void 0) merged.exit_code = input.exitCode;
196
222
  }
197
223
  if (warnings.length > 0) {
198
- const existing = merged.warnings;
199
- if (existing === void 0 || existing === null) merged.warnings = warnings;
200
- else if (Array.isArray(existing)) merged.warnings = [...existing, ...warnings.filter((warning) => !existing.includes(warning))];
224
+ if (merged.warnings === void 0 || merged.warnings === null) merged.warnings = warnings;
201
225
  else merged.cli_warnings = warnings;
202
226
  }
203
227
  return merged;
@@ -227,7 +251,8 @@ function guardJsonOutput(cls) {
227
251
  exitCode: thrown ? isExitSignal(thrown.error) ? errorExitCode(thrown.error) ?? 0 : errorExitCode(thrown.error) ?? (typeof process.exitCode === "number" && process.exitCode !== 0 ? process.exitCode : 1) : typeof process.exitCode === "number" ? process.exitCode : 0,
228
252
  stderr: capture.stderr,
229
253
  stdout: capture.stdout.join(""),
230
- thrown
254
+ thrown,
255
+ retryIds: takeRetryIds(this)
231
256
  });
232
257
  console.log(JSON.stringify(document, null, 2));
233
258
  if (thrown) {
@@ -318,4 +343,4 @@ function guardJsonOutputCommands(commands) {
318
343
  if (flags && "json" in flags || enableJsonFlag === true) guardJsonOutput(command);
319
344
  }
320
345
  }
321
- export { jsonOutputRequested as n, runStreamingJson as r, guardJsonOutputCommands as t };
346
+ export { thrownErrorBody as a, runStreamingJson as i, guardJsonOutputCommands as n, jsonOutputRequested as r, JSON_OUTPUT_FLAG as t };