docusaurus-plugin-openapi-docs 1.1.8 → 1.1.11

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.
@@ -6,7 +6,7 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  * ========================================================================== */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.createResponseSchema = void 0;
9
+ exports.createResponseSchema = exports.mergeAllOf = void 0;
10
10
  const createDescription_1 = require("./createDescription");
11
11
  const createDetails_1 = require("./createDetails");
12
12
  const createDetailsSummary_1 = require("./createDetailsSummary");
@@ -27,6 +27,7 @@ function mergeAllOf(allOf) {
27
27
  return true;
28
28
  },
29
29
  },
30
+ ignoreAdditionalProperties: true,
30
31
  });
31
32
  const required = allOf.reduce((acc, cur) => {
32
33
  if (Array.isArray(cur.required)) {
@@ -37,6 +38,7 @@ function mergeAllOf(allOf) {
37
38
  }, []);
38
39
  return { mergedSchemas, required };
39
40
  }
41
+ exports.mergeAllOf = mergeAllOf;
40
42
  /**
41
43
  * For handling nested anyOf/oneOf.
42
44
  */
@@ -235,90 +237,100 @@ function createItems(schema) {
235
237
  /**
236
238
  * For handling discriminators that do not map to a same-level property
237
239
  */
238
- function createDiscriminator(schema) {
239
- var _a;
240
- const discriminator = schema.discriminator;
241
- const propertyName = discriminator === null || discriminator === void 0 ? void 0 : discriminator.propertyName;
242
- const propertyType = "string"; // should always be string
243
- const mapping = discriminator === null || discriminator === void 0 ? void 0 : discriminator.mapping;
244
- // Explicit mapping is required since we can't support implicit
245
- if (mapping === undefined) {
246
- return undefined;
247
- }
248
- // Attempt to get the property description we want to display
249
- // TODO: how to make it predictable when handling allOf
250
- let propertyDescription;
251
- const firstMappingSchema = mapping[Object.keys(mapping)[0]];
252
- if (firstMappingSchema.properties !== undefined) {
253
- propertyDescription =
254
- firstMappingSchema.properties[propertyName].description;
255
- }
256
- if (firstMappingSchema.allOf !== undefined) {
257
- const { mergedSchemas } = mergeAllOf(firstMappingSchema.allOf);
258
- if (mergedSchemas.properties !== undefined) {
259
- propertyDescription =
260
- (_a = mergedSchemas.properties[propertyName]) === null || _a === void 0 ? void 0 : _a.description;
261
- }
262
- }
263
- if (propertyDescription === undefined) {
264
- if (schema.properties !== undefined &&
265
- schema.properties[propertyName] !== undefined) {
266
- propertyDescription = schema.properties[propertyName].description;
267
- }
268
- }
269
- return (0, utils_1.create)("div", {
270
- className: "discriminatorItem",
271
- children: (0, utils_1.create)("div", {
272
- children: [
273
- (0, utils_1.create)("strong", {
274
- style: { paddingLeft: "1rem" },
275
- children: propertyName,
276
- }),
277
- (0, utils_1.guard)(propertyType, (name) => (0, utils_1.create)("span", {
278
- style: { opacity: "0.6" },
279
- children: ` ${propertyType}`,
280
- })),
281
- (0, utils_1.guard)((0, schema_1.getQualifierMessage)(schema.discriminator), (message) => (0, utils_1.create)("div", {
282
- style: {
283
- paddingLeft: "1rem",
284
- },
285
- children: (0, createDescription_1.createDescription)(message),
286
- })),
287
- (0, utils_1.guard)(propertyDescription, (description) => (0, utils_1.create)("div", {
288
- style: {
289
- paddingLeft: "1rem",
290
- },
291
- children: (0, createDescription_1.createDescription)(description),
292
- })),
293
- (0, utils_1.create)("DiscriminatorTabs", {
294
- children: Object.keys(mapping).map((key, index) => {
295
- if (mapping[key].allOf !== undefined) {
296
- const { mergedSchemas } = mergeAllOf(mapping[key].allOf);
297
- // Cleanup duplicate property from mapping schema
298
- delete mergedSchemas.properties[propertyName];
299
- mapping[key] = mergedSchemas;
300
- }
301
- if (mapping[key].properties !== undefined) {
302
- // Cleanup duplicate property from mapping schema
303
- delete mapping[key].properties[propertyName];
304
- }
305
- const label = key;
306
- return (0, utils_1.create)("TabItem", {
307
- label: label,
308
- value: `${index}-item-discriminator`,
309
- children: [
310
- (0, utils_1.create)("div", {
311
- style: { marginLeft: "-4px" },
312
- children: createNodes(mapping[key]),
313
- }),
314
- ],
315
- });
316
- }),
317
- }),
318
- ],
319
- }),
320
- });
321
- }
240
+ // function createDiscriminator(schema: SchemaObject) {
241
+ // const discriminator = schema.discriminator;
242
+ // const propertyName = discriminator?.propertyName;
243
+ // const propertyType = "string"; // should always be string
244
+ // const mapping: any = discriminator?.mapping;
245
+ // // Explicit mapping is required since we can't support implicit
246
+ // if (mapping === undefined) {
247
+ // return undefined;
248
+ // }
249
+ // // Attempt to get the property description we want to display
250
+ // // TODO: how to make it predictable when handling allOf
251
+ // let propertyDescription;
252
+ // const firstMappingSchema = mapping[Object.keys(mapping)[0]];
253
+ // if (firstMappingSchema.properties !== undefined) {
254
+ // propertyDescription =
255
+ // firstMappingSchema.properties![propertyName!].description;
256
+ // }
257
+ // if (firstMappingSchema.allOf !== undefined) {
258
+ // const { mergedSchemas }: { mergedSchemas: SchemaObject } = mergeAllOf(
259
+ // firstMappingSchema.allOf
260
+ // );
261
+ // if (mergedSchemas.properties !== undefined) {
262
+ // propertyDescription =
263
+ // mergedSchemas.properties[propertyName!]?.description;
264
+ // }
265
+ // }
266
+ // if (propertyDescription === undefined) {
267
+ // if (
268
+ // schema.properties !== undefined &&
269
+ // schema.properties![propertyName!] !== undefined
270
+ // ) {
271
+ // propertyDescription = schema.properties![propertyName!].description;
272
+ // }
273
+ // }
274
+ // return create("div", {
275
+ // className: "discriminatorItem",
276
+ // children: create("div", {
277
+ // children: [
278
+ // create("strong", {
279
+ // style: { paddingLeft: "1rem" },
280
+ // children: propertyName,
281
+ // }),
282
+ // guard(propertyType, (name) =>
283
+ // create("span", {
284
+ // style: { opacity: "0.6" },
285
+ // children: ` ${propertyType}`,
286
+ // })
287
+ // ),
288
+ // guard(getQualifierMessage(schema.discriminator as any), (message) =>
289
+ // create("div", {
290
+ // style: {
291
+ // paddingLeft: "1rem",
292
+ // },
293
+ // children: createDescription(message),
294
+ // })
295
+ // ),
296
+ // guard(propertyDescription, (description) =>
297
+ // create("div", {
298
+ // style: {
299
+ // paddingLeft: "1rem",
300
+ // },
301
+ // children: createDescription(description),
302
+ // })
303
+ // ),
304
+ // create("DiscriminatorTabs", {
305
+ // children: Object.keys(mapping!).map((key, index) => {
306
+ // if (mapping[key].allOf !== undefined) {
307
+ // const { mergedSchemas }: { mergedSchemas: SchemaObject } =
308
+ // mergeAllOf(mapping[key].allOf);
309
+ // // Cleanup duplicate property from mapping schema
310
+ // delete mergedSchemas.properties![propertyName!];
311
+ // mapping[key] = mergedSchemas;
312
+ // }
313
+ // if (mapping[key].properties !== undefined) {
314
+ // // Cleanup duplicate property from mapping schema
315
+ // delete mapping[key].properties![propertyName!];
316
+ // }
317
+ // const label = key;
318
+ // return create("TabItem", {
319
+ // label: label,
320
+ // value: `${index}-item-discriminator`,
321
+ // children: [
322
+ // create("div", {
323
+ // style: { marginLeft: "-4px" },
324
+ // children: createNodes(mapping[key]),
325
+ // }),
326
+ // ],
327
+ // });
328
+ // }),
329
+ // }),
330
+ // ],
331
+ // }),
332
+ // });
333
+ // }
322
334
  function createDetailsNode(name, schemaName, schema, required) {
323
335
  return (0, utils_1.create)("SchemaItem", {
324
336
  collapsible: true,
@@ -493,9 +505,9 @@ function createEdges({ name, schema, required, discriminator, }) {
493
505
  * Creates a hierarchical level of a schema tree. Nodes produce edges that can branch into sub-nodes with edges, recursively.
494
506
  */
495
507
  function createNodes(schema) {
496
- if (schema.discriminator !== undefined) {
497
- return createDiscriminator(schema);
498
- }
508
+ // if (schema.discriminator !== undefined) {
509
+ // return createDiscriminator(schema);
510
+ // }
499
511
  if (schema.oneOf !== undefined || schema.anyOf !== undefined) {
500
512
  return createAnyOneOf(schema);
501
513
  }
@@ -627,8 +639,11 @@ function createResponseSchema({ title, body, ...rest }) {
627
639
  }),
628
640
  ],
629
641
  }),
630
- responseExamples && (0, createStatusCodes_1.createResponseExamples)(responseExamples),
631
- responseExample && (0, createStatusCodes_1.createResponseExample)(responseExample),
642
+ firstBody && (0, createStatusCodes_1.createExampleFromSchema)(firstBody, mimeType),
643
+ responseExamples &&
644
+ (0, createStatusCodes_1.createResponseExamples)(responseExamples, mimeType),
645
+ responseExample &&
646
+ (0, createStatusCodes_1.createResponseExample)(responseExample, mimeType),
632
647
  ],
633
648
  }),
634
649
  ],
@@ -1,8 +1,10 @@
1
1
  import { ApiItem } from "../types";
2
+ export default function json2xml(o: any, tab: any): string;
2
3
  interface Props {
3
4
  responses: ApiItem["responses"];
4
5
  }
5
- export declare function createResponseExamples(responseExamples: any): string[];
6
- export declare function createResponseExample(responseExample: any): string;
6
+ export declare function createResponseExamples(responseExamples: any, mimeType: string): string[];
7
+ export declare function createResponseExample(responseExample: any, mimeType: string): string;
8
+ export declare function createExampleFromSchema(schema: any, mimeType: string): string | undefined;
7
9
  export declare function createStatusCodes({ responses }: Props): string | undefined;
8
10
  export {};
@@ -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;