@dogsbay/format-openapi 0.2.0-beta.117 → 0.2.0-beta.118
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core.d.ts +66 -0
- package/dist/core.d.ts.map +1 -0
- package/dist/core.js +658 -0
- package/dist/core.js.map +1 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/parse.d.ts +9 -26
- package/dist/parse.d.ts.map +1 -1
- package/dist/parse.js +9 -596
- package/dist/parse.js.map +1 -1
- package/dist/spec-path.d.ts +34 -0
- package/dist/spec-path.d.ts.map +1 -0
- package/dist/spec-path.js +69 -0
- package/dist/spec-path.js.map +1 -0
- package/package.json +12 -3
package/dist/core.js
ADDED
|
@@ -0,0 +1,658 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The OpenAPI conversion with no file or network access, and no Node
|
|
3
|
+
* built-ins: it runs in a Cloudflare Worker (dogsbay-ssg) as well as in
|
|
4
|
+
* the CLI. A spec comes in as text (or an already-parsed object) and goes
|
|
5
|
+
* out as `endpoint` TreeNodes; finding and reading the file is the
|
|
6
|
+
* caller's (parse.ts for the CLI, a build's file map for dogsbay-ssg).
|
|
7
|
+
*
|
|
8
|
+
* text ─ parseSpecText ─▶ object ─ prepareSpec ─▶ spec ─┬ embedSpec (one page's body)
|
|
9
|
+
* └ specToPages (a page per operation)
|
|
10
|
+
*
|
|
11
|
+
* `prepareSpec` is the expensive step (upgrade, dereference); `createSpecCache`
|
|
12
|
+
* runs it once per spec file per build. `findSpec` holds the rules for which
|
|
13
|
+
* file a page's `:::openapi{spec}` may name.
|
|
14
|
+
*/
|
|
15
|
+
import { dereference, upgrade } from "@scalar/openapi-parser";
|
|
16
|
+
import MarkdownIt from "markdown-it";
|
|
17
|
+
import { operationToSlug, slugifyTag } from "./slug.js";
|
|
18
|
+
import { resolveCodeSamples } from "./code-samples.js";
|
|
19
|
+
export { findSpec, specCandidates } from "./spec-path.js";
|
|
20
|
+
const HTTP_METHODS = [
|
|
21
|
+
"get",
|
|
22
|
+
"post",
|
|
23
|
+
"put",
|
|
24
|
+
"patch",
|
|
25
|
+
"delete",
|
|
26
|
+
"head",
|
|
27
|
+
"options",
|
|
28
|
+
];
|
|
29
|
+
/**
|
|
30
|
+
* A dereferenced spec (`prepareSpec`) as a page per operation, a page per tag
|
|
31
|
+
* and an info page, with their nav: for a spec that IS the source.
|
|
32
|
+
*/
|
|
33
|
+
export function specToPages(spec, options = {}) {
|
|
34
|
+
const hrefPrefix = normalisePrefix(options.hrefPrefix);
|
|
35
|
+
const pages = [];
|
|
36
|
+
// Collect tag descriptions (from spec.tags[]) for tag overview pages.
|
|
37
|
+
const tagDescriptions = new Map();
|
|
38
|
+
for (const t of spec.tags || []) {
|
|
39
|
+
if (t?.name)
|
|
40
|
+
tagDescriptions.set(t.name, t.description || "");
|
|
41
|
+
}
|
|
42
|
+
const tagToOps = new Map();
|
|
43
|
+
const untagged = [];
|
|
44
|
+
for (const op of collectOperations(spec)) {
|
|
45
|
+
const { method, path, operation, props } = op;
|
|
46
|
+
const primaryTag = props.tags?.[0];
|
|
47
|
+
const tagSlug = primaryTag ? slugifyTag(primaryTag) : "";
|
|
48
|
+
const opSlug = operationToSlug(method, path, operation.operationId);
|
|
49
|
+
const slug = tagSlug ? `${tagSlug}/${opSlug}` : opSlug;
|
|
50
|
+
const title = operation.summary || `${method.toUpperCase()} ${path}`;
|
|
51
|
+
const tree = [
|
|
52
|
+
{
|
|
53
|
+
type: "endpoint",
|
|
54
|
+
props: structuredClone(props),
|
|
55
|
+
},
|
|
56
|
+
];
|
|
57
|
+
const headings = [{ depth: 1, slug: opSlug, text: title }];
|
|
58
|
+
pages.push({
|
|
59
|
+
slug,
|
|
60
|
+
title,
|
|
61
|
+
tree,
|
|
62
|
+
headings,
|
|
63
|
+
});
|
|
64
|
+
const summary = {
|
|
65
|
+
slug,
|
|
66
|
+
title,
|
|
67
|
+
method,
|
|
68
|
+
path,
|
|
69
|
+
deprecated: operation.deprecated === true,
|
|
70
|
+
};
|
|
71
|
+
if (primaryTag) {
|
|
72
|
+
const arr = tagToOps.get(primaryTag) || [];
|
|
73
|
+
arr.push(summary);
|
|
74
|
+
tagToOps.set(primaryTag, arr);
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
untagged.push(summary);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
// Tag overview pages.
|
|
81
|
+
for (const [tagName, ops] of tagToOps) {
|
|
82
|
+
const tagSlug = slugifyTag(tagName);
|
|
83
|
+
const description = tagDescriptions.get(tagName) || "";
|
|
84
|
+
pages.push(buildTagPage(tagName, tagSlug, description, ops));
|
|
85
|
+
}
|
|
86
|
+
// Root info page.
|
|
87
|
+
const info = spec.info || {};
|
|
88
|
+
pages.push(buildInfoPage(info.title || "API Reference", info.description || "", info.version || ""));
|
|
89
|
+
// Build nav: tag groups in declaration order, with operations as
|
|
90
|
+
// children. Untagged operations land under a synthetic "Other" group
|
|
91
|
+
// so they're discoverable but visually separated.
|
|
92
|
+
const nav = buildNav(spec, pages, tagToOps, untagged, hrefPrefix);
|
|
93
|
+
return { pages, nav };
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Spec text → object. JSON is read natively; YAML needs a parser passed in
|
|
97
|
+
* (`yaml`'s `parse`, js-yaml's `load`), so a host that has one does not
|
|
98
|
+
* bundle a second.
|
|
99
|
+
*/
|
|
100
|
+
export function parseSpecText(text, parseYaml) {
|
|
101
|
+
try {
|
|
102
|
+
return JSON.parse(text);
|
|
103
|
+
}
|
|
104
|
+
catch (err) {
|
|
105
|
+
if (!parseYaml)
|
|
106
|
+
throw new Error(`not JSON, and no YAML parser was given (${err.message})`);
|
|
107
|
+
return parseYaml(text);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* A parsed spec → the dereferenced OpenAPI 3 document every function here
|
|
112
|
+
* reads. Swagger 2.0 is upgraded to OpenAPI 3 first: everything below reads
|
|
113
|
+
* the 3.x shape (`requestBody`, response `content`, `servers`), so a 2.0
|
|
114
|
+
* spec parsed without the upgrade lost every body parameter and response
|
|
115
|
+
* schema while still producing plausible-looking pages. Canonical's Anbox
|
|
116
|
+
* Cloud API specs are 2.0.
|
|
117
|
+
*/
|
|
118
|
+
export async function prepareSpec(parsed, label = "the spec") {
|
|
119
|
+
let doc = parsed;
|
|
120
|
+
if (!doc || typeof doc !== "object")
|
|
121
|
+
throw new Error(`${label} is not an OpenAPI document`);
|
|
122
|
+
if (typeof doc.swagger === "string" && doc.swagger.startsWith("2")) {
|
|
123
|
+
doc = upgrade(doc).specification;
|
|
124
|
+
}
|
|
125
|
+
const { schema: spec } = await dereference(doc);
|
|
126
|
+
if (!spec)
|
|
127
|
+
throw new Error(`Failed to dereference OpenAPI spec at ${label}`);
|
|
128
|
+
return spec;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Prepared specs by path, each parsed and dereferenced once however many
|
|
132
|
+
* pages embed it. `read` returns the file's text; failures are cached too,
|
|
133
|
+
* so a broken spec is reported per page without being parsed again.
|
|
134
|
+
*/
|
|
135
|
+
export function createSpecCache(read, parseYaml) {
|
|
136
|
+
const specs = new Map();
|
|
137
|
+
return {
|
|
138
|
+
get(path) {
|
|
139
|
+
let p = specs.get(path);
|
|
140
|
+
if (!p) {
|
|
141
|
+
p = Promise.resolve()
|
|
142
|
+
.then(() => read(path))
|
|
143
|
+
.then((text) => prepareSpec(parseSpecText(text, parseYaml), path));
|
|
144
|
+
specs.set(path, p);
|
|
145
|
+
}
|
|
146
|
+
return p;
|
|
147
|
+
},
|
|
148
|
+
get size() {
|
|
149
|
+
return specs.size;
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
/** Every operation in the spec, in document order, as `endpoint` props. */
|
|
154
|
+
function collectOperations(spec) {
|
|
155
|
+
const baseUrl = spec.servers?.[0]?.url || "";
|
|
156
|
+
const out = [];
|
|
157
|
+
for (const [path, pathItem] of Object.entries(spec.paths || {})) {
|
|
158
|
+
if (!pathItem)
|
|
159
|
+
continue;
|
|
160
|
+
for (const method of HTTP_METHODS) {
|
|
161
|
+
const operation = pathItem[method];
|
|
162
|
+
if (!operation)
|
|
163
|
+
continue;
|
|
164
|
+
const opTags = Array.isArray(operation.tags) ? operation.tags : [];
|
|
165
|
+
const props = {
|
|
166
|
+
method,
|
|
167
|
+
path,
|
|
168
|
+
baseUrl,
|
|
169
|
+
summary: operation.summary || undefined,
|
|
170
|
+
description: operation.description || undefined,
|
|
171
|
+
operationId: operation.operationId || undefined,
|
|
172
|
+
tags: opTags.length > 0 ? opTags : undefined,
|
|
173
|
+
deprecated: operation.deprecated === true ? true : undefined,
|
|
174
|
+
parameters: extractParameters(operation, pathItem),
|
|
175
|
+
...extractRequestBody(operation),
|
|
176
|
+
responses: extractResponses(operation),
|
|
177
|
+
codeSamples: resolveCodeSamples(operation, method, path, baseUrl),
|
|
178
|
+
};
|
|
179
|
+
out.push({ method, path, operation, props });
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return out;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* A whole spec as the BODY of one page: what Swagger UI or Redoc show when a
|
|
186
|
+
* page embeds them. The info version and description, then a section per tag
|
|
187
|
+
* (declared order, then first use; untagged operations last, under "Other"),
|
|
188
|
+
* each holding its operations as `endpoint` nodes in document order.
|
|
189
|
+
*
|
|
190
|
+
* `spec` is a dereferenced spec (`prepareSpec`), and is not modified: the
|
|
191
|
+
* nodes are copies, so one parsed spec serves every page that embeds it.
|
|
192
|
+
*
|
|
193
|
+
* `specToPages` is the other shape — a page per operation — for a spec
|
|
194
|
+
* that IS the source. This one is for a spec a prose page points at.
|
|
195
|
+
*/
|
|
196
|
+
export function embedSpec(spec, options = {}) {
|
|
197
|
+
const level = Math.min(6, Math.max(2, options.level ?? 2));
|
|
198
|
+
const tree = [];
|
|
199
|
+
const headings = [];
|
|
200
|
+
const used = new Set(options.usedIds ?? []);
|
|
201
|
+
// A description's own headings go under the section that holds it, with
|
|
202
|
+
// ids, and into the outline (a `# Title` was a second H1).
|
|
203
|
+
const describe = (text, minLevel) => {
|
|
204
|
+
const d = descriptionHtml(text, minLevel, used);
|
|
205
|
+
headings.push(...d.headings);
|
|
206
|
+
tree.push({ type: "prose", html: d.html });
|
|
207
|
+
};
|
|
208
|
+
const info = spec.info || {};
|
|
209
|
+
if (info.version) {
|
|
210
|
+
tree.push({
|
|
211
|
+
type: "prose",
|
|
212
|
+
html: `<p class="text-sm text-muted-foreground">Version ${escapeHtml(String(info.version))}</p>`,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
if (typeof info.description === "string" && info.description.trim()) {
|
|
216
|
+
describe(info.description, level);
|
|
217
|
+
}
|
|
218
|
+
const tagDescriptions = new Map();
|
|
219
|
+
for (const t of spec.tags || []) {
|
|
220
|
+
if (t?.name)
|
|
221
|
+
tagDescriptions.set(t.name, t.description || "");
|
|
222
|
+
}
|
|
223
|
+
const groups = new Map();
|
|
224
|
+
for (const t of tagDescriptions.keys())
|
|
225
|
+
groups.set(t, []);
|
|
226
|
+
const untagged = [];
|
|
227
|
+
const operations = collectOperations(spec);
|
|
228
|
+
for (const { props } of operations) {
|
|
229
|
+
// A copy: enum, default and example values are the spec's own objects, and the spec is shared.
|
|
230
|
+
const node = { type: "endpoint", props: structuredClone(props) };
|
|
231
|
+
const tag = props.tags?.[0];
|
|
232
|
+
if (!tag) {
|
|
233
|
+
untagged.push(node);
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
if (!groups.has(tag))
|
|
237
|
+
groups.set(tag, []);
|
|
238
|
+
groups.get(tag).push(node);
|
|
239
|
+
}
|
|
240
|
+
if (untagged.length)
|
|
241
|
+
groups.set("\u0000other", untagged);
|
|
242
|
+
for (const [tag, nodes] of groups) {
|
|
243
|
+
if (!nodes.length)
|
|
244
|
+
continue;
|
|
245
|
+
const text = tag === "\u0000other" ? "Other" : tag;
|
|
246
|
+
const id = uniqueId(slugifyTag(text) || "section", used);
|
|
247
|
+
tree.push({
|
|
248
|
+
type: "heading",
|
|
249
|
+
props: { level, id },
|
|
250
|
+
inline: [{ type: "text", text: text }],
|
|
251
|
+
});
|
|
252
|
+
headings.push({ depth: level, slug: id, text });
|
|
253
|
+
const description = tagDescriptions.get(tag);
|
|
254
|
+
if (description)
|
|
255
|
+
describe(description, level + 1);
|
|
256
|
+
tree.push(...nodes);
|
|
257
|
+
}
|
|
258
|
+
return { tree, headings, operations: operations.length };
|
|
259
|
+
}
|
|
260
|
+
function uniqueId(base, used) {
|
|
261
|
+
let id = base;
|
|
262
|
+
for (let n = 2; used.has(id); n++)
|
|
263
|
+
id = `${base}-${n}`;
|
|
264
|
+
used.add(id);
|
|
265
|
+
return id;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* A description (CommonMark) → HTML with its headings shifted so the
|
|
269
|
+
* highest is `minLevel`, each given a unique id, and listed for the TOC.
|
|
270
|
+
* Raw HTML stays off (see `md`).
|
|
271
|
+
*/
|
|
272
|
+
function descriptionHtml(text, minLevel, used) {
|
|
273
|
+
const tokens = md.parse(text, {});
|
|
274
|
+
const levels = tokens.filter((t) => t.type === "heading_open").map((t) => Number(t.tag.slice(1)));
|
|
275
|
+
const headings = [];
|
|
276
|
+
if (levels.length) {
|
|
277
|
+
const shift = Math.max(0, minLevel - Math.min(...levels));
|
|
278
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
279
|
+
const t = tokens[i];
|
|
280
|
+
if (t.type !== "heading_open")
|
|
281
|
+
continue;
|
|
282
|
+
const depth = Math.min(6, Number(t.tag.slice(1)) + shift);
|
|
283
|
+
const inline = tokens[i + 1];
|
|
284
|
+
const label = (inline?.children ?? [])
|
|
285
|
+
.filter((c) => c.type === "text" || c.type === "code_inline")
|
|
286
|
+
.map((c) => c.content)
|
|
287
|
+
.join("")
|
|
288
|
+
.trim();
|
|
289
|
+
const id = uniqueId(slugifyTag(label) || "section", used);
|
|
290
|
+
t.tag = `h${depth}`;
|
|
291
|
+
t.attrSet("id", id);
|
|
292
|
+
if (tokens[i + 2]?.type === "heading_close")
|
|
293
|
+
tokens[i + 2].tag = `h${depth}`;
|
|
294
|
+
headings.push({ depth, slug: id, text: label });
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
return { html: md.renderer.render(tokens, md.options, {}).trim(), headings };
|
|
298
|
+
}
|
|
299
|
+
function normalisePrefix(p) {
|
|
300
|
+
if (!p)
|
|
301
|
+
return "";
|
|
302
|
+
return p.replace(/\/+$/, "");
|
|
303
|
+
}
|
|
304
|
+
/** The one non-null type of a schema: OpenAPI 3.1 (and an upgraded 2.0
|
|
305
|
+
* spec) writes a nullable object as `type: [object, "null"]`. */
|
|
306
|
+
function primaryType(schema) {
|
|
307
|
+
if (!schema)
|
|
308
|
+
return undefined;
|
|
309
|
+
if (!Array.isArray(schema.type))
|
|
310
|
+
return schema.type;
|
|
311
|
+
const types = schema.type.filter((t) => t !== "null");
|
|
312
|
+
return types.length === 1 ? types[0] : undefined;
|
|
313
|
+
}
|
|
314
|
+
function schemaToType(schema, depth = 0) {
|
|
315
|
+
if (!schema)
|
|
316
|
+
return "any";
|
|
317
|
+
// An array of itself (a cycle once dereferenced) has no finite name.
|
|
318
|
+
if (depth > SCHEMA_MAX_DEPTH)
|
|
319
|
+
return "any";
|
|
320
|
+
// OpenAPI 3.1 (and a Swagger 2.0 spec upgraded to it) writes nullable as
|
|
321
|
+
// a type list: `type: [string, "null"]`.
|
|
322
|
+
if (Array.isArray(schema.type)) {
|
|
323
|
+
const types = schema.type.filter((t) => t !== "null");
|
|
324
|
+
if (types.length === 1)
|
|
325
|
+
return schemaToType({ ...schema, type: types[0] }, depth + 1);
|
|
326
|
+
return types.length ? types.join(" | ") : "null";
|
|
327
|
+
}
|
|
328
|
+
if (schema.type === "array") {
|
|
329
|
+
const itemType = schema.items ? schemaToType(schema.items, depth + 1) : "any";
|
|
330
|
+
return `${itemType}[]`;
|
|
331
|
+
}
|
|
332
|
+
return schema.type || "any";
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Bounds on walking one schema. A dereferenced spec is a GRAPH: a
|
|
336
|
+
* self-referencing schema is a cycle (the walk overflowed the stack), and
|
|
337
|
+
* eight properties that each reference the level below grow 8× per level
|
|
338
|
+
* (a 2.2 KB spec made 18 MB of tree at depth 6). Past a bound the walk stops
|
|
339
|
+
* and the property shows without children.
|
|
340
|
+
*/
|
|
341
|
+
const SCHEMA_MAX_DEPTH = 10;
|
|
342
|
+
const SCHEMA_MAX_NODES = 2000;
|
|
343
|
+
const newWalk = () => ({ nodes: SCHEMA_MAX_NODES, depth: 0, ancestors: new Set() });
|
|
344
|
+
/** Run `f` one level into `schema`, or return `empty` past a bound. */
|
|
345
|
+
function descend(walk, schema, empty, f) {
|
|
346
|
+
if (!schema || typeof schema !== "object")
|
|
347
|
+
return f();
|
|
348
|
+
if (walk.depth >= SCHEMA_MAX_DEPTH || walk.nodes <= 0 || walk.ancestors.has(schema))
|
|
349
|
+
return empty;
|
|
350
|
+
walk.depth++;
|
|
351
|
+
walk.ancestors.add(schema);
|
|
352
|
+
try {
|
|
353
|
+
return f();
|
|
354
|
+
}
|
|
355
|
+
finally {
|
|
356
|
+
walk.depth--;
|
|
357
|
+
walk.ancestors.delete(schema);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
function extractSchemaProperties(schema, requiredFields, walk = newWalk()) {
|
|
361
|
+
if (!schema?.properties)
|
|
362
|
+
return [];
|
|
363
|
+
return descend(walk, schema, [], () => propertiesOf(schema, requiredFields, walk));
|
|
364
|
+
}
|
|
365
|
+
function propertiesOf(schema, requiredFields, walk) {
|
|
366
|
+
const required = requiredFields || schema.required || [];
|
|
367
|
+
const out = [];
|
|
368
|
+
for (const [name, prop] of Object.entries(schema.properties)) {
|
|
369
|
+
if (walk.nodes-- <= 0)
|
|
370
|
+
break;
|
|
371
|
+
out.push(propertyOf(name, prop ?? {}, required, walk));
|
|
372
|
+
}
|
|
373
|
+
return out;
|
|
374
|
+
}
|
|
375
|
+
function propertyOf(name, prop, required, walk) {
|
|
376
|
+
const result = {
|
|
377
|
+
name,
|
|
378
|
+
type: schemaToType(prop),
|
|
379
|
+
required: required.includes(name),
|
|
380
|
+
};
|
|
381
|
+
if (prop.description)
|
|
382
|
+
result.description = prop.description;
|
|
383
|
+
if (prop.nullable)
|
|
384
|
+
result.nullable = true;
|
|
385
|
+
if (prop.format)
|
|
386
|
+
result.format = prop.format;
|
|
387
|
+
if (prop.enum)
|
|
388
|
+
result.enum = prop.enum;
|
|
389
|
+
if (prop.default !== undefined)
|
|
390
|
+
result.default = prop.default;
|
|
391
|
+
if (prop.example !== undefined)
|
|
392
|
+
result.example = prop.example;
|
|
393
|
+
if (primaryType(prop) === "object" && prop.properties) {
|
|
394
|
+
result.properties = extractSchemaProperties(prop, undefined, walk);
|
|
395
|
+
}
|
|
396
|
+
if (primaryType(prop) === "array" && prop.items) {
|
|
397
|
+
if (primaryType(prop.items) === "object" && prop.items.properties) {
|
|
398
|
+
result.items = extractSchemaProperties(prop.items, undefined, walk);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
return result;
|
|
402
|
+
}
|
|
403
|
+
function extractParameters(operation, pathItem) {
|
|
404
|
+
const params = [];
|
|
405
|
+
const allParams = [
|
|
406
|
+
...(pathItem.parameters || []),
|
|
407
|
+
...(operation.parameters || []),
|
|
408
|
+
];
|
|
409
|
+
// Operation-level wins over path-level on (name, in) collision.
|
|
410
|
+
const seen = new Map();
|
|
411
|
+
for (const p of allParams) {
|
|
412
|
+
if (!p?.name || !p?.in)
|
|
413
|
+
continue;
|
|
414
|
+
seen.set(`${p.name}:${p.in}`, p);
|
|
415
|
+
}
|
|
416
|
+
for (const p of seen.values()) {
|
|
417
|
+
const param = {
|
|
418
|
+
name: p.name,
|
|
419
|
+
in: p.in,
|
|
420
|
+
type: schemaToType(p.schema),
|
|
421
|
+
required: p.required || false,
|
|
422
|
+
};
|
|
423
|
+
if (p.description)
|
|
424
|
+
param.description = p.description;
|
|
425
|
+
if (p.schema?.default !== undefined) {
|
|
426
|
+
param.default = String(p.schema.default);
|
|
427
|
+
}
|
|
428
|
+
if (p.schema?.enum)
|
|
429
|
+
param.enum = p.schema.enum;
|
|
430
|
+
if (p.deprecated)
|
|
431
|
+
param.deprecated = true;
|
|
432
|
+
params.push(param);
|
|
433
|
+
}
|
|
434
|
+
return params;
|
|
435
|
+
}
|
|
436
|
+
function extractRequestBody(operation) {
|
|
437
|
+
const body = operation.requestBody;
|
|
438
|
+
if (!body?.content)
|
|
439
|
+
return {};
|
|
440
|
+
const mediaType = body.content["application/json"] ||
|
|
441
|
+
Object.values(body.content)[0];
|
|
442
|
+
if (!mediaType?.schema)
|
|
443
|
+
return {};
|
|
444
|
+
return {
|
|
445
|
+
requestBody: extractSchemaProperties(mediaType.schema),
|
|
446
|
+
requestBodyExample: generateExampleFromSchema(mediaType.schema),
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
function extractResponses(operation) {
|
|
450
|
+
if (!operation.responses)
|
|
451
|
+
return [];
|
|
452
|
+
return Object.entries(operation.responses)
|
|
453
|
+
.filter(([status]) => status !== "default")
|
|
454
|
+
.map(([status, resp]) => {
|
|
455
|
+
const result = {
|
|
456
|
+
status,
|
|
457
|
+
description: resp.description,
|
|
458
|
+
};
|
|
459
|
+
if (resp.content) {
|
|
460
|
+
const contentTypeKey = resp.content["application/json"]
|
|
461
|
+
? "application/json"
|
|
462
|
+
: Object.keys(resp.content)[0];
|
|
463
|
+
const mediaType = resp.content[contentTypeKey];
|
|
464
|
+
if (contentTypeKey)
|
|
465
|
+
result.contentType = contentTypeKey;
|
|
466
|
+
if (mediaType?.schema) {
|
|
467
|
+
const rawSchema = mediaType.schema;
|
|
468
|
+
const schema = primaryType(rawSchema) === "array" && rawSchema.items
|
|
469
|
+
? rawSchema.items
|
|
470
|
+
: rawSchema;
|
|
471
|
+
result.schema = extractSchemaProperties(schema);
|
|
472
|
+
result.example = generateExampleFromSchema(rawSchema);
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
return result;
|
|
476
|
+
})
|
|
477
|
+
.sort((a, b) => a.status.localeCompare(b.status));
|
|
478
|
+
}
|
|
479
|
+
function generateExampleFromSchema(schema, walk = newWalk()) {
|
|
480
|
+
if (!schema)
|
|
481
|
+
return undefined;
|
|
482
|
+
if (walk.nodes-- <= 0)
|
|
483
|
+
return null;
|
|
484
|
+
return descend(walk, schema, null, () => exampleOf(schema, walk));
|
|
485
|
+
}
|
|
486
|
+
function exampleOf(schema, walk) {
|
|
487
|
+
if (schema.example !== undefined)
|
|
488
|
+
return schema.example;
|
|
489
|
+
switch (primaryType(schema)) {
|
|
490
|
+
case "object": {
|
|
491
|
+
if (!schema.properties)
|
|
492
|
+
return {};
|
|
493
|
+
const obj = {};
|
|
494
|
+
for (const [name, prop] of Object.entries(schema.properties)) {
|
|
495
|
+
obj[name] = generateExampleFromSchema(prop, walk);
|
|
496
|
+
}
|
|
497
|
+
return obj;
|
|
498
|
+
}
|
|
499
|
+
case "array":
|
|
500
|
+
return schema.items ? [generateExampleFromSchema(schema.items, walk)] : [];
|
|
501
|
+
case "string": {
|
|
502
|
+
if (schema.enum)
|
|
503
|
+
return schema.enum[0];
|
|
504
|
+
if (schema.format === "date-time")
|
|
505
|
+
return "2024-01-01T00:00:00Z";
|
|
506
|
+
if (schema.format === "email")
|
|
507
|
+
return "user@example.com";
|
|
508
|
+
if (schema.format === "uri" || schema.format === "url") {
|
|
509
|
+
return "https://example.com";
|
|
510
|
+
}
|
|
511
|
+
if (schema.format === "uuid") {
|
|
512
|
+
return "550e8400-e29b-41d4-a716-446655440000";
|
|
513
|
+
}
|
|
514
|
+
if (schema.default !== undefined)
|
|
515
|
+
return schema.default;
|
|
516
|
+
return "string";
|
|
517
|
+
}
|
|
518
|
+
case "integer":
|
|
519
|
+
return schema.example ?? schema.default ?? 0;
|
|
520
|
+
case "number":
|
|
521
|
+
return schema.example ?? schema.default ?? 0.0;
|
|
522
|
+
case "boolean":
|
|
523
|
+
return schema.example ?? schema.default ?? true;
|
|
524
|
+
default:
|
|
525
|
+
return null;
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
function buildTagPage(tagName, tagSlug, description, ops) {
|
|
529
|
+
const tree = [
|
|
530
|
+
{
|
|
531
|
+
type: "heading",
|
|
532
|
+
props: { level: 1, id: tagSlug },
|
|
533
|
+
inline: [{ type: "text", text: tagName }],
|
|
534
|
+
},
|
|
535
|
+
];
|
|
536
|
+
const used = new Set([tagSlug]);
|
|
537
|
+
const described = description ? descriptionHtml(description, 2, used) : null;
|
|
538
|
+
if (described)
|
|
539
|
+
tree.push({ type: "prose", html: described.html });
|
|
540
|
+
const items = ops.map((op) => ({
|
|
541
|
+
type: "list-item",
|
|
542
|
+
children: [
|
|
543
|
+
{
|
|
544
|
+
type: "prose",
|
|
545
|
+
html: `<a href="${escapeAttr(op.slug)}"><code>${op.method.toUpperCase()}</code> ${escapeHtml(op.path)}</a>${op.deprecated ? ' <span class="text-muted-foreground">(deprecated)</span>' : ""}`,
|
|
546
|
+
},
|
|
547
|
+
],
|
|
548
|
+
}));
|
|
549
|
+
tree.push({ type: "unordered-list", children: items });
|
|
550
|
+
return {
|
|
551
|
+
slug: tagSlug,
|
|
552
|
+
title: tagName,
|
|
553
|
+
tree,
|
|
554
|
+
headings: [{ depth: 1, slug: tagSlug, text: tagName }, ...(described?.headings ?? [])],
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
function buildInfoPage(title, description, version) {
|
|
558
|
+
const tree = [
|
|
559
|
+
{
|
|
560
|
+
type: "heading",
|
|
561
|
+
props: { level: 1, id: "api-reference" },
|
|
562
|
+
inline: [{ type: "text", text: title }],
|
|
563
|
+
},
|
|
564
|
+
];
|
|
565
|
+
if (version) {
|
|
566
|
+
tree.push({
|
|
567
|
+
type: "prose",
|
|
568
|
+
html: `<p class="text-sm text-muted-foreground">Version ${escapeHtml(version)}</p>`,
|
|
569
|
+
});
|
|
570
|
+
}
|
|
571
|
+
const described = description ? descriptionHtml(description, 2, new Set(["api-reference"])) : null;
|
|
572
|
+
if (described)
|
|
573
|
+
tree.push({ type: "prose", html: described.html });
|
|
574
|
+
return {
|
|
575
|
+
slug: "index",
|
|
576
|
+
title,
|
|
577
|
+
tree,
|
|
578
|
+
headings: [{ depth: 1, slug: "api-reference", text: title }, ...(described?.headings ?? [])],
|
|
579
|
+
};
|
|
580
|
+
}
|
|
581
|
+
function buildNav(spec, pages, tagToOps, untagged, hrefPrefix) {
|
|
582
|
+
const nav = [];
|
|
583
|
+
const root = {
|
|
584
|
+
label: (spec.info && spec.info.title) || "API Reference",
|
|
585
|
+
href: hrefHref(hrefPrefix, "index"),
|
|
586
|
+
};
|
|
587
|
+
nav.push(root);
|
|
588
|
+
// Use spec.tags[] order when present so authors control nav order.
|
|
589
|
+
const declaredTagOrder = Array.isArray(spec.tags)
|
|
590
|
+
? spec.tags.map((t) => t?.name).filter(Boolean)
|
|
591
|
+
: [];
|
|
592
|
+
const seenTags = new Set();
|
|
593
|
+
const tagOrder = [];
|
|
594
|
+
for (const t of declaredTagOrder) {
|
|
595
|
+
if (tagToOps.has(t)) {
|
|
596
|
+
tagOrder.push(t);
|
|
597
|
+
seenTags.add(t);
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
for (const t of tagToOps.keys()) {
|
|
601
|
+
if (!seenTags.has(t))
|
|
602
|
+
tagOrder.push(t);
|
|
603
|
+
}
|
|
604
|
+
for (const tagName of tagOrder) {
|
|
605
|
+
const ops = tagToOps.get(tagName) || [];
|
|
606
|
+
const tagSlug = slugifyTag(tagName);
|
|
607
|
+
nav.push({
|
|
608
|
+
label: tagName,
|
|
609
|
+
href: hrefHref(hrefPrefix, tagSlug),
|
|
610
|
+
children: ops
|
|
611
|
+
.slice()
|
|
612
|
+
.sort(byMethodThenPath)
|
|
613
|
+
.map((op) => ({
|
|
614
|
+
label: op.title,
|
|
615
|
+
href: hrefHref(hrefPrefix, op.slug),
|
|
616
|
+
})),
|
|
617
|
+
});
|
|
618
|
+
}
|
|
619
|
+
if (untagged.length > 0) {
|
|
620
|
+
nav.push({
|
|
621
|
+
label: "Other",
|
|
622
|
+
children: untagged
|
|
623
|
+
.slice()
|
|
624
|
+
.sort(byMethodThenPath)
|
|
625
|
+
.map((op) => ({
|
|
626
|
+
label: op.title,
|
|
627
|
+
href: hrefHref(hrefPrefix, op.slug),
|
|
628
|
+
})),
|
|
629
|
+
});
|
|
630
|
+
}
|
|
631
|
+
return nav;
|
|
632
|
+
}
|
|
633
|
+
function byMethodThenPath(a, b) {
|
|
634
|
+
const m = a.method.localeCompare(b.method);
|
|
635
|
+
if (m !== 0)
|
|
636
|
+
return m;
|
|
637
|
+
return a.path.localeCompare(b.path);
|
|
638
|
+
}
|
|
639
|
+
function hrefHref(prefix, slug) {
|
|
640
|
+
const p = prefix ? `${prefix}/` : "/";
|
|
641
|
+
return slug === "index" ? p : `${p}${slug}`;
|
|
642
|
+
}
|
|
643
|
+
function escapeHtml(s) {
|
|
644
|
+
return s
|
|
645
|
+
.replace(/&/g, "&")
|
|
646
|
+
.replace(/</g, "<")
|
|
647
|
+
.replace(/>/g, ">");
|
|
648
|
+
}
|
|
649
|
+
function escapeAttr(s) {
|
|
650
|
+
return s.replace(/&/g, "&").replace(/"/g, """);
|
|
651
|
+
}
|
|
652
|
+
/**
|
|
653
|
+
* A spec description (CommonMark, per OpenAPI) → HTML, raw HTML off: a `<`
|
|
654
|
+
* in the spec is escaped, and markdown-it refuses `javascript:` links.
|
|
655
|
+
* Plain-text paragraphs showed a spec's `## Authentication` literally.
|
|
656
|
+
*/
|
|
657
|
+
const md = new MarkdownIt({ html: false, linkify: false });
|
|
658
|
+
//# sourceMappingURL=core.js.map
|