docusaurus-plugin-openapi-docs 0.0.0-1000

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.
Files changed (163) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +361 -0
  3. package/lib/index.d.ts +9 -0
  4. package/lib/index.js +709 -0
  5. package/lib/markdown/createArrayBracket.d.ts +2 -0
  6. package/lib/markdown/createArrayBracket.js +36 -0
  7. package/lib/markdown/createAuthentication.d.ts +2 -0
  8. package/lib/markdown/createAuthentication.js +171 -0
  9. package/lib/markdown/createAuthorization.d.ts +1 -0
  10. package/lib/markdown/createAuthorization.js +15 -0
  11. package/lib/markdown/createCallbackMethodEndpoint.d.ts +1 -0
  12. package/lib/markdown/createCallbackMethodEndpoint.js +20 -0
  13. package/lib/markdown/createCallbacks.d.ts +6 -0
  14. package/lib/markdown/createCallbacks.js +77 -0
  15. package/lib/markdown/createContactInfo.d.ts +2 -0
  16. package/lib/markdown/createContactInfo.js +39 -0
  17. package/lib/markdown/createDeprecationNotice.d.ts +6 -0
  18. package/lib/markdown/createDeprecationNotice.js +20 -0
  19. package/lib/markdown/createDescription.d.ts +1 -0
  20. package/lib/markdown/createDescription.js +13 -0
  21. package/lib/markdown/createDetails.d.ts +2 -0
  22. package/lib/markdown/createDetails.js +17 -0
  23. package/lib/markdown/createDetailsSummary.d.ts +2 -0
  24. package/lib/markdown/createDetailsSummary.js +17 -0
  25. package/lib/markdown/createDownload.d.ts +1 -0
  26. package/lib/markdown/createDownload.js +16 -0
  27. package/lib/markdown/createHeading.d.ts +1 -0
  28. package/lib/markdown/createHeading.js +20 -0
  29. package/lib/markdown/createLicense.d.ts +2 -0
  30. package/lib/markdown/createLicense.js +32 -0
  31. package/lib/markdown/createLogo.d.ts +2 -0
  32. package/lib/markdown/createLogo.js +18 -0
  33. package/lib/markdown/createMethodEndpoint.d.ts +1 -0
  34. package/lib/markdown/createMethodEndpoint.js +20 -0
  35. package/lib/markdown/createParamsDetails.d.ts +6 -0
  36. package/lib/markdown/createParamsDetails.js +18 -0
  37. package/lib/markdown/createRequestBodyDetails.d.ts +13 -0
  38. package/lib/markdown/createRequestBodyDetails.js +13 -0
  39. package/lib/markdown/createRequestHeader.d.ts +1 -0
  40. package/lib/markdown/createRequestHeader.js +21 -0
  41. package/lib/markdown/createRequestSchema.d.ts +14 -0
  42. package/lib/markdown/createRequestSchema.js +20 -0
  43. package/lib/markdown/createResponseSchema.d.ts +14 -0
  44. package/lib/markdown/createResponseSchema.js +20 -0
  45. package/lib/markdown/createSchema.d.ts +9 -0
  46. package/lib/markdown/createSchema.js +668 -0
  47. package/lib/markdown/createSchema.test.d.ts +1 -0
  48. package/lib/markdown/createSchema.test.js +913 -0
  49. package/lib/markdown/createStatusCodes.d.ts +9 -0
  50. package/lib/markdown/createStatusCodes.js +63 -0
  51. package/lib/markdown/createTermsOfService.d.ts +1 -0
  52. package/lib/markdown/createTermsOfService.js +31 -0
  53. package/lib/markdown/createVendorExtensions.d.ts +1 -0
  54. package/lib/markdown/createVendorExtensions.js +24 -0
  55. package/lib/markdown/createVersionBadge.d.ts +1 -0
  56. package/lib/markdown/createVersionBadge.js +19 -0
  57. package/lib/markdown/index.d.ts +5 -0
  58. package/lib/markdown/index.js +92 -0
  59. package/lib/markdown/schema.d.ts +3 -0
  60. package/lib/markdown/schema.js +154 -0
  61. package/lib/markdown/schema.test.d.ts +1 -0
  62. package/lib/markdown/schema.test.js +181 -0
  63. package/lib/markdown/utils.d.ts +20 -0
  64. package/lib/markdown/utils.js +68 -0
  65. package/lib/openapi/createRequestExample.d.ts +2 -0
  66. package/lib/openapi/createRequestExample.js +191 -0
  67. package/lib/openapi/createResponseExample.d.ts +2 -0
  68. package/lib/openapi/createResponseExample.js +192 -0
  69. package/lib/openapi/index.d.ts +1 -0
  70. package/lib/openapi/index.js +12 -0
  71. package/lib/openapi/openapi.d.ts +12 -0
  72. package/lib/openapi/openapi.js +544 -0
  73. package/lib/openapi/openapi.test.d.ts +1 -0
  74. package/lib/openapi/openapi.test.js +33 -0
  75. package/lib/openapi/types.d.ts +354 -0
  76. package/lib/openapi/types.js +8 -0
  77. package/lib/openapi/utils/loadAndResolveSpec.d.ts +2 -0
  78. package/lib/openapi/utils/loadAndResolveSpec.js +153 -0
  79. package/lib/openapi/utils/services/OpenAPIParser.d.ts +52 -0
  80. package/lib/openapi/utils/services/OpenAPIParser.js +343 -0
  81. package/lib/openapi/utils/services/RedocNormalizedOptions.d.ts +100 -0
  82. package/lib/openapi/utils/services/RedocNormalizedOptions.js +170 -0
  83. package/lib/openapi/utils/types/index.d.ts +2 -0
  84. package/lib/openapi/utils/types/index.js +23 -0
  85. package/lib/openapi/utils/types/open-api.d.ts +305 -0
  86. package/lib/openapi/utils/types/open-api.js +8 -0
  87. package/lib/openapi/utils/types.d.ts +306 -0
  88. package/lib/openapi/utils/types.js +8 -0
  89. package/lib/openapi/utils/utils/JsonPointer.d.ts +51 -0
  90. package/lib/openapi/utils/utils/JsonPointer.js +95 -0
  91. package/lib/openapi/utils/utils/helpers.d.ts +43 -0
  92. package/lib/openapi/utils/utils/helpers.js +230 -0
  93. package/lib/openapi/utils/utils/index.d.ts +3 -0
  94. package/lib/openapi/utils/utils/index.js +25 -0
  95. package/lib/openapi/utils/utils/openapi.d.ts +40 -0
  96. package/lib/openapi/utils/utils/openapi.js +605 -0
  97. package/lib/options.d.ts +2 -0
  98. package/lib/options.js +69 -0
  99. package/lib/sidebars/index.d.ts +4 -0
  100. package/lib/sidebars/index.js +226 -0
  101. package/lib/sidebars/utils.d.ts +2 -0
  102. package/lib/sidebars/utils.js +30 -0
  103. package/lib/types.d.ts +135 -0
  104. package/lib/types.js +8 -0
  105. package/package.json +69 -0
  106. package/src/index.ts +945 -0
  107. package/src/markdown/__snapshots__/createSchema.test.ts.snap +1605 -0
  108. package/src/markdown/createArrayBracket.ts +35 -0
  109. package/src/markdown/createAuthentication.ts +201 -0
  110. package/src/markdown/createAuthorization.ts +13 -0
  111. package/src/markdown/createCallbackMethodEndpoint.ts +19 -0
  112. package/src/markdown/createCallbacks.ts +101 -0
  113. package/src/markdown/createContactInfo.ts +41 -0
  114. package/src/markdown/createDeprecationNotice.ts +31 -0
  115. package/src/markdown/createDescription.ts +12 -0
  116. package/src/markdown/createDetails.ts +16 -0
  117. package/src/markdown/createDetailsSummary.ts +16 -0
  118. package/src/markdown/createDownload.ts +15 -0
  119. package/src/markdown/createHeading.ts +23 -0
  120. package/src/markdown/createLicense.ts +34 -0
  121. package/src/markdown/createLogo.ts +21 -0
  122. package/src/markdown/createMethodEndpoint.ts +19 -0
  123. package/src/markdown/createParamsDetails.ts +22 -0
  124. package/src/markdown/createRequestBodyDetails.ts +24 -0
  125. package/src/markdown/createRequestHeader.ts +24 -0
  126. package/src/markdown/createRequestSchema.ts +32 -0
  127. package/src/markdown/createResponseSchema.ts +32 -0
  128. package/src/markdown/createSchema.test.ts +1075 -0
  129. package/src/markdown/createSchema.ts +862 -0
  130. package/src/markdown/createStatusCodes.ts +63 -0
  131. package/src/markdown/createTermsOfService.ts +30 -0
  132. package/src/markdown/createVendorExtensions.ts +22 -0
  133. package/src/markdown/createVersionBadge.ts +22 -0
  134. package/src/markdown/index.ts +144 -0
  135. package/src/markdown/schema.test.ts +208 -0
  136. package/src/markdown/schema.ts +185 -0
  137. package/src/markdown/utils.ts +89 -0
  138. package/src/openapi/__fixtures__/examples/openapi.yaml +49 -0
  139. package/src/openapi/createRequestExample.ts +235 -0
  140. package/src/openapi/createResponseExample.ts +238 -0
  141. package/src/openapi/index.ts +8 -0
  142. package/src/openapi/openapi.test.ts +40 -0
  143. package/src/openapi/openapi.ts +697 -0
  144. package/src/openapi/types.ts +455 -0
  145. package/src/openapi/utils/loadAndResolveSpec.ts +171 -0
  146. package/src/openapi/utils/services/OpenAPIParser.ts +434 -0
  147. package/src/openapi/utils/services/RedocNormalizedOptions.ts +330 -0
  148. package/src/openapi/utils/types/index.ts +10 -0
  149. package/src/openapi/utils/types/open-api.ts +303 -0
  150. package/src/openapi/utils/types.ts +303 -0
  151. package/src/openapi/utils/utils/JsonPointer.ts +99 -0
  152. package/src/openapi/utils/utils/helpers.ts +239 -0
  153. package/src/openapi/utils/utils/index.ts +11 -0
  154. package/src/openapi/utils/utils/openapi.ts +771 -0
  155. package/src/openapi-to-postmanv2.d.ts +10 -0
  156. package/src/options.ts +77 -0
  157. package/src/plugin-content-docs-types.d.ts +42 -0
  158. package/src/plugin-openapi.d.ts +87 -0
  159. package/src/postman-collection.d.ts +10 -0
  160. package/src/sidebars/index.ts +322 -0
  161. package/src/sidebars/utils.ts +29 -0
  162. package/src/types.ts +176 -0
  163. package/tsconfig.json +7 -0
@@ -0,0 +1,544 @@
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.readOpenapiFiles = readOpenapiFiles;
13
+ exports.processOpenapiFiles = processOpenapiFiles;
14
+ exports.processOpenapiFile = processOpenapiFile;
15
+ exports.getTagDisplayName = getTagDisplayName;
16
+ const path_1 = __importDefault(require("path"));
17
+ const utils_1 = require("@docusaurus/utils");
18
+ const chalk_1 = __importDefault(require("chalk"));
19
+ const fs_extra_1 = __importDefault(require("fs-extra"));
20
+ const cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
21
+ const kebabCase_1 = __importDefault(require("lodash/kebabCase"));
22
+ const unionBy_1 = __importDefault(require("lodash/unionBy"));
23
+ const uniq_1 = __importDefault(require("lodash/uniq"));
24
+ const openapi_to_postmanv2_1 = __importDefault(require("openapi-to-postmanv2"));
25
+ const postman_collection_1 = __importDefault(require("postman-collection"));
26
+ const createRequestExample_1 = require("./createRequestExample");
27
+ const index_1 = require("../index");
28
+ const createResponseExample_1 = require("./createResponseExample");
29
+ const loadAndResolveSpec_1 = require("./utils/loadAndResolveSpec");
30
+ /**
31
+ * Convenience function for converting raw JSON to a Postman Collection object.
32
+ */
33
+ function jsonToCollection(data) {
34
+ return new Promise((resolve, reject) => {
35
+ let schemaPack = new openapi_to_postmanv2_1.default.SchemaPack({ type: "json", data }, { schemaFaker: false });
36
+ schemaPack.computedOptions.schemaFaker = false;
37
+ schemaPack.convert((_err, conversionResult) => {
38
+ if (!conversionResult.result) {
39
+ return reject(conversionResult.reason);
40
+ }
41
+ return resolve(new postman_collection_1.default.Collection(conversionResult.output[0].data));
42
+ });
43
+ });
44
+ }
45
+ /**
46
+ * Creates a Postman Collection object from an OpenAPI definition.
47
+ */
48
+ async function createPostmanCollection(openapiData) {
49
+ var _a, _b, _c, _d, _e, _f, _g, _h;
50
+ // Create copy of openapiData
51
+ const data = (0, cloneDeep_1.default)(openapiData);
52
+ // Including `servers` breaks postman, so delete all of them.
53
+ delete data.servers;
54
+ for (let pathItemObject of Object.values(data.paths)) {
55
+ delete pathItemObject.servers;
56
+ (_a = pathItemObject.get) === null || _a === void 0 ? true : delete _a.servers;
57
+ (_b = pathItemObject.put) === null || _b === void 0 ? true : delete _b.servers;
58
+ (_c = pathItemObject.post) === null || _c === void 0 ? true : delete _c.servers;
59
+ (_d = pathItemObject.delete) === null || _d === void 0 ? true : delete _d.servers;
60
+ (_e = pathItemObject.options) === null || _e === void 0 ? true : delete _e.servers;
61
+ (_f = pathItemObject.head) === null || _f === void 0 ? true : delete _f.servers;
62
+ (_g = pathItemObject.patch) === null || _g === void 0 ? true : delete _g.servers;
63
+ (_h = pathItemObject.trace) === null || _h === void 0 ? true : delete _h.servers;
64
+ }
65
+ return await jsonToCollection(data);
66
+ }
67
+ function createItems(openapiData, options, sidebarOptions) {
68
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7;
69
+ // TODO: Find a better way to handle this
70
+ let items = [];
71
+ const infoIdSpaces = openapiData.info.title.replace(" ", "-").toLowerCase();
72
+ const infoId = (0, kebabCase_1.default)(infoIdSpaces);
73
+ if (openapiData.info.description || openapiData.info.title) {
74
+ // Only create an info page if we have a description.
75
+ const infoDescription = (_a = openapiData.info) === null || _a === void 0 ? void 0 : _a.description;
76
+ let splitDescription;
77
+ if (infoDescription) {
78
+ splitDescription = infoDescription.match(/[^\r\n]+/g);
79
+ }
80
+ const infoPage = {
81
+ type: "info",
82
+ id: infoId,
83
+ unversionedId: infoId,
84
+ title: openapiData.info.title
85
+ ? openapiData.info.title.replace(/((?:^|[^\\])(?:\\{2})*)"/g, "$1'")
86
+ : "",
87
+ description: openapiData.info.description
88
+ ? openapiData.info.description.replace(/((?:^|[^\\])(?:\\{2})*)"/g, "$1'")
89
+ : "",
90
+ frontMatter: {
91
+ description: splitDescription
92
+ ? splitDescription[0]
93
+ .replace(/((?:^|[^\\])(?:\\{2})*)"/g, "$1'")
94
+ .replace(/\s+$/, "")
95
+ : "",
96
+ },
97
+ securitySchemes: (_b = openapiData.components) === null || _b === void 0 ? void 0 : _b.securitySchemes,
98
+ info: {
99
+ ...openapiData.info,
100
+ tags: openapiData.tags,
101
+ title: (_c = openapiData.info.title) !== null && _c !== void 0 ? _c : "Introduction",
102
+ logo: openapiData.info["x-logo"],
103
+ darkLogo: openapiData.info["x-dark-logo"],
104
+ },
105
+ };
106
+ items.push(infoPage);
107
+ }
108
+ for (let [path, pathObject] of Object.entries(openapiData.paths)) {
109
+ const { $ref, description, parameters, servers, summary, ...rest } = pathObject;
110
+ for (let [method, operationObject] of Object.entries({ ...rest })) {
111
+ const title = (_e = (_d = operationObject.summary) !== null && _d !== void 0 ? _d : operationObject.operationId) !== null && _e !== void 0 ? _e : "Missing summary";
112
+ if (operationObject.description === undefined) {
113
+ operationObject.description =
114
+ (_g = (_f = operationObject.summary) !== null && _f !== void 0 ? _f : operationObject.operationId) !== null && _g !== void 0 ? _g : "";
115
+ }
116
+ const baseId = operationObject.operationId
117
+ ? (0, kebabCase_1.default)(operationObject.operationId)
118
+ : (0, kebabCase_1.default)(operationObject.summary);
119
+ const extensions = [];
120
+ const commonExtensions = ["x-codeSamples"];
121
+ for (const [key, value] of Object.entries(operationObject)) {
122
+ if (key.startsWith("x-") && !commonExtensions.includes(key)) {
123
+ extensions.push({ key, value });
124
+ }
125
+ }
126
+ const servers = (_j = (_h = operationObject.servers) !== null && _h !== void 0 ? _h : pathObject.servers) !== null && _j !== void 0 ? _j : openapiData.servers;
127
+ const security = (_k = operationObject.security) !== null && _k !== void 0 ? _k : openapiData.security;
128
+ // Add security schemes so we know how to handle security.
129
+ const securitySchemes = (_l = openapiData.components) === null || _l === void 0 ? void 0 : _l.securitySchemes;
130
+ // Make sure schemes are lowercase. See: https://github.com/cloud-annotations/docusaurus-plugin-openapi/issues/79
131
+ if (securitySchemes) {
132
+ for (let securityScheme of Object.values(securitySchemes)) {
133
+ if (securityScheme.type === "http") {
134
+ securityScheme.scheme = securityScheme.scheme.toLowerCase();
135
+ }
136
+ }
137
+ }
138
+ let jsonRequestBodyExample;
139
+ const content = (_m = operationObject.requestBody) === null || _m === void 0 ? void 0 : _m.content;
140
+ let body;
141
+ for (let key in content) {
142
+ if (key.toLowerCase() === "application/json" ||
143
+ key.toLowerCase() === "application/json; charset=utf-8") {
144
+ body = content[key];
145
+ break;
146
+ }
147
+ }
148
+ if (body === null || body === void 0 ? void 0 : body.schema) {
149
+ jsonRequestBodyExample = (0, createRequestExample_1.sampleRequestFromSchema)(body.schema);
150
+ }
151
+ // Handle vendor JSON media types
152
+ const bodyContent = (_o = operationObject.requestBody) === null || _o === void 0 ? void 0 : _o.content;
153
+ if (bodyContent && Object.keys(bodyContent).length > 0) {
154
+ const firstBodyContentKey = Object.keys(bodyContent)[0];
155
+ if (firstBodyContentKey.endsWith("+json")) {
156
+ const firstBody = bodyContent[firstBodyContentKey];
157
+ if (firstBody === null || firstBody === void 0 ? void 0 : firstBody.schema) {
158
+ jsonRequestBodyExample = (0, createRequestExample_1.sampleRequestFromSchema)(firstBody.schema);
159
+ }
160
+ }
161
+ }
162
+ // TODO: Don't include summary temporarilly
163
+ const { summary, ...defaults } = operationObject;
164
+ // Merge common parameters with operation parameters
165
+ // Operation params take precendence over common params
166
+ if (parameters !== undefined) {
167
+ if (operationObject.parameters !== undefined) {
168
+ defaults.parameters = (0, unionBy_1.default)(operationObject.parameters, parameters, "name");
169
+ }
170
+ else {
171
+ defaults.parameters = parameters;
172
+ }
173
+ }
174
+ const opDescription = operationObject.description;
175
+ let splitDescription;
176
+ if (opDescription) {
177
+ splitDescription = opDescription.match(/[^\r\n]+/g);
178
+ }
179
+ const apiPage = {
180
+ type: "api",
181
+ id: baseId,
182
+ infoId: infoId !== null && infoId !== void 0 ? infoId : "",
183
+ unversionedId: baseId,
184
+ title: title ? title.replace(/((?:^|[^\\])(?:\\{2})*)"/g, "$1'") : "",
185
+ description: operationObject.description
186
+ ? operationObject.description.replace(/((?:^|[^\\])(?:\\{2})*)"/g, "$1'")
187
+ : "",
188
+ frontMatter: {
189
+ description: splitDescription
190
+ ? splitDescription[0]
191
+ .replace(/((?:^|[^\\])(?:\\{2})*)"/g, "$1'")
192
+ .replace(/\s+$/, "")
193
+ : "",
194
+ ...((options === null || options === void 0 ? void 0 : options.proxy) && { proxy: options.proxy }),
195
+ ...((options === null || options === void 0 ? void 0 : options.hideSendButton) && {
196
+ hide_send_button: options.hideSendButton,
197
+ }),
198
+ ...((options === null || options === void 0 ? void 0 : options.showExtensions) && {
199
+ show_extensions: options.showExtensions,
200
+ }),
201
+ },
202
+ api: {
203
+ ...defaults,
204
+ ...(extensions.length > 0 && { extensions: extensions }),
205
+ tags: operationObject.tags,
206
+ method,
207
+ path,
208
+ servers,
209
+ security,
210
+ securitySchemes,
211
+ jsonRequestBodyExample,
212
+ info: openapiData.info,
213
+ },
214
+ };
215
+ items.push(apiPage);
216
+ }
217
+ }
218
+ // Gather x-webhooks endpoints
219
+ for (let [path, pathObject] of Object.entries((_q = (_p = openapiData["x-webhooks"]) !== null && _p !== void 0 ? _p : openapiData["webhooks"]) !== null && _q !== void 0 ? _q : {})) {
220
+ path = "webhook";
221
+ const { $ref, description, parameters, servers, summary, ...rest } = pathObject;
222
+ for (let [method, operationObject] of Object.entries({ ...rest })) {
223
+ method = "event";
224
+ const title = (_s = (_r = operationObject.summary) !== null && _r !== void 0 ? _r : operationObject.operationId) !== null && _s !== void 0 ? _s : "Missing summary";
225
+ if (operationObject.description === undefined) {
226
+ operationObject.description =
227
+ (_u = (_t = operationObject.summary) !== null && _t !== void 0 ? _t : operationObject.operationId) !== null && _u !== void 0 ? _u : "";
228
+ }
229
+ const baseId = operationObject.operationId
230
+ ? (0, kebabCase_1.default)(operationObject.operationId)
231
+ : (0, kebabCase_1.default)(operationObject.summary);
232
+ const extensions = [];
233
+ const commonExtensions = ["x-codeSamples"];
234
+ for (const [key, value] of Object.entries(operationObject)) {
235
+ if (key.startsWith("x-") && !commonExtensions.includes(key)) {
236
+ extensions.push({ key, value });
237
+ }
238
+ }
239
+ const servers = (_w = (_v = operationObject.servers) !== null && _v !== void 0 ? _v : pathObject.servers) !== null && _w !== void 0 ? _w : openapiData.servers;
240
+ const security = (_x = operationObject.security) !== null && _x !== void 0 ? _x : openapiData.security;
241
+ // Add security schemes so we know how to handle security.
242
+ const securitySchemes = (_y = openapiData.components) === null || _y === void 0 ? void 0 : _y.securitySchemes;
243
+ // Make sure schemes are lowercase. See: https://github.com/cloud-annotations/docusaurus-plugin-openapi/issues/79
244
+ if (securitySchemes) {
245
+ for (let securityScheme of Object.values(securitySchemes)) {
246
+ if (securityScheme.type === "http") {
247
+ securityScheme.scheme = securityScheme.scheme.toLowerCase();
248
+ }
249
+ }
250
+ }
251
+ let jsonRequestBodyExample;
252
+ const content = (_z = operationObject.requestBody) === null || _z === void 0 ? void 0 : _z.content;
253
+ let body;
254
+ for (let key in content) {
255
+ if (key.toLowerCase() === "application/json" ||
256
+ key.toLowerCase() === "application/json; charset=utf-8") {
257
+ body = content[key];
258
+ break;
259
+ }
260
+ }
261
+ if (body === null || body === void 0 ? void 0 : body.schema) {
262
+ jsonRequestBodyExample = (0, createRequestExample_1.sampleRequestFromSchema)(body.schema);
263
+ }
264
+ // Handle vendor JSON media types
265
+ const bodyContent = (_0 = operationObject.requestBody) === null || _0 === void 0 ? void 0 : _0.content;
266
+ if (bodyContent) {
267
+ const firstBodyContentKey = Object.keys(bodyContent)[0];
268
+ if (firstBodyContentKey.endsWith("+json")) {
269
+ const firstBody = bodyContent[firstBodyContentKey];
270
+ if (firstBody === null || firstBody === void 0 ? void 0 : firstBody.schema) {
271
+ jsonRequestBodyExample = (0, createRequestExample_1.sampleRequestFromSchema)(firstBody.schema);
272
+ }
273
+ }
274
+ }
275
+ // TODO: Don't include summary temporarilly
276
+ const { summary, ...defaults } = operationObject;
277
+ // Merge common parameters with operation parameters
278
+ // Operation params take precendence over common params
279
+ if (parameters !== undefined) {
280
+ if (operationObject.parameters !== undefined) {
281
+ defaults.parameters = (0, unionBy_1.default)(operationObject.parameters, parameters, "name");
282
+ }
283
+ else {
284
+ defaults.parameters = parameters;
285
+ }
286
+ }
287
+ const opDescription = operationObject.description;
288
+ let splitDescription;
289
+ if (opDescription) {
290
+ splitDescription = opDescription.match(/[^\r\n]+/g);
291
+ }
292
+ const apiPage = {
293
+ type: "api",
294
+ id: baseId,
295
+ infoId: infoId !== null && infoId !== void 0 ? infoId : "",
296
+ unversionedId: baseId,
297
+ title: title ? title.replace(/((?:^|[^\\])(?:\\{2})*)"/g, "$1'") : "",
298
+ description: operationObject.description
299
+ ? operationObject.description.replace(/((?:^|[^\\])(?:\\{2})*)"/g, "$1'")
300
+ : "",
301
+ frontMatter: {
302
+ description: splitDescription
303
+ ? splitDescription[0]
304
+ .replace(/((?:^|[^\\])(?:\\{2})*)"/g, "$1'")
305
+ .replace(/\s+$/, "")
306
+ : "",
307
+ ...((options === null || options === void 0 ? void 0 : options.proxy) && { proxy: options.proxy }),
308
+ ...((options === null || options === void 0 ? void 0 : options.hideSendButton) && {
309
+ hide_send_button: options.hideSendButton,
310
+ }),
311
+ ...((options === null || options === void 0 ? void 0 : options.showExtensions) && {
312
+ show_extensions: options.showExtensions,
313
+ }),
314
+ },
315
+ api: {
316
+ ...defaults,
317
+ ...(extensions.length > 0 && { extensions: extensions }),
318
+ tags: operationObject.tags,
319
+ method,
320
+ path,
321
+ servers,
322
+ security,
323
+ securitySchemes,
324
+ jsonRequestBodyExample,
325
+ info: openapiData.info,
326
+ },
327
+ };
328
+ items.push(apiPage);
329
+ }
330
+ }
331
+ if ((options === null || options === void 0 ? void 0 : options.showSchemas) === true ||
332
+ Object.entries((_2 = (_1 = openapiData === null || openapiData === void 0 ? void 0 : openapiData.components) === null || _1 === void 0 ? void 0 : _1.schemas) !== null && _2 !== void 0 ? _2 : {})
333
+ .flatMap(([_, s]) => s["x-tags"])
334
+ .filter((item) => !!item).length > 0) {
335
+ // Gather schemas
336
+ for (let [schema, schemaObject] of Object.entries((_4 = (_3 = openapiData === null || openapiData === void 0 ? void 0 : openapiData.components) === null || _3 === void 0 ? void 0 : _3.schemas) !== null && _4 !== void 0 ? _4 : {})) {
337
+ if ((options === null || options === void 0 ? void 0 : options.showSchemas) === true || schemaObject["x-tags"]) {
338
+ const baseIdSpaces = (_6 = (_5 = schemaObject === null || schemaObject === void 0 ? void 0 : schemaObject.title) === null || _5 === void 0 ? void 0 : _5.replace(" ", "-").toLowerCase()) !== null && _6 !== void 0 ? _6 : "";
339
+ const baseId = (0, kebabCase_1.default)(baseIdSpaces);
340
+ const schemaDescription = schemaObject.description;
341
+ let splitDescription;
342
+ if (schemaDescription) {
343
+ splitDescription = schemaDescription.match(/[^\r\n]+/g);
344
+ }
345
+ const schemaPage = {
346
+ type: "schema",
347
+ id: baseId,
348
+ infoId: infoId !== null && infoId !== void 0 ? infoId : "",
349
+ unversionedId: baseId,
350
+ title: schemaObject.title
351
+ ? schemaObject.title.replace(/((?:^|[^\\])(?:\\{2})*)"/g, "$1'")
352
+ : schema,
353
+ description: schemaObject.description
354
+ ? schemaObject.description.replace(/((?:^|[^\\])(?:\\{2})*)"/g, "$1'")
355
+ : "",
356
+ frontMatter: {
357
+ description: splitDescription
358
+ ? splitDescription[0]
359
+ .replace(/((?:^|[^\\])(?:\\{2})*)"/g, "$1'")
360
+ .replace(/\s+$/, "")
361
+ : "",
362
+ sample: JSON.stringify((0, createResponseExample_1.sampleResponseFromSchema)(schemaObject)),
363
+ },
364
+ schema: schemaObject,
365
+ };
366
+ items.push(schemaPage);
367
+ }
368
+ }
369
+ }
370
+ if ((sidebarOptions === null || sidebarOptions === void 0 ? void 0 : sidebarOptions.categoryLinkSource) === "tag") {
371
+ // Get global tags
372
+ const tags = (_7 = openapiData.tags) !== null && _7 !== void 0 ? _7 : [];
373
+ // Get operation tags
374
+ const apiItems = items.filter((item) => {
375
+ return item.type === "api";
376
+ });
377
+ const operationTags = (0, uniq_1.default)(apiItems
378
+ .flatMap((item) => item.api.tags)
379
+ .filter((item) => !!item));
380
+ // eslint-disable-next-line array-callback-return
381
+ tags
382
+ .filter((tag) => operationTags.includes(tag.name)) // include only tags referenced by operation
383
+ // eslint-disable-next-line array-callback-return
384
+ .map((tag) => {
385
+ var _a;
386
+ const description = getTagDisplayName(tag.name, (_a = openapiData.tags) !== null && _a !== void 0 ? _a : []);
387
+ const tagId = (0, kebabCase_1.default)(tag.name);
388
+ const splitDescription = description.match(/[^\r\n]+/g);
389
+ const tagPage = {
390
+ type: "tag",
391
+ id: tagId,
392
+ unversionedId: tagId,
393
+ title: description !== null && description !== void 0 ? description : "",
394
+ description: description !== null && description !== void 0 ? description : "",
395
+ frontMatter: {
396
+ description: splitDescription
397
+ ? splitDescription[0]
398
+ .replace(/((?:^|[^\\])(?:\\{2})*)"/g, "$1'")
399
+ .replace(/\s+$/, "")
400
+ : "",
401
+ },
402
+ tag: {
403
+ ...tag,
404
+ },
405
+ };
406
+ items.push(tagPage);
407
+ });
408
+ }
409
+ return items;
410
+ }
411
+ /**
412
+ * Attach Postman Request objects to the corresponding ApiItems.
413
+ */
414
+ function bindCollectionToApiItems(items, postmanCollection) {
415
+ postmanCollection.forEachItem((item) => {
416
+ const method = item.request.method.toLowerCase();
417
+ const path = item.request.url
418
+ .getPath({ unresolved: true }) // unresolved returns "/:variableName" instead of "/<type>"
419
+ .replace(/(?<![a-z0-9-_]+):([a-z0-9-_]+)/gi, "{$1}"); // replace "/:variableName" with "/{variableName}"
420
+ const apiItem = items.find((item) => {
421
+ if (item.type === "info" ||
422
+ item.type === "tag" ||
423
+ item.type === "schema") {
424
+ return false;
425
+ }
426
+ return item.api.path === path && item.api.method === method;
427
+ });
428
+ if ((apiItem === null || apiItem === void 0 ? void 0 : apiItem.type) === "api") {
429
+ apiItem.api.postman = item.request;
430
+ }
431
+ });
432
+ }
433
+ async function readOpenapiFiles(openapiPath) {
434
+ if (!(0, index_1.isURL)(openapiPath)) {
435
+ const stat = await fs_extra_1.default.lstat(openapiPath);
436
+ if (stat.isDirectory()) {
437
+ // TODO: Add config for inlcude/ignore
438
+ const allFiles = await (0, utils_1.Globby)(["**/*.{json,yaml,yml}"], {
439
+ cwd: openapiPath,
440
+ ignore: utils_1.GlobExcludeDefault,
441
+ deep: 1,
442
+ });
443
+ const sources = allFiles.filter((x) => !x.includes("_category_")); // todo: regex exclude?
444
+ return Promise.all(sources.map(async (source) => {
445
+ // TODO: make a function for this
446
+ const fullPath = (0, utils_1.posixPath)(path_1.default.join(openapiPath, source));
447
+ const data = (await (0, loadAndResolveSpec_1.loadAndResolveSpec)(fullPath));
448
+ return {
449
+ source: fullPath, // This will be aliased in process.
450
+ sourceDirName: path_1.default.dirname(source),
451
+ data,
452
+ };
453
+ }));
454
+ }
455
+ }
456
+ const data = (await (0, loadAndResolveSpec_1.loadAndResolveSpec)(openapiPath));
457
+ return [
458
+ {
459
+ source: openapiPath, // This will be aliased in process.
460
+ sourceDirName: ".",
461
+ data,
462
+ },
463
+ ];
464
+ }
465
+ async function processOpenapiFiles(files, options, sidebarOptions) {
466
+ const promises = files.map(async (file) => {
467
+ if (file.data !== undefined) {
468
+ const processedFile = await processOpenapiFile(file.data, options, sidebarOptions);
469
+ const itemsObjectsArray = processedFile[0].map((item) => ({
470
+ ...item,
471
+ }));
472
+ const tags = processedFile[1];
473
+ const tagGroups = processedFile[2];
474
+ return [itemsObjectsArray, tags, tagGroups];
475
+ }
476
+ console.warn(chalk_1.default.yellow(`WARNING: the following OpenAPI spec returned undefined: ${file.source}`));
477
+ return [];
478
+ });
479
+ const metadata = await Promise.all(promises);
480
+ const items = metadata
481
+ .map(function (x) {
482
+ return x[0];
483
+ })
484
+ .flat()
485
+ .filter(function (x) {
486
+ // Remove undefined items due to transient parsing errors
487
+ return x !== undefined;
488
+ });
489
+ const tags = metadata
490
+ .map(function (x) {
491
+ return x[1];
492
+ })
493
+ .filter(function (x) {
494
+ // Remove undefined tags due to transient parsing errors
495
+ return x !== undefined;
496
+ });
497
+ const tagGroups = metadata
498
+ .map(function (x) {
499
+ return x[2];
500
+ })
501
+ .flat()
502
+ .filter(function (x) {
503
+ // Remove undefined tags due to transient parsing errors
504
+ return x !== undefined;
505
+ });
506
+ return [
507
+ items,
508
+ tags,
509
+ tagGroups,
510
+ ];
511
+ }
512
+ async function processOpenapiFile(openapiData, options, sidebarOptions) {
513
+ const postmanCollection = await createPostmanCollection(openapiData);
514
+ const items = createItems(openapiData, options, sidebarOptions);
515
+ bindCollectionToApiItems(items, postmanCollection);
516
+ let tags = [];
517
+ if (openapiData.tags !== undefined) {
518
+ tags = openapiData.tags;
519
+ }
520
+ let tagGroups = [];
521
+ if (openapiData["x-tagGroups"] !== undefined) {
522
+ tagGroups = openapiData["x-tagGroups"];
523
+ }
524
+ return [items, tags, tagGroups];
525
+ }
526
+ // order for picking items as a display name of tags
527
+ const tagDisplayNameProperties = ["x-displayName", "name"];
528
+ function getTagDisplayName(tagName, tags) {
529
+ var _a;
530
+ // find the very own tagObject
531
+ const tagObject = (_a = tags.find((tagObject) => tagObject.name === tagName)) !== null && _a !== void 0 ? _a : {
532
+ // if none found, just fake one
533
+ name: tagName,
534
+ };
535
+ // return the first found and filled value from the property list
536
+ for (const property of tagDisplayNameProperties) {
537
+ const displayName = tagObject[property];
538
+ if (typeof displayName === "string") {
539
+ return displayName;
540
+ }
541
+ }
542
+ // always default to the tagName
543
+ return tagName;
544
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,33 @@
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
+ const path_1 = __importDefault(require("path"));
13
+ // eslint-disable-next-line import/no-extraneous-dependencies
14
+ const utils_1 = require("@docusaurus/utils");
15
+ const _1 = require(".");
16
+ // npx jest packages/docusaurus-plugin-openapi/src/openapi/openapi.test.ts --watch
17
+ describe("openapi", () => {
18
+ describe("readOpenapiFiles", () => {
19
+ it("readOpenapiFiles", async () => {
20
+ var _a, _b;
21
+ const results = await (0, _1.readOpenapiFiles)((0, utils_1.posixPath)(path_1.default.join(__dirname, "__fixtures__/examples")));
22
+ const categoryMeta = results.find((x) => x.source.endsWith("_category_.json"));
23
+ expect(categoryMeta).toBeFalsy();
24
+ // console.log(results);
25
+ const yaml = results.find((x) => x.source.endsWith("openapi.yaml"));
26
+ expect(yaml).toBeTruthy();
27
+ expect(yaml === null || yaml === void 0 ? void 0 : yaml.sourceDirName).toBe(".");
28
+ expect(yaml === null || yaml === void 0 ? void 0 : yaml.data.tags).toBeDefined();
29
+ expect(yaml === null || yaml === void 0 ? void 0 : yaml.data["x-tagGroups"]).toBeDefined();
30
+ expect((_b = (_a = yaml === null || yaml === void 0 ? void 0 : yaml.data.components) === null || _a === void 0 ? void 0 : _a.schemas) === null || _b === void 0 ? void 0 : _b.HelloString["x-tags"]).toBeDefined();
31
+ });
32
+ });
33
+ });