@camunda8/docusaurus-plugin-openapi-docs 4.5.1

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