@magmonium/cli 0.8.29 → 0.8.30
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/bin/cli.js +4 -0
- package/bin/commands/api-ops.js +130 -0
- package/bin/commands/gen-code.js +1 -0
- package/bin/commands/validate.js +583 -0
- package/codegen/index.js +640 -94
- package/codegen/invariant-catalog.js +2788 -0
- package/codegen/swagger-helper.js +478 -0
- package/package.json +1 -1
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// apps/m-one-ui/src/app/pages/design/lib/swagger-helper.ts
|
|
21
|
+
var swagger_helper_exports = {};
|
|
22
|
+
__export(swagger_helper_exports, {
|
|
23
|
+
callSegments: () => callSegments,
|
|
24
|
+
decomposePath: () => decomposePath,
|
|
25
|
+
docJson: () => docJson,
|
|
26
|
+
operationNameHint: () => operationNameHint,
|
|
27
|
+
readDoc: () => readDoc,
|
|
28
|
+
readSpec: () => readSpec,
|
|
29
|
+
unboundParams: () => unboundParams
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(swagger_helper_exports);
|
|
32
|
+
var import_codegen = require("./index.js");
|
|
33
|
+
function schemaIdentity(schema) {
|
|
34
|
+
if (!schema) return "missing";
|
|
35
|
+
if (schema.$ref) return `ref:${schema.$ref}`;
|
|
36
|
+
if (schema["x-prefixItems"])
|
|
37
|
+
return `tuple:[${schema["x-prefixItems"].map((item) => `${item.title ?? ""}:${schemaIdentity(item)}`).join(",")}]`;
|
|
38
|
+
if (schema.type === "array") return `array:${schemaIdentity(schema.items)}`;
|
|
39
|
+
if (schema.type === "object" || schema.properties) {
|
|
40
|
+
const properties = Object.entries(schema.properties ?? {}).sort(([left], [right]) => left.localeCompare(right)).map(([name, child]) => `${name}:${schemaIdentity(child)}`).join(",");
|
|
41
|
+
const additional = schema.additionalProperties && typeof schema.additionalProperties === "object" ? `:${schemaIdentity(schema.additionalProperties)}` : "";
|
|
42
|
+
return `object:{${properties}}${additional}`;
|
|
43
|
+
}
|
|
44
|
+
return `primitive:${schema.type ?? "?"}`;
|
|
45
|
+
}
|
|
46
|
+
function decomposePath(path) {
|
|
47
|
+
const trailing = path.endsWith("/");
|
|
48
|
+
const parts = path.split("/").filter((p) => p !== "");
|
|
49
|
+
const [domain = "", rawVersion = "", ...rest] = parts;
|
|
50
|
+
const isVersion = rawVersion === "v1" || rawVersion === "v2";
|
|
51
|
+
const segments = isVersion ? rest : rawVersion ? [rawVersion, ...rest] : rest;
|
|
52
|
+
if (trailing) segments.push("");
|
|
53
|
+
return {
|
|
54
|
+
domain,
|
|
55
|
+
version: rawVersion === "v2" ? "v2" : void 0,
|
|
56
|
+
segments
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
var PRIMITIVES = {
|
|
60
|
+
string: "string",
|
|
61
|
+
// JavaScript has no integer, so both arrive as `number`. Lossy and harmless
|
|
62
|
+
// (ADR 0025).
|
|
63
|
+
integer: "number",
|
|
64
|
+
number: "number",
|
|
65
|
+
boolean: "boolean"
|
|
66
|
+
};
|
|
67
|
+
function refName(ref) {
|
|
68
|
+
const last = ref.split("/").pop() ?? "";
|
|
69
|
+
return last;
|
|
70
|
+
}
|
|
71
|
+
function operationNameHint(operationId) {
|
|
72
|
+
return (0, import_codegen.normalizeShapeName)(operationId).split("-").filter((part) => part && !/^v\d+$/.test(part)).join("-");
|
|
73
|
+
}
|
|
74
|
+
function refuse(unmapped, memberPath, reason) {
|
|
75
|
+
unmapped.push(`${memberPath || "(response)"} (${reason})`);
|
|
76
|
+
}
|
|
77
|
+
function readSchema(schema, nameHint, ctx, memberPath, identity, pointer = []) {
|
|
78
|
+
if (!schema) return void 0;
|
|
79
|
+
if (schema.$ref) {
|
|
80
|
+
const name = refName(schema.$ref);
|
|
81
|
+
if (ctx.seen.has(name)) {
|
|
82
|
+
refuse(ctx.unmapped, memberPath, `recursive $ref ${name}`);
|
|
83
|
+
return void 0;
|
|
84
|
+
}
|
|
85
|
+
const target = ctx.definitions[name];
|
|
86
|
+
if (!target) {
|
|
87
|
+
refuse(ctx.unmapped, memberPath, `unresolved $ref ${name}`);
|
|
88
|
+
return void 0;
|
|
89
|
+
}
|
|
90
|
+
const seen = new Set(ctx.seen).add(name);
|
|
91
|
+
return readSchema(
|
|
92
|
+
target,
|
|
93
|
+
name,
|
|
94
|
+
{ ...ctx, seen },
|
|
95
|
+
memberPath,
|
|
96
|
+
`ref:${name}`,
|
|
97
|
+
pointer
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
if (schema.type === "array" && schema["x-prefixItems"]) {
|
|
101
|
+
return readTuple(schema["x-prefixItems"], nameHint, ctx, memberPath, pointer);
|
|
102
|
+
}
|
|
103
|
+
if (schema.type === "array") {
|
|
104
|
+
const items = schema.items;
|
|
105
|
+
if (items?.type === "array" && !items["x-prefixItems"]) {
|
|
106
|
+
refuse(ctx.unmapped, memberPath, "nested array");
|
|
107
|
+
return void 0;
|
|
108
|
+
}
|
|
109
|
+
const inner = readSchema(
|
|
110
|
+
items,
|
|
111
|
+
nameHint,
|
|
112
|
+
ctx,
|
|
113
|
+
memberPath,
|
|
114
|
+
void 0,
|
|
115
|
+
pointer
|
|
116
|
+
);
|
|
117
|
+
if (!inner) return void 0;
|
|
118
|
+
if (inner.cardinality !== "one") {
|
|
119
|
+
refuse(ctx.unmapped, memberPath, "collection of collection");
|
|
120
|
+
return void 0;
|
|
121
|
+
}
|
|
122
|
+
return { dataType: inner.dataType, cardinality: "many" };
|
|
123
|
+
}
|
|
124
|
+
if (schema.type === "object" || schema.properties) {
|
|
125
|
+
const props = schema.properties;
|
|
126
|
+
if (!props || Object.keys(props).length === 0) {
|
|
127
|
+
const extra = schema.additionalProperties;
|
|
128
|
+
if (extra && typeof extra === "object") {
|
|
129
|
+
const inner = readSchema(
|
|
130
|
+
extra,
|
|
131
|
+
nameHint,
|
|
132
|
+
ctx,
|
|
133
|
+
memberPath,
|
|
134
|
+
void 0,
|
|
135
|
+
pointer
|
|
136
|
+
);
|
|
137
|
+
if (!inner || (0, import_codegen.isMapCardinality)(inner.cardinality)) {
|
|
138
|
+
refuse(ctx.unmapped, memberPath, "map of map");
|
|
139
|
+
return void 0;
|
|
140
|
+
}
|
|
141
|
+
return {
|
|
142
|
+
dataType: inner.dataType,
|
|
143
|
+
cardinality: inner.cardinality === "many" ? "map-many" : "map"
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
refuse(ctx.unmapped, memberPath, "object with no properties");
|
|
147
|
+
return void 0;
|
|
148
|
+
}
|
|
149
|
+
const shapeIdentity = identity ?? `anonymous:${schemaIdentity(schema)}`;
|
|
150
|
+
const existing = ctx.shapeIds.get(shapeIdentity);
|
|
151
|
+
if (existing) {
|
|
152
|
+
return {
|
|
153
|
+
dataType: { type: "shape", shapeId: existing },
|
|
154
|
+
cardinality: "one"
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
const keys = [];
|
|
158
|
+
for (const [rawKey, child] of Object.entries(props)) {
|
|
159
|
+
const key = (0, import_codegen.normalizePropertyName)(rawKey);
|
|
160
|
+
const childHint = `${nameHint}-${rawKey}`;
|
|
161
|
+
const read = readSchema(
|
|
162
|
+
child,
|
|
163
|
+
childHint,
|
|
164
|
+
ctx,
|
|
165
|
+
memberPath ? `${memberPath}.${rawKey}` : rawKey,
|
|
166
|
+
void 0,
|
|
167
|
+
[...pointer, key]
|
|
168
|
+
);
|
|
169
|
+
if (!read) continue;
|
|
170
|
+
keys.push({
|
|
171
|
+
id: (0, import_codegen.stableId)(`${ctx.idSeed}|${shapeIdentity}|${key}`),
|
|
172
|
+
name: key,
|
|
173
|
+
dataType: read.dataType,
|
|
174
|
+
cardinality: read.cardinality
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
if (keys.length === 0) {
|
|
178
|
+
refuse(ctx.unmapped, memberPath, "no expressible keys");
|
|
179
|
+
return void 0;
|
|
180
|
+
}
|
|
181
|
+
const shape = {
|
|
182
|
+
id: (0, import_codegen.stableId)(`${ctx.idSeed}|${shapeIdentity}`),
|
|
183
|
+
name: (0, import_codegen.normalizeShapeName)(nameHint),
|
|
184
|
+
keys,
|
|
185
|
+
// Stamped on the way out so Recheck can ask the spec about this shape
|
|
186
|
+
// again. A request walk carries no origin and mints none.
|
|
187
|
+
...ctx.origin ? { origin: { ...ctx.origin, pointer } } : {}
|
|
188
|
+
};
|
|
189
|
+
ctx.shapeIds.set(shapeIdentity, shape.id);
|
|
190
|
+
ctx.shapes.push(shape);
|
|
191
|
+
return {
|
|
192
|
+
dataType: { type: "shape", shapeId: shape.id },
|
|
193
|
+
cardinality: "one"
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
const primitive = schema.type ? PRIMITIVES[schema.type] : void 0;
|
|
197
|
+
if (!primitive) {
|
|
198
|
+
refuse(ctx.unmapped, memberPath, `unsupported type ${schema.type ?? "?"}`);
|
|
199
|
+
return void 0;
|
|
200
|
+
}
|
|
201
|
+
return { dataType: { type: primitive }, cardinality: "one" };
|
|
202
|
+
}
|
|
203
|
+
function readTuple(items, nameHint, ctx, memberPath, pointer) {
|
|
204
|
+
const shapeIdentity = `anonymous:${schemaIdentity({ type: "array", "x-prefixItems": items })}`;
|
|
205
|
+
const existing = ctx.shapeIds.get(shapeIdentity);
|
|
206
|
+
if (existing) {
|
|
207
|
+
return { dataType: { type: "shape", shapeId: existing }, cardinality: "one" };
|
|
208
|
+
}
|
|
209
|
+
const keys = [];
|
|
210
|
+
for (const [index, item] of items.entries()) {
|
|
211
|
+
const primitive = item.type ? PRIMITIVES[item.type] : void 0;
|
|
212
|
+
if (!primitive || item.$ref) {
|
|
213
|
+
refuse(ctx.unmapped, memberPath, `tuple position ${index} not a primitive`);
|
|
214
|
+
return void 0;
|
|
215
|
+
}
|
|
216
|
+
keys.push({
|
|
217
|
+
id: (0, import_codegen.stableId)(`${ctx.idSeed}|${shapeIdentity}|${index}`),
|
|
218
|
+
name: (0, import_codegen.normalizePropertyName)(item.title ?? `item-${index}`),
|
|
219
|
+
dataType: { type: primitive },
|
|
220
|
+
cardinality: "one"
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
if (keys.length === 0) {
|
|
224
|
+
refuse(ctx.unmapped, memberPath, "empty tuple");
|
|
225
|
+
return void 0;
|
|
226
|
+
}
|
|
227
|
+
const shape = {
|
|
228
|
+
id: (0, import_codegen.stableId)(`${ctx.idSeed}|${shapeIdentity}`),
|
|
229
|
+
name: (0, import_codegen.normalizeShapeName)(nameHint),
|
|
230
|
+
keys,
|
|
231
|
+
positional: true,
|
|
232
|
+
...ctx.origin ? { origin: { ...ctx.origin, pointer } } : {}
|
|
233
|
+
};
|
|
234
|
+
ctx.shapeIds.set(shapeIdentity, shape.id);
|
|
235
|
+
ctx.shapes.push(shape);
|
|
236
|
+
return { dataType: { type: "shape", shapeId: shape.id }, cardinality: "one" };
|
|
237
|
+
}
|
|
238
|
+
function undescribed(code, description) {
|
|
239
|
+
return {
|
|
240
|
+
code,
|
|
241
|
+
tier: "undescribed",
|
|
242
|
+
shapes: [],
|
|
243
|
+
dataType: void 0,
|
|
244
|
+
cardinality: "one",
|
|
245
|
+
unmapped: [],
|
|
246
|
+
...description ? { description } : {}
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
function readResponses(raw, definitions, nameHint, verb, path) {
|
|
250
|
+
const out = [];
|
|
251
|
+
let plainHintTaken = false;
|
|
252
|
+
for (const [code, value] of Object.entries(raw.responses ?? {})) {
|
|
253
|
+
if (!/^\d{3}$/.test(code)) continue;
|
|
254
|
+
const schema = value?.schema;
|
|
255
|
+
if (!schema) {
|
|
256
|
+
out.push(undescribed(code, value?.description));
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
const plain = (0, import_codegen.isSuccessCode)(code) && !plainHintTaken;
|
|
260
|
+
if (plain) plainHintTaken = true;
|
|
261
|
+
const tier = schema.$ref ? "named" : "anonymous";
|
|
262
|
+
const shapes = [];
|
|
263
|
+
const unmapped = [];
|
|
264
|
+
const read = readSchema(
|
|
265
|
+
schema,
|
|
266
|
+
plain ? `${nameHint}-response` : `${nameHint}-${code}-response`,
|
|
267
|
+
{
|
|
268
|
+
definitions,
|
|
269
|
+
shapes,
|
|
270
|
+
shapeIds: /* @__PURE__ */ new Map(),
|
|
271
|
+
unmapped,
|
|
272
|
+
seen: /* @__PURE__ */ new Set(),
|
|
273
|
+
origin: { verb, path, code },
|
|
274
|
+
idSeed: `${verb} ${path} ${code}`
|
|
275
|
+
},
|
|
276
|
+
""
|
|
277
|
+
);
|
|
278
|
+
out.push({
|
|
279
|
+
code,
|
|
280
|
+
tier,
|
|
281
|
+
shapes,
|
|
282
|
+
dataType: read?.dataType,
|
|
283
|
+
cardinality: read?.cardinality ?? "one",
|
|
284
|
+
unmapped,
|
|
285
|
+
...value?.description ? { description: value.description } : {},
|
|
286
|
+
doc: readDoc(schema, definitions, "", true)
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
return out.sort((a, b) => a.code.localeCompare(b.code));
|
|
290
|
+
}
|
|
291
|
+
function readDoc(schema, definitions, name, required, seen = /* @__PURE__ */ new Set()) {
|
|
292
|
+
const doc = {
|
|
293
|
+
name,
|
|
294
|
+
type: "unknown",
|
|
295
|
+
required,
|
|
296
|
+
members: [],
|
|
297
|
+
...schema?.description ? { description: schema.description } : {},
|
|
298
|
+
...schema?.example === void 0 ? {} : { example: String(schema.example) },
|
|
299
|
+
...schema?.enum?.length ? { enumValues: schema.enum.map(String) } : {}
|
|
300
|
+
};
|
|
301
|
+
if (!schema) return doc;
|
|
302
|
+
if (schema.$ref) {
|
|
303
|
+
const ref = refName(schema.$ref);
|
|
304
|
+
const target = definitions[ref];
|
|
305
|
+
if (!target || seen.has(ref)) return { ...doc, type: ref };
|
|
306
|
+
const inner = readDoc(
|
|
307
|
+
target,
|
|
308
|
+
definitions,
|
|
309
|
+
name,
|
|
310
|
+
required,
|
|
311
|
+
new Set(seen).add(ref)
|
|
312
|
+
);
|
|
313
|
+
return { ...inner, ...doc, type: ref, members: inner.members };
|
|
314
|
+
}
|
|
315
|
+
const tuple = schema["x-prefixItems"];
|
|
316
|
+
if (schema.type === "array" && tuple) {
|
|
317
|
+
const members = tuple.map(
|
|
318
|
+
(item, index) => readDoc(item, definitions, item.title ?? `item${index}`, true, seen)
|
|
319
|
+
);
|
|
320
|
+
const type = `[${members.map((m) => `${m.name}: ${m.type}`).join(", ")}]`;
|
|
321
|
+
return { ...doc, type, members };
|
|
322
|
+
}
|
|
323
|
+
if (schema.type === "array") {
|
|
324
|
+
const item = readDoc(schema.items, definitions, name, required, seen);
|
|
325
|
+
return { ...doc, type: `${item.type}[]`, members: item.members };
|
|
326
|
+
}
|
|
327
|
+
if (schema.type === "object" || schema.properties) {
|
|
328
|
+
const mandatory = new Set(schema.required ?? []);
|
|
329
|
+
const members = Object.entries(schema.properties ?? {}).map(
|
|
330
|
+
([key, child]) => readDoc(child, definitions, key, mandatory.has(key), seen)
|
|
331
|
+
);
|
|
332
|
+
const extra = schema.additionalProperties;
|
|
333
|
+
if (!members.length && extra && typeof extra === "object") {
|
|
334
|
+
const value = readDoc(extra, definitions, "<key>", false, seen);
|
|
335
|
+
return { ...doc, type: "object", members: [value] };
|
|
336
|
+
}
|
|
337
|
+
return { ...doc, type: "object", members };
|
|
338
|
+
}
|
|
339
|
+
const format = schema.format ? `($${schema.format})` : "";
|
|
340
|
+
return { ...doc, type: `${schema.type ?? "unknown"}${format}` };
|
|
341
|
+
}
|
|
342
|
+
function docJson(doc) {
|
|
343
|
+
return jsonOf(doc, 0);
|
|
344
|
+
}
|
|
345
|
+
function jsonOf(doc, depth) {
|
|
346
|
+
if (!doc.members.length) return doc.type;
|
|
347
|
+
const pad = " ".repeat(depth + 1);
|
|
348
|
+
if (doc.type.startsWith("[")) {
|
|
349
|
+
const positions = doc.members.map((member, i) => {
|
|
350
|
+
const comma = i < doc.members.length - 1 ? "," : "";
|
|
351
|
+
return `${pad}${jsonOf(member, depth + 1)}${comma} // ${member.name}`;
|
|
352
|
+
}).join("\n");
|
|
353
|
+
const tuple = `[
|
|
354
|
+
${positions}
|
|
355
|
+
${" ".repeat(depth)}]`;
|
|
356
|
+
return doc.type.endsWith("[]") ? `${tuple}[]` : tuple;
|
|
357
|
+
}
|
|
358
|
+
const body = doc.members.map((member) => `${pad}"${member.name}": ${jsonOf(member, depth + 1)}`).join(",\n");
|
|
359
|
+
const object = `{
|
|
360
|
+
${body}
|
|
361
|
+
${" ".repeat(depth)}}`;
|
|
362
|
+
return doc.type.endsWith("[]") ? `${object}[]` : object;
|
|
363
|
+
}
|
|
364
|
+
var PARAM_LOCATIONS = {
|
|
365
|
+
query: "query",
|
|
366
|
+
path: "path",
|
|
367
|
+
body: "body",
|
|
368
|
+
formData: "formData"
|
|
369
|
+
};
|
|
370
|
+
function readParams(raw, definitions, nameHint) {
|
|
371
|
+
const params = [];
|
|
372
|
+
for (const p of raw.parameters ?? []) {
|
|
373
|
+
const location = p.in ? PARAM_LOCATIONS[p.in] : void 0;
|
|
374
|
+
if (!location || !p.name) continue;
|
|
375
|
+
const shapes = [];
|
|
376
|
+
const unmapped = [];
|
|
377
|
+
const body = location === "body" || location === "formData";
|
|
378
|
+
const read = readSchema(
|
|
379
|
+
p.schema ?? { type: p.type, items: p.items },
|
|
380
|
+
body ? `${nameHint}-request` : `${nameHint}-${p.name}`,
|
|
381
|
+
{
|
|
382
|
+
definitions,
|
|
383
|
+
shapes,
|
|
384
|
+
shapeIds: /* @__PURE__ */ new Map(),
|
|
385
|
+
unmapped,
|
|
386
|
+
seen: /* @__PURE__ */ new Set(),
|
|
387
|
+
idSeed: `${nameHint} ${p.in} ${p.name}`
|
|
388
|
+
},
|
|
389
|
+
p.name
|
|
390
|
+
);
|
|
391
|
+
const raw2 = p.schema ?? {
|
|
392
|
+
type: p.type,
|
|
393
|
+
format: p.format,
|
|
394
|
+
items: p.items,
|
|
395
|
+
description: p.description,
|
|
396
|
+
enum: p.enum,
|
|
397
|
+
example: p.example
|
|
398
|
+
};
|
|
399
|
+
params.push({
|
|
400
|
+
name: p.name,
|
|
401
|
+
location,
|
|
402
|
+
required: p.required === true,
|
|
403
|
+
dataType: read?.dataType,
|
|
404
|
+
cardinality: read?.cardinality ?? "one",
|
|
405
|
+
description: p.description,
|
|
406
|
+
// Kept rather than dropped: an InputPath walks into `payload`, and the
|
|
407
|
+
// members it walks are these (ADR 0027).
|
|
408
|
+
shapes,
|
|
409
|
+
unmapped,
|
|
410
|
+
doc: readDoc(raw2, definitions, p.name, p.required === true)
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
return params;
|
|
414
|
+
}
|
|
415
|
+
function withSharedParams(rawOp, shared) {
|
|
416
|
+
const own = rawOp.parameters ?? [];
|
|
417
|
+
const declared = new Set(own.map((p) => `${p.in}:${p.name}`));
|
|
418
|
+
const inherited = shared.filter((p) => !declared.has(`${p.in}:${p.name}`));
|
|
419
|
+
return { ...rawOp, parameters: [...inherited, ...own] };
|
|
420
|
+
}
|
|
421
|
+
function readSpec(raw) {
|
|
422
|
+
const definitions = raw.definitions ?? {};
|
|
423
|
+
const operations = [];
|
|
424
|
+
for (const [path, verbs] of Object.entries(raw.paths ?? {})) {
|
|
425
|
+
const { domain, version, segments } = decomposePath(path);
|
|
426
|
+
const shared = verbs.parameters ?? [];
|
|
427
|
+
for (const verb of import_codegen.HTTP_VERBS) {
|
|
428
|
+
const rawOp = verbs[verb];
|
|
429
|
+
if (!rawOp) continue;
|
|
430
|
+
const op = shared.length ? withSharedParams(rawOp, shared) : rawOp;
|
|
431
|
+
const operationId = rawOp.operationId ?? `${verb}-${path.replace(/[^a-zA-Z0-9]+/g, "-")}`;
|
|
432
|
+
operations.push({
|
|
433
|
+
id: `${verb} ${path}`,
|
|
434
|
+
verb,
|
|
435
|
+
path,
|
|
436
|
+
domain,
|
|
437
|
+
version,
|
|
438
|
+
segments,
|
|
439
|
+
operationId,
|
|
440
|
+
summary: rawOp.summary ?? rawOp.description ?? "",
|
|
441
|
+
params: readParams(op, definitions, operationNameHint(operationId)),
|
|
442
|
+
responses: readResponses(
|
|
443
|
+
rawOp,
|
|
444
|
+
definitions,
|
|
445
|
+
operationNameHint(operationId),
|
|
446
|
+
verb,
|
|
447
|
+
path
|
|
448
|
+
)
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
const byDomain = {};
|
|
453
|
+
for (const op of operations) {
|
|
454
|
+
(byDomain[op.domain] ??= []).push(op);
|
|
455
|
+
}
|
|
456
|
+
return { operations, byDomain };
|
|
457
|
+
}
|
|
458
|
+
function callSegments(op, boundPathParams) {
|
|
459
|
+
return op.segments.map((segment) => {
|
|
460
|
+
const match = /^\{(.+)\}$/.exec(segment);
|
|
461
|
+
if (!match) return segment;
|
|
462
|
+
return boundPathParams[match[1]] ?? match[1];
|
|
463
|
+
});
|
|
464
|
+
}
|
|
465
|
+
function unboundParams(op, boundNames) {
|
|
466
|
+
const bound = new Set(boundNames);
|
|
467
|
+
return op.params.filter((p) => !bound.has(p.name));
|
|
468
|
+
}
|
|
469
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
470
|
+
0 && (module.exports = {
|
|
471
|
+
callSegments,
|
|
472
|
+
decomposePath,
|
|
473
|
+
docJson,
|
|
474
|
+
operationNameHint,
|
|
475
|
+
readDoc,
|
|
476
|
+
readSpec,
|
|
477
|
+
unboundParams
|
|
478
|
+
});
|