consulti 0.0.1 → 0.2.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/dist/errors.js ADDED
@@ -0,0 +1,35 @@
1
+ import { EXIT } from "./exit-codes.js";
2
+ /** Every failure path throws this; the top level prints it as one JSON object on stderr. */
3
+ export class CliError extends Error {
4
+ exit;
5
+ code;
6
+ status;
7
+ retryAfter;
8
+ creditsUsed;
9
+ hint;
10
+ extra;
11
+ constructor(init) {
12
+ super(init.error);
13
+ this.exit = init.exit;
14
+ this.code = init.code ?? null;
15
+ this.status = init.status ?? null;
16
+ this.retryAfter = init.retryAfter ?? null;
17
+ this.creditsUsed = init.creditsUsed ?? null;
18
+ this.hint = init.hint ?? null;
19
+ this.extra = init.extra ?? {};
20
+ }
21
+ toJSON() {
22
+ return {
23
+ error: this.message,
24
+ code: this.code,
25
+ status: this.status,
26
+ retry_after: this.retryAfter,
27
+ credits_used: this.creditsUsed,
28
+ hint: this.hint,
29
+ ...this.extra,
30
+ };
31
+ }
32
+ }
33
+ export function usageError(message, code = "usage", hint) {
34
+ return new CliError({ exit: EXIT.usage, error: message, code, hint });
35
+ }
@@ -0,0 +1,31 @@
1
+ export const EXIT = {
2
+ ok: 0,
3
+ unexpected: 1,
4
+ usage: 2,
5
+ auth: 3,
6
+ plan: 4,
7
+ unavailable: 5,
8
+ network: 6,
9
+ };
10
+ // Canonical text: .omc/team/consulti-cli/exit-table.md. The README carries the same wording.
11
+ export const EXIT_TABLE = [
12
+ [0, "Success."],
13
+ [1, "Unexpected error, including HTTP 404 and any 5xx other than 502, 503 and 504. On a billable command, run consulti credits before retrying."],
14
+ [2, "Usage or validation: a missing or bad flag, a bad path argument, or HTTP 400 or 422 from the API."],
15
+ [3, "Auth: no key, a malformed key (code invalid_key_format, from CONSULTI_API_KEY, the saved file or consulti login), or HTTP 401."],
16
+ [4, "Plan or credits: HTTP 402, or a 403 with a JSON body."],
17
+ [5, "Rate limited, unavailable or blocked: HTTP 429 or 503 (retry_after is set when the server sends one), or a 403 or 429 with a non-JSON body, which means the request was blocked before it reached the API (code blocked)."],
18
+ [6, "Network error or timeout, including a 502 or 504 with a non-JSON body (code gateway_timeout). On a billable command, run consulti credits before retrying."],
19
+ ];
20
+ /** Maps an HTTP error status to a CLI exit code. */
21
+ export function exitForStatus(status) {
22
+ if (status === 400 || status === 422)
23
+ return EXIT.usage;
24
+ if (status === 401)
25
+ return EXIT.auth;
26
+ if (status === 402 || status === 403)
27
+ return EXIT.plan;
28
+ if (status === 429 || status === 503)
29
+ return EXIT.unavailable;
30
+ return EXIT.unexpected;
31
+ }
package/dist/flags.js ADDED
@@ -0,0 +1,57 @@
1
+ import { InvalidArgumentError, Option } from "commander";
2
+ /** `businessIds` and `linkedin_url` become `--business-ids` and `--linkedin-url`. */
3
+ export function flagName(name) {
4
+ return name
5
+ .replace(/_/g, "-")
6
+ .replace(/([a-z0-9])([A-Z])/g, "$1-$2")
7
+ .toLowerCase();
8
+ }
9
+ /** The flag name for a param: its override when the manifest sets one, otherwise derived from the wire name. */
10
+ export function paramFlag(p) {
11
+ return p.flag ?? flagName(p.name);
12
+ }
13
+ function toNumber(value) {
14
+ const trimmed = value.trim();
15
+ const n = Number(trimmed);
16
+ if (!/^-?\d+(\.\d+)?$/.test(trimmed) || !Number.isFinite(n)) {
17
+ throw new InvalidArgumentError(`"${value}" is not a number.`);
18
+ }
19
+ return n;
20
+ }
21
+ function collect(convert) {
22
+ return (value, previous) => {
23
+ const parts = value
24
+ .split(",")
25
+ .map((part) => part.trim())
26
+ .filter((part) => part !== "");
27
+ if (parts.length === 0)
28
+ throw new InvalidArgumentError("expected at least one value.");
29
+ return [...(previous ?? []), ...parts.map(convert)];
30
+ };
31
+ }
32
+ function describe(p) {
33
+ const text = p.desc.replace(/\s+/g, " ").trim();
34
+ const note = p.type.endsWith("[]") ? "; repeat the flag or use commas, and use --body for values that contain commas" : "";
35
+ return `${text} [${p.type}${p.required ? ", required" : ""}${note}]`;
36
+ }
37
+ /** Commander options for one param. Booleans get `--flag` and `--no-flag`. */
38
+ export function optionsFor(p) {
39
+ const flag = `--${paramFlag(p)}`;
40
+ const desc = describe(p);
41
+ switch (p.type) {
42
+ case "string":
43
+ return [new Option(`${flag} <value>`, desc)];
44
+ case "number":
45
+ return [new Option(`${flag} <number>`, desc).argParser(toNumber)];
46
+ case "string[]":
47
+ return [new Option(`${flag} <values>`, desc).argParser(collect((v) => v))];
48
+ case "number[]":
49
+ return [new Option(`${flag} <numbers>`, desc).argParser(collect(toNumber))];
50
+ case "boolean":
51
+ return [new Option(flag, desc), new Option(`--no-${paramFlag(p)}`, `Same as ${flag} false.`)];
52
+ }
53
+ }
54
+ /** The attribute commander stores a param's value under. */
55
+ export function attributeFor(p) {
56
+ return optionsFor(p)[0].attributeName();
57
+ }