@wirecat/cli-base 0.111.0 → 0.112.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 +42 -0
- package/dist/codegen/discovery.js +10 -0
- package/dist/codegen/fake.d.ts +15 -2
- package/dist/codegen/fake.js +86 -5
- package/dist/codegen/index.d.ts +3 -1
- package/dist/codegen/index.js +3 -1
- package/dist/codegen/openapi.js +69 -11
- package/dist/codegen/sign-in.d.ts +22 -0
- package/dist/codegen/sign-in.js +30 -0
- package/docs/standards/kit-surface.md +12 -9
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -8,6 +8,48 @@ Versions before 0.8.0 are in the [git tags](https://github.com/WireCatLabs/cli-b
|
|
|
8
8
|
Every entry says what changed as a caller sees it, why, and what to watch for — the rules are
|
|
9
9
|
[`docs/dev/CONVENTIONS.md`](docs/dev/CONVENTIONS.md#the-changelog).
|
|
10
10
|
|
|
11
|
+
## 0.112.0 — 17.11.2026
|
|
12
|
+
|
|
13
|
+
### Added
|
|
14
|
+
|
|
15
|
+
- **`adaptOpenApi` reads an OpenAPI 3.1 field of several types.** `type: [string, number, "null"]`, as Resend's file
|
|
16
|
+
types its contact properties, becomes a union of one schema per type, nullable when the list holds `null`; each
|
|
17
|
+
member keeps the `enum` values and constraints that fit its type. Such a field stopped generation before. The types,
|
|
18
|
+
the Valibot schemas and `fakeApi` check it as any other union.
|
|
19
|
+
- **`adaptOpenApi` reads an upload sent as bytes.** A body with no JSON, multipart or form type whose schema is a binary
|
|
20
|
+
string (or none), as Petstore 3's `application/octet-stream` image upload, keeps its content type and no schema;
|
|
21
|
+
the manifest's `request.contentType` says how it travels. It stopped generation before. cli-core sends it from
|
|
22
|
+
`--body-file`.
|
|
23
|
+
- **`signInRequirements` and `signInPlace` say which sign-in a call needs and where the token goes.** The
|
|
24
|
+
requirements are the operation's own `security`, else the document's; a call whose requirements are absent or only
|
|
25
|
+
`{}` needs none. cli-core's `api` group signs by them and `fakeApi` checks by them, so a CLI's test and its real
|
|
26
|
+
calls follow one rule.
|
|
27
|
+
- **`fakeApi` checks the document's sign-in and each operation's own scheme.** `security` gives it the manifest's
|
|
28
|
+
default requirements, which an operation's own `security` (`[]` included) replaces; with `auth`, a call the
|
|
29
|
+
document leaves open no longer needs the header. `token` with `securitySchemes` checks each call by its own
|
|
30
|
+
schemes: `Bearer <token>`, an `api_key` header, or a key in the query, and none for an open call. A CLI's test
|
|
31
|
+
then drops the copy of the document's `security` onto each operation. An upload's bytes are taken as sent, under
|
|
32
|
+
the operation's content type.
|
|
33
|
+
- **`adaptOpenApi` reads three more shapes found in public specs.** An `allOf` member that holds only a description,
|
|
34
|
+
as Resend writes one beside a reference, now describes the field instead of stopping generation. A schema with only
|
|
35
|
+
`nullable: true`, as GitHub's file has, is a nullable "anything". An integer marked `unix-time` (Stripe) or
|
|
36
|
+
`unixtime` (OpenAI) is a plain integer; any other unknown integer format still stops generation. An `anyOf` or
|
|
37
|
+
`oneOf` whose members only add `required` fields to the object they sit on, as Resend's "give at least one of",
|
|
38
|
+
becomes a union of that whole object, once with each; before, each member was an empty object no body matched.
|
|
39
|
+
- **`adaptDiscovery` carries a Discovery document's OAuth.** A document with `auth.oauth2` gives the model an `oauth2`
|
|
40
|
+
scheme with its scopes, and each of its methods `security: [{ oauth2: <its scopes> }]`. Before, the manifest named
|
|
41
|
+
no sign-in, and cli-core sent the token only because a manifest without schemes got a bearer header.
|
|
42
|
+
|
|
43
|
+
### Fixed
|
|
44
|
+
|
|
45
|
+
- **`fakeApi` answers a string schema with JSON text.** A sample answer of type string, as Petstore's `loginUser`, was
|
|
46
|
+
sent unquoted, as a file is, so the CLI could not read it. A string an answer in `answers` gives is still sent as it
|
|
47
|
+
is.
|
|
48
|
+
- **`sampleOf` gives a value the generated schemas accept for a simple `pattern` and a binary string.** A pattern of
|
|
49
|
+
literals, classes and counts, as Resend's `^#[0-9A-Fa-f]{6}$`, gets a matching string (`#000000`); a binary
|
|
50
|
+
string gets `attach://xxxxxx`, which the Valibot schema asks for. Before, a CLI's generated test sent a sample its
|
|
51
|
+
own check refused.
|
|
52
|
+
|
|
11
53
|
## 0.111.0 — 17.11.2026
|
|
12
54
|
|
|
13
55
|
### Added
|
|
@@ -66,6 +66,9 @@ export const adaptDiscovery = (documents, options = {}) => {
|
|
|
66
66
|
const operations = [];
|
|
67
67
|
const metadata = { scopes: {}, omissions: [] };
|
|
68
68
|
const services = {};
|
|
69
|
+
// A document's `auth.oauth2` is Google's OAuth 2.0, the one sign-in its methods take.
|
|
70
|
+
const scopes = {};
|
|
71
|
+
let signsIn = false;
|
|
69
72
|
const fail = (problem) => {
|
|
70
73
|
throw new CodegenError([problem]);
|
|
71
74
|
};
|
|
@@ -153,6 +156,11 @@ export const adaptDiscovery = (documents, options = {}) => {
|
|
|
153
156
|
if (typeof document.rootUrl !== "string" || typeof document.servicePath !== "string")
|
|
154
157
|
fail(`invalid Discovery document: ${service} has no rootUrl or servicePath`);
|
|
155
158
|
services[service] = { rootUrl: String(document.rootUrl), servicePath: String(document.servicePath) };
|
|
159
|
+
const oauth2 = object(document.auth) && object(document.auth.oauth2) ? document.auth.oauth2 : undefined;
|
|
160
|
+
if (oauth2)
|
|
161
|
+
signsIn = true;
|
|
162
|
+
for (const [scope, value] of Object.entries(oauth2 && object(oauth2.scopes) ? oauth2.scopes : {}))
|
|
163
|
+
scopes[scope] = object(value) && typeof value.description === "string" ? value.description : "";
|
|
156
164
|
for (const [id, value] of Object.entries(document.schemas))
|
|
157
165
|
schemas.push({ id: `${service}_${id}`, schema: node(value, service, `/schemas/${id}`) });
|
|
158
166
|
const walk = (resources, path) => {
|
|
@@ -198,6 +206,7 @@ export const adaptDiscovery = (documents, options = {}) => {
|
|
|
198
206
|
};
|
|
199
207
|
}),
|
|
200
208
|
source: { location: `${service}${location}/methods/${name}` },
|
|
209
|
+
...(oauth2 ? { security: [{ oauth2: metadata.scopes[raw.id] ?? [] }] } : {}),
|
|
201
210
|
...(raw.deprecated === true ? { deprecated: true } : {}),
|
|
202
211
|
};
|
|
203
212
|
if (object(raw.request))
|
|
@@ -234,6 +243,7 @@ export const adaptDiscovery = (documents, options = {}) => {
|
|
|
234
243
|
},
|
|
235
244
|
operations: operations.sort((a, b) => a.id.localeCompare(b.id)),
|
|
236
245
|
schemas,
|
|
246
|
+
...(signsIn ? { securitySchemes: { oauth2: { type: "oauth2", flows: { authorizationCode: { scopes } } } } } : {}),
|
|
237
247
|
},
|
|
238
248
|
metadata,
|
|
239
249
|
services,
|
package/dist/codegen/fake.d.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* A name typed from memory then fails a test instead of reaching a real account.
|
|
6
6
|
*/
|
|
7
7
|
import type { ManifestOperation } from "./manifest.js";
|
|
8
|
-
import type { SchemaNode } from "./model.js";
|
|
8
|
+
import type { SchemaNode, SecurityRequirement, SecurityScheme } from "./model.js";
|
|
9
9
|
export interface FakeCall {
|
|
10
10
|
method: string;
|
|
11
11
|
url: URL;
|
|
@@ -32,11 +32,24 @@ export interface FakeApiOptions {
|
|
|
32
32
|
definitions: Readonly<Record<string, SchemaNode>>;
|
|
33
33
|
/** The server the CLI calls: `https://cloud-api.yandex.net`. */
|
|
34
34
|
baseUrl: string;
|
|
35
|
-
/**
|
|
35
|
+
/**
|
|
36
|
+
* The header and value every call to `baseUrl` must carry, unless its `security` (its own, else the document's) is
|
|
37
|
+
* empty; omit for a service without sign-in. `token` instead checks each call by its schemes.
|
|
38
|
+
*/
|
|
36
39
|
auth?: {
|
|
37
40
|
header: string;
|
|
38
41
|
value: string;
|
|
39
42
|
};
|
|
43
|
+
/**
|
|
44
|
+
* The token a call must carry where one of its security schemes puts it (`securitySchemes`): `Bearer <token>`, an
|
|
45
|
+
* `api_key` header, a key in the query. A call whose security, its own or else the document's, is absent or empty
|
|
46
|
+
* needs none, as cli-core's `api` group then sends none.
|
|
47
|
+
*/
|
|
48
|
+
token?: string;
|
|
49
|
+
/** The document's default requirements, the manifest's `security`; an operation's own `security` replaces them. */
|
|
50
|
+
security?: readonly SecurityRequirement[];
|
|
51
|
+
/** The manifest's `securitySchemes`, which `token` is checked by. */
|
|
52
|
+
securitySchemes?: Readonly<Record<string, SecurityScheme>>;
|
|
40
53
|
/** Answers by operation id. One without an answer gets a sample built from its response schema, with status 200. */
|
|
41
54
|
answers?: Readonly<Record<string, Answering>>;
|
|
42
55
|
/**
|
package/dist/codegen/fake.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { identifier } from "./names.js";
|
|
2
|
+
import { signInPlace, signInRequirements } from "./sign-in.js";
|
|
2
3
|
const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
3
4
|
const resolved = (node, definitions) => {
|
|
4
5
|
let at = node;
|
|
@@ -129,6 +130,46 @@ export const schemaProblems = (value, node, definitions, where = "body", loose =
|
|
|
129
130
|
}
|
|
130
131
|
}
|
|
131
132
|
};
|
|
133
|
+
/**
|
|
134
|
+
* A string a simple pattern accepts: anchors, literals, `[...]` classes, `\d`, `\w` and `{n}`, `{n,m}`, `+`, `*`, `?`.
|
|
135
|
+
* `undefined` for anything else, and for a sample the pattern still refuses.
|
|
136
|
+
*/
|
|
137
|
+
const patternSample = (pattern) => {
|
|
138
|
+
const atoms = [];
|
|
139
|
+
let at = pattern.startsWith("^") ? 1 : 0;
|
|
140
|
+
const end = pattern.endsWith("$") && !pattern.endsWith("\\$") ? pattern.length - 1 : pattern.length;
|
|
141
|
+
while (at < end) {
|
|
142
|
+
const char = pattern[at];
|
|
143
|
+
let atom;
|
|
144
|
+
if (char === "[") {
|
|
145
|
+
const close = pattern.indexOf("]", at + 1);
|
|
146
|
+
if (close < 0 || pattern[at + 1] === "^")
|
|
147
|
+
return undefined;
|
|
148
|
+
atom = pattern[at + 1] === "\\" ? (pattern[at + 2] === "d" ? "0" : "x") : pattern[at + 1];
|
|
149
|
+
at = close + 1;
|
|
150
|
+
}
|
|
151
|
+
else if (char === "\\") {
|
|
152
|
+
const next = pattern[at + 1] ?? "";
|
|
153
|
+
atom = next === "d" ? "0" : next === "w" ? "x" : /[^a-zA-Z0-9]/.test(next) ? next : "";
|
|
154
|
+
if (!atom)
|
|
155
|
+
return undefined;
|
|
156
|
+
at += 2;
|
|
157
|
+
}
|
|
158
|
+
else if ("()|.".includes(char))
|
|
159
|
+
return undefined;
|
|
160
|
+
else {
|
|
161
|
+
atom = char;
|
|
162
|
+
at += 1;
|
|
163
|
+
}
|
|
164
|
+
const quantifier = /^(\{(\d+)(,\d*)?\}|\+|\*|\?)/.exec(pattern.slice(at, end));
|
|
165
|
+
const times = quantifier ? (quantifier[2] !== undefined ? Number(quantifier[2]) : quantifier[0] === "+" ? 1 : 0) : 1;
|
|
166
|
+
if (quantifier)
|
|
167
|
+
at += quantifier[0].length;
|
|
168
|
+
atoms.push(atom.repeat(times));
|
|
169
|
+
}
|
|
170
|
+
const sample = atoms.join("");
|
|
171
|
+
return regexOf(pattern)?.test(sample) ? sample : undefined;
|
|
172
|
+
};
|
|
132
173
|
/** A value the schema accepts: the first enum value, the minimum, one array item, every object field. */
|
|
133
174
|
export const sampleOf = (node, definitions, seen = new Set()) => {
|
|
134
175
|
switch (node.type) {
|
|
@@ -143,8 +184,14 @@ export const sampleOf = (node, definitions, seen = new Set()) => {
|
|
|
143
184
|
const parts = objectParts(node, definitions);
|
|
144
185
|
return parts ? sampleOf({ type: "object", ...parts }, definitions, seen) : null;
|
|
145
186
|
}
|
|
146
|
-
case "string":
|
|
147
|
-
|
|
187
|
+
case "string": {
|
|
188
|
+
if (node.enum?.[0] !== undefined)
|
|
189
|
+
return node.enum[0];
|
|
190
|
+
if (node.format === "binary")
|
|
191
|
+
return "attach://xxxxxx";
|
|
192
|
+
const plain = "x".repeat(Math.max(node.minLength ?? 6, 1)).slice(0, node.maxLength ?? 64);
|
|
193
|
+
return node.pattern === undefined ? plain : (patternSample(node.pattern) ?? plain);
|
|
194
|
+
}
|
|
148
195
|
case "integer":
|
|
149
196
|
case "number":
|
|
150
197
|
return (node.type === "integer" ? node.enum?.[0] : undefined) ?? node.minimum ?? Math.min(1, node.maximum ?? 1);
|
|
@@ -199,6 +246,9 @@ const specificity = (template) => [
|
|
|
199
246
|
-template.replace(/\{[^}]+\}/g, "").length,
|
|
200
247
|
];
|
|
201
248
|
const JSON_BODY = /[/+]json\s*(;|$)/i;
|
|
249
|
+
const JSON_TYPE = { "content-type": "application/json" };
|
|
250
|
+
const FORM_BODY = /^(multipart\/form-data|application\/x-www-form-urlencoded)\s*(;|$)/i;
|
|
251
|
+
const mediaType = (type) => type?.split(";")[0]?.trim().toLowerCase();
|
|
202
252
|
const hostAnswer = (hosts, host) => hosts?.[host] ??
|
|
203
253
|
Object.entries(hosts ?? {}).find(([pattern]) => pattern.startsWith("*.") && host.endsWith(pattern.slice(1)) && host.length > pattern.length - 1)?.[1];
|
|
204
254
|
const scalar = (raw, node) => {
|
|
@@ -221,6 +271,21 @@ export const fakeApi = (options) => {
|
|
|
221
271
|
};
|
|
222
272
|
const definitions = options.definitions;
|
|
223
273
|
const problemsOf = (value, node, where) => schemaProblems(value, node, definitions, where, options.looseObjects);
|
|
274
|
+
const placesOf = (requirements) => {
|
|
275
|
+
const names = [...new Set(requirements.flatMap((one) => Object.keys(one)))];
|
|
276
|
+
const places = names.flatMap((name) => {
|
|
277
|
+
const scheme = options.securitySchemes?.[name];
|
|
278
|
+
const place = scheme && signInPlace(scheme);
|
|
279
|
+
return place ? [place] : [];
|
|
280
|
+
});
|
|
281
|
+
return { names, places };
|
|
282
|
+
};
|
|
283
|
+
const keyParameters = new Set(Object.values(options.securitySchemes ?? {}).flatMap((scheme) => {
|
|
284
|
+
const place = signInPlace(scheme);
|
|
285
|
+
return place?.in === "query" ? [place.name] : [];
|
|
286
|
+
}));
|
|
287
|
+
const carries = (request, url, place, token) => (place.in === "header" ? request.headers.get(place.name) : url.searchParams.get(place.name)) === place.value(token);
|
|
288
|
+
const leaks = (request, url, token) => [...request.headers.values()].some((value) => value.includes(token)) || url.search.includes(token);
|
|
224
289
|
const fetch = async (input, init) => {
|
|
225
290
|
const request = new Request(input, init);
|
|
226
291
|
const url = new URL(request.url);
|
|
@@ -246,6 +311,8 @@ export const fakeApi = (options) => {
|
|
|
246
311
|
return refuse(400, `${url.host} was called over ${url.protocol}`);
|
|
247
312
|
if (options.auth && request.headers.has(options.auth.header))
|
|
248
313
|
return refuse(400, `the ${options.auth.header} header was sent to ${url.host}, which is not the service`);
|
|
314
|
+
if (options.token !== undefined && leaks(request, url, options.token))
|
|
315
|
+
return refuse(400, `the token was sent to ${url.host}, which is not the service`);
|
|
249
316
|
const { status = 200, body, headers } = await answer(call);
|
|
250
317
|
return json(status, body, headers);
|
|
251
318
|
}
|
|
@@ -264,13 +331,21 @@ export const fakeApi = (options) => {
|
|
|
264
331
|
call.operation = operation.id;
|
|
265
332
|
call.path = matchPath(operation.binding.path, path) ?? {};
|
|
266
333
|
const where = `${operation.id}`;
|
|
267
|
-
const
|
|
334
|
+
const declaredSecurity = operation.security ?? options.security;
|
|
335
|
+
const open = declaredSecurity?.every((one) => Object.keys(one).length === 0) === true;
|
|
268
336
|
if (options.auth && !open && request.headers.get(options.auth.header) !== options.auth.value)
|
|
269
337
|
return refuse(401, `${where}: no ${options.auth.header} header with the expected sign-in`);
|
|
338
|
+
const requirements = signInRequirements(operation, options.security);
|
|
339
|
+
if (options.token !== undefined && requirements) {
|
|
340
|
+
const { names, places } = placesOf(requirements);
|
|
341
|
+
const token = options.token;
|
|
342
|
+
if (!places.some((place) => carries(request, url, place, token)))
|
|
343
|
+
return refuse(401, `${where}: no sign-in where its security names it (${names.join(" or ")})`);
|
|
344
|
+
}
|
|
270
345
|
const problems = [];
|
|
271
346
|
const declared = (location) => operation.parameters.filter((one) => one.in === location);
|
|
272
347
|
for (const name of url.searchParams.keys())
|
|
273
|
-
if (!declared("query").some((one) => one.name === name))
|
|
348
|
+
if (!declared("query").some((one) => one.name === name) && !keyParameters.has(name))
|
|
274
349
|
problems.push(`${where}: no query parameter "${name}"`);
|
|
275
350
|
for (const parameter of [...declared("query"), ...declared("path")]) {
|
|
276
351
|
const values = parameter.in === "query" ? url.searchParams.getAll(parameter.name) : [call.path[parameter.name]];
|
|
@@ -292,6 +367,11 @@ export const fakeApi = (options) => {
|
|
|
292
367
|
if (text !== "") {
|
|
293
368
|
if (!operation.request)
|
|
294
369
|
problems.push(`${where}: takes no body`);
|
|
370
|
+
else if (contentType && !JSON_BODY.test(contentType) && !FORM_BODY.test(contentType)) {
|
|
371
|
+
const sent = mediaType(request.headers.get("content-type"));
|
|
372
|
+
if (sent !== mediaType(contentType))
|
|
373
|
+
problems.push(`${where}: the body is sent as ${sent ?? "no content type"}, not ${contentType}`);
|
|
374
|
+
}
|
|
295
375
|
else if (contentType && !JSON_BODY.test(contentType)) {
|
|
296
376
|
try {
|
|
297
377
|
const form = await new Response(bytes, { headers: request.headers }).formData();
|
|
@@ -326,8 +406,9 @@ export const fakeApi = (options) => {
|
|
|
326
406
|
}
|
|
327
407
|
const response = operation.response?.schema ? definitions[operation.response.schema] : undefined;
|
|
328
408
|
const answering = options.answers?.[operation.id];
|
|
409
|
+
// A sample of a string answer is JSON text, never the bytes of a download.
|
|
329
410
|
if (!answering)
|
|
330
|
-
return json(200, response ? sampleOf(response, definitions) : undefined);
|
|
411
|
+
return json(200, response ? JSON.stringify(sampleOf(response, definitions)) : undefined, JSON_TYPE);
|
|
331
412
|
const { status = 200, body, headers } = await answering(call);
|
|
332
413
|
// A scenario that answers off the schema tests a service that does not exist.
|
|
333
414
|
if (status >= 200 && status < 300 && response && body !== undefined) {
|
package/dist/codegen/index.d.ts
CHANGED
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
* files with (`identifier`, `requestSchema`), and format-neutral: it parses no file format and
|
|
5
5
|
* adds no runtime dependency. `adaptOpenApi` reads an OpenAPI document and `adaptDiscovery` Google Discovery
|
|
6
6
|
* documents the consumer has parsed; `upgradeToOpenApi3` brings Swagger 1.2 and 2.0 to OpenAPI 3 through two optional
|
|
7
|
-
* peer dependencies. `fakeApi` serves the generated manifest as a strict fake for a CLI's tests
|
|
7
|
+
* peer dependencies. `fakeApi` serves the generated manifest as a strict fake for a CLI's tests, and `signInRequirements` with
|
|
8
|
+
* `signInPlace` say which sign-in a call needs and where its token goes. An adapter from
|
|
8
9
|
* Postman or anything else lives in the consumer.
|
|
9
10
|
*/
|
|
10
11
|
export { adaptDiscovery, type DiscoveryMetadata, type DiscoveryOptions, type DiscoveryProvenance, type DiscoveryResult, type DiscoveryService, readDiscovery, type SyncDiscoveryOptions, syncDiscovery, } from "./discovery.js";
|
|
@@ -18,6 +19,7 @@ export { adaptOpenApi, OpenApiError } from "./openapi.js";
|
|
|
18
19
|
export { detectPagination } from "./pagination.js";
|
|
19
20
|
export { type PatchedSpec, type PatchSpecOptions, patchSpec, type SpecPatch } from "./patch.js";
|
|
20
21
|
export { type AuthOverride, applyNames, applyOverrides, applyPagination, applyPermissionRule, CodegenError, type CommandNames, type GenerateOptions, generate, type PermissionRule, READING_POST_REASON, validateModel, type WriteOptions, writeArtifacts, } from "./pipeline.js";
|
|
22
|
+
export { type SignInPlace, signInPlace, signInRequirements } from "./sign-in.js";
|
|
21
23
|
export { API_SKILL_GROUPS, API_SKILL_INDEX, type ApiSkillOptions, apiSkillFiles } from "./skill.js";
|
|
22
24
|
export { upgradeToOpenApi3 } from "./swagger.js";
|
|
23
25
|
export { typesGenerator } from "./typescript.js";
|
package/dist/codegen/index.js
CHANGED
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
* files with (`identifier`, `requestSchema`), and format-neutral: it parses no file format and
|
|
5
5
|
* adds no runtime dependency. `adaptOpenApi` reads an OpenAPI document and `adaptDiscovery` Google Discovery
|
|
6
6
|
* documents the consumer has parsed; `upgradeToOpenApi3` brings Swagger 1.2 and 2.0 to OpenAPI 3 through two optional
|
|
7
|
-
* peer dependencies. `fakeApi` serves the generated manifest as a strict fake for a CLI's tests
|
|
7
|
+
* peer dependencies. `fakeApi` serves the generated manifest as a strict fake for a CLI's tests, and `signInRequirements` with
|
|
8
|
+
* `signInPlace` say which sign-in a call needs and where its token goes. An adapter from
|
|
8
9
|
* Postman or anything else lives in the consumer.
|
|
9
10
|
*/
|
|
10
11
|
export { adaptDiscovery, readDiscovery, syncDiscovery, } from "./discovery.js";
|
|
@@ -17,6 +18,7 @@ export { adaptOpenApi, OpenApiError } from "./openapi.js";
|
|
|
17
18
|
export { detectPagination } from "./pagination.js";
|
|
18
19
|
export { patchSpec } from "./patch.js";
|
|
19
20
|
export { applyNames, applyOverrides, applyPagination, applyPermissionRule, CodegenError, generate, READING_POST_REASON, validateModel, writeArtifacts, } from "./pipeline.js";
|
|
21
|
+
export { signInPlace, signInRequirements } from "./sign-in.js";
|
|
20
22
|
export { API_SKILL_GROUPS, API_SKILL_INDEX, apiSkillFiles } from "./skill.js";
|
|
21
23
|
export { upgradeToOpenApi3 } from "./swagger.js";
|
|
22
24
|
export { typesGenerator } from "./typescript.js";
|
package/dist/codegen/openapi.js
CHANGED
|
@@ -32,6 +32,10 @@ const KNOWN = new Set([
|
|
|
32
32
|
"$ref",
|
|
33
33
|
"discriminator",
|
|
34
34
|
]);
|
|
35
|
+
/** Seconds since 1970 as Stripe (`unix-time`) and OpenAI (`unixtime`) mark them: a plain integer. */
|
|
36
|
+
const SECONDS = new Set(["unix-time", "unixtime"]);
|
|
37
|
+
/** What a type list's union keeps for itself instead of repeating it on each member. */
|
|
38
|
+
const TYPE_LIST_OWN = new Set(["description", "default", "deprecated", "nullable", "const", "enum", "title"]);
|
|
35
39
|
const PRIMITIVE_CONSTRAINTS = ["pattern", "minLength", "maxLength", "minimum", "maximum", "enum"];
|
|
36
40
|
/** What the HTTP client sets itself; as flags they would only invite a second, wrong value. */
|
|
37
41
|
const TRANSPORT_HEADERS = new Set(["authorization", "user-agent", "content-type", "accept"]);
|
|
@@ -104,6 +108,13 @@ const strings = (source, keys) => Object.fromEntries(keys.flatMap((key) => (type
|
|
|
104
108
|
const isNullOnly = (source) => isObject(source) &&
|
|
105
109
|
(source.type === "null" ||
|
|
106
110
|
(Array.isArray(source.type) && source.type.length > 0 && source.type.every((t) => t === "null")));
|
|
111
|
+
const fits = (value, type) => type === "integer"
|
|
112
|
+
? Number.isInteger(value)
|
|
113
|
+
: type === "array"
|
|
114
|
+
? Array.isArray(value)
|
|
115
|
+
: type === "object"
|
|
116
|
+
? isObject(value)
|
|
117
|
+
: typeof value === type;
|
|
107
118
|
const pick = (source, keys) => Object.fromEntries(keys.map((key) => [key, source[key]]));
|
|
108
119
|
const typeOfConst = (value) => typeof value === "string"
|
|
109
120
|
? "string"
|
|
@@ -130,6 +141,13 @@ const typedFromExample = (schema) => {
|
|
|
130
141
|
};
|
|
131
142
|
return isObject(schema) && !marked(schema);
|
|
132
143
|
};
|
|
144
|
+
const isRequiredOnly = (member) => isObject(member) && Array.isArray(member.required) && Object.keys(member).every((key) => key === "required");
|
|
145
|
+
const isNote = (member) => isObject(member) && Object.keys(member).every((keyword) => IGNORED.has(keyword) || keyword === "description");
|
|
146
|
+
/** A body schema that says "any bytes": none, `{}`, or a binary string, as 3.0 writes it or 3.1 does. */
|
|
147
|
+
const isBytes = (schema) => schema === undefined ||
|
|
148
|
+
(isObject(schema) &&
|
|
149
|
+
(Object.keys(schema).length === 0 ||
|
|
150
|
+
(schema.type === "string" && (schema.format === "binary" || schema.contentMediaType !== undefined))));
|
|
133
151
|
class Adapter {
|
|
134
152
|
problems = [];
|
|
135
153
|
document;
|
|
@@ -173,15 +191,12 @@ class Adapter {
|
|
|
173
191
|
return common;
|
|
174
192
|
}
|
|
175
193
|
/** OpenAPI 3.1 writes `nullable` as `type: [T, "null"]`. */
|
|
176
|
-
#type(source, common
|
|
194
|
+
#type(source, common) {
|
|
177
195
|
if (Array.isArray(source.type)) {
|
|
178
196
|
const types = source.type.filter((type) => type !== "null");
|
|
179
197
|
if (types.length !== source.type.length)
|
|
180
198
|
common.nullable = true;
|
|
181
|
-
|
|
182
|
-
return types[0];
|
|
183
|
-
this.problems.push(`${where}: a schema of several types ${JSON.stringify(source.type)}`);
|
|
184
|
-
return null;
|
|
199
|
+
return types[0];
|
|
185
200
|
}
|
|
186
201
|
if (source.type !== undefined)
|
|
187
202
|
return source.type;
|
|
@@ -225,9 +240,23 @@ class Adapter {
|
|
|
225
240
|
if (keyword) {
|
|
226
241
|
if (Array.isArray(source.type) && source.type.includes("null"))
|
|
227
242
|
common.nullable = true;
|
|
228
|
-
|
|
243
|
+
const members = source[keyword];
|
|
244
|
+
// Resend's "give at least one of": members that only add required fields to the object they sit on.
|
|
245
|
+
if (isObject(source.properties) && members.every(isRequiredOnly)) {
|
|
246
|
+
const { [keyword]: _, ...object } = source;
|
|
247
|
+
const required = Array.isArray(source.required) ? source.required : [];
|
|
248
|
+
const whole = members.map((member) => ({
|
|
249
|
+
...object,
|
|
250
|
+
required: [...required, ...member.required],
|
|
251
|
+
}));
|
|
252
|
+
return this.#union(whole, keyword, common, where);
|
|
253
|
+
}
|
|
254
|
+
return this.#union(members, keyword, common, where);
|
|
229
255
|
}
|
|
230
|
-
const
|
|
256
|
+
const types = Array.isArray(source.type) ? source.type.filter((type) => type !== "null") : [];
|
|
257
|
+
if (types.length > 1)
|
|
258
|
+
return this.#typeList(source, types, common, where);
|
|
259
|
+
const type = this.#type(source, common);
|
|
231
260
|
const values = source.const !== undefined ? [source.const] : Array.isArray(source.enum) ? source.enum : undefined;
|
|
232
261
|
switch (type) {
|
|
233
262
|
case "string": {
|
|
@@ -248,7 +277,7 @@ class Adapter {
|
|
|
248
277
|
const node = { ...common, type: "integer" };
|
|
249
278
|
if (source.format === "int32" || source.format === "int64")
|
|
250
279
|
node.format = source.format;
|
|
251
|
-
else if (source.format !== undefined)
|
|
280
|
+
else if (source.format !== undefined && !SECONDS.has(String(source.format)))
|
|
252
281
|
this.problems.push(`${where}: unsupported integer format "${source.format}"`);
|
|
253
282
|
if (typeof source.minimum === "number")
|
|
254
283
|
node.minimum = source.minimum;
|
|
@@ -289,7 +318,7 @@ class Adapter {
|
|
|
289
318
|
case null:
|
|
290
319
|
return { type: "unknown" };
|
|
291
320
|
case undefined:
|
|
292
|
-
if (Object.keys(source).every((keyword) => IGNORED.has(keyword) || keyword === "description")) {
|
|
321
|
+
if (Object.keys(source).every((keyword) => IGNORED.has(keyword) || keyword === "description" || keyword === "nullable")) {
|
|
293
322
|
return { ...common, type: "unknown" };
|
|
294
323
|
}
|
|
295
324
|
this.problems.push(`${where}: a schema with no type`);
|
|
@@ -299,6 +328,23 @@ class Adapter {
|
|
|
299
328
|
return { type: "unknown" };
|
|
300
329
|
}
|
|
301
330
|
}
|
|
331
|
+
/**
|
|
332
|
+
* OpenAPI 3.1's `type: [string, number, "null"]`: a union of one schema per type, each keeping the constraints and
|
|
333
|
+
* enum values that fit it; a type no enum value fits is left out.
|
|
334
|
+
*/
|
|
335
|
+
#typeList(source, types, common, where) {
|
|
336
|
+
if (source.type.includes("null"))
|
|
337
|
+
common.nullable = true;
|
|
338
|
+
const values = source.const !== undefined ? [source.const] : Array.isArray(source.enum) ? source.enum : undefined;
|
|
339
|
+
const rest = Object.fromEntries(Object.entries(source).filter(([key]) => !TYPE_LIST_OWN.has(key)));
|
|
340
|
+
const of = types.flatMap((type) => {
|
|
341
|
+
const fitting = values?.filter((value) => fits(value, type));
|
|
342
|
+
if (fitting?.length === 0)
|
|
343
|
+
return [];
|
|
344
|
+
return [this.schema({ ...rest, type, ...(fitting ? { enum: fitting } : {}) }, where)];
|
|
345
|
+
});
|
|
346
|
+
return { ...common, type: "union", of };
|
|
347
|
+
}
|
|
302
348
|
#object(source, common, where) {
|
|
303
349
|
const properties = {};
|
|
304
350
|
for (const [name, property] of Object.entries(isObject(source.properties) ? source.properties : {})) {
|
|
@@ -380,6 +426,14 @@ class Adapter {
|
|
|
380
426
|
}
|
|
381
427
|
}
|
|
382
428
|
#allOf(source, members, common, where) {
|
|
429
|
+
// A member with only a description, as Resend writes one beside a reference, describes the field and adds nothing.
|
|
430
|
+
const notes = members.filter(isNote);
|
|
431
|
+
if (notes.length > 0 && notes.length < members.length) {
|
|
432
|
+
const [note] = notes.flatMap((one) => typeof one.description === "string" && one.description.trim() ? [one] : []);
|
|
433
|
+
if (note && !common.description)
|
|
434
|
+
common.description = note.description;
|
|
435
|
+
return this.#allOf(source, members.filter((one) => !isNote(one)), common, where);
|
|
436
|
+
}
|
|
383
437
|
const distributed = this.#distribute(members, common, where);
|
|
384
438
|
if (distributed)
|
|
385
439
|
return distributed;
|
|
@@ -448,7 +502,7 @@ class Adapter {
|
|
|
448
502
|
}
|
|
449
503
|
/**
|
|
450
504
|
* JSON when offered, unless a multipart body carries a file, which JSON cannot; then multipart,
|
|
451
|
-
* then a form
|
|
505
|
+
* then a form, then bytes (a type whose schema is a binary string). Anything else refuses the operation.
|
|
452
506
|
*/
|
|
453
507
|
#requestBody(source, where) {
|
|
454
508
|
const content = isObject(source.content) ? source.content : {};
|
|
@@ -460,8 +514,12 @@ class Adapter {
|
|
|
460
514
|
const body = { confidence: "contract", required: source.required === true };
|
|
461
515
|
if (offered.length === 0)
|
|
462
516
|
return body;
|
|
517
|
+
// An upload sent as the bytes themselves: `application/octet-stream`, an image type.
|
|
518
|
+
const raw = type ? undefined : offered.find((candidate) => isBytes(schemaOf(candidate)));
|
|
519
|
+
if (raw)
|
|
520
|
+
return { ...body, contentType: raw };
|
|
463
521
|
if (!type) {
|
|
464
|
-
this.problems.push(`${where}: a body must be JSON, multipart
|
|
522
|
+
this.problems.push(`${where}: a body must be JSON, multipart, a form or bytes, found ${offered.join(", ")}`);
|
|
465
523
|
return body;
|
|
466
524
|
}
|
|
467
525
|
// Only a charset may be dropped: `application/merge-patch+json` must travel as itself.
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which sign-in one call of a generated manifest needs and where its token goes. cli-core's `api` group signs requests
|
|
3
|
+
* by this and `fakeApi` checks them by it, so a CLI's test and its real calls cannot disagree.
|
|
4
|
+
*/
|
|
5
|
+
import type { SecurityRequirement, SecurityScheme } from "./model.js";
|
|
6
|
+
/** Where one scheme carries a token, and what the header or query parameter then holds. */
|
|
7
|
+
export interface SignInPlace {
|
|
8
|
+
in: "header" | "query";
|
|
9
|
+
/** A header name in lower case, or a query parameter as the spec spells it. */
|
|
10
|
+
name: string;
|
|
11
|
+
value: (token: string) => string;
|
|
12
|
+
}
|
|
13
|
+
/** `undefined` for a scheme a token cannot be sent by: basic, digest, a key in a cookie, mutual TLS. */
|
|
14
|
+
export declare const signInPlace: (scheme: SecurityScheme) => SignInPlace | undefined;
|
|
15
|
+
/**
|
|
16
|
+
* The requirements one call signs in by: the operation's own `security`, else the document's. `undefined` when it needs
|
|
17
|
+
* no sign-in: neither says anything, as in a spec without security, or every requirement is `{}` (`security: []`
|
|
18
|
+
* included). An optional sign-in (`[{}, { oauth: [] }]`) still signs in.
|
|
19
|
+
*/
|
|
20
|
+
export declare const signInRequirements: (operation: {
|
|
21
|
+
security?: readonly SecurityRequirement[];
|
|
22
|
+
}, document: readonly SecurityRequirement[] | undefined) => readonly SecurityRequirement[] | undefined;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const BEARER = { in: "header", name: "authorization", value: (token) => `Bearer ${token}` };
|
|
2
|
+
/** `undefined` for a scheme a token cannot be sent by: basic, digest, a key in a cookie, mutual TLS. */
|
|
3
|
+
export const signInPlace = (scheme) => {
|
|
4
|
+
if (scheme.type === "apiKey") {
|
|
5
|
+
if (!scheme.name || (scheme.in !== "header" && scheme.in !== "query"))
|
|
6
|
+
return undefined;
|
|
7
|
+
return {
|
|
8
|
+
in: scheme.in,
|
|
9
|
+
name: scheme.in === "header" ? scheme.name.toLowerCase() : scheme.name,
|
|
10
|
+
value: (token) => token,
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
if (scheme.type === "http") {
|
|
14
|
+
if (!scheme.scheme || /^bearer$/i.test(scheme.scheme))
|
|
15
|
+
return BEARER;
|
|
16
|
+
if (/^(basic|digest)$/i.test(scheme.scheme))
|
|
17
|
+
return undefined;
|
|
18
|
+
return { in: "header", name: "authorization", value: (token) => `${scheme.scheme} ${token}` };
|
|
19
|
+
}
|
|
20
|
+
return scheme.type === "oauth2" || scheme.type === "openIdConnect" ? BEARER : undefined;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* The requirements one call signs in by: the operation's own `security`, else the document's. `undefined` when it needs
|
|
24
|
+
* no sign-in: neither says anything, as in a spec without security, or every requirement is `{}` (`security: []`
|
|
25
|
+
* included). An optional sign-in (`[{}, { oauth: [] }]`) still signs in.
|
|
26
|
+
*/
|
|
27
|
+
export const signInRequirements = (operation, document) => {
|
|
28
|
+
const requirements = operation.security ?? document;
|
|
29
|
+
return requirements?.some((one) => Object.keys(one).length > 0) ? requirements : undefined;
|
|
30
|
+
};
|
|
@@ -81,7 +81,7 @@ it changed, for the CLI's provenance.
|
|
|
81
81
|
|
|
82
82
|
From the source the generator reads:
|
|
83
83
|
|
|
84
|
-
- **Operations**: method and path, parameters, the request body (JSON, multipart or
|
|
84
|
+
- **Operations**: method and path, parameters, the request body (JSON, multipart, a form, or bytes for an upload), and the first 2xx answer
|
|
85
85
|
with a JSON body (`200`, `201` as HubSpot creates, `202`, `207`, or `2XX`). `application/hal+json`,
|
|
86
86
|
`application/json; charset=utf-8` and other JSON types count as JSON.
|
|
87
87
|
- **Effect**: GET and HEAD read, POST, PUT and PATCH write, DELETE is destructive.
|
|
@@ -130,14 +130,17 @@ From the source the generator reads:
|
|
|
130
130
|
|
|
131
131
|
### A strict fake for tests
|
|
132
132
|
|
|
133
|
-
`fakeApi({ operations, definitions, baseUrl,
|
|
134
|
-
a `fetch` for a CLI's tests. It refuses, with a 4xx and a line in `refusals`, an unknown
|
|
135
|
-
mistyped query parameter (a repeated one is a list), a missing required one, a body field
|
|
136
|
-
form or multipart),
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
133
|
+
`fakeApi({ operations, definitions, baseUrl, security, securitySchemes, token, answers, hosts })` serves the generated
|
|
134
|
+
manifest and definitions as a `fetch` for a CLI's tests. It refuses, with a 4xx and a line in `refusals`, an unknown
|
|
135
|
+
path or method, an unknown or mistyped query parameter (a repeated one is a list), a missing required one, a body field
|
|
136
|
+
the schema lacks (JSON, a form or multipart), an upload under another content type, and a call without its sign-in.
|
|
137
|
+
`token` is checked where the call's schemes put it (`Bearer <token>`, an `api_key` header, a key in the query), by
|
|
138
|
+
`signInRequirements`: the operation's own `security`, else the document's `security`, and none when neither names
|
|
139
|
+
one, as cli-core then sends none. The older `auth: { header, value }` is one header every call needs, unless its
|
|
140
|
+
`security` is empty. An operation answers a sample built from its response schema, as JSON, or what `answers` gives
|
|
141
|
+
for it, checked against that schema: an answer off the schema throws. A string, bytes or a stream an answer gives is
|
|
142
|
+
sent as it is, for a file. `hosts` answers the other hosts a link points at (`downloader.disk.yandex.ru`,
|
|
143
|
+
`*.disk.yandex.net`), and refuses a call there over another scheme or with the token. A CLI writes only its scenarios. `looseObjects` lets unknown fields through, for a
|
|
141
144
|
source whose subtypes the definitions cannot tell apart.
|
|
142
145
|
|
|
143
146
|
### The API part of the agent skill
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wirecat/cli-base",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.112.0",
|
|
4
4
|
"description": "The parts every command line tool needs: output modes, terminal rendering, error model, exit codes, credentials, clocks",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Viacheslav Ptsarev",
|