docusaurus-plugin-openapi-docs 4.3.7 → 4.5.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/README.md +32 -27
- package/lib/index.js +11 -6
- package/lib/markdown/createAuthentication.js +4 -2
- package/lib/markdown/schema.js +3 -0
- package/lib/openapi/createRequestExample.js +2 -179
- package/lib/openapi/createResponseExample.js +2 -180
- package/lib/openapi/createSchemaExample.d.ts +7 -0
- package/lib/openapi/createSchemaExample.js +231 -0
- package/lib/openapi/openapi.js +51 -2
- package/lib/openapi/types.d.ts +57 -59
- package/lib/openapi/utils/services/OpenAPIParser.d.ts +1 -1
- package/lib/openapi/utils/types.d.ts +1 -0
- package/lib/options.js +3 -0
- package/lib/types.d.ts +4 -1
- package/package.json +2 -2
- package/src/index.ts +13 -5
- package/src/markdown/createAuthentication.ts +12 -4
- package/src/markdown/schema.ts +3 -0
- package/src/openapi/createRequestExample.ts +2 -224
- package/src/openapi/createResponseExample.ts +2 -227
- package/src/openapi/createSchemaExample.ts +292 -0
- package/src/openapi/openapi.ts +19 -2
- package/src/openapi/types.ts +62 -59
- package/src/openapi/utils/types.ts +1 -0
- package/src/options.ts +3 -0
- package/src/plugin-openapi.d.ts +3 -2
- package/src/types.ts +5 -1
- package/src/postman-collection.d.ts +0 -10
package/README.md
CHANGED
|
@@ -41,17 +41,17 @@ Key Features:
|
|
|
41
41
|
|
|
42
42
|
| Docusaurus OpenAPI Docs | Docusaurus |
|
|
43
43
|
| ----------------------- | --------------- |
|
|
44
|
-
| 4.0.x (current) | `3.5.0 - 3.
|
|
44
|
+
| 4.0.x (current) | `3.5.0 - 3.8.1` |
|
|
45
45
|
| 3.0.x (end-of-support) | `3.0.1 - 3.4.0` |
|
|
46
46
|
| 2.2.3 (legacy) | `2.4.1 - 2.4.3` |
|
|
47
47
|
| 1.7.3 (end-of-support) | `2.0.1 - 2.2.0` |
|
|
48
48
|
|
|
49
49
|
## Bootstrapping from Template (new Docusaurus site)
|
|
50
50
|
|
|
51
|
-
Run the following to bootstrap a
|
|
51
|
+
Run the following to bootstrap a Docusaurus v3 site (classic theme) with `docusaurus-openapi-docs`:
|
|
52
52
|
|
|
53
53
|
```bash
|
|
54
|
-
npx create-docusaurus@3.
|
|
54
|
+
npx create-docusaurus@3.8.1 my-website --package-manager yarn
|
|
55
55
|
```
|
|
56
56
|
|
|
57
57
|
> When prompted to select a template choose `Git repository`.
|
|
@@ -155,22 +155,25 @@ The `docusaurus-plugin-openapi-docs` plugin can be configured with the following
|
|
|
155
155
|
|
|
156
156
|
`config` can be configured with the following options:
|
|
157
157
|
|
|
158
|
-
| Name | Type | Default | Description
|
|
159
|
-
| -------------------- | --------- | ------- |
|
|
160
|
-
| `specPath` | `string` | `null` | Designated URL or path to the source of an OpenAPI specification file or directory of multiple OpenAPI specification files.
|
|
161
|
-
| `
|
|
162
|
-
| `proxy` | `string` | `null` | _Optional:_ Proxy URL to prepend to base URL when performing API requests from browser.
|
|
163
|
-
| `template` | `string` | `null` | _Optional:_ Customize MDX content with a desired template.
|
|
164
|
-
| `
|
|
165
|
-
| `
|
|
166
|
-
| `
|
|
167
|
-
| `
|
|
168
|
-
| `
|
|
169
|
-
| `
|
|
170
|
-
| `
|
|
171
|
-
| `
|
|
172
|
-
| `
|
|
173
|
-
| `
|
|
158
|
+
| Name | Type | Default | Description |
|
|
159
|
+
| -------------------- | --------- | ------- | --------------------------------------------------------------------------------------------------------------------------- |
|
|
160
|
+
| `specPath` | `string` | `null` | Designated URL or path to the source of an OpenAPI specification file or directory of multiple OpenAPI specification files. |
|
|
161
|
+
| `outputDir` | `string` | `null` | Desired output path for generated MDX and sidebar files. |
|
|
162
|
+
| `proxy` | `string` | `null` | _Optional:_ Proxy URL to prepend to base URL when performing API requests from browser. |
|
|
163
|
+
| `template` | `string` | `null` | _Optional:_ Customize MDX content with a desired template. |
|
|
164
|
+
| `infoTemplate` | `string` | `null` | _Optional:_ Customize MDX content for **info** pages only. |
|
|
165
|
+
| `tagTemplate` | `string` | `null` | _Optional:_ Customize MDX content for **tag** pages only. |
|
|
166
|
+
| `schemaTemplate` | `string` | `null` | _Optional:_ Customize MDX content for **schema** pages only. |
|
|
167
|
+
| `downloadUrl` | `string` | `null` | _Optional:_ Designated URL for downloading OpenAPI specification. (requires `info` section/doc) |
|
|
168
|
+
| `hideSendButton` | `boolean` | `null` | _Optional:_ If set to `true`, hides the “Send API Request” button in the API demo panel. |
|
|
169
|
+
| `showExtensions` | `boolean` | `null` | _Optional:_ If set to `true`, renders operation‑level vendor‑extensions in descriptions. |
|
|
170
|
+
| `sidebarOptions` | `object` | `null` | _Optional:_ Set of options for sidebar configuration. See below for a list of supported options. |
|
|
171
|
+
| `version` | `string` | `null` | _Optional:_ Version assigned to a single or micro‑spec API specified in `specPath`. |
|
|
172
|
+
| `label` | `string` | `null` | _Optional:_ Version label used when generating the version‑selector dropdown menu. |
|
|
173
|
+
| `baseUrl` | `string` | `null` | _Optional:_ Base URL for versioned docs in the version‑selector dropdown. |
|
|
174
|
+
| `versions` | `object` | `null` | _Optional:_ Options for versioning configuration. See below for a list of supported options. |
|
|
175
|
+
| `markdownGenerators` | `object` | `null` | _Optional:_ Customize MDX content via generator functions. See below for a list of supported options. |
|
|
176
|
+
| `showSchemas` | `boolean` | `null` | _Optional:_ If set to `true`, generates standalone schema pages and adds them to the sidebar. |
|
|
174
177
|
|
|
175
178
|
### sidebarOptions
|
|
176
179
|
|
|
@@ -186,15 +189,17 @@ The `docusaurus-plugin-openapi-docs` plugin can be configured with the following
|
|
|
186
189
|
| `sidebarGenerators` | `object` | `null` | Optional: Customize sidebar rendering with callback functions. |
|
|
187
190
|
|
|
188
191
|
> You may optionally configure a `sidebarOptions`. In doing so, an individual `sidebar.js` slice with the configured options will be generated within the respective `outputDir`.
|
|
192
|
+
> Use `x-position` vendor extension (or the front matter `position`) on operations to explicitly order sidebar items.
|
|
189
193
|
|
|
190
194
|
`versions` can be configured with the following options:
|
|
191
195
|
|
|
192
|
-
| Name
|
|
193
|
-
|
|
|
194
|
-
| `specPath`
|
|
195
|
-
| `
|
|
196
|
-
| `label`
|
|
197
|
-
| `baseUrl`
|
|
196
|
+
| Name | Type | Default | Description |
|
|
197
|
+
| ------------- | -------- | ------- | -------------------------------------------------------------------------------------------------------------------------- |
|
|
198
|
+
| `specPath` | `string` | `null` | Designated URL or path to the source of an OpenAPI specification file or a directory of micro OpenAPI specification files. |
|
|
199
|
+
| `outputDir` | `string` | `null` | Desired output path for versioned, generated MDX files. |
|
|
200
|
+
| `label` | `string` | `null` | _Optional:_ Version label used when generating the version selector dropdown menu. |
|
|
201
|
+
| `baseUrl` | `string` | `null` | _Optional:_ Version base URL used when generating the version selector dropdown menu. |
|
|
202
|
+
| `downloadUrl` | `string` | `null` | _Optional:_ Designated URL for downloading the versioned OpenAPI specification. |
|
|
198
203
|
|
|
199
204
|
> All versions will automatically inherit `sidebarOptions` from the parent/base config.
|
|
200
205
|
|
|
@@ -326,11 +331,11 @@ yarn docusaurus gen-api-docs:version petstore:all
|
|
|
326
331
|
|
|
327
332
|
> This will generate API docs for all of the OpenAPI specification (OAS) files referenced in your `versions` config and will also generate a `versions.json` file.
|
|
328
333
|
|
|
329
|
-
>
|
|
334
|
+
> Substitute `all` with a specific version ID to generate or clean a specific version. Generating for `all` or a single version ID will automatically update the `versions.json` file.
|
|
330
335
|
|
|
331
336
|
## Developer Quick Start
|
|
332
337
|
|
|
333
|
-
> Looking to make a contribution? Make sure to
|
|
338
|
+
> Looking to make a contribution? Make sure to check out our contributing guide.
|
|
334
339
|
|
|
335
340
|
After [forking](https://github.com/PaloAltoNetworks/docusaurus-openapi-docs/fork) the main repository, run the following:
|
|
336
341
|
|
package/lib/index.js
CHANGED
|
@@ -78,7 +78,7 @@ function pluginOpenAPIDocs(context, options) {
|
|
|
78
78
|
let docPath = docData ? (docData.path ? docData.path : "docs") : undefined;
|
|
79
79
|
async function generateApiDocs(options, pluginId) {
|
|
80
80
|
var _a, _b, _c, _d;
|
|
81
|
-
let { specPath, outputDir, template, markdownGenerators, downloadUrl, sidebarOptions, disableCompression, } = options;
|
|
81
|
+
let { specPath, outputDir, template, infoTemplate, tagTemplate, schemaTemplate, markdownGenerators, downloadUrl, sidebarOptions, disableCompression, } = options;
|
|
82
82
|
// Remove trailing slash before proceeding
|
|
83
83
|
outputDir = outputDir.replace(/\/$/, "");
|
|
84
84
|
// Override docPath if pluginId provided
|
|
@@ -163,7 +163,9 @@ show_extensions: true
|
|
|
163
163
|
|
|
164
164
|
{{{markdown}}}
|
|
165
165
|
`;
|
|
166
|
-
const infoMdTemplate =
|
|
166
|
+
const infoMdTemplate = infoTemplate
|
|
167
|
+
? fs_1.default.readFileSync(infoTemplate).toString()
|
|
168
|
+
: `---
|
|
167
169
|
id: {{{id}}}
|
|
168
170
|
title: "{{{title}}}"
|
|
169
171
|
description: "{{{frontMatter.description}}}"
|
|
@@ -181,7 +183,9 @@ import {useCurrentSidebarCategory} from '@docusaurus/theme-common';
|
|
|
181
183
|
<DocCardList items={useCurrentSidebarCategory().items}/>
|
|
182
184
|
\`\`\`
|
|
183
185
|
`;
|
|
184
|
-
const tagMdTemplate =
|
|
186
|
+
const tagMdTemplate = tagTemplate
|
|
187
|
+
? fs_1.default.readFileSync(tagTemplate).toString()
|
|
188
|
+
: `---
|
|
185
189
|
id: {{{id}}}
|
|
186
190
|
title: "{{{frontMatter.description}}}"
|
|
187
191
|
description: "{{{frontMatter.description}}}"
|
|
@@ -197,7 +201,9 @@ import {useCurrentSidebarCategory} from '@docusaurus/theme-common';
|
|
|
197
201
|
<DocCardList items={useCurrentSidebarCategory().items}/>
|
|
198
202
|
\`\`\`
|
|
199
203
|
`;
|
|
200
|
-
const schemaMdTemplate =
|
|
204
|
+
const schemaMdTemplate = schemaTemplate
|
|
205
|
+
? fs_1.default.readFileSync(schemaTemplate).toString()
|
|
206
|
+
: `---
|
|
201
207
|
id: {{{id}}}
|
|
202
208
|
title: "{{{title}}}"
|
|
203
209
|
description: "{{{frontMatter.description}}}"
|
|
@@ -270,11 +276,10 @@ custom_edit_url: null
|
|
|
270
276
|
}
|
|
271
277
|
}
|
|
272
278
|
}
|
|
273
|
-
// TODO: determine if we actually want/need this
|
|
274
279
|
if (item.type === "info") {
|
|
275
280
|
if (!fs_1.default.existsSync(`${outputDir}/${item.id}.info.mdx`)) {
|
|
276
281
|
try {
|
|
277
|
-
(sidebarOptions === null || sidebarOptions === void 0 ? void 0 : sidebarOptions.categoryLinkSource) === "info" // Only use utils template if set to "info"
|
|
282
|
+
(sidebarOptions === null || sidebarOptions === void 0 ? void 0 : sidebarOptions.categoryLinkSource) === "info" || infoTemplate // Only use utils template if set to "info" or if infoTemplate is set
|
|
278
283
|
? fs_1.default.writeFileSync(`${outputDir}/${item.id}.info.mdx`, utils, "utf8")
|
|
279
284
|
: fs_1.default.writeFileSync(`${outputDir}/${item.id}.info.mdx`, view, "utf8");
|
|
280
285
|
console.log(chalk_1.default.green(`Successfully created "${outputDir}/${item.id}.info.mdx"`));
|
|
@@ -13,7 +13,7 @@ function createAuthentication(securitySchemes) {
|
|
|
13
13
|
if (!securitySchemes || !Object.keys(securitySchemes).length)
|
|
14
14
|
return "";
|
|
15
15
|
const createAuthenticationTable = (securityScheme) => {
|
|
16
|
-
const { bearerFormat, flows, name, scheme, type, openIdConnectUrl } = securityScheme;
|
|
16
|
+
const { bearerFormat, flows, name, scheme, type, openIdConnectUrl, in: paramIn, } = securityScheme;
|
|
17
17
|
const createSecuritySchemeTypeRow = () => (0, utils_1.create)("tr", {
|
|
18
18
|
children: [
|
|
19
19
|
(0, utils_1.create)("th", { children: "Security Scheme Type:" }),
|
|
@@ -54,7 +54,9 @@ function createAuthentication(securitySchemes) {
|
|
|
54
54
|
createSecuritySchemeTypeRow(),
|
|
55
55
|
(0, utils_1.create)("tr", {
|
|
56
56
|
children: [
|
|
57
|
-
(0, utils_1.create)("th", {
|
|
57
|
+
(0, utils_1.create)("th", {
|
|
58
|
+
children: `${paramIn.charAt(0).toUpperCase() + paramIn.slice(1)} parameter name:`,
|
|
59
|
+
}),
|
|
58
60
|
(0, utils_1.create)("td", { children: name }),
|
|
59
61
|
],
|
|
60
62
|
}),
|
package/lib/markdown/schema.js
CHANGED
|
@@ -11,6 +11,9 @@ exports.getQualifierMessage = getQualifierMessage;
|
|
|
11
11
|
function prettyName(schema, circular) {
|
|
12
12
|
var _a, _b, _c, _d, _e;
|
|
13
13
|
if (schema.format) {
|
|
14
|
+
if (schema.type) {
|
|
15
|
+
return `${schema.type}<${schema.format}>`;
|
|
16
|
+
}
|
|
14
17
|
return schema.format;
|
|
15
18
|
}
|
|
16
19
|
if (schema.allOf) {
|
|
@@ -5,187 +5,10 @@
|
|
|
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
|
-
};
|
|
11
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
9
|
exports.sampleRequestFromSchema = void 0;
|
|
13
|
-
const
|
|
14
|
-
const merge_1 = __importDefault(require("lodash/merge"));
|
|
15
|
-
const createSchema_1 = require("../markdown/createSchema");
|
|
16
|
-
const primitives = {
|
|
17
|
-
string: {
|
|
18
|
-
default: () => "string",
|
|
19
|
-
email: () => "user@example.com",
|
|
20
|
-
date: () => "2024-07-29",
|
|
21
|
-
"date-time": () => "2024-07-29T15:51:28.071Z",
|
|
22
|
-
uuid: () => "3fa85f64-5717-4562-b3fc-2c963f66afa6",
|
|
23
|
-
hostname: () => "example.com",
|
|
24
|
-
ipv4: () => "198.51.100.42",
|
|
25
|
-
ipv6: () => "2001:0db8:5b96:0000:0000:426f:8e17:642a",
|
|
26
|
-
},
|
|
27
|
-
number: {
|
|
28
|
-
default: () => 0,
|
|
29
|
-
float: () => 0.0,
|
|
30
|
-
},
|
|
31
|
-
integer: {
|
|
32
|
-
default: () => 0,
|
|
33
|
-
},
|
|
34
|
-
boolean: {
|
|
35
|
-
default: (schema) => typeof schema.default === "boolean" ? schema.default : true,
|
|
36
|
-
},
|
|
37
|
-
object: {},
|
|
38
|
-
array: {},
|
|
39
|
-
};
|
|
40
|
-
function sampleRequestFromProp(name, prop, obj) {
|
|
41
|
-
// Handle resolved circular props
|
|
42
|
-
if (typeof prop === "object" && Object.keys(prop).length === 0) {
|
|
43
|
-
obj[name] = prop;
|
|
44
|
-
return obj;
|
|
45
|
-
}
|
|
46
|
-
// TODO: handle discriminators
|
|
47
|
-
if (prop.oneOf) {
|
|
48
|
-
obj[name] = (0, exports.sampleRequestFromSchema)(prop.oneOf[0]);
|
|
49
|
-
}
|
|
50
|
-
else if (prop.anyOf) {
|
|
51
|
-
obj[name] = (0, exports.sampleRequestFromSchema)(prop.anyOf[0]);
|
|
52
|
-
}
|
|
53
|
-
else if (prop.allOf) {
|
|
54
|
-
const mergedSchemas = (0, createSchema_1.mergeAllOf)(prop);
|
|
55
|
-
sampleRequestFromProp(name, mergedSchemas, obj);
|
|
56
|
-
}
|
|
57
|
-
else {
|
|
58
|
-
obj[name] = (0, exports.sampleRequestFromSchema)(prop);
|
|
59
|
-
}
|
|
60
|
-
return obj;
|
|
61
|
-
}
|
|
10
|
+
const createSchemaExample_1 = require("./createSchemaExample");
|
|
62
11
|
const sampleRequestFromSchema = (schema = {}) => {
|
|
63
|
-
|
|
64
|
-
// deep copy schema before processing
|
|
65
|
-
let schemaCopy = JSON.parse(JSON.stringify(schema));
|
|
66
|
-
let { type, example, allOf, properties, items, oneOf, anyOf } = schemaCopy;
|
|
67
|
-
if (example !== undefined) {
|
|
68
|
-
return example;
|
|
69
|
-
}
|
|
70
|
-
if (oneOf) {
|
|
71
|
-
if (properties) {
|
|
72
|
-
const combinedSchemas = (0, merge_1.default)(schemaCopy, oneOf[0]);
|
|
73
|
-
delete combinedSchemas.oneOf;
|
|
74
|
-
return (0, exports.sampleRequestFromSchema)(combinedSchemas);
|
|
75
|
-
}
|
|
76
|
-
// Just go with first schema
|
|
77
|
-
return (0, exports.sampleRequestFromSchema)(oneOf[0]);
|
|
78
|
-
}
|
|
79
|
-
if (anyOf) {
|
|
80
|
-
if (properties) {
|
|
81
|
-
const combinedSchemas = (0, merge_1.default)(schemaCopy, anyOf[0]);
|
|
82
|
-
delete combinedSchemas.anyOf;
|
|
83
|
-
return (0, exports.sampleRequestFromSchema)(combinedSchemas);
|
|
84
|
-
}
|
|
85
|
-
// Just go with first schema
|
|
86
|
-
return (0, exports.sampleRequestFromSchema)(anyOf[0]);
|
|
87
|
-
}
|
|
88
|
-
if (allOf) {
|
|
89
|
-
const mergedSchemas = (0, createSchema_1.mergeAllOf)(schemaCopy);
|
|
90
|
-
if (mergedSchemas.properties) {
|
|
91
|
-
for (const [key, value] of Object.entries(mergedSchemas.properties)) {
|
|
92
|
-
if ((value.readOnly && value.readOnly === true) || value.deprecated) {
|
|
93
|
-
delete mergedSchemas.properties[key];
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
if (properties) {
|
|
98
|
-
const combinedSchemas = (0, merge_1.default)(schemaCopy, mergedSchemas);
|
|
99
|
-
delete combinedSchemas.allOf;
|
|
100
|
-
return (0, exports.sampleRequestFromSchema)(combinedSchemas);
|
|
101
|
-
}
|
|
102
|
-
return (0, exports.sampleRequestFromSchema)(mergedSchemas);
|
|
103
|
-
}
|
|
104
|
-
if (!type) {
|
|
105
|
-
if (properties) {
|
|
106
|
-
type = "object";
|
|
107
|
-
}
|
|
108
|
-
else if (items) {
|
|
109
|
-
type = "array";
|
|
110
|
-
}
|
|
111
|
-
else {
|
|
112
|
-
return;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
if (type === "object") {
|
|
116
|
-
let obj = {};
|
|
117
|
-
for (let [name, prop] of Object.entries(properties !== null && properties !== void 0 ? properties : {})) {
|
|
118
|
-
if (prop.properties) {
|
|
119
|
-
for (const [key, value] of Object.entries(prop.properties)) {
|
|
120
|
-
if ((value.readOnly && value.readOnly === true) ||
|
|
121
|
-
value.deprecated) {
|
|
122
|
-
delete prop.properties[key];
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
if (prop.items && prop.items.properties) {
|
|
127
|
-
for (const [key, value] of Object.entries(prop.items.properties)) {
|
|
128
|
-
if ((value.readOnly && value.readOnly === true) ||
|
|
129
|
-
value.deprecated) {
|
|
130
|
-
delete prop.items.properties[key];
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
if (prop.readOnly && prop.readOnly === true) {
|
|
135
|
-
continue;
|
|
136
|
-
}
|
|
137
|
-
if (prop.deprecated) {
|
|
138
|
-
continue;
|
|
139
|
-
}
|
|
140
|
-
// Resolve schema from prop recursively
|
|
141
|
-
obj = sampleRequestFromProp(name, prop, obj);
|
|
142
|
-
}
|
|
143
|
-
return obj;
|
|
144
|
-
}
|
|
145
|
-
if (type === "array") {
|
|
146
|
-
if (Array.isArray(items === null || items === void 0 ? void 0 : items.anyOf)) {
|
|
147
|
-
return items === null || items === void 0 ? void 0 : items.anyOf.map((item) => (0, exports.sampleRequestFromSchema)(item));
|
|
148
|
-
}
|
|
149
|
-
if (Array.isArray(items === null || items === void 0 ? void 0 : items.oneOf)) {
|
|
150
|
-
return items === null || items === void 0 ? void 0 : items.oneOf.map((item) => (0, exports.sampleRequestFromSchema)(item));
|
|
151
|
-
}
|
|
152
|
-
return normalizeArray((0, exports.sampleRequestFromSchema)(items));
|
|
153
|
-
}
|
|
154
|
-
if (schemaCopy.enum) {
|
|
155
|
-
if (schemaCopy.default) {
|
|
156
|
-
return schemaCopy.default;
|
|
157
|
-
}
|
|
158
|
-
return normalizeArray(schemaCopy.enum)[0];
|
|
159
|
-
}
|
|
160
|
-
if ((schema.readOnly && schema.readOnly === true) ||
|
|
161
|
-
schemaCopy.deprecated) {
|
|
162
|
-
return undefined;
|
|
163
|
-
}
|
|
164
|
-
return primitive(schemaCopy);
|
|
165
|
-
}
|
|
166
|
-
catch (err) {
|
|
167
|
-
console.error(chalk_1.default.yellow("WARNING: failed to create example from schema object:", err));
|
|
168
|
-
return;
|
|
169
|
-
}
|
|
12
|
+
return (0, createSchemaExample_1.sampleFromSchema)(schema, { type: "request" });
|
|
170
13
|
};
|
|
171
14
|
exports.sampleRequestFromSchema = sampleRequestFromSchema;
|
|
172
|
-
function primitive(schema = {}) {
|
|
173
|
-
let { type, format } = schema;
|
|
174
|
-
if (type === undefined) {
|
|
175
|
-
return;
|
|
176
|
-
}
|
|
177
|
-
let fn = schema.default ? () => schema.default : primitives[type].default;
|
|
178
|
-
if (format !== undefined) {
|
|
179
|
-
fn = primitives[type][format] || fn;
|
|
180
|
-
}
|
|
181
|
-
if (fn) {
|
|
182
|
-
return fn(schema);
|
|
183
|
-
}
|
|
184
|
-
return "Unknown Type: " + schema.type;
|
|
185
|
-
}
|
|
186
|
-
function normalizeArray(arr) {
|
|
187
|
-
if (Array.isArray(arr)) {
|
|
188
|
-
return arr;
|
|
189
|
-
}
|
|
190
|
-
return [arr];
|
|
191
|
-
}
|
|
@@ -5,188 +5,10 @@
|
|
|
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
|
-
};
|
|
11
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
9
|
exports.sampleResponseFromSchema = void 0;
|
|
13
|
-
const
|
|
14
|
-
const merge_1 = __importDefault(require("lodash/merge"));
|
|
15
|
-
const createSchema_1 = require("../markdown/createSchema");
|
|
16
|
-
const primitives = {
|
|
17
|
-
string: {
|
|
18
|
-
default: () => "string",
|
|
19
|
-
email: () => "user@example.com",
|
|
20
|
-
date: () => "2024-07-29",
|
|
21
|
-
"date-time": () => "2024-07-29T15:51:28.071Z",
|
|
22
|
-
uuid: () => "3fa85f64-5717-4562-b3fc-2c963f66afa6",
|
|
23
|
-
hostname: () => "example.com",
|
|
24
|
-
ipv4: () => "198.51.100.42",
|
|
25
|
-
ipv6: () => "2001:0db8:5b96:0000:0000:426f:8e17:642a",
|
|
26
|
-
},
|
|
27
|
-
number: {
|
|
28
|
-
default: () => 0,
|
|
29
|
-
float: () => 0.0,
|
|
30
|
-
},
|
|
31
|
-
integer: {
|
|
32
|
-
default: () => 0,
|
|
33
|
-
},
|
|
34
|
-
boolean: {
|
|
35
|
-
default: (schema) => typeof schema.default === "boolean" ? schema.default : true,
|
|
36
|
-
},
|
|
37
|
-
object: {},
|
|
38
|
-
array: {},
|
|
39
|
-
};
|
|
40
|
-
function sampleResponseFromProp(name, prop, obj) {
|
|
41
|
-
// Handle resolved circular props
|
|
42
|
-
if (typeof prop === "object" && Object.keys(prop).length === 0) {
|
|
43
|
-
obj[name] = prop;
|
|
44
|
-
return obj;
|
|
45
|
-
}
|
|
46
|
-
// TODO: handle discriminators
|
|
47
|
-
if (prop.oneOf) {
|
|
48
|
-
obj[name] = (0, exports.sampleResponseFromSchema)(prop.oneOf[0]);
|
|
49
|
-
}
|
|
50
|
-
else if (prop.anyOf) {
|
|
51
|
-
obj[name] = (0, exports.sampleResponseFromSchema)(prop.anyOf[0]);
|
|
52
|
-
}
|
|
53
|
-
else if (prop.allOf) {
|
|
54
|
-
const mergedSchemas = (0, createSchema_1.mergeAllOf)(prop);
|
|
55
|
-
sampleResponseFromProp(name, mergedSchemas, obj);
|
|
56
|
-
}
|
|
57
|
-
else {
|
|
58
|
-
obj[name] = (0, exports.sampleResponseFromSchema)(prop);
|
|
59
|
-
}
|
|
60
|
-
return obj;
|
|
61
|
-
}
|
|
10
|
+
const createSchemaExample_1 = require("./createSchemaExample");
|
|
62
11
|
const sampleResponseFromSchema = (schema = {}) => {
|
|
63
|
-
|
|
64
|
-
// deep copy schema before processing
|
|
65
|
-
let schemaCopy = JSON.parse(JSON.stringify(schema));
|
|
66
|
-
let { type, example, allOf, properties, items, oneOf, anyOf } = schemaCopy;
|
|
67
|
-
if (example !== undefined) {
|
|
68
|
-
return example;
|
|
69
|
-
}
|
|
70
|
-
if (allOf) {
|
|
71
|
-
const mergedSchemas = (0, createSchema_1.mergeAllOf)(schemaCopy);
|
|
72
|
-
if (mergedSchemas.properties) {
|
|
73
|
-
for (const [key, value] of Object.entries(mergedSchemas.properties)) {
|
|
74
|
-
if ((value.writeOnly && value.writeOnly === true) ||
|
|
75
|
-
value.deprecated) {
|
|
76
|
-
delete mergedSchemas.properties[key];
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
if (properties) {
|
|
81
|
-
const combinedSchemas = (0, merge_1.default)(schemaCopy, mergedSchemas);
|
|
82
|
-
delete combinedSchemas.allOf;
|
|
83
|
-
return (0, exports.sampleResponseFromSchema)(combinedSchemas);
|
|
84
|
-
}
|
|
85
|
-
return (0, exports.sampleResponseFromSchema)(mergedSchemas);
|
|
86
|
-
}
|
|
87
|
-
if (oneOf) {
|
|
88
|
-
if (properties) {
|
|
89
|
-
const combinedSchemas = (0, merge_1.default)(schemaCopy, oneOf[0]);
|
|
90
|
-
delete combinedSchemas.oneOf;
|
|
91
|
-
return (0, exports.sampleResponseFromSchema)(combinedSchemas);
|
|
92
|
-
}
|
|
93
|
-
// Just go with first schema
|
|
94
|
-
return (0, exports.sampleResponseFromSchema)(oneOf[0]);
|
|
95
|
-
}
|
|
96
|
-
if (anyOf) {
|
|
97
|
-
if (properties) {
|
|
98
|
-
const combinedSchemas = (0, merge_1.default)(schemaCopy, anyOf[0]);
|
|
99
|
-
delete combinedSchemas.anyOf;
|
|
100
|
-
return (0, exports.sampleResponseFromSchema)(combinedSchemas);
|
|
101
|
-
}
|
|
102
|
-
// Just go with first schema
|
|
103
|
-
return (0, exports.sampleResponseFromSchema)(anyOf[0]);
|
|
104
|
-
}
|
|
105
|
-
if (!type) {
|
|
106
|
-
if (properties) {
|
|
107
|
-
type = "object";
|
|
108
|
-
}
|
|
109
|
-
else if (items) {
|
|
110
|
-
type = "array";
|
|
111
|
-
}
|
|
112
|
-
else {
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
if (type === "object") {
|
|
117
|
-
let obj = {};
|
|
118
|
-
for (let [name, prop] of Object.entries(properties !== null && properties !== void 0 ? properties : {})) {
|
|
119
|
-
if (prop.properties) {
|
|
120
|
-
for (const [key, value] of Object.entries(prop.properties)) {
|
|
121
|
-
if ((value.writeOnly && value.writeOnly === true) ||
|
|
122
|
-
value.deprecated) {
|
|
123
|
-
delete prop.properties[key];
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
if (prop.items && prop.items.properties) {
|
|
128
|
-
for (const [key, value] of Object.entries(prop.items.properties)) {
|
|
129
|
-
if ((value.writeOnly && value.writeOnly === true) ||
|
|
130
|
-
value.deprecated) {
|
|
131
|
-
delete prop.items.properties[key];
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
if (prop.writeOnly && prop.writeOnly === true) {
|
|
136
|
-
continue;
|
|
137
|
-
}
|
|
138
|
-
if (prop.deprecated) {
|
|
139
|
-
continue;
|
|
140
|
-
}
|
|
141
|
-
// Resolve schema from prop recursively
|
|
142
|
-
obj = sampleResponseFromProp(name, prop, obj);
|
|
143
|
-
}
|
|
144
|
-
return obj;
|
|
145
|
-
}
|
|
146
|
-
if (type === "array") {
|
|
147
|
-
if (Array.isArray(items === null || items === void 0 ? void 0 : items.anyOf)) {
|
|
148
|
-
return items === null || items === void 0 ? void 0 : items.anyOf.map((item) => (0, exports.sampleResponseFromSchema)(item));
|
|
149
|
-
}
|
|
150
|
-
if (Array.isArray(items === null || items === void 0 ? void 0 : items.oneOf)) {
|
|
151
|
-
return items === null || items === void 0 ? void 0 : items.oneOf.map((item) => (0, exports.sampleResponseFromSchema)(item));
|
|
152
|
-
}
|
|
153
|
-
return [(0, exports.sampleResponseFromSchema)(items)];
|
|
154
|
-
}
|
|
155
|
-
if (schemaCopy.enum) {
|
|
156
|
-
if (schemaCopy.default) {
|
|
157
|
-
return schemaCopy.default;
|
|
158
|
-
}
|
|
159
|
-
return normalizeArray(schemaCopy.enum)[0];
|
|
160
|
-
}
|
|
161
|
-
if ((schemaCopy.writeOnly && schemaCopy.writeOnly === true) ||
|
|
162
|
-
schemaCopy.deprecated) {
|
|
163
|
-
return undefined;
|
|
164
|
-
}
|
|
165
|
-
return primitive(schemaCopy);
|
|
166
|
-
}
|
|
167
|
-
catch (err) {
|
|
168
|
-
console.error(chalk_1.default.yellow("WARNING: failed to create example from schema object:", err));
|
|
169
|
-
return;
|
|
170
|
-
}
|
|
12
|
+
return (0, createSchemaExample_1.sampleFromSchema)(schema, { type: "response" });
|
|
171
13
|
};
|
|
172
14
|
exports.sampleResponseFromSchema = sampleResponseFromSchema;
|
|
173
|
-
function primitive(schema = {}) {
|
|
174
|
-
let { type, format } = schema;
|
|
175
|
-
if (type === undefined) {
|
|
176
|
-
return;
|
|
177
|
-
}
|
|
178
|
-
let fn = schema.default ? () => schema.default : primitives[type].default;
|
|
179
|
-
if (format !== undefined) {
|
|
180
|
-
fn = primitives[type][format] || fn;
|
|
181
|
-
}
|
|
182
|
-
if (fn) {
|
|
183
|
-
return fn(schema);
|
|
184
|
-
}
|
|
185
|
-
return "Unknown Type: " + schema.type;
|
|
186
|
-
}
|
|
187
|
-
function normalizeArray(arr) {
|
|
188
|
-
if (Array.isArray(arr)) {
|
|
189
|
-
return arr;
|
|
190
|
-
}
|
|
191
|
-
return [arr];
|
|
192
|
-
}
|