@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
package/lib/index.js ADDED
@@ -0,0 +1,714 @@
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.isURL = isURL;
13
+ exports.getDocsPluginConfig = getDocsPluginConfig;
14
+ exports.default = pluginOpenAPIDocs;
15
+ const fs_1 = __importDefault(require("fs"));
16
+ const path_1 = __importDefault(require("path"));
17
+ const zlib_1 = __importDefault(require("zlib"));
18
+ const utils_1 = require("@docusaurus/utils");
19
+ const chalk_1 = __importDefault(require("chalk"));
20
+ const json5_1 = __importDefault(require("json5"));
21
+ const mustache_1 = require("mustache");
22
+ const markdown_1 = require("./markdown");
23
+ const openapi_1 = require("./openapi");
24
+ const options_1 = require("./options");
25
+ const sidebars_1 = __importDefault(require("./sidebars"));
26
+ function isURL(str) {
27
+ return /^(https?:)\/\//m.test(str);
28
+ }
29
+ function getDocsPluginConfig(presetsPlugins, plugin, pluginId) {
30
+ // eslint-disable-next-line array-callback-return
31
+ const filteredConfig = presetsPlugins.filter((data) => {
32
+ // Search presets
33
+ if (Array.isArray(data)) {
34
+ if (typeof data[0] === "string" && data[0].endsWith(pluginId)) {
35
+ return data[1];
36
+ }
37
+ // Search plugin-content-docs instances
38
+ if (typeof data[0] === "string" && data[0] === plugin) {
39
+ const configPluginId = data[1].id ? data[1].id : "default";
40
+ if (configPluginId === pluginId) {
41
+ return data[1];
42
+ }
43
+ }
44
+ }
45
+ })[0];
46
+ if (filteredConfig) {
47
+ // Search presets, e.g. "classic"
48
+ if (filteredConfig[0].endsWith(pluginId)) {
49
+ return filteredConfig[1].docs;
50
+ }
51
+ // Search plugin-content-docs instances
52
+ if (filteredConfig[0] === plugin) {
53
+ const configPluginId = filteredConfig[1].id
54
+ ? filteredConfig[1].id
55
+ : "default";
56
+ if (configPluginId === pluginId) {
57
+ return filteredConfig[1];
58
+ }
59
+ }
60
+ }
61
+ return;
62
+ }
63
+ function getPluginConfig(plugins, pluginId) {
64
+ return plugins.filter((data) => data[1].id === pluginId)[0][1];
65
+ }
66
+ function getPluginInstances(plugins) {
67
+ return plugins.filter((data) => data[0] === "@camunda8/docusaurus-plugin-openapi-docs");
68
+ }
69
+ function pluginOpenAPIDocs(context, options) {
70
+ const { config, docsPlugin = "@docusaurus/plugin-content-docs", docsPluginId, } = options;
71
+ const { siteDir, siteConfig } = context;
72
+ // Get routeBasePath and path from plugin-content-docs or preset
73
+ const presets = siteConfig.presets;
74
+ const plugins = siteConfig.plugins;
75
+ const presetsPlugins = presets.concat(plugins);
76
+ let docData = getDocsPluginConfig(presetsPlugins, docsPlugin, docsPluginId);
77
+ let docRouteBasePath = docData ? docData.routeBasePath : undefined;
78
+ let docPath = docData ? (docData.path ? docData.path : "docs") : undefined;
79
+ async function generateApiDocs(options, pluginId) {
80
+ var _a, _b, _c, _d;
81
+ let { specPath, outputDir, template, infoTemplate, tagTemplate, schemaTemplate, markdownGenerators, downloadUrl, sidebarOptions, disableCompression, } = options;
82
+ // Remove trailing slash before proceeding
83
+ outputDir = outputDir.replace(/\/$/, "");
84
+ // Override docPath if pluginId provided
85
+ if (pluginId) {
86
+ docData = getDocsPluginConfig(presetsPlugins, docsPlugin, pluginId);
87
+ docRouteBasePath = docData ? docData.routeBasePath : undefined;
88
+ docPath = docData ? (docData.path ? docData.path : "docs") : undefined;
89
+ }
90
+ const contentPath = isURL(specPath)
91
+ ? specPath
92
+ : path_1.default.resolve(siteDir, specPath);
93
+ try {
94
+ const openapiFiles = await (0, openapi_1.readOpenapiFiles)(contentPath);
95
+ const [loadedApi, tags, tagGroups] = await (0, openapi_1.processOpenapiFiles)(openapiFiles, options, sidebarOptions, siteDir);
96
+ if (!fs_1.default.existsSync(outputDir)) {
97
+ try {
98
+ fs_1.default.mkdirSync(outputDir, { recursive: true });
99
+ console.log(chalk_1.default.green(`Successfully created "${outputDir}"`));
100
+ }
101
+ catch (err) {
102
+ console.error(chalk_1.default.red(`Failed to create "${outputDir}"`), chalk_1.default.yellow(err));
103
+ }
104
+ }
105
+ // TODO: figure out better way to set default
106
+ if (Object.keys(sidebarOptions !== null && sidebarOptions !== void 0 ? sidebarOptions : {}).length > 0) {
107
+ const sidebarSlice = (0, sidebars_1.default)(sidebarOptions, options, loadedApi, tags, docPath, tagGroups);
108
+ let sidebarSliceTemplate = `import type { SidebarsConfig } from "@docusaurus/plugin-content-docs";\n\n`;
109
+ sidebarSliceTemplate += `const sidebar: SidebarsConfig = {{{slice}}};\n\n`;
110
+ sidebarSliceTemplate += `export default sidebar.apisidebar;\n`;
111
+ const view = (0, mustache_1.render)(sidebarSliceTemplate, {
112
+ slice: json5_1.default.stringify({ apisidebar: sidebarSlice }, { space: 2, quote: '"' }),
113
+ });
114
+ if (!fs_1.default.existsSync(`${outputDir}/sidebar.ts`)) {
115
+ try {
116
+ fs_1.default.writeFileSync(`${outputDir}/sidebar.ts`, view, "utf8");
117
+ console.log(chalk_1.default.green(`Successfully created "${outputDir}/sidebar.ts"`));
118
+ }
119
+ catch (err) {
120
+ console.error(chalk_1.default.red(`Failed to write "${outputDir}/sidebar.ts"`), chalk_1.default.yellow(err));
121
+ }
122
+ }
123
+ }
124
+ const mdTemplate = template
125
+ ? fs_1.default.readFileSync(template).toString()
126
+ : `---
127
+ id: {{{id}}}
128
+ title: "{{{title}}}"
129
+ description: "{{{frontMatter.description}}}"
130
+ {{^api}}
131
+ sidebar_label: Introduction
132
+ {{/api}}
133
+ {{#api}}
134
+ sidebar_label: "{{{title}}}"
135
+ {{/api}}
136
+ {{^api}}
137
+ sidebar_position: 0
138
+ {{/api}}
139
+ hide_title: true
140
+ {{#api}}
141
+ hide_table_of_contents: true
142
+ {{/api}}
143
+ {{#json}}
144
+ api: {{{json}}}
145
+ {{/json}}
146
+ {{#api.method}}
147
+ sidebar_class_name: "{{{api.method}}} api-method"
148
+ {{/api.method}}
149
+ {{#infoPath}}
150
+ info_path: {{{infoPath}}}
151
+ {{/infoPath}}
152
+ custom_edit_url: null
153
+ {{#frontMatter.proxy}}
154
+ proxy: {{{frontMatter.proxy}}}
155
+ {{/frontMatter.proxy}}
156
+ {{#frontMatter.hide_send_button}}
157
+ hide_send_button: true
158
+ {{/frontMatter.hide_send_button}}
159
+ {{#frontMatter.show_extensions}}
160
+ show_extensions: true
161
+ {{/frontMatter.show_extensions}}
162
+ {{#frontMatter.mask_credentials_disabled}}
163
+ mask_credentials: false
164
+ {{/frontMatter.mask_credentials_disabled}}
165
+ ---
166
+
167
+ {{{markdown}}}
168
+ `;
169
+ const infoMdTemplate = infoTemplate
170
+ ? fs_1.default.readFileSync(infoTemplate).toString()
171
+ : `---
172
+ id: {{{id}}}
173
+ title: "{{{title}}}"
174
+ description: "{{{frontMatter.description}}}"
175
+ sidebar_label: "{{{title}}}"
176
+ hide_title: true
177
+ custom_edit_url: null
178
+ ---
179
+
180
+ {{{markdown}}}
181
+
182
+ \`\`\`mdx-code-block
183
+ import DocCardList from '@theme/DocCardList';
184
+ import {useCurrentSidebarCategory} from '@docusaurus/theme-common';
185
+
186
+ <DocCardList items={useCurrentSidebarCategory().items}/>
187
+ \`\`\`
188
+ `;
189
+ const tagMdTemplate = tagTemplate
190
+ ? fs_1.default.readFileSync(tagTemplate).toString()
191
+ : `---
192
+ id: {{{id}}}
193
+ title: "{{{frontMatter.description}}}"
194
+ description: "{{{frontMatter.description}}}"
195
+ custom_edit_url: null
196
+ ---
197
+
198
+ {{{markdown}}}
199
+
200
+ \`\`\`mdx-code-block
201
+ import DocCardList from '@theme/DocCardList';
202
+ import {useCurrentSidebarCategory} from '@docusaurus/theme-common';
203
+
204
+ <DocCardList items={useCurrentSidebarCategory().items}/>
205
+ \`\`\`
206
+ `;
207
+ const schemaMdTemplate = schemaTemplate
208
+ ? fs_1.default.readFileSync(schemaTemplate).toString()
209
+ : `---
210
+ id: {{{id}}}
211
+ title: "{{{title}}}"
212
+ description: "{{{frontMatter.description}}}"
213
+ sidebar_label: "{{{title}}}"
214
+ hide_title: true
215
+ {{#schema}}
216
+ hide_table_of_contents: true
217
+ {{/schema}}
218
+ schema: true
219
+ sample: {{{frontMatter.sample}}}
220
+ custom_edit_url: null
221
+ ---
222
+
223
+ {{{markdown}}}
224
+ `;
225
+ const apiPageGenerator = (_a = markdownGenerators === null || markdownGenerators === void 0 ? void 0 : markdownGenerators.createApiPageMD) !== null && _a !== void 0 ? _a : markdown_1.createApiPageMD;
226
+ const infoPageGenerator = (_b = markdownGenerators === null || markdownGenerators === void 0 ? void 0 : markdownGenerators.createInfoPageMD) !== null && _b !== void 0 ? _b : markdown_1.createInfoPageMD;
227
+ const tagPageGenerator = (_c = markdownGenerators === null || markdownGenerators === void 0 ? void 0 : markdownGenerators.createTagPageMD) !== null && _c !== void 0 ? _c : markdown_1.createTagPageMD;
228
+ const schemaPageGenerator = (_d = markdownGenerators === null || markdownGenerators === void 0 ? void 0 : markdownGenerators.createSchemaPageMD) !== null && _d !== void 0 ? _d : markdown_1.createSchemaPageMD;
229
+ const pageGeneratorByType = {
230
+ api: apiPageGenerator,
231
+ info: infoPageGenerator,
232
+ tag: tagPageGenerator,
233
+ schema: schemaPageGenerator,
234
+ };
235
+ loadedApi.map(async (item) => {
236
+ if (downloadUrl) {
237
+ item.downloadUrl = downloadUrl;
238
+ }
239
+ const markdown = pageGeneratorByType[item.type](item);
240
+ item.markdown = markdown;
241
+ if (item.type === "api") {
242
+ // opportunity to compress JSON
243
+ // const serialize = (o: any) => {
244
+ // return zlib.deflateSync(JSON.stringify(o)).toString("base64");
245
+ // };
246
+ // const deserialize = (s: any) => {
247
+ // return zlib.inflateSync(Buffer.from(s, "base64")).toString();
248
+ // };
249
+ disableCompression === true
250
+ ? (item.json = JSON.stringify(item.api))
251
+ : (item.json = zlib_1.default
252
+ .deflateSync(JSON.stringify(item.api))
253
+ .toString("base64"));
254
+ let infoBasePath = `${outputDir}/${item.infoId}`;
255
+ if (docRouteBasePath) {
256
+ infoBasePath = `${docRouteBasePath}/${outputDir
257
+ .split(docPath)[1]
258
+ .replace(/^\/+/g, "")}/${item.infoId}`.replace(/^\/+/g, "");
259
+ }
260
+ if (item.infoId)
261
+ item.infoPath = infoBasePath;
262
+ }
263
+ const view = (0, mustache_1.render)(mdTemplate, item);
264
+ const utils = (0, mustache_1.render)(infoMdTemplate, item);
265
+ // eslint-disable-next-line testing-library/render-result-naming-convention
266
+ const tagUtils = (0, mustache_1.render)(tagMdTemplate, item);
267
+ if (item.type === "api") {
268
+ if (!fs_1.default.existsSync(`${outputDir}/${item.id}.api.mdx`)) {
269
+ try {
270
+ // kebabCase(arg) returns 0-length string when arg is undefined
271
+ if (item.id.length === 0) {
272
+ throw Error("Operation must have summary or operationId defined");
273
+ }
274
+ fs_1.default.writeFileSync(`${outputDir}/${item.id}.api.mdx`, view, "utf8");
275
+ console.log(chalk_1.default.green(`Successfully created "${outputDir}/${item.id}.api.mdx"`));
276
+ }
277
+ catch (err) {
278
+ console.error(chalk_1.default.red(`Failed to write "${outputDir}/${item.id}.api.mdx"`), chalk_1.default.yellow(err));
279
+ }
280
+ }
281
+ }
282
+ if (item.type === "info") {
283
+ if (!fs_1.default.existsSync(`${outputDir}/${item.id}.info.mdx`)) {
284
+ try {
285
+ (sidebarOptions === null || sidebarOptions === void 0 ? void 0 : sidebarOptions.categoryLinkSource) === "info" || infoTemplate // Only use utils template if set to "info" or if infoTemplate is set
286
+ ? fs_1.default.writeFileSync(`${outputDir}/${item.id}.info.mdx`, utils, "utf8")
287
+ : fs_1.default.writeFileSync(`${outputDir}/${item.id}.info.mdx`, view, "utf8");
288
+ console.log(chalk_1.default.green(`Successfully created "${outputDir}/${item.id}.info.mdx"`));
289
+ }
290
+ catch (err) {
291
+ console.error(chalk_1.default.red(`Failed to write "${outputDir}/${item.id}.info.mdx"`), chalk_1.default.yellow(err));
292
+ }
293
+ }
294
+ }
295
+ if (item.type === "tag") {
296
+ if (!fs_1.default.existsSync(`${outputDir}/${item.id}.tag.mdx`)) {
297
+ try {
298
+ fs_1.default.writeFileSync(`${outputDir}/${item.id}.tag.mdx`, tagUtils, "utf8");
299
+ console.log(chalk_1.default.green(`Successfully created "${outputDir}/${item.id}.tag.mdx"`));
300
+ }
301
+ catch (err) {
302
+ console.error(chalk_1.default.red(`Failed to write "${outputDir}/${item.id}.tag.mdx"`), chalk_1.default.yellow(err));
303
+ }
304
+ }
305
+ }
306
+ if (item.type === "schema") {
307
+ if (!fs_1.default.existsSync(`${outputDir}/schemas/${item.id}.schema.mdx`)) {
308
+ if (!fs_1.default.existsSync(`${outputDir}/schemas`)) {
309
+ try {
310
+ fs_1.default.mkdirSync(`${outputDir}/schemas`, { recursive: true });
311
+ console.log(chalk_1.default.green(`Successfully created "${outputDir}/schemas"`));
312
+ }
313
+ catch (err) {
314
+ console.error(chalk_1.default.red(`Failed to create "${outputDir}/schemas"`), chalk_1.default.yellow(err));
315
+ }
316
+ }
317
+ try {
318
+ // kebabCase(arg) returns 0-length string when arg is undefined
319
+ if (item.id.length === 0) {
320
+ throw Error("Schema must have title defined");
321
+ }
322
+ // eslint-disable-next-line testing-library/render-result-naming-convention
323
+ const schemaView = (0, mustache_1.render)(schemaMdTemplate, item);
324
+ fs_1.default.writeFileSync(`${outputDir}/schemas/${item.id}.schema.mdx`, schemaView, "utf8");
325
+ console.log(chalk_1.default.green(`Successfully created "${outputDir}/${item.id}.schema.mdx"`));
326
+ }
327
+ catch (err) {
328
+ console.error(chalk_1.default.red(`Failed to write "${outputDir}/${item.id}.schema.mdx"`), chalk_1.default.yellow(err));
329
+ }
330
+ }
331
+ }
332
+ return;
333
+ });
334
+ return;
335
+ }
336
+ catch (e) {
337
+ console.error(chalk_1.default.red(`Loading of api failed for "${contentPath}"`));
338
+ throw e;
339
+ }
340
+ }
341
+ async function cleanApiDocs(options) {
342
+ const { outputDir } = options;
343
+ const apiDir = (0, utils_1.posixPath)(path_1.default.join(siteDir, outputDir));
344
+ const apiMdxFiles = await (0, utils_1.Globby)(["*.api.mdx", "*.info.mdx", "*.tag.mdx"], {
345
+ cwd: path_1.default.resolve(apiDir),
346
+ deep: 1,
347
+ });
348
+ const sidebarFile = await (0, utils_1.Globby)(["sidebar.js", "sidebar.ts"], {
349
+ cwd: path_1.default.resolve(apiDir),
350
+ deep: 1,
351
+ });
352
+ apiMdxFiles.map((mdx) => fs_1.default.unlink(`${apiDir}/${mdx}`, (err) => {
353
+ if (err) {
354
+ console.error(chalk_1.default.red(`Cleanup failed for "${apiDir}/${mdx}"`), chalk_1.default.yellow(err));
355
+ }
356
+ else {
357
+ console.log(chalk_1.default.green(`Cleanup succeeded for "${apiDir}/${mdx}"`));
358
+ }
359
+ }));
360
+ try {
361
+ fs_1.default.rmSync(`${apiDir}/schemas`, { recursive: true });
362
+ console.log(chalk_1.default.green(`Cleanup succeeded for "${apiDir}/schemas"`));
363
+ }
364
+ catch (err) {
365
+ if (err.code !== "ENOENT") {
366
+ console.error(chalk_1.default.red(`Cleanup failed for "${apiDir}/schemas"`), chalk_1.default.yellow(err));
367
+ }
368
+ }
369
+ sidebarFile.map((sidebar) => fs_1.default.unlink(`${apiDir}/${sidebar}`, (err) => {
370
+ if (err) {
371
+ console.error(chalk_1.default.red(`Cleanup failed for "${apiDir}/${sidebar}"`), chalk_1.default.yellow(err));
372
+ }
373
+ else {
374
+ console.log(chalk_1.default.green(`Cleanup succeeded for "${apiDir}/${sidebar}"`));
375
+ }
376
+ }));
377
+ }
378
+ async function generateVersions(versions, outputDir) {
379
+ let versionsArray = [];
380
+ for (const [version, metadata] of Object.entries(versions)) {
381
+ versionsArray.push({
382
+ version: version,
383
+ label: metadata.label,
384
+ baseUrl: metadata.baseUrl,
385
+ downloadUrl: metadata.downloadUrl,
386
+ });
387
+ }
388
+ if (!fs_1.default.existsSync(outputDir)) {
389
+ try {
390
+ fs_1.default.mkdirSync(outputDir, { recursive: true });
391
+ console.log(chalk_1.default.green(`Successfully created "${outputDir}"`));
392
+ }
393
+ catch (err) {
394
+ console.error(chalk_1.default.red(`Failed to create "${outputDir}"`), chalk_1.default.yellow(err));
395
+ }
396
+ }
397
+ const versionsJson = JSON.stringify(versionsArray, null, 2);
398
+ try {
399
+ fs_1.default.writeFileSync(`${outputDir}/versions.json`, versionsJson + "\n", "utf8");
400
+ console.log(chalk_1.default.green(`Successfully created "${outputDir}/versions.json"`));
401
+ }
402
+ catch (err) {
403
+ console.error(chalk_1.default.red(`Failed to write "${outputDir}/versions.json"`), chalk_1.default.yellow(err));
404
+ }
405
+ }
406
+ async function cleanVersions(outputDir) {
407
+ if (fs_1.default.existsSync(`${outputDir}/versions.json`)) {
408
+ fs_1.default.unlink(`${outputDir}/versions.json`, (err) => {
409
+ if (err) {
410
+ console.error(chalk_1.default.red(`Cleanup failed for "${outputDir}/versions.json"`), chalk_1.default.yellow(err));
411
+ }
412
+ else {
413
+ console.log(chalk_1.default.green(`Cleanup succeeded for "${outputDir}/versions.json"`));
414
+ }
415
+ });
416
+ }
417
+ }
418
+ async function generateAllVersions(options, pluginId) {
419
+ const parentOptions = Object.assign({}, options);
420
+ const { versions } = parentOptions;
421
+ if (versions != null && Object.keys(versions).length > 0) {
422
+ const version = parentOptions.version;
423
+ const label = parentOptions.label;
424
+ const baseUrl = parentOptions.baseUrl;
425
+ let parentVersion = {};
426
+ parentVersion[version] = { label: label, baseUrl: baseUrl };
427
+ const mergedVersions = Object.assign(parentVersion, versions);
428
+ // Prepare for merge
429
+ delete parentOptions.versions;
430
+ delete parentOptions.version;
431
+ delete parentOptions.label;
432
+ delete parentOptions.baseUrl;
433
+ delete parentOptions.downloadUrl;
434
+ await generateVersions(mergedVersions, parentOptions.outputDir);
435
+ Object.keys(versions).forEach(async (key) => {
436
+ if (key === "all") {
437
+ console.error(chalk_1.default.red("Can't use id 'all' for OpenAPI docs versions configuration key."));
438
+ }
439
+ const versionOptions = versions[key];
440
+ const mergedOptions = {
441
+ ...parentOptions,
442
+ ...versionOptions,
443
+ };
444
+ await generateApiDocs(mergedOptions, pluginId);
445
+ });
446
+ }
447
+ }
448
+ async function cleanAllVersions(options) {
449
+ const parentOptions = Object.assign({}, options);
450
+ const { versions } = parentOptions;
451
+ delete parentOptions.versions;
452
+ if (versions != null && Object.keys(versions).length > 0) {
453
+ await cleanVersions(parentOptions.outputDir);
454
+ Object.keys(versions).forEach(async (key) => {
455
+ const versionOptions = versions[key];
456
+ const mergedOptions = {
457
+ ...parentOptions,
458
+ ...versionOptions,
459
+ };
460
+ await cleanApiDocs(mergedOptions);
461
+ });
462
+ }
463
+ }
464
+ return {
465
+ name: `@camunda8/docusaurus-plugin-openapi-docs`,
466
+ extendCli(cli) {
467
+ cli
468
+ .command(`gen-api-docs`)
469
+ .description(`Generates OpenAPI docs in MDX file format and sidebar.ts (if enabled).`)
470
+ .usage("<id>")
471
+ .arguments("<id>")
472
+ .option("-p, --plugin-id <plugin>", "OpenAPI docs plugin ID.")
473
+ .option("--all-versions", "Generate all versions.")
474
+ .action(async (id, instance) => {
475
+ var _a;
476
+ const options = instance.opts();
477
+ const pluginId = options.pluginId;
478
+ const allVersions = options.allVersions;
479
+ const pluginInstances = getPluginInstances(plugins);
480
+ let targetConfig;
481
+ let targetDocsPluginId;
482
+ if (pluginId) {
483
+ try {
484
+ const pluginConfig = getPluginConfig(plugins, pluginId);
485
+ targetConfig = (_a = pluginConfig.config) !== null && _a !== void 0 ? _a : {};
486
+ targetDocsPluginId = pluginConfig.docsPluginId;
487
+ }
488
+ catch {
489
+ console.error(chalk_1.default.red(`OpenAPI docs plugin ID '${pluginId}' not found.`));
490
+ return;
491
+ }
492
+ }
493
+ else {
494
+ if (pluginInstances.length > 1) {
495
+ console.error(chalk_1.default.red("OpenAPI docs plugin ID must be specified when more than one plugin instance exists."));
496
+ return;
497
+ }
498
+ targetConfig = config;
499
+ }
500
+ if (id === "all") {
501
+ if (targetConfig[id]) {
502
+ console.error(chalk_1.default.red("Can't use id 'all' for OpenAPI docs configuration key."));
503
+ }
504
+ else {
505
+ Object.keys(targetConfig).forEach(async function (key) {
506
+ await generateApiDocs(targetConfig[key], targetDocsPluginId);
507
+ if (allVersions) {
508
+ await generateAllVersions(targetConfig[key], targetDocsPluginId);
509
+ }
510
+ });
511
+ }
512
+ }
513
+ else if (!targetConfig[id]) {
514
+ console.error(chalk_1.default.red(`ID '${id}' does not exist in OpenAPI docs config.`));
515
+ }
516
+ else {
517
+ await generateApiDocs(targetConfig[id], targetDocsPluginId);
518
+ if (allVersions) {
519
+ await generateAllVersions(targetConfig[id], targetDocsPluginId);
520
+ }
521
+ }
522
+ });
523
+ cli
524
+ .command(`gen-api-docs:version`)
525
+ .description(`Generates versioned OpenAPI docs in MDX file format, versions.js and sidebar.ts (if enabled).`)
526
+ .usage("<id:version>")
527
+ .arguments("<id:version>")
528
+ .option("-p, --plugin-id <plugin>", "OpenAPI docs plugin ID.")
529
+ .action(async (id, instance) => {
530
+ var _a;
531
+ const options = instance.opts();
532
+ const pluginId = options.pluginId;
533
+ const pluginInstances = getPluginInstances(plugins);
534
+ let targetConfig;
535
+ let targetDocsPluginId;
536
+ if (pluginId) {
537
+ try {
538
+ const pluginConfig = getPluginConfig(plugins, pluginId);
539
+ targetConfig = (_a = pluginConfig.config) !== null && _a !== void 0 ? _a : {};
540
+ targetDocsPluginId = pluginConfig.docsPluginId;
541
+ }
542
+ catch {
543
+ console.error(chalk_1.default.red(`OpenAPI docs plugin ID '${pluginId}' not found.`));
544
+ return;
545
+ }
546
+ }
547
+ else {
548
+ if (pluginInstances.length > 1) {
549
+ console.error(chalk_1.default.red("OpenAPI docs plugin ID must be specified when more than one plugin instance exists."));
550
+ return;
551
+ }
552
+ targetConfig = config;
553
+ }
554
+ const [parentId, versionId] = id.split(":");
555
+ const parentConfig = Object.assign({}, targetConfig[parentId]);
556
+ const version = parentConfig.version;
557
+ const label = parentConfig.label;
558
+ const baseUrl = parentConfig.baseUrl;
559
+ let parentVersion = {};
560
+ parentVersion[version] = { label: label, baseUrl: baseUrl };
561
+ const { versions } = targetConfig[parentId];
562
+ const mergedVersions = Object.assign(parentVersion, versions);
563
+ // Prepare for merge
564
+ delete parentConfig.versions;
565
+ delete parentConfig.version;
566
+ delete parentConfig.label;
567
+ delete parentConfig.baseUrl;
568
+ delete parentConfig.downloadUrl;
569
+ // TODO: handle when no versions are defined by version command is passed
570
+ if (versionId === "all") {
571
+ if (versions[id]) {
572
+ console.error(chalk_1.default.red("Can't use id 'all' for OpenAPI docs versions configuration key."));
573
+ }
574
+ else {
575
+ await generateVersions(mergedVersions, parentConfig.outputDir);
576
+ Object.keys(versions).forEach(async (key) => {
577
+ const versionConfig = versions[key];
578
+ const mergedConfig = {
579
+ ...parentConfig,
580
+ ...versionConfig,
581
+ };
582
+ await generateApiDocs(mergedConfig, targetDocsPluginId);
583
+ });
584
+ }
585
+ }
586
+ else if (!versions[versionId]) {
587
+ console.error(chalk_1.default.red(`Version ID '${versionId}' does not exist in OpenAPI docs versions config.`));
588
+ }
589
+ else {
590
+ const versionConfig = versions[versionId];
591
+ const mergedConfig = {
592
+ ...parentConfig,
593
+ ...versionConfig,
594
+ };
595
+ await generateVersions(mergedVersions, parentConfig.outputDir);
596
+ await generateApiDocs(mergedConfig, targetDocsPluginId);
597
+ }
598
+ });
599
+ cli
600
+ .command(`clean-api-docs`)
601
+ .description(`Clears the generated OpenAPI docs MDX files and sidebar.ts (if enabled).`)
602
+ .usage("<id>")
603
+ .arguments("<id>")
604
+ .option("-p, --plugin-id <plugin>", "OpenAPI docs plugin ID.")
605
+ .option("--all-versions", "Clean all versions.")
606
+ .action(async (id, instance) => {
607
+ var _a;
608
+ const options = instance.opts();
609
+ const pluginId = options.pluginId;
610
+ const allVersions = options.allVersions;
611
+ const pluginInstances = getPluginInstances(plugins);
612
+ let targetConfig;
613
+ if (pluginId) {
614
+ try {
615
+ const pluginConfig = getPluginConfig(plugins, pluginId);
616
+ targetConfig = (_a = pluginConfig.config) !== null && _a !== void 0 ? _a : {};
617
+ }
618
+ catch {
619
+ console.error(chalk_1.default.red(`OpenAPI docs plugin ID '${pluginId}' not found.`));
620
+ return;
621
+ }
622
+ }
623
+ else {
624
+ if (pluginInstances.length > 1) {
625
+ console.error(chalk_1.default.red("OpenAPI docs plugin ID must be specified when more than one plugin instance exists."));
626
+ return;
627
+ }
628
+ targetConfig = config;
629
+ }
630
+ if (id === "all") {
631
+ if (targetConfig[id]) {
632
+ console.error(chalk_1.default.red("Can't use id 'all' for OpenAPI docs configuration key."));
633
+ }
634
+ else {
635
+ Object.keys(targetConfig).forEach(async function (key) {
636
+ await cleanApiDocs(targetConfig[key]);
637
+ if (allVersions) {
638
+ await cleanAllVersions(targetConfig[key]);
639
+ }
640
+ });
641
+ }
642
+ }
643
+ else {
644
+ await cleanApiDocs(targetConfig[id]);
645
+ if (allVersions) {
646
+ await cleanAllVersions(targetConfig[id]);
647
+ }
648
+ }
649
+ });
650
+ cli
651
+ .command(`clean-api-docs:version`)
652
+ .description(`Clears the versioned, generated OpenAPI docs MDX files, versions.json and sidebar.ts (if enabled).`)
653
+ .usage("<id:version>")
654
+ .arguments("<id:version>")
655
+ .option("-p, --plugin-id <plugin>", "OpenAPI docs plugin ID.")
656
+ .action(async (id, instance) => {
657
+ var _a;
658
+ const options = instance.opts();
659
+ const pluginId = options.pluginId;
660
+ const pluginInstances = getPluginInstances(plugins);
661
+ let targetConfig;
662
+ if (pluginId) {
663
+ try {
664
+ const pluginConfig = getPluginConfig(plugins, pluginId);
665
+ targetConfig = (_a = pluginConfig.config) !== null && _a !== void 0 ? _a : {};
666
+ }
667
+ catch {
668
+ console.error(chalk_1.default.red(`OpenAPI docs plugin ID '${pluginId}' not found.`));
669
+ return;
670
+ }
671
+ }
672
+ else {
673
+ if (pluginInstances.length > 1) {
674
+ console.error(chalk_1.default.red("OpenAPI docs plugin ID must be specified when more than one plugin instance exists."));
675
+ return;
676
+ }
677
+ targetConfig = config;
678
+ }
679
+ const [parentId, versionId] = id.split(":");
680
+ const { versions } = targetConfig[parentId];
681
+ const parentConfig = Object.assign({}, targetConfig[parentId]);
682
+ delete parentConfig.versions;
683
+ if (versionId === "all") {
684
+ if (versions[id]) {
685
+ chalk_1.default.red("Can't use id 'all' for OpenAPI docs versions configuration key.");
686
+ }
687
+ else {
688
+ await cleanVersions(parentConfig.outputDir);
689
+ Object.keys(versions).forEach(async (key) => {
690
+ const versionConfig = versions[key];
691
+ const mergedConfig = {
692
+ ...parentConfig,
693
+ ...versionConfig,
694
+ };
695
+ await cleanApiDocs(mergedConfig);
696
+ });
697
+ }
698
+ }
699
+ else {
700
+ const versionConfig = versions[versionId];
701
+ const mergedConfig = {
702
+ ...parentConfig,
703
+ ...versionConfig,
704
+ };
705
+ await cleanApiDocs(mergedConfig);
706
+ }
707
+ });
708
+ },
709
+ };
710
+ }
711
+ pluginOpenAPIDocs.validateOptions = ({ options, validate }) => {
712
+ const validatedOptions = validate(options_1.OptionsSchema, options);
713
+ return validatedOptions;
714
+ };