docusaurus-plugin-openapi-docs 0.0.0-360 → 0.0.0-361
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/lib/openapi/createExample.js +59 -49
- package/package.json +2 -2
- package/src/openapi/createExample.ts +59 -50
|
@@ -5,8 +5,12 @@
|
|
|
5
5
|
* This source code is licensed under the MIT license found in the
|
|
6
6
|
* LICENSE file in the root directory of this source tree.
|
|
7
7
|
* ========================================================================== */
|
|
8
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
+
};
|
|
8
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
12
|
exports.sampleFromSchema = void 0;
|
|
13
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
10
14
|
const primitives = {
|
|
11
15
|
string: {
|
|
12
16
|
default: () => "string",
|
|
@@ -31,64 +35,70 @@ const primitives = {
|
|
|
31
35
|
array: {},
|
|
32
36
|
};
|
|
33
37
|
const sampleFromSchema = (schema = {}) => {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
if (allOf) {
|
|
39
|
-
// TODO: We are just assuming it will always be an object for now
|
|
40
|
-
let obj = {
|
|
41
|
-
type: "object",
|
|
42
|
-
properties: {},
|
|
43
|
-
required: [], // NOTE: We shouldn't need to worry about required
|
|
44
|
-
};
|
|
45
|
-
for (let item of allOf) {
|
|
46
|
-
if (item.properties) {
|
|
47
|
-
obj.properties = {
|
|
48
|
-
...obj.properties,
|
|
49
|
-
...item.properties,
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
return (0, exports.sampleFromSchema)(obj);
|
|
54
|
-
}
|
|
55
|
-
if (!type) {
|
|
56
|
-
if (properties) {
|
|
57
|
-
type = "object";
|
|
38
|
+
try {
|
|
39
|
+
let { type, example, allOf, properties, items } = schema;
|
|
40
|
+
if (example !== undefined) {
|
|
41
|
+
return example;
|
|
58
42
|
}
|
|
59
|
-
|
|
60
|
-
|
|
43
|
+
if (allOf) {
|
|
44
|
+
// TODO: We are just assuming it will always be an object for now
|
|
45
|
+
let obj = {
|
|
46
|
+
type: "object",
|
|
47
|
+
properties: {},
|
|
48
|
+
required: [], // NOTE: We shouldn't need to worry about required
|
|
49
|
+
};
|
|
50
|
+
for (let item of allOf) {
|
|
51
|
+
if (item.properties) {
|
|
52
|
+
obj.properties = {
|
|
53
|
+
...obj.properties,
|
|
54
|
+
...item.properties,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return (0, exports.sampleFromSchema)(obj);
|
|
61
59
|
}
|
|
62
|
-
|
|
63
|
-
|
|
60
|
+
if (!type) {
|
|
61
|
+
if (properties) {
|
|
62
|
+
type = "object";
|
|
63
|
+
}
|
|
64
|
+
else if (items) {
|
|
65
|
+
type = "array";
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
64
70
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
+
if (type === "object") {
|
|
72
|
+
let obj = {};
|
|
73
|
+
for (let [name, prop] of Object.entries(properties !== null && properties !== void 0 ? properties : {})) {
|
|
74
|
+
if (prop.deprecated) {
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
obj[name] = (0, exports.sampleFromSchema)(prop);
|
|
71
78
|
}
|
|
72
|
-
obj
|
|
79
|
+
return obj;
|
|
73
80
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
81
|
+
if (type === "array") {
|
|
82
|
+
if (Array.isArray(items === null || items === void 0 ? void 0 : items.anyOf)) {
|
|
83
|
+
return items === null || items === void 0 ? void 0 : items.anyOf.map((item) => (0, exports.sampleFromSchema)(item));
|
|
84
|
+
}
|
|
85
|
+
if (Array.isArray(items === null || items === void 0 ? void 0 : items.oneOf)) {
|
|
86
|
+
return items === null || items === void 0 ? void 0 : items.oneOf.map((item) => (0, exports.sampleFromSchema)(item));
|
|
87
|
+
}
|
|
88
|
+
return [(0, exports.sampleFromSchema)(items)];
|
|
79
89
|
}
|
|
80
|
-
if (
|
|
81
|
-
|
|
90
|
+
if (schema.enum) {
|
|
91
|
+
if (schema.default) {
|
|
92
|
+
return schema.default;
|
|
93
|
+
}
|
|
94
|
+
return normalizeArray(schema.enum)[0];
|
|
82
95
|
}
|
|
83
|
-
return
|
|
96
|
+
return primitive(schema);
|
|
84
97
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
return normalizeArray(schema.enum)[0];
|
|
98
|
+
catch (err) {
|
|
99
|
+
console.error(chalk_1.default.yellow("WARNING: failed to create example from schema object:", err));
|
|
100
|
+
return;
|
|
90
101
|
}
|
|
91
|
-
return primitive(schema);
|
|
92
102
|
};
|
|
93
103
|
exports.sampleFromSchema = sampleFromSchema;
|
|
94
104
|
function primitive(schema = {}) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "docusaurus-plugin-openapi-docs",
|
|
3
3
|
"description": "OpenAPI plugin for Docusaurus.",
|
|
4
|
-
"version": "0.0.0-
|
|
4
|
+
"version": "0.0.0-361",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"openapi",
|
|
@@ -60,5 +60,5 @@
|
|
|
60
60
|
"engines": {
|
|
61
61
|
"node": ">=14"
|
|
62
62
|
},
|
|
63
|
-
"gitHead": "
|
|
63
|
+
"gitHead": "e87c5db5b36a32b85f7ba0aaea0a0280be498954"
|
|
64
64
|
}
|
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
* ========================================================================== */
|
|
7
7
|
|
|
8
|
+
import chalk from "chalk";
|
|
9
|
+
|
|
8
10
|
import { SchemaObject } from "./types";
|
|
9
11
|
|
|
10
12
|
interface OASTypeToTypeMap {
|
|
@@ -48,71 +50,78 @@ const primitives: Primitives = {
|
|
|
48
50
|
};
|
|
49
51
|
|
|
50
52
|
export const sampleFromSchema = (schema: SchemaObject = {}): any => {
|
|
51
|
-
|
|
53
|
+
try {
|
|
54
|
+
let { type, example, allOf, properties, items } = schema;
|
|
52
55
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
+
if (example !== undefined) {
|
|
57
|
+
return example;
|
|
58
|
+
}
|
|
56
59
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
60
|
+
if (allOf) {
|
|
61
|
+
// TODO: We are just assuming it will always be an object for now
|
|
62
|
+
let obj: SchemaObject = {
|
|
63
|
+
type: "object",
|
|
64
|
+
properties: {},
|
|
65
|
+
required: [], // NOTE: We shouldn't need to worry about required
|
|
66
|
+
};
|
|
67
|
+
for (let item of allOf) {
|
|
68
|
+
if (item.properties) {
|
|
69
|
+
obj.properties = {
|
|
70
|
+
...obj.properties,
|
|
71
|
+
...item.properties,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
70
74
|
}
|
|
75
|
+
return sampleFromSchema(obj);
|
|
71
76
|
}
|
|
72
|
-
return sampleFromSchema(obj);
|
|
73
|
-
}
|
|
74
77
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
78
|
+
if (!type) {
|
|
79
|
+
if (properties) {
|
|
80
|
+
type = "object";
|
|
81
|
+
} else if (items) {
|
|
82
|
+
type = "array";
|
|
83
|
+
} else {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
82
86
|
}
|
|
83
|
-
}
|
|
84
87
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
88
|
+
if (type === "object") {
|
|
89
|
+
let obj: any = {};
|
|
90
|
+
for (let [name, prop] of Object.entries(properties ?? {})) {
|
|
91
|
+
if (prop.deprecated) {
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
obj[name] = sampleFromSchema(prop);
|
|
90
95
|
}
|
|
91
|
-
obj
|
|
96
|
+
return obj;
|
|
92
97
|
}
|
|
93
|
-
return obj;
|
|
94
|
-
}
|
|
95
98
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
99
|
+
if (type === "array") {
|
|
100
|
+
if (Array.isArray(items?.anyOf)) {
|
|
101
|
+
return items?.anyOf.map((item) => sampleFromSchema(item));
|
|
102
|
+
}
|
|
100
103
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
+
if (Array.isArray(items?.oneOf)) {
|
|
105
|
+
return items?.oneOf.map((item) => sampleFromSchema(item));
|
|
106
|
+
}
|
|
104
107
|
|
|
105
|
-
|
|
106
|
-
|
|
108
|
+
return [sampleFromSchema(items)];
|
|
109
|
+
}
|
|
107
110
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
+
if (schema.enum) {
|
|
112
|
+
if (schema.default) {
|
|
113
|
+
return schema.default;
|
|
114
|
+
}
|
|
115
|
+
return normalizeArray(schema.enum)[0];
|
|
111
116
|
}
|
|
112
|
-
return normalizeArray(schema.enum)[0];
|
|
113
|
-
}
|
|
114
117
|
|
|
115
|
-
|
|
118
|
+
return primitive(schema);
|
|
119
|
+
} catch (err) {
|
|
120
|
+
console.error(
|
|
121
|
+
chalk.yellow("WARNING: failed to create example from schema object:", err)
|
|
122
|
+
);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
116
125
|
};
|
|
117
126
|
|
|
118
127
|
function primitive(schema: SchemaObject = {}) {
|