@wirecat/cli-base 0.106.0 → 0.108.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 +125 -0
- package/README.md +2 -1
- package/dist/codegen/fake.d.ts +64 -0
- package/dist/codegen/fake.js +341 -0
- package/dist/codegen/index.d.ts +11 -4
- package/dist/codegen/index.js +10 -3
- package/dist/codegen/manifest.d.ts +4 -1
- package/dist/codegen/manifest.js +69 -2
- package/dist/codegen/merge.d.ts +37 -0
- package/dist/codegen/merge.js +257 -0
- package/dist/codegen/model.d.ts +94 -0
- package/dist/codegen/openapi.d.ts +5 -1
- package/dist/codegen/openapi.js +102 -10
- package/dist/codegen/pagination.d.ts +14 -0
- package/dist/codegen/pagination.js +129 -0
- package/dist/codegen/patch.d.ts +28 -0
- package/dist/codegen/patch.js +75 -0
- package/dist/codegen/pipeline.d.ts +52 -1
- package/dist/codegen/pipeline.js +198 -4
- package/dist/codegen/swagger.d.ts +7 -0
- package/dist/codegen/swagger.js +39 -0
- package/dist/credentials.d.ts +2 -2
- package/dist/credentials.js +17 -3
- package/dist/dev/conformance.d.ts +0 -2
- package/dist/dev/conformance.js +3 -9
- package/dist/dev/main.js +11 -3
- package/dist/dev/ready.d.ts +30 -0
- package/dist/dev/ready.js +249 -0
- package/dist/dev/slow-tests.d.ts +6 -1
- package/dist/dev/slow-tests.js +14 -2
- package/dist/http/fetch.js +21 -1
- package/dist/standards/audit.js +2 -1
- package/dist/standards/errors.js +6 -0
- package/dist/standards/index.d.ts +1 -0
- package/dist/standards/index.js +1 -0
- package/dist/standards/monthly.d.ts +5 -0
- package/dist/standards/monthly.js +12 -0
- package/docs/standards/README.md +1 -1
- package/docs/standards/ci.md +4 -2
- package/docs/standards/kit-surface.md +138 -0
- package/docs/standards/new-cli.md +14 -7
- package/docs/standards/testing.md +22 -1
- package/package.json +11 -1
package/dist/codegen/manifest.js
CHANGED
|
@@ -3,13 +3,16 @@ import { SchemaTree } from "./tree.js";
|
|
|
3
3
|
const withoutUndefined = (value) => Object.fromEntries(Object.entries(value).filter(([, field]) => field !== undefined));
|
|
4
4
|
const toManifest = (operation, tree) => withoutUndefined({
|
|
5
5
|
id: operation.id,
|
|
6
|
-
command: kebab(operation.id),
|
|
6
|
+
command: operation.command ?? kebab(operation.id),
|
|
7
7
|
binding: operation.binding,
|
|
8
8
|
effect: operation.effect,
|
|
9
9
|
summary: operation.summary,
|
|
10
10
|
description: operation.description,
|
|
11
11
|
tags: operation.tags,
|
|
12
12
|
deprecated: operation.deprecated,
|
|
13
|
+
security: operation.security,
|
|
14
|
+
permissions: operation.permissions,
|
|
15
|
+
pagination: operation.pagination,
|
|
13
16
|
parameters: operation.parameters.map((parameter) => withoutUndefined({ ...parameter })),
|
|
14
17
|
request: operation.requestBody
|
|
15
18
|
? withoutUndefined({
|
|
@@ -30,10 +33,21 @@ const toManifest = (operation, tree) => withoutUndefined({
|
|
|
30
33
|
export const manifestGenerator = (options) => (model) => {
|
|
31
34
|
const tree = new SchemaTree(model);
|
|
32
35
|
const operations = model.operations.map((operation) => toManifest(operation, tree));
|
|
36
|
+
// Only what the source has, so a manifest from a source without servers or schemes does not change.
|
|
37
|
+
const extras = [
|
|
38
|
+
["servers", "ApiServer", "readonly ApiServer[]", model.servers],
|
|
39
|
+
["securitySchemes", "SecurityScheme", "Readonly<Record<string, SecurityScheme>>", model.securitySchemes],
|
|
40
|
+
["security", "SecurityRequirement", "readonly SecurityRequirement[]", model.security],
|
|
41
|
+
].filter(([, , , value]) => value !== undefined);
|
|
42
|
+
const types = ["ManifestOperation", ...extras.map(([, type]) => type)].sort();
|
|
33
43
|
const content = [
|
|
34
|
-
`import type {
|
|
44
|
+
`import type { ${types.join(", ")} } from "${options.coreImport ?? "@wirecat/cli-base/codegen"}"`,
|
|
35
45
|
"",
|
|
36
46
|
`export const operations: readonly ManifestOperation[] = ${JSON.stringify(operations, null, 2)}`,
|
|
47
|
+
...extras.flatMap(([name, , type, value]) => [
|
|
48
|
+
"",
|
|
49
|
+
`export const ${name}: ${type} = ${JSON.stringify(value, null, 2)}`,
|
|
50
|
+
]),
|
|
37
51
|
"",
|
|
38
52
|
].join("\n");
|
|
39
53
|
return [{ path: options.path, content }];
|
|
@@ -59,6 +73,10 @@ export const coverageGenerator = (options) => (model) => {
|
|
|
59
73
|
return `| \`${entry.id}\` | \`${binding}\` | \`${options.command(entry)}\` | ${entry.effect} | ${entry.request?.confidence ?? "—"} | ${status} | ${cell(entry.summary)} |`;
|
|
60
74
|
});
|
|
61
75
|
const version = model.source.version ? ` ${model.source.version}` : "";
|
|
76
|
+
const reasoned = model.operations.filter((operation) => operation.effectReason);
|
|
77
|
+
const paged = model.operations.filter((operation) => operation.pagination);
|
|
78
|
+
const guarded = model.operations.flatMap((operation) => (operation.permissions ?? []).map((permission) => ({ id: operation.id, permission })));
|
|
79
|
+
const when = ({ body, query }) => [...(body ?? []).map((flag) => `body \`${flag}\``), ...(query ?? []).map((flag) => `query \`${flag}\``)].join(" or ");
|
|
62
80
|
const content = [
|
|
63
81
|
`# ${options.title}`,
|
|
64
82
|
"",
|
|
@@ -67,6 +85,55 @@ export const coverageGenerator = (options) => (model) => {
|
|
|
67
85
|
"| Operation | Call | Command | Effect | Request source | Status | Summary |",
|
|
68
86
|
"|---|---|---|---|---|---|---|",
|
|
69
87
|
...rows,
|
|
88
|
+
...(reasoned.length > 0
|
|
89
|
+
? [
|
|
90
|
+
"",
|
|
91
|
+
"## Effects not taken from the HTTP method",
|
|
92
|
+
"",
|
|
93
|
+
"| Operation | Effect | Why |",
|
|
94
|
+
"|---|---|---|",
|
|
95
|
+
...reasoned.map((one) => `| \`${one.id}\` | ${one.effect} | ${cell(one.effectReason)} |`),
|
|
96
|
+
]
|
|
97
|
+
: []),
|
|
98
|
+
...(paged.length > 0
|
|
99
|
+
? [
|
|
100
|
+
"",
|
|
101
|
+
"## Pagination",
|
|
102
|
+
"",
|
|
103
|
+
"| Operation | Style | Request | Answer | Source |",
|
|
104
|
+
"|---|---|---|---|---|",
|
|
105
|
+
...paged.map(({ id, pagination, paginationSource }) => {
|
|
106
|
+
const page = pagination;
|
|
107
|
+
const request = [
|
|
108
|
+
"cursor" in page ? page.cursor : undefined,
|
|
109
|
+
"offset" in page ? page.offset : undefined,
|
|
110
|
+
"page" in page ? page.page : undefined,
|
|
111
|
+
"limit" in page ? page.limit : undefined,
|
|
112
|
+
].flatMap((one) => (one ? [`${one.in} \`${one.name}\``] : []));
|
|
113
|
+
const answer = [
|
|
114
|
+
"next" in page ? `next \`${page.next}\`` : undefined,
|
|
115
|
+
page.items ? `items \`${page.items}\`` : undefined,
|
|
116
|
+
"hasMore" in page && page.hasMore ? `more \`${page.hasMore}\`` : undefined,
|
|
117
|
+
"total" in page && page.total ? `total \`${page.total}\`` : undefined,
|
|
118
|
+
].filter(Boolean);
|
|
119
|
+
return `| \`${id}\` | ${page.style} | ${request.join(", ") || "—"} | ${answer.join(", ")} | ${cell(paginationSource)} |`;
|
|
120
|
+
}),
|
|
121
|
+
]
|
|
122
|
+
: []),
|
|
123
|
+
...(model.securityReason ? ["", "## Sign-in", "", `Not from the source: ${cell(model.securityReason)}`] : []),
|
|
124
|
+
...(guarded.length > 0
|
|
125
|
+
? [
|
|
126
|
+
"",
|
|
127
|
+
"## Permission keys",
|
|
128
|
+
"",
|
|
129
|
+
"| Operation | Key | When |",
|
|
130
|
+
"|---|---|---|",
|
|
131
|
+
...guarded.map(({ id, permission }) => {
|
|
132
|
+
const flags = when(permission);
|
|
133
|
+
return `| \`${id}\` | \`${permission.key}\` | ${flags ? `${flags} is true` : "always"} |`;
|
|
134
|
+
}),
|
|
135
|
+
]
|
|
136
|
+
: []),
|
|
70
137
|
"",
|
|
71
138
|
].join("\n");
|
|
72
139
|
return [{ path: options.path, content }];
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
type Json = Record<string, unknown>;
|
|
2
|
+
export interface SpecSource {
|
|
3
|
+
/** Short and stable: `contacts`, `associations-4`. Its PascalCase form prefixes what this source renames. */
|
|
4
|
+
name: string;
|
|
5
|
+
/** The spec as downloaded, JSON text, so the provenance hash is the file's; or the parsed document. */
|
|
6
|
+
document: unknown;
|
|
7
|
+
/** Where it was downloaded from, for the provenance. */
|
|
8
|
+
url?: string;
|
|
9
|
+
}
|
|
10
|
+
export interface MergedSource {
|
|
11
|
+
name: string;
|
|
12
|
+
url?: string;
|
|
13
|
+
/** Of the text when `document` was text, as `sha256sum` gives for the file; else of the document as JSON. */
|
|
14
|
+
sha256: string;
|
|
15
|
+
title?: string;
|
|
16
|
+
version?: string;
|
|
17
|
+
operations: number;
|
|
18
|
+
}
|
|
19
|
+
export interface MergeReport {
|
|
20
|
+
sources: MergedSource[];
|
|
21
|
+
/** `schemas/Paging`: same shape in several sources, kept once. */
|
|
22
|
+
merged: string[];
|
|
23
|
+
/** `schemas/BatchResponse → AssociationsBatchResponse (associations)`: a real difference, renamed. */
|
|
24
|
+
renamed: string[];
|
|
25
|
+
/** `/crm/v3/associations/{a}/{b} (deals)`: a path an earlier source already brought with the same operations. */
|
|
26
|
+
sharedPaths: string[];
|
|
27
|
+
}
|
|
28
|
+
export interface MergeOptions {
|
|
29
|
+
title: string;
|
|
30
|
+
version: string;
|
|
31
|
+
}
|
|
32
|
+
/** Throws `OpenApiError` with every conflict at once: a path, an operation id or a sign-in two sources disagree on. */
|
|
33
|
+
export declare const mergeOpenApi: (sources: readonly SpecSource[], options: MergeOptions) => {
|
|
34
|
+
document: Json;
|
|
35
|
+
report: MergeReport;
|
|
36
|
+
};
|
|
37
|
+
export {};
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Several OpenAPI 3 documents of one service → one, for a service that publishes a spec per object (HubSpot: 127).
|
|
3
|
+
* Components that share a name and differ only in their text are merged; a real difference keeps both, the later one
|
|
4
|
+
* under its source's prefix, with every reference inside that source rewritten. A path two sources share must hold
|
|
5
|
+
* the same operations. What was merged, renamed and skipped is returned for the CLI's provenance.
|
|
6
|
+
*/
|
|
7
|
+
import { createHash } from "node:crypto";
|
|
8
|
+
import { isDeepStrictEqual } from "node:util";
|
|
9
|
+
import { isObject, OpenApiError } from "./openapi.js";
|
|
10
|
+
const METHODS = ["get", "put", "post", "delete", "options", "head", "patch", "trace"];
|
|
11
|
+
/** What two copies of one component may differ in and still be one. */
|
|
12
|
+
const TEXT = new Set(["description", "summary", "example", "examples", "title", "externalDocs"]);
|
|
13
|
+
/** Under these keys a `description` or `title` is a field's name, not text. */
|
|
14
|
+
const NAMED = new Set(["properties", "patternProperties", "$defs", "definitions"]);
|
|
15
|
+
const shape = (node, named = false) => Array.isArray(node)
|
|
16
|
+
? node.map((item) => shape(item))
|
|
17
|
+
: isObject(node)
|
|
18
|
+
? Object.fromEntries(Object.entries(node).flatMap(([key, value]) => !named && (TEXT.has(key) || key.startsWith("x-")) ? [] : [[key, shape(value, !named && NAMED.has(key))]]))
|
|
19
|
+
: node;
|
|
20
|
+
const sameShape = (one, other) => isDeepStrictEqual(shape(one), shape(other));
|
|
21
|
+
/**
|
|
22
|
+
* Two path items, each `$ref` followed into the merged components: a source whose answer schema was renamed
|
|
23
|
+
* (`PublicDefaultAssociation_1` for `PublicDefaultAssociation`) still describes the same path.
|
|
24
|
+
*/
|
|
25
|
+
const sameThrough = (components, one, other, seen = new Set(), named = false) => {
|
|
26
|
+
const target = (ref) => {
|
|
27
|
+
const found = COMPONENT_REF.exec(ref);
|
|
28
|
+
return found && !found[3] ? components[found[1] ?? ""]?.[decode(found[2] ?? "")] : undefined;
|
|
29
|
+
};
|
|
30
|
+
if (Array.isArray(one) && Array.isArray(other))
|
|
31
|
+
return one.length === other.length && one.every((item, index) => sameThrough(components, item, other[index], seen));
|
|
32
|
+
if (!isObject(one) || !isObject(other))
|
|
33
|
+
return isDeepStrictEqual(one, other);
|
|
34
|
+
if (!named && typeof one.$ref === "string" && typeof other.$ref === "string") {
|
|
35
|
+
if (one.$ref === other.$ref)
|
|
36
|
+
return true;
|
|
37
|
+
const pair = `${one.$ref} ${other.$ref}`;
|
|
38
|
+
if (seen.has(pair))
|
|
39
|
+
return true;
|
|
40
|
+
seen.add(pair);
|
|
41
|
+
const [left, right] = [target(one.$ref), target(other.$ref)];
|
|
42
|
+
return left !== undefined && right !== undefined && sameThrough(components, left, right, seen);
|
|
43
|
+
}
|
|
44
|
+
const keys = (node) => Object.keys(node).filter((key) => named || !(TEXT.has(key) || key.startsWith("x-")));
|
|
45
|
+
const [left, right] = [keys(one), keys(other)];
|
|
46
|
+
return (left.length === right.length &&
|
|
47
|
+
left.every((key) => Object.hasOwn(other, key) &&
|
|
48
|
+
right.includes(key) &&
|
|
49
|
+
sameThrough(components, one[key], other[key], seen, !named && NAMED.has(key))));
|
|
50
|
+
};
|
|
51
|
+
/** A source's document-wide sign-in would be lost in the merged document, so its operations carry it. */
|
|
52
|
+
const withOwnSecurity = (document) => {
|
|
53
|
+
if (!Array.isArray(document.security) || !isObject(document.paths))
|
|
54
|
+
return document;
|
|
55
|
+
const { security, ...rest } = document;
|
|
56
|
+
const paths = Object.fromEntries(Object.entries(document.paths).map(([path, item]) => [
|
|
57
|
+
path,
|
|
58
|
+
isObject(item)
|
|
59
|
+
? Object.fromEntries(Object.entries(item).map(([method, operation]) => [
|
|
60
|
+
method,
|
|
61
|
+
METHODS.includes(method) && isObject(operation) && operation.security === undefined
|
|
62
|
+
? { ...operation, security }
|
|
63
|
+
: operation,
|
|
64
|
+
]))
|
|
65
|
+
: item,
|
|
66
|
+
]));
|
|
67
|
+
return { ...rest, paths };
|
|
68
|
+
};
|
|
69
|
+
const pascal = (name) => name
|
|
70
|
+
.split(/[^A-Za-z0-9]+/)
|
|
71
|
+
.filter(Boolean)
|
|
72
|
+
.map((word) => word[0]?.toUpperCase() + word.slice(1))
|
|
73
|
+
.join("");
|
|
74
|
+
const decode = (segment) => segment.replaceAll("~1", "/").replaceAll("~0", "~");
|
|
75
|
+
const encode = (segment) => segment.replaceAll("~", "~0").replaceAll("/", "~1");
|
|
76
|
+
const COMPONENT_REF = /^#\/components\/([^/]+)\/([^/]+)(\/.*)?$/;
|
|
77
|
+
const rewriteRefs = (node, renames) => {
|
|
78
|
+
if (Array.isArray(node))
|
|
79
|
+
return node.map((item) => rewriteRefs(item, renames));
|
|
80
|
+
if (!isObject(node))
|
|
81
|
+
return node;
|
|
82
|
+
return Object.fromEntries(Object.entries(node).map(([key, value]) => {
|
|
83
|
+
if (key !== "$ref" || typeof value !== "string")
|
|
84
|
+
return [key, rewriteRefs(value, renames)];
|
|
85
|
+
const found = COMPONENT_REF.exec(value);
|
|
86
|
+
const renamed = found && renames.get(`${found[1]}/${decode(found[2] ?? "")}`);
|
|
87
|
+
return [key, renamed ? `#/components/${found[1]}/${encode(renamed)}${found[3] ?? ""}` : value];
|
|
88
|
+
}));
|
|
89
|
+
};
|
|
90
|
+
/** One sign-in scheme as two specs may both describe it: each lists only its own object's scopes. */
|
|
91
|
+
const withoutScopes = (scheme) => isObject(scheme.flows)
|
|
92
|
+
? {
|
|
93
|
+
...scheme,
|
|
94
|
+
flows: Object.fromEntries(Object.entries(scheme.flows).map(([flow, body]) => [flow, isObject(body) ? { ...body, scopes: {} } : body])),
|
|
95
|
+
}
|
|
96
|
+
: scheme;
|
|
97
|
+
const versionOrder = (one, other) => one.localeCompare(other, undefined, { numeric: true, sensitivity: "base" });
|
|
98
|
+
/** Throws `OpenApiError` with every conflict at once: a path, an operation id or a sign-in two sources disagree on. */
|
|
99
|
+
export const mergeOpenApi = (sources, options) => {
|
|
100
|
+
const problems = [];
|
|
101
|
+
const components = {};
|
|
102
|
+
const paths = {};
|
|
103
|
+
const servers = new Map();
|
|
104
|
+
const tags = new Map();
|
|
105
|
+
const operationIds = new Map();
|
|
106
|
+
const report = { sources: [], merged: [], renamed: [], sharedPaths: [] };
|
|
107
|
+
const versions = new Set();
|
|
108
|
+
const names = new Set();
|
|
109
|
+
for (const { name, document: given, url } of sources) {
|
|
110
|
+
const text = typeof given === "string" ? given : JSON.stringify(given);
|
|
111
|
+
let document;
|
|
112
|
+
try {
|
|
113
|
+
document = typeof given === "string" ? JSON.parse(given) : given;
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
problems.push(`${name}: not JSON`);
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (!isObject(document) || typeof document.openapi !== "string" || !document.openapi.startsWith("3.")) {
|
|
120
|
+
problems.push(`${name}: not an OpenAPI 3 document`);
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
if (names.has(name))
|
|
124
|
+
problems.push(`${name}: two sources share this name`);
|
|
125
|
+
names.add(name);
|
|
126
|
+
versions.add(document.openapi);
|
|
127
|
+
const info = isObject(document.info) ? document.info : {};
|
|
128
|
+
const own = isObject(document.components) ? document.components : {};
|
|
129
|
+
// A component that only refers to a renamed one differs too, so renaming repeats until nothing more changes.
|
|
130
|
+
const renames = new Map();
|
|
131
|
+
for (let grown = true; grown;) {
|
|
132
|
+
grown = false;
|
|
133
|
+
const current = rewriteRefs(own, renames);
|
|
134
|
+
for (const [section, entries] of Object.entries(current)) {
|
|
135
|
+
if (section === "securitySchemes" || !isObject(entries))
|
|
136
|
+
continue;
|
|
137
|
+
for (const [key, value] of Object.entries(entries)) {
|
|
138
|
+
const have = components[section]?.[key];
|
|
139
|
+
if (have === undefined || renames.has(`${section}/${key}`) || sameShape(have, value))
|
|
140
|
+
continue;
|
|
141
|
+
renames.set(`${section}/${key}`, `${pascal(name)}${key}`);
|
|
142
|
+
grown = true;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
const targets = new Set();
|
|
147
|
+
const before = problems.length;
|
|
148
|
+
for (const [from, to] of renames) {
|
|
149
|
+
const section = from.split("/")[0] ?? "";
|
|
150
|
+
const ownEntries = isObject(own[section]) ? own[section] : {};
|
|
151
|
+
if (components[section]?.[to] !== undefined || Object.hasOwn(ownEntries, to) || targets.has(`${section}/${to}`))
|
|
152
|
+
problems.push(`${name}: ${from} would be renamed ${to}, a name already taken; give the source another name`);
|
|
153
|
+
targets.add(`${section}/${to}`);
|
|
154
|
+
}
|
|
155
|
+
if (problems.length > before)
|
|
156
|
+
continue;
|
|
157
|
+
const rewritten = rewriteRefs(withOwnSecurity(document), renames);
|
|
158
|
+
const rewrittenComponents = isObject(rewritten.components) ? rewritten.components : {};
|
|
159
|
+
for (const [section, entries] of Object.entries(rewrittenComponents)) {
|
|
160
|
+
if (!isObject(entries))
|
|
161
|
+
continue;
|
|
162
|
+
components[section] ??= {};
|
|
163
|
+
const into = components[section];
|
|
164
|
+
for (const [key, value] of Object.entries(entries)) {
|
|
165
|
+
if (section === "securitySchemes") {
|
|
166
|
+
const have = into[key];
|
|
167
|
+
if (have === undefined)
|
|
168
|
+
into[key] = structuredClone(value);
|
|
169
|
+
else if (isObject(have) &&
|
|
170
|
+
isObject(value) &&
|
|
171
|
+
have.type === "oauth2" &&
|
|
172
|
+
sameShape(withoutScopes(have), withoutScopes(value))) {
|
|
173
|
+
// Each spec lists only its own object's scopes; the service's sign-in is one.
|
|
174
|
+
have.flows ??= {};
|
|
175
|
+
const flows = have.flows;
|
|
176
|
+
for (const [flow, body] of Object.entries(isObject(value.flows) ? value.flows : {})) {
|
|
177
|
+
if (!isObject(body))
|
|
178
|
+
continue;
|
|
179
|
+
flows[flow] ??= structuredClone(body);
|
|
180
|
+
const target = flows[flow];
|
|
181
|
+
target.scopes = {
|
|
182
|
+
...(isObject(target.scopes) ? target.scopes : {}),
|
|
183
|
+
...(isObject(body.scopes) ? body.scopes : {}),
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
else if (!sameShape(have, value))
|
|
188
|
+
problems.push(`${name}: security scheme "${key}" differs from an earlier source's`);
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
const target = renames.get(`${section}/${key}`) ?? key;
|
|
192
|
+
if (target !== key) {
|
|
193
|
+
report.renamed.push(`${section}/${key} → ${target} (${name})`);
|
|
194
|
+
into[target] = value;
|
|
195
|
+
}
|
|
196
|
+
else if (into[key] === undefined)
|
|
197
|
+
into[key] = value;
|
|
198
|
+
else if (!sameShape(into[key], value))
|
|
199
|
+
problems.push(`${name}: ${section}/${key} differs from the one merged`);
|
|
200
|
+
else if (!report.merged.includes(`${section}/${key}`))
|
|
201
|
+
report.merged.push(`${section}/${key}`);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
let operations = 0;
|
|
205
|
+
for (const [path, item] of Object.entries(isObject(rewritten.paths) ? rewritten.paths : {})) {
|
|
206
|
+
if (paths[path] !== undefined) {
|
|
207
|
+
if (sameThrough(components, paths[path], item))
|
|
208
|
+
report.sharedPaths.push(`${path} (${name})`);
|
|
209
|
+
else
|
|
210
|
+
problems.push(`${name}: ${path} differs from an earlier source's`);
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
paths[path] = item;
|
|
214
|
+
for (const method of METHODS) {
|
|
215
|
+
const operation = isObject(item) ? item[method] : undefined;
|
|
216
|
+
if (!isObject(operation))
|
|
217
|
+
continue;
|
|
218
|
+
operations++;
|
|
219
|
+
if (typeof operation.operationId !== "string")
|
|
220
|
+
continue;
|
|
221
|
+
const earlier = operationIds.get(operation.operationId);
|
|
222
|
+
if (earlier)
|
|
223
|
+
problems.push(`${name}: operation id "${operation.operationId}" is also ${earlier}'s`);
|
|
224
|
+
else
|
|
225
|
+
operationIds.set(operation.operationId, name);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
for (const server of Array.isArray(rewritten.servers) ? rewritten.servers : [])
|
|
229
|
+
if (isObject(server) && typeof server.url === "string" && !servers.has(server.url))
|
|
230
|
+
servers.set(server.url, server);
|
|
231
|
+
for (const tag of Array.isArray(rewritten.tags) ? rewritten.tags : [])
|
|
232
|
+
if (isObject(tag) && typeof tag.name === "string" && !tags.has(tag.name))
|
|
233
|
+
tags.set(tag.name, tag);
|
|
234
|
+
report.sources.push({
|
|
235
|
+
name,
|
|
236
|
+
...(url ? { url } : {}),
|
|
237
|
+
sha256: createHash("sha256").update(text).digest("hex"),
|
|
238
|
+
...(typeof info.title === "string" ? { title: info.title } : {}),
|
|
239
|
+
...(typeof info.version === "string" ? { version: info.version } : {}),
|
|
240
|
+
operations,
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
// 3.0 reads `nullable` and 3.1 `type: [T, "null"]`; one merged document would misstate one of them.
|
|
244
|
+
if (new Set([...versions].map((version) => version.slice(0, 3))).size > 1)
|
|
245
|
+
problems.push(`the sources mix OpenAPI versions ${[...versions].sort(versionOrder).join(", ")}; convert them to one first`);
|
|
246
|
+
if (problems.length > 0)
|
|
247
|
+
throw new OpenApiError(problems);
|
|
248
|
+
const document = {
|
|
249
|
+
openapi: [...versions].sort(versionOrder).at(-1) ?? "3.0.3",
|
|
250
|
+
info: { title: options.title, version: options.version },
|
|
251
|
+
...(servers.size > 0 ? { servers: [...servers.values()] } : {}),
|
|
252
|
+
...(tags.size > 0 ? { tags: [...tags.values()] } : {}),
|
|
253
|
+
paths,
|
|
254
|
+
components,
|
|
255
|
+
};
|
|
256
|
+
return { document, report };
|
|
257
|
+
};
|
package/dist/codegen/model.d.ts
CHANGED
|
@@ -116,6 +116,8 @@ export interface SourceReference {
|
|
|
116
116
|
export interface ApiOperation {
|
|
117
117
|
/** The source's own id where it has one. Never re-derived when it does. */
|
|
118
118
|
id: string;
|
|
119
|
+
/** Set by `generate`'s `names`; otherwise the command is the kebab-case id. */
|
|
120
|
+
command?: string;
|
|
119
121
|
binding: Binding;
|
|
120
122
|
summary?: string;
|
|
121
123
|
description?: string;
|
|
@@ -124,9 +126,93 @@ export interface ApiOperation {
|
|
|
124
126
|
requestBody?: ApiBody;
|
|
125
127
|
response?: ApiBody;
|
|
126
128
|
effect?: Effect;
|
|
129
|
+
/** Why `effect` is not what the HTTP method says: a default rule or an override. Listed on the coverage page. */
|
|
130
|
+
effectReason?: string;
|
|
131
|
+
/** Which security requirements the operation has, when it differs from the document's; `[]` is "none". */
|
|
132
|
+
security?: readonly SecurityRequirement[];
|
|
133
|
+
/** How to ask for the next page, set by `generate`'s `pagination` detection or an override. */
|
|
134
|
+
pagination?: Pagination;
|
|
135
|
+
/** `detected`, or `override: <reason>`, for the coverage page. */
|
|
136
|
+
paginationSource?: string;
|
|
137
|
+
/** Set by `generate`'s `permissions` rule: the CLI's own keys a call must pass, beside `api.<operation>`. */
|
|
138
|
+
permissions?: readonly OperationPermission[];
|
|
127
139
|
deprecated?: boolean;
|
|
128
140
|
source: SourceReference;
|
|
129
141
|
}
|
|
142
|
+
/** A request parameter or a top-level body field. */
|
|
143
|
+
export interface PageParameter {
|
|
144
|
+
in: "query" | "body";
|
|
145
|
+
name: string;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* How an operation pages, for an executor that reads every page (`--all`). Answer fields are dotted paths into the
|
|
149
|
+
* answer: `next_cursor`, `paging.next.after`, `_embedded.items`.
|
|
150
|
+
*/
|
|
151
|
+
export type Pagination = {
|
|
152
|
+
style: "cursor";
|
|
153
|
+
/** Where the next request carries the cursor. */
|
|
154
|
+
cursor: PageParameter;
|
|
155
|
+
/** Where the answer holds the next cursor; absent or empty on the last page. */
|
|
156
|
+
next: string;
|
|
157
|
+
items?: string;
|
|
158
|
+
/** A boolean the answer sets to false on the last page, where the service has one. */
|
|
159
|
+
hasMore?: string;
|
|
160
|
+
limit?: PageParameter;
|
|
161
|
+
} | {
|
|
162
|
+
style: "offset";
|
|
163
|
+
offset: PageParameter;
|
|
164
|
+
limit?: PageParameter;
|
|
165
|
+
items: string;
|
|
166
|
+
total?: string;
|
|
167
|
+
} | {
|
|
168
|
+
style: "page";
|
|
169
|
+
page: PageParameter;
|
|
170
|
+
limit?: PageParameter;
|
|
171
|
+
items: string;
|
|
172
|
+
total?: string;
|
|
173
|
+
}
|
|
174
|
+
/** The answer holds the next page's whole URL. */
|
|
175
|
+
| {
|
|
176
|
+
style: "link";
|
|
177
|
+
next: string;
|
|
178
|
+
items?: string;
|
|
179
|
+
};
|
|
180
|
+
/** A key a call must pass: always, or, when `body` or `query` is given, only when one of those flags is `true`. */
|
|
181
|
+
export interface OperationPermission {
|
|
182
|
+
key: string;
|
|
183
|
+
/** Top-level request body fields. */
|
|
184
|
+
body?: readonly string[];
|
|
185
|
+
/** Query parameters. */
|
|
186
|
+
query?: readonly string[];
|
|
187
|
+
}
|
|
188
|
+
/** The scheme names an operation needs, each with its OAuth scopes; one requirement of the list is enough. */
|
|
189
|
+
export type SecurityRequirement = Readonly<Record<string, readonly string[]>>;
|
|
190
|
+
export interface OAuthFlow {
|
|
191
|
+
authorizationUrl?: string;
|
|
192
|
+
tokenUrl?: string;
|
|
193
|
+
refreshUrl?: string;
|
|
194
|
+
scopes: Readonly<Record<string, string>>;
|
|
195
|
+
}
|
|
196
|
+
/** OpenAPI's Security Scheme Object, with only the fields of its type. */
|
|
197
|
+
export interface SecurityScheme {
|
|
198
|
+
type: "apiKey" | "http" | "oauth2" | "openIdConnect" | "mutualTLS";
|
|
199
|
+
description?: string;
|
|
200
|
+
name?: string;
|
|
201
|
+
in?: "query" | "header" | "cookie";
|
|
202
|
+
scheme?: string;
|
|
203
|
+
bearerFormat?: string;
|
|
204
|
+
flows?: Readonly<Partial<Record<"implicit" | "password" | "clientCredentials" | "authorizationCode", OAuthFlow>>>;
|
|
205
|
+
openIdConnectUrl?: string;
|
|
206
|
+
}
|
|
207
|
+
export interface ApiServer {
|
|
208
|
+
url: string;
|
|
209
|
+
description?: string;
|
|
210
|
+
variables?: Readonly<Record<string, {
|
|
211
|
+
default: string;
|
|
212
|
+
enum?: readonly string[];
|
|
213
|
+
description?: string;
|
|
214
|
+
}>>;
|
|
215
|
+
}
|
|
130
216
|
export interface SourceInfo {
|
|
131
217
|
kind: "openapi" | "postman" | "other";
|
|
132
218
|
version?: string;
|
|
@@ -137,9 +223,17 @@ export interface ApiModel {
|
|
|
137
223
|
source: SourceInfo;
|
|
138
224
|
operations: readonly ApiOperation[];
|
|
139
225
|
schemas: readonly ApiSchema[];
|
|
226
|
+
servers?: readonly ApiServer[];
|
|
227
|
+
securitySchemes?: Readonly<Record<string, SecurityScheme>>;
|
|
228
|
+
/** The document's default requirements, which an operation's own `security` replaces. */
|
|
229
|
+
security?: readonly SecurityRequirement[];
|
|
230
|
+
/** Set when `generate`'s `auth` replaced the source's sign-in: its reason, for the coverage page. */
|
|
231
|
+
securityReason?: string;
|
|
140
232
|
}
|
|
141
233
|
export interface Override {
|
|
142
234
|
effect?: Effect;
|
|
235
|
+
/** How the operation pages, where detection misses or misreads it; `null` says it does not page. */
|
|
236
|
+
pagination?: Pagination | null;
|
|
143
237
|
/** Required: an override nobody can explain is one nobody dares remove. */
|
|
144
238
|
reason: string;
|
|
145
239
|
}
|
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* An OpenAPI 3.0 or 3.1 document, already parsed from YAML or JSON → the format-neutral `ApiModel`.
|
|
2
|
+
* An OpenAPI 3.0 or 3.1 document, already parsed from YAML or JSON → the format-neutral `ApiModel`. Swagger 1.2 and
|
|
3
|
+
* 2.0 come here through `upgradeToOpenApi3`.
|
|
3
4
|
*
|
|
4
5
|
* Strict on purpose: a keyword this does not understand stops generation and names the place,
|
|
5
6
|
* because a constraint dropped here is a request the server refuses at run time.
|
|
6
7
|
*/
|
|
7
8
|
import type { ApiModel } from "./model.js";
|
|
9
|
+
type Json = Record<string, unknown>;
|
|
8
10
|
export declare class OpenApiError extends Error {
|
|
9
11
|
readonly problems: readonly string[];
|
|
10
12
|
constructor(problems: readonly string[]);
|
|
11
13
|
}
|
|
14
|
+
export declare const isObject: (value: unknown) => value is Json;
|
|
12
15
|
/** Throws `OpenApiError` with every problem at once, so one run shows all a spec update broke. */
|
|
13
16
|
export declare const adaptOpenApi: (document: unknown, source?: {
|
|
14
17
|
sourceUrl?: string;
|
|
15
18
|
sourceRevision?: string;
|
|
16
19
|
}) => ApiModel;
|
|
20
|
+
export {};
|