fumadocs-openapi 2.0.2 → 2.0.3
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/index.js +29 -41
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -11,8 +11,7 @@ function createElement(name, props, ...child) {
|
|
|
11
11
|
return s.join("\n\n");
|
|
12
12
|
}
|
|
13
13
|
function p(child) {
|
|
14
|
-
if (!child)
|
|
15
|
-
return "";
|
|
14
|
+
if (!child) return "";
|
|
16
15
|
return child.replace("<", "\\<").replace(">", "\\>");
|
|
17
16
|
}
|
|
18
17
|
function span(child) {
|
|
@@ -67,10 +66,8 @@ function noRef(v) {
|
|
|
67
66
|
return v;
|
|
68
67
|
}
|
|
69
68
|
function getPreferredMedia(body) {
|
|
70
|
-
if (Object.keys(body).length === 0)
|
|
71
|
-
|
|
72
|
-
if ("application/json" in body)
|
|
73
|
-
return body["application/json"];
|
|
69
|
+
if (Object.keys(body).length === 0) return void 0;
|
|
70
|
+
if ("application/json" in body) return body["application/json"];
|
|
74
71
|
return Object.values(body)[0];
|
|
75
72
|
}
|
|
76
73
|
function getValue(value) {
|
|
@@ -85,8 +82,7 @@ function createEndpoint(path, method, baseUrl = "https://example.com") {
|
|
|
85
82
|
const schema = noRef(
|
|
86
83
|
param.schema ?? getPreferredMedia(param.content ?? {})?.schema
|
|
87
84
|
);
|
|
88
|
-
if (!schema)
|
|
89
|
-
continue;
|
|
85
|
+
if (!schema) continue;
|
|
90
86
|
params.push({
|
|
91
87
|
name: param.name,
|
|
92
88
|
in: param.in,
|
|
@@ -98,8 +94,7 @@ function createEndpoint(path, method, baseUrl = "https://example.com") {
|
|
|
98
94
|
for (const [code, value] of Object.entries(method.responses)) {
|
|
99
95
|
const mediaTypes = noRef(value).content ?? {};
|
|
100
96
|
const responseSchema = noRef(getPreferredMedia(mediaTypes)?.schema);
|
|
101
|
-
if (!responseSchema)
|
|
102
|
-
continue;
|
|
97
|
+
if (!responseSchema) continue;
|
|
103
98
|
responses[code] = {
|
|
104
99
|
schema: responseSchema
|
|
105
100
|
};
|
|
@@ -108,8 +103,7 @@ function createEndpoint(path, method, baseUrl = "https://example.com") {
|
|
|
108
103
|
const queryParams = new URLSearchParams();
|
|
109
104
|
for (const param of params) {
|
|
110
105
|
const value = generateSample(method.method, param.schema);
|
|
111
|
-
if (param.in === "query")
|
|
112
|
-
queryParams.append(param.name, getValue(value));
|
|
106
|
+
if (param.in === "query") queryParams.append(param.name, getValue(value));
|
|
113
107
|
if (param.in === "path")
|
|
114
108
|
pathWithParameters = pathWithParameters.replace(
|
|
115
109
|
`{${param.name}}`,
|
|
@@ -169,8 +163,7 @@ function getSampleRequest(endpoint) {
|
|
|
169
163
|
console.log("Request example for form data is not supported");
|
|
170
164
|
}
|
|
171
165
|
}
|
|
172
|
-
if (endpoint.body)
|
|
173
|
-
s.push(`-d '${getValue2(endpoint.body)}'`);
|
|
166
|
+
if (endpoint.body) s.push(`-d '${getValue2(endpoint.body)}'`);
|
|
174
167
|
return s.join(" \\\n ");
|
|
175
168
|
}
|
|
176
169
|
function getValue2(value) {
|
|
@@ -192,10 +185,8 @@ function isObject(schema) {
|
|
|
192
185
|
return schema.type === "object" || schema.properties !== void 0;
|
|
193
186
|
}
|
|
194
187
|
function schemaElement(name, schema, { parseObject, ...ctx }) {
|
|
195
|
-
if (schema.readOnly && !ctx.readOnly)
|
|
196
|
-
|
|
197
|
-
if (schema.writeOnly && !ctx.writeOnly)
|
|
198
|
-
return "";
|
|
188
|
+
if (schema.readOnly && !ctx.readOnly) return "";
|
|
189
|
+
if (schema.writeOnly && !ctx.writeOnly) return "";
|
|
199
190
|
const child = [];
|
|
200
191
|
function field(key, value) {
|
|
201
192
|
child.push(span(`${key}: \`${value}\``));
|
|
@@ -269,14 +260,14 @@ function schemaElement(name, schema, { parseObject, ...ctx }) {
|
|
|
269
260
|
);
|
|
270
261
|
}
|
|
271
262
|
function resolveObjectType(schema) {
|
|
272
|
-
if (isObject(schema))
|
|
273
|
-
return schema;
|
|
263
|
+
if (isObject(schema)) return schema;
|
|
274
264
|
if (schema.type === "array") {
|
|
275
265
|
return resolveObjectType(noRef(schema.items));
|
|
276
266
|
}
|
|
277
267
|
}
|
|
278
268
|
function getSchemaType(schema) {
|
|
279
269
|
if (schema.nullable) {
|
|
270
|
+
if (!schema.type) return "null";
|
|
280
271
|
return `${getSchemaType({ ...schema, nullable: false })} | null`;
|
|
281
272
|
}
|
|
282
273
|
if (schema.type === "array")
|
|
@@ -287,10 +278,8 @@ function getSchemaType(schema) {
|
|
|
287
278
|
return schema.allOf.map((one) => getSchemaType(noRef(one))).join(" & ");
|
|
288
279
|
if (schema.anyOf)
|
|
289
280
|
return `Any properties in ${schema.anyOf.map((one) => getSchemaType(noRef(one))).join(", ")}`;
|
|
290
|
-
if (schema.type)
|
|
291
|
-
|
|
292
|
-
if (isObject(schema))
|
|
293
|
-
return "object";
|
|
281
|
+
if (schema.type) return schema.type;
|
|
282
|
+
if (isObject(schema)) return "object";
|
|
294
283
|
throw new Error(`Cannot detect object type: ${JSON.stringify(schema)}`);
|
|
295
284
|
}
|
|
296
285
|
|
|
@@ -299,15 +288,12 @@ async function renderOperation(path, method, baseUrl) {
|
|
|
299
288
|
const info = [];
|
|
300
289
|
const example = [];
|
|
301
290
|
const title = method.summary ?? method.operationId;
|
|
302
|
-
if (title)
|
|
303
|
-
|
|
304
|
-
if (method.description)
|
|
305
|
-
info.push(p(method.description));
|
|
291
|
+
if (title) info.push(`## ${title}`);
|
|
292
|
+
if (method.description) info.push(p(method.description));
|
|
306
293
|
const body = noRef(method.requestBody);
|
|
307
294
|
if (body) {
|
|
308
295
|
const bodySchema = getPreferredMedia(body.content)?.schema;
|
|
309
|
-
if (!bodySchema)
|
|
310
|
-
throw new Error();
|
|
296
|
+
if (!bodySchema) throw new Error();
|
|
311
297
|
info.push(
|
|
312
298
|
`### Request Body${!body.required ? " (Optional)" : ""}`,
|
|
313
299
|
p(body.description),
|
|
@@ -325,8 +311,7 @@ async function renderOperation(path, method, baseUrl) {
|
|
|
325
311
|
const schema = noRef(
|
|
326
312
|
param.schema ?? getPreferredMedia(param.content ?? {})?.schema
|
|
327
313
|
);
|
|
328
|
-
if (!schema)
|
|
329
|
-
continue;
|
|
314
|
+
if (!schema) continue;
|
|
330
315
|
const content = schemaElement(
|
|
331
316
|
param.name,
|
|
332
317
|
{
|
|
@@ -397,8 +382,7 @@ async function getResponseTabs(endpoint, operation) {
|
|
|
397
382
|
);
|
|
398
383
|
}
|
|
399
384
|
}
|
|
400
|
-
if (items.length === 0)
|
|
401
|
-
return "";
|
|
385
|
+
if (items.length === 0) return "";
|
|
402
386
|
return tabs(
|
|
403
387
|
{
|
|
404
388
|
items
|
|
@@ -416,16 +400,20 @@ async function generate(pathOrDocument, options = {}) {
|
|
|
416
400
|
const tag = options.tag ? document.tags?.find((item) => item.name === options.tag) : void 0;
|
|
417
401
|
const routes = Object.entries(document.paths).map(
|
|
418
402
|
([key, value]) => {
|
|
419
|
-
if (!value)
|
|
420
|
-
|
|
421
|
-
|
|
403
|
+
if (!value) throw new Error("Invalid schema");
|
|
404
|
+
const methodKeys = [
|
|
405
|
+
"get",
|
|
406
|
+
"post",
|
|
407
|
+
"patch",
|
|
408
|
+
"delete",
|
|
409
|
+
"head",
|
|
410
|
+
"put"
|
|
411
|
+
];
|
|
422
412
|
const methods = [];
|
|
423
413
|
for (const methodKey of methodKeys) {
|
|
424
414
|
const operation = value[methodKey];
|
|
425
|
-
if (!operation)
|
|
426
|
-
|
|
427
|
-
if (tag && !operation.tags?.includes(tag.name))
|
|
428
|
-
continue;
|
|
415
|
+
if (!operation) continue;
|
|
416
|
+
if (tag && !operation.tags?.includes(tag.name)) continue;
|
|
429
417
|
methods.push(buildOperation(methodKey, operation));
|
|
430
418
|
}
|
|
431
419
|
return {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fumadocs-openapi",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.3",
|
|
4
4
|
"description": "Generate MDX docs for your OpenAPI spec",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"NextJs",
|
|
@@ -17,9 +17,9 @@
|
|
|
17
17
|
"dist"
|
|
18
18
|
],
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@apidevtools/json-schema-ref-parser": "^11.6.
|
|
20
|
+
"@apidevtools/json-schema-ref-parser": "^11.6.4",
|
|
21
21
|
"fast-glob": "^3.3.1",
|
|
22
|
-
"json-schema-to-typescript": "^14.0.
|
|
22
|
+
"json-schema-to-typescript": "^14.0.5",
|
|
23
23
|
"openapi-sampler": "^1.5.1"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|