@tracken/cli 0.0.0-stage → 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/CHANGELOG.md +376 -0
- package/LICENSE +21 -0
- package/README.md +307 -2
- package/dist/bin/tracken.d.ts +2 -0
- package/dist/bin/tracken.js +5 -0
- package/dist/cli/args.d.ts +17 -0
- package/dist/cli/args.js +114 -0
- package/dist/cli/commands/api.d.ts +2 -0
- package/dist/cli/commands/api.js +73 -0
- package/dist/cli/commands/auth.d.ts +8 -0
- package/dist/cli/commands/auth.js +141 -0
- package/dist/cli/commands/mcp.d.ts +19 -0
- package/dist/cli/commands/mcp.js +89 -0
- package/dist/cli/commands/org.d.ts +24 -0
- package/dist/cli/commands/org.js +123 -0
- package/dist/cli/context.d.ts +54 -0
- package/dist/cli/context.js +135 -0
- package/dist/cli/execute.d.ts +16 -0
- package/dist/cli/execute.js +112 -0
- package/dist/cli/help.d.ts +8 -0
- package/dist/cli/help.js +153 -0
- package/dist/cli/main.d.ts +2 -0
- package/dist/cli/main.js +114 -0
- package/dist/cli/operation-args.d.ts +25 -0
- package/dist/cli/operation-args.js +245 -0
- package/dist/core/auth/oauth.d.ts +39 -0
- package/dist/core/auth/oauth.js +102 -0
- package/dist/core/auth/scopes.d.ts +19 -0
- package/dist/core/auth/scopes.js +49 -0
- package/dist/core/auth/secret-store.d.ts +60 -0
- package/dist/core/auth/secret-store.js +141 -0
- package/dist/core/auth/session.d.ts +75 -0
- package/dist/core/auth/session.js +200 -0
- package/dist/core/catalog.d.ts +12 -0
- package/dist/core/catalog.js +33 -0
- package/dist/core/client.d.ts +22 -0
- package/dist/core/client.js +45 -0
- package/dist/core/config.d.ts +27 -0
- package/dist/core/config.js +109 -0
- package/dist/core/environments.d.ts +21 -0
- package/dist/core/environments.js +53 -0
- package/dist/core/errors.d.ts +87 -0
- package/dist/core/errors.js +226 -0
- package/dist/core/index.d.ts +21 -0
- package/dist/core/index.js +22 -0
- package/dist/core/jobs.d.ts +15 -0
- package/dist/core/jobs.js +19 -0
- package/dist/core/operations.d.ts +108 -0
- package/dist/core/operations.js +7 -0
- package/dist/core/output/format.d.ts +19 -0
- package/dist/core/output/format.js +62 -0
- package/dist/core/output/render.d.ts +53 -0
- package/dist/core/output/render.js +229 -0
- package/dist/core/pagination.d.ts +2 -0
- package/dist/core/pagination.js +3 -0
- package/dist/core/request.d.ts +29 -0
- package/dist/core/request.js +109 -0
- package/dist/core/version.d.ts +10 -0
- package/dist/core/version.js +13 -0
- package/dist/generated/mcp-tools.d.ts +2 -0
- package/dist/generated/mcp-tools.js +5937 -0
- package/dist/generated/operations.d.ts +4 -0
- package/dist/generated/operations.js +17701 -0
- package/dist/mcp/errors.d.ts +34 -0
- package/dist/mcp/errors.js +91 -0
- package/dist/mcp/live.d.ts +3 -0
- package/dist/mcp/live.js +5 -0
- package/dist/mcp/prompts.d.ts +23 -0
- package/dist/mcp/prompts.js +101 -0
- package/dist/mcp/resources.d.ts +25 -0
- package/dist/mcp/resources.js +125 -0
- package/dist/mcp/server.d.ts +20 -0
- package/dist/mcp/server.js +59 -0
- package/dist/mcp/session.d.ts +60 -0
- package/dist/mcp/session.js +105 -0
- package/dist/mcp/tools.d.ts +17 -0
- package/dist/mcp/tools.js +181 -0
- package/dist/mcp/types.d.ts +25 -0
- package/dist/mcp/types.js +2 -0
- package/node_modules/@tracken/sdk/LICENSE +21 -0
- package/node_modules/@tracken/sdk/README.md +72 -0
- package/node_modules/@tracken/sdk/SOURCE.json +10 -0
- package/node_modules/@tracken/sdk/dist/client.d.ts +94 -0
- package/node_modules/@tracken/sdk/dist/client.js +258 -0
- package/node_modules/@tracken/sdk/dist/credentials.d.ts +60 -0
- package/node_modules/@tracken/sdk/dist/credentials.js +122 -0
- package/node_modules/@tracken/sdk/dist/decimal.d.ts +20 -0
- package/node_modules/@tracken/sdk/dist/decimal.js +67 -0
- package/node_modules/@tracken/sdk/dist/environments.d.ts +14 -0
- package/node_modules/@tracken/sdk/dist/environments.js +16 -0
- package/node_modules/@tracken/sdk/dist/errors.d.ts +126 -0
- package/node_modules/@tracken/sdk/dist/errors.js +165 -0
- package/node_modules/@tracken/sdk/dist/generated/operations.d.ts +1955 -0
- package/node_modules/@tracken/sdk/dist/generated/operations.js +2745 -0
- package/node_modules/@tracken/sdk/dist/generated/schema.d.ts +17988 -0
- package/node_modules/@tracken/sdk/dist/generated/schema.js +5 -0
- package/node_modules/@tracken/sdk/dist/index.d.ts +27 -0
- package/node_modules/@tracken/sdk/dist/index.js +24 -0
- package/node_modules/@tracken/sdk/dist/jobs.d.ts +23 -0
- package/node_modules/@tracken/sdk/dist/jobs.js +80 -0
- package/node_modules/@tracken/sdk/dist/operations.d.ts +23 -0
- package/node_modules/@tracken/sdk/dist/operations.js +2 -0
- package/node_modules/@tracken/sdk/dist/pagination.d.ts +19 -0
- package/node_modules/@tracken/sdk/dist/pagination.js +49 -0
- package/node_modules/@tracken/sdk/dist/request.d.ts +27 -0
- package/node_modules/@tracken/sdk/dist/request.js +10 -0
- package/node_modules/@tracken/sdk/dist/runtime.d.ts +22 -0
- package/node_modules/@tracken/sdk/dist/runtime.js +33 -0
- package/node_modules/@tracken/sdk/dist/tracken.d.ts +74 -0
- package/node_modules/@tracken/sdk/dist/tracken.js +203 -0
- package/node_modules/@tracken/sdk/dist/types.d.ts +130 -0
- package/node_modules/@tracken/sdk/dist/types.js +1 -0
- package/node_modules/@tracken/sdk/dist/version.d.ts +2 -0
- package/node_modules/@tracken/sdk/dist/version.js +2 -0
- package/node_modules/@tracken/sdk/package.json +42 -0
- package/node_modules/openapi-fetch/CHANGELOG.md +565 -0
- package/node_modules/openapi-fetch/CONTRIBUTING.md +102 -0
- package/node_modules/openapi-fetch/LICENSE +21 -0
- package/node_modules/openapi-fetch/README.md +122 -0
- package/node_modules/openapi-fetch/dist/index.cjs +547 -0
- package/node_modules/openapi-fetch/dist/index.cjs.map +1 -0
- package/node_modules/openapi-fetch/dist/index.d.cts +337 -0
- package/node_modules/openapi-fetch/dist/index.d.mts +335 -0
- package/node_modules/openapi-fetch/dist/index.d.ts +337 -0
- package/node_modules/openapi-fetch/dist/index.mjs +531 -0
- package/node_modules/openapi-fetch/dist/index.mjs.map +1 -0
- package/node_modules/openapi-fetch/package.json +77 -0
- package/node_modules/openapi-fetch/src/index.d.ts +345 -0
- package/node_modules/openapi-fetch/src/index.js +689 -0
- package/node_modules/openapi-typescript-helpers/CHANGELOG.md +98 -0
- package/node_modules/openapi-typescript-helpers/LICENSE +21 -0
- package/node_modules/openapi-typescript-helpers/README.md +5 -0
- package/node_modules/openapi-typescript-helpers/dist/index.cjs +3 -0
- package/node_modules/openapi-typescript-helpers/dist/index.cjs.map +1 -0
- package/node_modules/openapi-typescript-helpers/dist/index.d.cts +167 -0
- package/node_modules/openapi-typescript-helpers/dist/index.d.mts +167 -0
- package/node_modules/openapi-typescript-helpers/dist/index.d.ts +167 -0
- package/node_modules/openapi-typescript-helpers/dist/index.mjs +2 -0
- package/node_modules/openapi-typescript-helpers/dist/index.mjs.map +1 -0
- package/node_modules/openapi-typescript-helpers/package.json +39 -0
- package/openapi/SOURCE.json +10 -0
- package/openapi/openapi.yaml +30513 -0
- package/package.json +63 -4
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import { formatCell, formatDate, formatDecimal, formatNumber } from "./format.js";
|
|
2
|
+
export const OUTPUT_MODES = ["json", "table", "csv", "ndjson"];
|
|
3
|
+
const MAX_CELL = 48;
|
|
4
|
+
/** More columns than this do not fit a terminal; the most telling ones are kept (`--columns` shows others). */
|
|
5
|
+
const MAX_TABLE_COLUMNS = 8;
|
|
6
|
+
const PRIORITY = [
|
|
7
|
+
"id",
|
|
8
|
+
"number",
|
|
9
|
+
"code",
|
|
10
|
+
"name",
|
|
11
|
+
"title",
|
|
12
|
+
"label",
|
|
13
|
+
"kind",
|
|
14
|
+
"type",
|
|
15
|
+
"status",
|
|
16
|
+
"date",
|
|
17
|
+
"due_date",
|
|
18
|
+
"period",
|
|
19
|
+
"total",
|
|
20
|
+
"amount",
|
|
21
|
+
"amount_due",
|
|
22
|
+
"direction",
|
|
23
|
+
"balance",
|
|
24
|
+
"currency",
|
|
25
|
+
"net_total",
|
|
26
|
+
"tax_total",
|
|
27
|
+
"contact_id",
|
|
28
|
+
"email",
|
|
29
|
+
"progress",
|
|
30
|
+
"updated_at",
|
|
31
|
+
];
|
|
32
|
+
const isRow = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
33
|
+
const isScalar = (value) => value === null || typeof value !== "object" || (Array.isArray(value) && value.every((entry) => typeof entry !== "object" || entry === null));
|
|
34
|
+
const hintOf = (hints, key) => {
|
|
35
|
+
const hint = hints?.[key];
|
|
36
|
+
return typeof hint === "string" ? hint : undefined;
|
|
37
|
+
};
|
|
38
|
+
const nestedHints = (hints, key) => {
|
|
39
|
+
const hint = hints?.[key];
|
|
40
|
+
return typeof hint === "object" ? hint : undefined;
|
|
41
|
+
};
|
|
42
|
+
const NUMERIC = new Set(["decimal", "integer", "number"]);
|
|
43
|
+
function clip(text) {
|
|
44
|
+
const flat = text.replace(/\s+/g, " ");
|
|
45
|
+
return flat.length > MAX_CELL ? `${flat.slice(0, MAX_CELL - 1)}…` : flat;
|
|
46
|
+
}
|
|
47
|
+
/** A plain text table; numeric columns align right. */
|
|
48
|
+
export function renderTable(headers, rows, rightAligned = []) {
|
|
49
|
+
const widths = headers.map((header, column) => Math.max(header.length, ...rows.map((row) => (row[column] ?? "").length)));
|
|
50
|
+
const line = (cells) => cells
|
|
51
|
+
.map((cell, column) => (rightAligned[column] ? cell.padStart(widths[column]) : cell.padEnd(widths[column])))
|
|
52
|
+
.join(" ")
|
|
53
|
+
.trimEnd();
|
|
54
|
+
return [line(headers), line(widths.map((width) => "-".repeat(width))), ...rows.map(line)].join("\n");
|
|
55
|
+
}
|
|
56
|
+
export function chooseColumns(items, requested) {
|
|
57
|
+
if (requested && requested.length > 0)
|
|
58
|
+
return requested;
|
|
59
|
+
const keys = [];
|
|
60
|
+
for (const item of items)
|
|
61
|
+
for (const [key, value] of Object.entries(item))
|
|
62
|
+
if (!keys.includes(key) && isScalar(value))
|
|
63
|
+
keys.push(key);
|
|
64
|
+
// A column whose values are objects in some rows is left to the record view.
|
|
65
|
+
const scalar = keys.filter((key) => items.every((item) => isScalar(item[key])));
|
|
66
|
+
if (scalar.length <= MAX_TABLE_COLUMNS)
|
|
67
|
+
return scalar;
|
|
68
|
+
const preferred = PRIORITY.filter((key) => scalar.includes(key));
|
|
69
|
+
return [...preferred, ...scalar.filter((key) => !preferred.includes(key))].slice(0, MAX_TABLE_COLUMNS);
|
|
70
|
+
}
|
|
71
|
+
export function renderListTable(items, options) {
|
|
72
|
+
if (items.length === 0)
|
|
73
|
+
return "(none)";
|
|
74
|
+
const rows = items.map((item) => (isRow(item) ? item : { value: item }));
|
|
75
|
+
const columns = chooseColumns(rows, options.columns);
|
|
76
|
+
const formats = columns.map((column) => hintOf(options.hints, column));
|
|
77
|
+
return renderTable(columns, rows.map((row) => columns.map((column, index) => clip(formatCell(row[column], formats[index], options)))), formats.map((format) => format !== undefined && NUMERIC.has(format)));
|
|
78
|
+
}
|
|
79
|
+
function renderRecordTable(record, options) {
|
|
80
|
+
const fields = [];
|
|
81
|
+
const sections = [];
|
|
82
|
+
const add = (key, value, hints) => {
|
|
83
|
+
if (isRow(value)) {
|
|
84
|
+
for (const [inner, innerValue] of Object.entries(value)) {
|
|
85
|
+
fields.push([`${key}.${inner}`, clip(formatCell(innerValue, hintOf(nestedHints(hints, key), inner), options))]);
|
|
86
|
+
}
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (Array.isArray(value) && value.some(isRow)) {
|
|
90
|
+
sections.push(`${key}:\n${renderListTable(value, { ...options, columns: undefined, hints: nestedHints(hints, key) })}`);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
fields.push([key, clip(formatCell(value, hintOf(hints, key), options))]);
|
|
94
|
+
};
|
|
95
|
+
const keys = options.columns && options.columns.length > 0 ? options.columns : Object.keys(record);
|
|
96
|
+
for (const key of keys)
|
|
97
|
+
add(key, record[key], options.hints);
|
|
98
|
+
return [renderTable(["field", "value"], fields), ...sections].join("\n\n");
|
|
99
|
+
}
|
|
100
|
+
const isReport = (data) => Array.isArray(data.columns) && Array.isArray(data.rows);
|
|
101
|
+
function reportCell(value, type) {
|
|
102
|
+
if (typeof value !== "string")
|
|
103
|
+
return value === null || value === undefined ? "" : String(value);
|
|
104
|
+
if (type === "decimal" || type === "integer")
|
|
105
|
+
return formatDecimal(value);
|
|
106
|
+
if (type === "date")
|
|
107
|
+
return formatDate(value);
|
|
108
|
+
return value;
|
|
109
|
+
}
|
|
110
|
+
/** A report's own columns and indented rows (`ReportResult`), in table form. */
|
|
111
|
+
export function renderReportTable(report) {
|
|
112
|
+
const headers = ["", ...report.columns.map((column) => column.label ?? column.key)];
|
|
113
|
+
const rows = report.rows.map((row) => {
|
|
114
|
+
const values = isRow(row.values) ? row.values : {};
|
|
115
|
+
const level = typeof row.level === "number" ? row.level : 0;
|
|
116
|
+
return [`${" ".repeat(level)}${String(row.label ?? row.key ?? "")}`, ...report.columns.map((column) => reportCell(values[column.key], column.type))];
|
|
117
|
+
});
|
|
118
|
+
const table = renderTable(headers, rows, [false, ...report.columns.map((column) => column.type === "decimal" || column.type === "integer")]);
|
|
119
|
+
return typeof report.name === "string" && report.name ? `${report.name}\n\n${table}` : table;
|
|
120
|
+
}
|
|
121
|
+
function csvValue(value) {
|
|
122
|
+
const text = value === null || value === undefined ? "" : typeof value === "object" ? JSON.stringify(value) : String(value);
|
|
123
|
+
return /[",\r\n]|^\s|\s$/.test(text) ? `"${text.replace(/"/g, '""')}"` : text;
|
|
124
|
+
}
|
|
125
|
+
export function csvLine(values) {
|
|
126
|
+
return values.map(csvValue).join(",");
|
|
127
|
+
}
|
|
128
|
+
/** CSV columns: every field of the records, nested ones as JSON, unless `--columns` names them. */
|
|
129
|
+
export function csvColumns(items, requested) {
|
|
130
|
+
if (requested && requested.length > 0)
|
|
131
|
+
return requested;
|
|
132
|
+
const keys = [];
|
|
133
|
+
for (const item of items)
|
|
134
|
+
if (isRow(item))
|
|
135
|
+
for (const key of Object.keys(item))
|
|
136
|
+
if (!keys.includes(key))
|
|
137
|
+
keys.push(key);
|
|
138
|
+
return keys;
|
|
139
|
+
}
|
|
140
|
+
export function renderCsv(items, requested) {
|
|
141
|
+
const rows = items.map((item) => (isRow(item) ? item : { value: item }));
|
|
142
|
+
const columns = csvColumns(rows, requested);
|
|
143
|
+
return [csvLine(columns), ...rows.map((row) => csvLine(columns.map((column) => row[column])))].join("\n");
|
|
144
|
+
}
|
|
145
|
+
function reportCsv(report) {
|
|
146
|
+
const header = csvLine(["key", "kind", "level", "label", ...report.columns.map((column) => column.label ?? column.key)]);
|
|
147
|
+
const lines = report.rows.map((row) => {
|
|
148
|
+
const values = isRow(row.values) ? row.values : {};
|
|
149
|
+
return csvLine([row.key, row.kind, row.level, row.label, ...report.columns.map((column) => values[column.key])]);
|
|
150
|
+
});
|
|
151
|
+
return [header, ...lines].join("\n");
|
|
152
|
+
}
|
|
153
|
+
/** Renders a whole JSON answer in the chosen mode. */
|
|
154
|
+
export function renderBody(body, mode, options = {}) {
|
|
155
|
+
if (mode === "json")
|
|
156
|
+
return JSON.stringify(body, null, 2);
|
|
157
|
+
const data = isRow(body) && "data" in body ? body.data : body;
|
|
158
|
+
if (mode === "ndjson") {
|
|
159
|
+
const records = Array.isArray(data) ? data : isRow(data) && isReport(data) ? data.rows : [data];
|
|
160
|
+
return records.map((record) => JSON.stringify(record)).join("\n");
|
|
161
|
+
}
|
|
162
|
+
if (mode === "csv") {
|
|
163
|
+
if (Array.isArray(data))
|
|
164
|
+
return renderCsv(data, options.columns);
|
|
165
|
+
if (isRow(data) && isReport(data))
|
|
166
|
+
return reportCsv(data);
|
|
167
|
+
return renderCsv([data], options.columns);
|
|
168
|
+
}
|
|
169
|
+
if (Array.isArray(data))
|
|
170
|
+
return renderListTable(data, options);
|
|
171
|
+
if (isRow(data))
|
|
172
|
+
return isReport(data) ? renderReportTable(data) : renderRecordTable(data, options);
|
|
173
|
+
if (typeof data === "number")
|
|
174
|
+
return formatNumber(data);
|
|
175
|
+
return data === undefined ? "" : String(data);
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Writes `--all` output a page at a time: JSON, NDJSON and CSV stream (one page in memory); a table
|
|
179
|
+
* needs every row for its column widths, so it collects them.
|
|
180
|
+
*/
|
|
181
|
+
export class ListWriter {
|
|
182
|
+
mode;
|
|
183
|
+
write;
|
|
184
|
+
options;
|
|
185
|
+
count = 0;
|
|
186
|
+
csvHeader;
|
|
187
|
+
collected = [];
|
|
188
|
+
constructor(mode, write, options = {}) {
|
|
189
|
+
this.mode = mode;
|
|
190
|
+
this.write = write;
|
|
191
|
+
this.options = options;
|
|
192
|
+
}
|
|
193
|
+
add(items) {
|
|
194
|
+
if (this.mode === "ndjson") {
|
|
195
|
+
for (const item of items)
|
|
196
|
+
this.write(`${JSON.stringify(item)}\n`);
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
if (this.mode === "json") {
|
|
200
|
+
for (const item of items)
|
|
201
|
+
this.write(`${this.count++ === 0 ? '{\n "data": [\n' : ",\n"} ${JSON.stringify(item)}`);
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
if (this.mode === "csv") {
|
|
205
|
+
if (items.length === 0)
|
|
206
|
+
return;
|
|
207
|
+
if (!this.csvHeader) {
|
|
208
|
+
this.csvHeader = csvColumns(items, this.options.columns);
|
|
209
|
+
this.write(`${csvLine(this.csvHeader)}\n`);
|
|
210
|
+
}
|
|
211
|
+
const columns = this.csvHeader;
|
|
212
|
+
for (const item of items)
|
|
213
|
+
this.write(`${csvLine(columns.map((column) => (isRow(item) ? item[column] : item)))}\n`);
|
|
214
|
+
this.count += items.length;
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
this.collected.push(...items);
|
|
218
|
+
}
|
|
219
|
+
/** `meta` closes a JSON document beside `data` (the pass's `sync.next_since`); other modes ignore it. */
|
|
220
|
+
end(meta) {
|
|
221
|
+
if (this.mode === "json") {
|
|
222
|
+
const tail = meta ? `,\n "meta": ${JSON.stringify(meta)}\n}\n` : "\n}\n";
|
|
223
|
+
this.write(this.count === 0 ? `{\n "data": []${tail}` : `\n ]${tail}`);
|
|
224
|
+
}
|
|
225
|
+
else if (this.mode === "table")
|
|
226
|
+
this.write(`${renderListTable(this.collected, this.options)}\n`);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
//# sourceMappingURL=render.js.map
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turns an operation and its inputs into a request, by the document's rules: path parameters into
|
|
3
|
+
* the template, query parameters serialized per their `style`/`explode`, bodies wrapped in the
|
|
4
|
+
* `{data}` envelope with operation options as its siblings. The CLI's flags and the MCP server's
|
|
5
|
+
* tool arguments both arrive here as an `OperationInput`.
|
|
6
|
+
*/
|
|
7
|
+
import type { ApiRequest as SdkApiRequest } from "@tracken/sdk";
|
|
8
|
+
import type { OperationSpec } from "./operations.js";
|
|
9
|
+
export { serializeQuery, withQuery } from "@tracken/sdk";
|
|
10
|
+
export type QueryScalar = string | number | boolean;
|
|
11
|
+
export type QueryValue = QueryScalar | QueryScalar[];
|
|
12
|
+
export interface OperationInput {
|
|
13
|
+
path?: Record<string, string>;
|
|
14
|
+
/** By wire name (`updated_since`). */
|
|
15
|
+
query?: Record<string, QueryValue | undefined>;
|
|
16
|
+
/** By wire name (`If-Match`). */
|
|
17
|
+
headers?: Record<string, string | undefined>;
|
|
18
|
+
/** The record inside `{data}`. */
|
|
19
|
+
data?: unknown;
|
|
20
|
+
/** Operation options beside `data` (`on_conflict`). */
|
|
21
|
+
options?: Record<string, unknown>;
|
|
22
|
+
/** Multipart form fields (`file`, `bank_account_id`). */
|
|
23
|
+
form?: Record<string, string | Blob>;
|
|
24
|
+
}
|
|
25
|
+
/** The SDK's request, carrying the CLI's operation spec (which the SDK reads as its `OperationRef`). */
|
|
26
|
+
export interface ApiRequest extends SdkApiRequest {
|
|
27
|
+
operation?: OperationSpec;
|
|
28
|
+
}
|
|
29
|
+
export declare function buildRequest(operation: OperationSpec, input?: OperationInput): ApiRequest;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { UsageError } from "./errors.js";
|
|
2
|
+
export { serializeQuery, withQuery } from "@tracken/sdk";
|
|
3
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
4
|
+
const scalarText = (value) => (typeof value === "boolean" ? (value ? "true" : "false") : String(value));
|
|
5
|
+
function queryPairs(param, value) {
|
|
6
|
+
const values = Array.isArray(value) ? value : [value];
|
|
7
|
+
if (param.type === "array" && param.explode === false)
|
|
8
|
+
return [[param.name, values.map(scalarText).join(",")]];
|
|
9
|
+
if (param.type !== "array" && values.length > 1)
|
|
10
|
+
throw new UsageError(`${param.name} takes one value.`);
|
|
11
|
+
return values.map((entry) => [param.name, scalarText(entry)]);
|
|
12
|
+
}
|
|
13
|
+
export function buildRequest(operation, input = {}) {
|
|
14
|
+
const pathParams = {};
|
|
15
|
+
for (const name of operation.positionals) {
|
|
16
|
+
const value = input.path?.[name];
|
|
17
|
+
if (value === undefined || value === "")
|
|
18
|
+
throw new UsageError(`Missing <${name}>.`);
|
|
19
|
+
const spec = operation.params.find((param) => param.in === "path" && param.name === name);
|
|
20
|
+
if (spec?.format === "uuid" && !UUID.test(value))
|
|
21
|
+
throw new UsageError(`<${name}> must be a UUID, not "${value}".`);
|
|
22
|
+
pathParams[name] = value;
|
|
23
|
+
}
|
|
24
|
+
for (const name of Object.keys(input.path ?? {})) {
|
|
25
|
+
if (!operation.positionals.includes(name))
|
|
26
|
+
throw new UsageError(`${operation.operationId} has no path parameter ${name}.`);
|
|
27
|
+
}
|
|
28
|
+
const query = [];
|
|
29
|
+
for (const [name, value] of Object.entries(input.query ?? {})) {
|
|
30
|
+
if (value === undefined)
|
|
31
|
+
continue;
|
|
32
|
+
const param = operation.params.find((entry) => entry.in === "query" && entry.name === name);
|
|
33
|
+
if (!param)
|
|
34
|
+
throw new UsageError(`${operation.operationId} has no query parameter ${name}.`);
|
|
35
|
+
query.push(...queryPairs(param, value));
|
|
36
|
+
}
|
|
37
|
+
for (const param of operation.params) {
|
|
38
|
+
if (param.in === "query" && param.required && !query.some(([name]) => name === param.name)) {
|
|
39
|
+
throw new UsageError(`Missing --${param.flag} (${param.description || param.name}).`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const headers = {};
|
|
43
|
+
for (const [name, value] of Object.entries(input.headers ?? {})) {
|
|
44
|
+
if (value === undefined)
|
|
45
|
+
continue;
|
|
46
|
+
const param = operation.params.find((entry) => entry.in === "header" && entry.name.toLowerCase() === name.toLowerCase());
|
|
47
|
+
if (!param)
|
|
48
|
+
throw new UsageError(`${operation.operationId} takes no ${name} header.`);
|
|
49
|
+
headers[param.name] = value;
|
|
50
|
+
}
|
|
51
|
+
const request = { method: operation.method, path: operation.path, pathParams, query, headers, operation };
|
|
52
|
+
const body = operation.body;
|
|
53
|
+
const options = Object.entries(input.options ?? {}).filter(([, value]) => value !== undefined);
|
|
54
|
+
if (!body) {
|
|
55
|
+
if (input.data !== undefined || options.length > 0 || input.form)
|
|
56
|
+
throw new UsageError(`${operation.operationId} takes no request body.`);
|
|
57
|
+
return request;
|
|
58
|
+
}
|
|
59
|
+
if (body.contentType === "multipart/form-data") {
|
|
60
|
+
if (input.data !== undefined)
|
|
61
|
+
throw new UsageError(`${operation.operationId} uploads a form; pass its fields as flags, not --data.`);
|
|
62
|
+
const form = new FormData();
|
|
63
|
+
for (const field of body.form) {
|
|
64
|
+
const value = input.form?.[field.name];
|
|
65
|
+
if (value === undefined) {
|
|
66
|
+
if (field.required)
|
|
67
|
+
throw new UsageError(`Missing --${field.flag}${field.type === "file" ? " <path>" : ""}.`);
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (typeof value === "string")
|
|
71
|
+
form.append(field.name, value);
|
|
72
|
+
else
|
|
73
|
+
form.append(field.name, value, value instanceof File ? value.name : field.name);
|
|
74
|
+
}
|
|
75
|
+
for (const name of Object.keys(input.form ?? {})) {
|
|
76
|
+
if (!body.form.some((field) => field.name === name))
|
|
77
|
+
throw new UsageError(`${operation.operationId} has no form field ${name}.`);
|
|
78
|
+
}
|
|
79
|
+
request.body = form;
|
|
80
|
+
return request;
|
|
81
|
+
}
|
|
82
|
+
for (const [name] of options) {
|
|
83
|
+
if (!body.options.some((option) => option.name === name))
|
|
84
|
+
throw new UsageError(`${operation.operationId} has no option ${name}.`);
|
|
85
|
+
}
|
|
86
|
+
if (input.data === undefined && options.length === 0) {
|
|
87
|
+
if (body.required)
|
|
88
|
+
throw new UsageError(`${operation.operationId} needs a body: --data '{…}', --data @file.json or --set field=value.`);
|
|
89
|
+
return request;
|
|
90
|
+
}
|
|
91
|
+
let data = input.data ?? {};
|
|
92
|
+
let envelopeOptions = [];
|
|
93
|
+
if (typeof data !== "object" || data === null || Array.isArray(data)) {
|
|
94
|
+
throw new UsageError("--data must be a JSON object: the record, or the whole {\"data\": …} body.");
|
|
95
|
+
}
|
|
96
|
+
// A whole body (`{"data": {…}, "on_conflict": …}`, or a saved GET answer) is accepted as is,
|
|
97
|
+
// unless the record itself has a field named `data`.
|
|
98
|
+
const keys = Object.keys(data);
|
|
99
|
+
if (keys.includes("data") && !body.fields.some((field) => field.name === "data") && keys.every((key) => key === "data" || body.options.some((option) => option.name === key))) {
|
|
100
|
+
const whole = data;
|
|
101
|
+
envelopeOptions = Object.entries(whole).filter(([key]) => key !== "data");
|
|
102
|
+
data = whole.data ?? {};
|
|
103
|
+
if (typeof data !== "object" || data === null || Array.isArray(data))
|
|
104
|
+
throw new UsageError("--data's \"data\" must be a JSON object.");
|
|
105
|
+
}
|
|
106
|
+
request.body = { data, ...Object.fromEntries(envelopeOptions), ...Object.fromEntries(options) };
|
|
107
|
+
return request;
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=request.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** The CLI's version; `tests/version.test.ts` keeps it equal to package.json's. */
|
|
2
|
+
export declare const VERSION = "0.2.0";
|
|
3
|
+
/**
|
|
4
|
+
* Sent on every request to Tracken ID and the API: the edge's WAF blocks requests without a
|
|
5
|
+
* User-Agent, and support can tell CLI traffic from the web app's.
|
|
6
|
+
*/
|
|
7
|
+
export declare function userAgent(product?: string): string;
|
|
8
|
+
export { AGENT_HEADER, agentLabel } from "@tracken/sdk";
|
|
9
|
+
/** What the CLI calls itself in `Tracken-Agent`. */
|
|
10
|
+
export declare const CLI_AGENT = "tracken-cli/0.2.0";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** The CLI's version; `tests/version.test.ts` keeps it equal to package.json's. */
|
|
2
|
+
export const VERSION = "0.2.0";
|
|
3
|
+
/**
|
|
4
|
+
* Sent on every request to Tracken ID and the API: the edge's WAF blocks requests without a
|
|
5
|
+
* User-Agent, and support can tell CLI traffic from the web app's.
|
|
6
|
+
*/
|
|
7
|
+
export function userAgent(product = "tracken-cli") {
|
|
8
|
+
return `${product}/${VERSION} (node ${process.versions.node}; ${process.platform}-${process.arch})`;
|
|
9
|
+
}
|
|
10
|
+
export { AGENT_HEADER, agentLabel } from "@tracken/sdk";
|
|
11
|
+
/** What the CLI calls itself in `Tracken-Agent`. */
|
|
12
|
+
export const CLI_AGENT = `tracken-cli/${VERSION}`;
|
|
13
|
+
//# sourceMappingURL=version.js.map
|