docusaurus-plugin-openapi-docs 1.1.8 → 1.1.9

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.
@@ -5,14 +5,62 @@
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
- exports.createStatusCodes = exports.createResponseExample = exports.createResponseExamples = void 0;
12
+ exports.createStatusCodes = exports.createExampleFromSchema = exports.createResponseExample = exports.createResponseExamples = void 0;
13
+ const xml_formatter_1 = __importDefault(require("xml-formatter"));
14
+ const createResponseExample_1 = require("../openapi/createResponseExample");
10
15
  const createDescription_1 = require("./createDescription");
11
16
  const createDetails_1 = require("./createDetails");
12
17
  const createDetailsSummary_1 = require("./createDetailsSummary");
13
18
  const createResponseSchema_1 = require("./createResponseSchema");
14
19
  const utils_1 = require("./utils");
15
20
  const utils_2 = require("./utils");
21
+ function json2xml(o, tab) {
22
+ var toXml = function (v, name, ind) {
23
+ var xml = "";
24
+ if (v instanceof Array) {
25
+ for (var i = 0, n = v.length; i < n; i++)
26
+ xml += ind + toXml(v[i], name, ind + "\t") + "\n";
27
+ }
28
+ else if (typeof v == "object") {
29
+ var hasChild = false;
30
+ xml += ind + "<" + name;
31
+ for (var m in v) {
32
+ if (m.charAt(0) === "@")
33
+ xml += " " + m.substr(1) + '="' + v[m].toString() + '"';
34
+ else
35
+ hasChild = true;
36
+ }
37
+ xml += hasChild ? ">" : "/>";
38
+ if (hasChild) {
39
+ for (var m2 in v) {
40
+ if (m2 === "#text")
41
+ xml += v[m2];
42
+ else if (m2 === "#cdata")
43
+ xml += "<![CDATA[" + v[m2] + "]]>";
44
+ else if (m2.charAt(0) !== "@")
45
+ xml += toXml(v[m2], m2, ind + "\t");
46
+ }
47
+ xml +=
48
+ (xml.charAt(xml.length - 1) === "\n" ? ind : "") +
49
+ "</" +
50
+ name +
51
+ ">";
52
+ }
53
+ }
54
+ else {
55
+ xml += ind + "<" + name + ">" + v.toString() + "</" + name + ">";
56
+ }
57
+ return xml;
58
+ }, xml = "";
59
+ for (var m3 in o)
60
+ xml += toXml(o[m3], m3, "");
61
+ return tab ? xml.replace(/\t/g, tab) : xml.replace(/\t|\n/g, "");
62
+ }
63
+ exports.default = json2xml;
16
64
  function createResponseHeaders(responseHeaders) {
17
65
  return (0, utils_2.guard)(responseHeaders, () => (0, utils_1.create)("ul", {
18
66
  style: { marginLeft: "1rem" },
@@ -53,7 +101,14 @@ function createResponseHeaders(responseHeaders) {
53
101
  ],
54
102
  }));
55
103
  }
56
- function createResponseExamples(responseExamples) {
104
+ function createResponseExamples(responseExamples, mimeType) {
105
+ let language = "shell";
106
+ if (mimeType.endsWith("json")) {
107
+ language = "json";
108
+ }
109
+ if (mimeType.endsWith("xml")) {
110
+ language = "xml";
111
+ }
57
112
  return Object.entries(responseExamples).map(([exampleName, exampleValue]) => {
58
113
  const camelToSpaceName = exampleName.replace(/([A-Z])/g, " $1");
59
114
  let finalFormattedName = camelToSpaceName.charAt(0).toUpperCase() + camelToSpaceName.slice(1);
@@ -64,6 +119,7 @@ function createResponseExamples(responseExamples) {
64
119
  children: [
65
120
  (0, utils_1.create)("ResponseSamples", {
66
121
  responseExample: JSON.stringify(exampleValue.value, null, 2),
122
+ language: language,
67
123
  }),
68
124
  ],
69
125
  });
@@ -74,13 +130,21 @@ function createResponseExamples(responseExamples) {
74
130
  children: [
75
131
  (0, utils_1.create)("ResponseSamples", {
76
132
  responseExample: exampleValue.value,
133
+ language: language,
77
134
  }),
78
135
  ],
79
136
  });
80
137
  });
81
138
  }
82
139
  exports.createResponseExamples = createResponseExamples;
83
- function createResponseExample(responseExample) {
140
+ function createResponseExample(responseExample, mimeType) {
141
+ let language = "shell";
142
+ if (mimeType.endsWith("json")) {
143
+ language = "json";
144
+ }
145
+ if (mimeType.endsWith("xml")) {
146
+ language = "xml";
147
+ }
84
148
  if (typeof responseExample === "object") {
85
149
  return (0, utils_1.create)("TabItem", {
86
150
  label: `Example`,
@@ -88,6 +152,7 @@ function createResponseExample(responseExample) {
88
152
  children: [
89
153
  (0, utils_1.create)("ResponseSamples", {
90
154
  responseExample: JSON.stringify(responseExample, null, 2),
155
+ language: language,
91
156
  }),
92
157
  ],
93
158
  });
@@ -98,11 +163,71 @@ function createResponseExample(responseExample) {
98
163
  children: [
99
164
  (0, utils_1.create)("ResponseSamples", {
100
165
  responseExample: responseExample,
166
+ language: language,
101
167
  }),
102
168
  ],
103
169
  });
104
170
  }
105
171
  exports.createResponseExample = createResponseExample;
172
+ function createExampleFromSchema(schema, mimeType) {
173
+ const responseExample = (0, createResponseExample_1.sampleResponseFromSchema)(schema);
174
+ if (mimeType.endsWith("xml")) {
175
+ let responseExampleObject;
176
+ try {
177
+ responseExampleObject = JSON.parse(JSON.stringify(responseExample));
178
+ }
179
+ catch {
180
+ return undefined;
181
+ }
182
+ if (typeof responseExampleObject === "object") {
183
+ let xmlExample;
184
+ try {
185
+ xmlExample = (0, xml_formatter_1.default)(json2xml(responseExampleObject, ""), {
186
+ indentation: " ",
187
+ lineSeparator: "\n",
188
+ collapseContent: true,
189
+ });
190
+ }
191
+ catch {
192
+ const xmlExampleWithRoot = { root: responseExampleObject };
193
+ try {
194
+ xmlExample = (0, xml_formatter_1.default)(json2xml(xmlExampleWithRoot, ""), {
195
+ indentation: " ",
196
+ lineSeparator: "\n",
197
+ collapseContent: true,
198
+ });
199
+ }
200
+ catch {
201
+ xmlExample = json2xml(responseExampleObject, "");
202
+ }
203
+ }
204
+ return (0, utils_1.create)("TabItem", {
205
+ label: `Example (from schema)`,
206
+ value: `Example (from schema)`,
207
+ children: [
208
+ (0, utils_1.create)("ResponseSamples", {
209
+ responseExample: xmlExample,
210
+ language: "xml",
211
+ }),
212
+ ],
213
+ });
214
+ }
215
+ }
216
+ if (typeof responseExample === "object") {
217
+ return (0, utils_1.create)("TabItem", {
218
+ label: `Example (from schema)`,
219
+ value: `Example (from schema)`,
220
+ children: [
221
+ (0, utils_1.create)("ResponseSamples", {
222
+ responseExample: JSON.stringify(responseExample, null, 2),
223
+ language: "json",
224
+ }),
225
+ ],
226
+ });
227
+ }
228
+ return undefined;
229
+ }
230
+ exports.createExampleFromSchema = createExampleFromSchema;
106
231
  function createStatusCodes({ responses }) {
107
232
  if (responses === undefined) {
108
233
  return undefined;
@@ -127,8 +252,8 @@ function createStatusCodes({ responses }) {
127
252
  }),
128
253
  responseHeaders &&
129
254
  (0, createDetails_1.createDetails)({
130
- "data-collaposed": false,
131
- open: true,
255
+ "data-collaposed": true,
256
+ open: false,
132
257
  style: { textAlign: "left", marginBottom: "1rem" },
133
258
  children: [
134
259
  (0, createDetailsSummary_1.createDetailsSummary)({
@@ -0,0 +1,2 @@
1
+ import { SchemaObject } from "./types";
2
+ export declare const sampleRequestFromSchema: (schema?: SchemaObject) => any;
@@ -9,7 +9,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
9
9
  return (mod && mod.__esModule) ? mod : { "default": mod };
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.sampleFromSchema = void 0;
12
+ exports.sampleRequestFromSchema = void 0;
13
13
  const chalk_1 = __importDefault(require("chalk"));
14
14
  const createRequestSchema_1 = require("../markdown/createRequestSchema");
15
15
  const primitives = {
@@ -36,12 +36,42 @@ const primitives = {
36
36
  object: {},
37
37
  array: {},
38
38
  };
39
- const sampleFromSchema = (schema = {}) => {
39
+ function sampleRequestFromProp(name, prop, obj) {
40
+ // Handle resolved circular props
41
+ if (typeof prop === "object" && Object.keys(prop).length === 0) {
42
+ obj[name] = prop;
43
+ return obj;
44
+ }
45
+ // TODO: handle discriminators
46
+ if (prop.oneOf) {
47
+ obj[name] = (0, exports.sampleRequestFromSchema)(prop.oneOf[0]);
48
+ }
49
+ else if (prop.anyOf) {
50
+ obj[name] = (0, exports.sampleRequestFromSchema)(prop.anyOf[0]);
51
+ }
52
+ else if (prop.allOf) {
53
+ const { mergedSchemas } = (0, createRequestSchema_1.mergeAllOf)(prop.allOf);
54
+ sampleRequestFromProp(name, mergedSchemas, obj);
55
+ }
56
+ else {
57
+ obj[name] = (0, exports.sampleRequestFromSchema)(prop);
58
+ }
59
+ return obj;
60
+ }
61
+ const sampleRequestFromSchema = (schema = {}) => {
40
62
  try {
41
- let { type, example, allOf, properties, items } = schema;
63
+ let { type, example, allOf, properties, items, oneOf, anyOf } = schema;
42
64
  if (example !== undefined) {
43
65
  return example;
44
66
  }
67
+ if (oneOf) {
68
+ // Just go with first schema
69
+ return (0, exports.sampleRequestFromSchema)(oneOf[0]);
70
+ }
71
+ if (anyOf) {
72
+ // Just go with first schema
73
+ return (0, exports.sampleRequestFromSchema)(anyOf[0]);
74
+ }
45
75
  if (allOf) {
46
76
  const { mergedSchemas } = (0, createRequestSchema_1.mergeAllOf)(allOf);
47
77
  if (mergedSchemas.properties) {
@@ -51,7 +81,7 @@ const sampleFromSchema = (schema = {}) => {
51
81
  }
52
82
  }
53
83
  }
54
- return (0, exports.sampleFromSchema)(mergedSchemas);
84
+ return (0, exports.sampleRequestFromSchema)(mergedSchemas);
55
85
  }
56
86
  if (!type) {
57
87
  if (properties) {
@@ -84,18 +114,19 @@ const sampleFromSchema = (schema = {}) => {
84
114
  if (prop.deprecated) {
85
115
  continue;
86
116
  }
87
- obj[name] = (0, exports.sampleFromSchema)(prop);
117
+ // Resolve schema from prop recursively
118
+ obj = sampleRequestFromProp(name, prop, obj);
88
119
  }
89
120
  return obj;
90
121
  }
91
122
  if (type === "array") {
92
123
  if (Array.isArray(items === null || items === void 0 ? void 0 : items.anyOf)) {
93
- return items === null || items === void 0 ? void 0 : items.anyOf.map((item) => (0, exports.sampleFromSchema)(item));
124
+ return items === null || items === void 0 ? void 0 : items.anyOf.map((item) => (0, exports.sampleRequestFromSchema)(item));
94
125
  }
95
126
  if (Array.isArray(items === null || items === void 0 ? void 0 : items.oneOf)) {
96
- return items === null || items === void 0 ? void 0 : items.oneOf.map((item) => (0, exports.sampleFromSchema)(item));
127
+ return items === null || items === void 0 ? void 0 : items.oneOf.map((item) => (0, exports.sampleRequestFromSchema)(item));
97
128
  }
98
- return [(0, exports.sampleFromSchema)(items)];
129
+ return [(0, exports.sampleRequestFromSchema)(items)];
99
130
  }
100
131
  if (schema.enum) {
101
132
  if (schema.default) {
@@ -113,7 +144,7 @@ const sampleFromSchema = (schema = {}) => {
113
144
  return;
114
145
  }
115
146
  };
116
- exports.sampleFromSchema = sampleFromSchema;
147
+ exports.sampleRequestFromSchema = sampleRequestFromSchema;
117
148
  function primitive(schema = {}) {
118
149
  let { type, format } = schema;
119
150
  if (type === undefined) {
@@ -0,0 +1,2 @@
1
+ import { SchemaObject } from "./types";
2
+ export declare const sampleResponseFromSchema: (schema?: SchemaObject) => any;
@@ -0,0 +1,167 @@
1
+ "use strict";
2
+ /* ============================================================================
3
+ * Copyright (c) Palo Alto Networks
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ * ========================================================================== */
8
+ var __importDefault = (this && this.__importDefault) || function (mod) {
9
+ return (mod && mod.__esModule) ? mod : { "default": mod };
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.sampleResponseFromSchema = void 0;
13
+ const chalk_1 = __importDefault(require("chalk"));
14
+ const createResponseSchema_1 = require("../markdown/createResponseSchema");
15
+ const primitives = {
16
+ string: {
17
+ default: () => "string",
18
+ email: () => "user@example.com",
19
+ date: () => new Date().toISOString().substring(0, 10),
20
+ "date-time": () => new Date().toISOString().substring(0, 10),
21
+ uuid: () => "3fa85f64-5717-4562-b3fc-2c963f66afa6",
22
+ hostname: () => "example.com",
23
+ ipv4: () => "198.51.100.42",
24
+ ipv6: () => "2001:0db8:5b96:0000:0000:426f:8e17:642a",
25
+ },
26
+ number: {
27
+ default: () => 0,
28
+ float: () => 0.0,
29
+ },
30
+ integer: {
31
+ default: () => 0,
32
+ },
33
+ boolean: {
34
+ default: (schema) => typeof schema.default === "boolean" ? schema.default : true,
35
+ },
36
+ object: {},
37
+ array: {},
38
+ };
39
+ function sampleResponseFromProp(name, prop, obj) {
40
+ // Handle resolved circular props
41
+ if (typeof prop === "object" && Object.keys(prop).length === 0) {
42
+ obj[name] = prop;
43
+ return obj;
44
+ }
45
+ // TODO: handle discriminators
46
+ if (prop.oneOf) {
47
+ obj[name] = (0, exports.sampleResponseFromSchema)(prop.oneOf[0]);
48
+ }
49
+ else if (prop.anyOf) {
50
+ obj[name] = (0, exports.sampleResponseFromSchema)(prop.anyOf[0]);
51
+ }
52
+ else if (prop.allOf) {
53
+ const { mergedSchemas } = (0, createResponseSchema_1.mergeAllOf)(prop.allOf);
54
+ sampleResponseFromProp(name, mergedSchemas, obj);
55
+ }
56
+ else {
57
+ obj[name] = (0, exports.sampleResponseFromSchema)(prop);
58
+ }
59
+ return obj;
60
+ }
61
+ const sampleResponseFromSchema = (schema = {}) => {
62
+ try {
63
+ let { type, example, allOf, oneOf, anyOf, properties, items } = schema;
64
+ if (example !== undefined) {
65
+ return example;
66
+ }
67
+ if (allOf) {
68
+ const { mergedSchemas } = (0, createResponseSchema_1.mergeAllOf)(allOf);
69
+ if (mergedSchemas.properties) {
70
+ for (const [key, value] of Object.entries(mergedSchemas.properties)) {
71
+ if (value.readOnly && value.readOnly === true) {
72
+ delete mergedSchemas.properties[key];
73
+ }
74
+ }
75
+ }
76
+ return (0, exports.sampleResponseFromSchema)(mergedSchemas);
77
+ }
78
+ if (oneOf) {
79
+ // Just go with first schema
80
+ return (0, exports.sampleResponseFromSchema)(oneOf[0]);
81
+ }
82
+ if (anyOf) {
83
+ // Just go with first schema
84
+ return (0, exports.sampleResponseFromSchema)(anyOf[0]);
85
+ }
86
+ if (!type) {
87
+ if (properties) {
88
+ type = "object";
89
+ }
90
+ else if (items) {
91
+ type = "array";
92
+ }
93
+ else {
94
+ return;
95
+ }
96
+ }
97
+ if (type === "object") {
98
+ let obj = {};
99
+ for (let [name, prop] of Object.entries(properties !== null && properties !== void 0 ? properties : {})) {
100
+ if (prop.properties) {
101
+ for (const [key, value] of Object.entries(prop.properties)) {
102
+ if (value.readOnly && value.readOnly === true) {
103
+ delete prop.properties[key];
104
+ }
105
+ }
106
+ }
107
+ if (prop.items && prop.items.properties) {
108
+ for (const [key, value] of Object.entries(prop.items.properties)) {
109
+ if (value.readOnly && value.readOnly === true) {
110
+ delete prop.items.properties[key];
111
+ }
112
+ }
113
+ }
114
+ if (prop.deprecated) {
115
+ continue;
116
+ }
117
+ // Resolve schema from prop recursively
118
+ obj = sampleResponseFromProp(name, prop, obj);
119
+ }
120
+ return obj;
121
+ }
122
+ if (type === "array") {
123
+ if (Array.isArray(items === null || items === void 0 ? void 0 : items.anyOf)) {
124
+ return items === null || items === void 0 ? void 0 : items.anyOf.map((item) => (0, exports.sampleResponseFromSchema)(item));
125
+ }
126
+ if (Array.isArray(items === null || items === void 0 ? void 0 : items.oneOf)) {
127
+ return items === null || items === void 0 ? void 0 : items.oneOf.map((item) => (0, exports.sampleResponseFromSchema)(item));
128
+ }
129
+ return [(0, exports.sampleResponseFromSchema)(items)];
130
+ }
131
+ if (schema.enum) {
132
+ if (schema.default) {
133
+ return schema.default;
134
+ }
135
+ return normalizeArray(schema.enum)[0];
136
+ }
137
+ if (schema.writeOnly && schema.writeOnly === true) {
138
+ return undefined;
139
+ }
140
+ return primitive(schema);
141
+ }
142
+ catch (err) {
143
+ console.error(chalk_1.default.yellow("WARNING: failed to create example from schema object:", err));
144
+ return;
145
+ }
146
+ };
147
+ exports.sampleResponseFromSchema = sampleResponseFromSchema;
148
+ function primitive(schema = {}) {
149
+ let { type, format } = schema;
150
+ if (type === undefined) {
151
+ return;
152
+ }
153
+ let fn = schema.default ? () => schema.default : primitives[type].default;
154
+ if (format !== undefined) {
155
+ fn = primitives[type][format] || fn;
156
+ }
157
+ if (fn) {
158
+ return fn(schema);
159
+ }
160
+ return "Unknown Type: " + schema.type;
161
+ }
162
+ function normalizeArray(arr) {
163
+ if (Array.isArray(arr)) {
164
+ return arr;
165
+ }
166
+ return [arr];
167
+ }
@@ -19,7 +19,7 @@ const fs_extra_1 = __importDefault(require("fs-extra"));
19
19
  const cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
20
20
  const kebabCase_1 = __importDefault(require("lodash/kebabCase"));
21
21
  const index_1 = require("../index");
22
- const createExample_1 = require("./createExample");
22
+ const createRequestExample_1 = require("./createRequestExample");
23
23
  const loadAndResolveSpec_1 = require("./utils/loadAndResolveSpec");
24
24
  /**
25
25
  * Convenience function for converting raw JSON to a Postman Collection object.
@@ -134,7 +134,7 @@ function createItems(openapiData, sidebarOptions) {
134
134
  let jsonRequestBodyExample;
135
135
  const body = (_o = (_m = operationObject.requestBody) === null || _m === void 0 ? void 0 : _m.content) === null || _o === void 0 ? void 0 : _o["application/json"];
136
136
  if (body === null || body === void 0 ? void 0 : body.schema) {
137
- jsonRequestBodyExample = (0, createExample_1.sampleFromSchema)(body.schema);
137
+ jsonRequestBodyExample = (0, createRequestExample_1.sampleRequestFromSchema)(body.schema);
138
138
  }
139
139
  // Handle vendor JSON media types
140
140
  const bodyContent = (_p = operationObject.requestBody) === null || _p === void 0 ? void 0 : _p.content;
@@ -143,7 +143,7 @@ function createItems(openapiData, sidebarOptions) {
143
143
  if (firstBodyContentKey.endsWith("+json")) {
144
144
  const firstBody = bodyContent[firstBodyContentKey];
145
145
  if (firstBody === null || firstBody === void 0 ? void 0 : firstBody.schema) {
146
- jsonRequestBodyExample = (0, createExample_1.sampleFromSchema)(firstBody.schema);
146
+ jsonRequestBodyExample = (0, createRequestExample_1.sampleRequestFromSchema)(firstBody.schema);
147
147
  }
148
148
  }
149
149
  }
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": "1.1.8",
4
+ "version": "1.1.9",
5
5
  "license": "MIT",
6
6
  "keywords": [
7
7
  "openapi",
@@ -59,7 +59,8 @@
59
59
  "slugify": "^1.6.5",
60
60
  "swagger2openapi": "^7.0.8",
61
61
  "url-template": "^3.0.0",
62
- "webpack": "^5.61.0"
62
+ "webpack": "^5.61.0",
63
+ "xml-formatter": "^2.6.1"
63
64
  },
64
65
  "peerDependencies": {
65
66
  "react": "^16.8.4 || ^17.0.0"
@@ -67,5 +68,5 @@
67
68
  "engines": {
68
69
  "node": ">=14"
69
70
  },
70
- "gitHead": "619aa16c52e6de8fd752ff583681b0da093b679c"
71
+ "gitHead": "c9153730f9f180dbd199fbc9cb66f8470339c877"
71
72
  }