@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,39 @@
1
+ /**
2
+ * Configuration for a single SDK example source.
3
+ */
4
+ export interface SdkExampleSource {
5
+ /** Language label shown in the code sample tab (e.g. "TypeScript", "Python", "C#"). */
6
+ lang: string;
7
+ /** Syntax highlight mode (e.g. "typescript", "python", "csharp"). */
8
+ highlight?: string;
9
+ /** Path to the operation-map.json file (relative to site root or absolute). */
10
+ operationMapPath: string;
11
+ }
12
+ /**
13
+ * A single entry in operation-map.json for one operationId.
14
+ */
15
+ interface OperationMapEntry {
16
+ file: string;
17
+ region: string;
18
+ label?: string;
19
+ }
20
+ /**
21
+ * Shape of operation-map.json: operationId -> entries[].
22
+ */
23
+ type OperationMap = Record<string, OperationMapEntry[]>;
24
+ /**
25
+ * Build x-codeSamples entries for a given operationId from all configured SDK sources.
26
+ *
27
+ * @param operationId - The OpenAPI operationId to look up.
28
+ * @param sdkExamples - Array of SDK example source configurations.
29
+ * @param siteDir - The Docusaurus site root directory (for resolving relative paths).
30
+ * @param fileCache - Shared cache to avoid re-reading the same source files.
31
+ * @param mapCache - Shared cache for parsed operation-map.json files.
32
+ * @returns Array of x-codeSamples entries, or empty array if none found.
33
+ */
34
+ export declare function buildCodeSamples(operationId: string, sdkExamples: SdkExampleSource[], siteDir: string, fileCache: Map<string, string>, mapCache: Map<string, OperationMap>): {
35
+ lang: string;
36
+ label: string;
37
+ source: string;
38
+ }[];
39
+ export {};
@@ -0,0 +1,141 @@
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.buildCodeSamples = buildCodeSamples;
13
+ const path_1 = __importDefault(require("path"));
14
+ const fs_extra_1 = __importDefault(require("fs-extra"));
15
+ // Region marker patterns for supported languages.
16
+ // Each pattern matches the start/end of a named region.
17
+ const REGION_PATTERNS = [
18
+ // TypeScript/JavaScript: //#region Name ... //#endregion [Name]
19
+ {
20
+ start: /^\s*\/\/#region\s+(.+?)\s*$/,
21
+ end: /^\s*\/\/#endregion(?:\s+(.+?))?\s*$/,
22
+ },
23
+ // C#: #region Name ... #endregion [Name]
24
+ {
25
+ start: /^\s*#region\s+(.+?)\s*$/,
26
+ end: /^\s*#endregion(?:\s+(.+?))?\s*$/,
27
+ },
28
+ // XML-style: // <RegionName> ... // </RegionName>
29
+ {
30
+ start: /^\s*\/\/\s*<([A-Za-z]\w*)>\s*$/,
31
+ end: /^\s*\/\/\s*<\/([A-Za-z]\w*)>\s*$/,
32
+ },
33
+ // Python: # region Name ... # endregion [Name]
34
+ {
35
+ start: /^\s*#\s*region\s+(.+?)\s*$/,
36
+ end: /^\s*#\s*endregion(?:\s+(.+?))?\s*$/,
37
+ },
38
+ ];
39
+ /**
40
+ * Extract the code between region markers from a source file's content.
41
+ * Returns the trimmed code block or undefined if the region is not found.
42
+ */
43
+ function extractRegion(content, regionName) {
44
+ const lines = content.split(/\r?\n/);
45
+ let capturing = false;
46
+ let capturedLines = [];
47
+ for (const line of lines) {
48
+ if (capturing) {
49
+ // Check if this line is an end marker for our region
50
+ for (const pattern of REGION_PATTERNS) {
51
+ const endMatch = line.match(pattern.end);
52
+ if (endMatch &&
53
+ (!endMatch[1] || endMatch[1].trim() === regionName)) {
54
+ // Found end marker — return captured content
55
+ return trimIndentation(capturedLines);
56
+ }
57
+ }
58
+ capturedLines.push(line);
59
+ }
60
+ else {
61
+ // Check if this line is a start marker for our region
62
+ for (const pattern of REGION_PATTERNS) {
63
+ const startMatch = line.match(pattern.start);
64
+ if (startMatch && startMatch[1].trim() === regionName) {
65
+ capturing = true;
66
+ capturedLines = [];
67
+ break;
68
+ }
69
+ }
70
+ }
71
+ }
72
+ return undefined;
73
+ }
74
+ /**
75
+ * Remove the common leading whitespace from all non-empty lines.
76
+ */
77
+ function trimIndentation(lines) {
78
+ const nonEmptyLines = lines.filter((l) => l.trim().length > 0);
79
+ if (nonEmptyLines.length === 0)
80
+ return "";
81
+ const minIndent = Math.min(...nonEmptyLines.map((l) => {
82
+ const match = l.match(/^(\s*)/);
83
+ return match ? match[1].length : 0;
84
+ }));
85
+ return lines
86
+ .map((l) => (l.trim().length === 0 ? "" : l.slice(minIndent)))
87
+ .join("\n")
88
+ .trim();
89
+ }
90
+ /**
91
+ * Build x-codeSamples entries for a given operationId from all configured SDK sources.
92
+ *
93
+ * @param operationId - The OpenAPI operationId to look up.
94
+ * @param sdkExamples - Array of SDK example source configurations.
95
+ * @param siteDir - The Docusaurus site root directory (for resolving relative paths).
96
+ * @param fileCache - Shared cache to avoid re-reading the same source files.
97
+ * @param mapCache - Shared cache for parsed operation-map.json files.
98
+ * @returns Array of x-codeSamples entries, or empty array if none found.
99
+ */
100
+ function buildCodeSamples(operationId, sdkExamples, siteDir, fileCache, mapCache) {
101
+ const samples = [];
102
+ for (const sdk of sdkExamples) {
103
+ const mapPath = path_1.default.resolve(siteDir, sdk.operationMapPath);
104
+ let operationMap = mapCache.get(mapPath);
105
+ if (!operationMap) {
106
+ if (!fs_extra_1.default.existsSync(mapPath)) {
107
+ console.warn(`SDK examples: operation-map not found at ${mapPath}`);
108
+ continue;
109
+ }
110
+ operationMap = fs_extra_1.default.readJSONSync(mapPath);
111
+ mapCache.set(mapPath, operationMap);
112
+ }
113
+ const entries = operationMap[operationId];
114
+ if (!entries || entries.length === 0)
115
+ continue;
116
+ const mapDir = path_1.default.dirname(mapPath);
117
+ for (const entry of entries) {
118
+ const filePath = path_1.default.resolve(mapDir, entry.file);
119
+ let fileContent = fileCache.get(filePath);
120
+ if (fileContent === undefined) {
121
+ if (!fs_extra_1.default.existsSync(filePath)) {
122
+ console.warn(`SDK examples: source file not found at ${filePath} (referenced by ${sdk.lang} operation-map for ${operationId})`);
123
+ continue;
124
+ }
125
+ fileContent = fs_extra_1.default.readFileSync(filePath, "utf-8");
126
+ fileCache.set(filePath, fileContent);
127
+ }
128
+ const code = extractRegion(fileContent, entry.region);
129
+ if (!code) {
130
+ console.warn(`SDK examples: region "${entry.region}" not found in ${filePath} (${sdk.lang}, ${operationId})`);
131
+ continue;
132
+ }
133
+ samples.push({
134
+ lang: sdk.lang,
135
+ label: "SDK",
136
+ source: code,
137
+ });
138
+ }
139
+ }
140
+ return samples;
141
+ }
@@ -0,0 +1,352 @@
1
+ import type { JSONSchema4, JSONSchema6, JSONSchema7, JSONSchema7TypeName } from "json-schema";
2
+ export interface OpenApiObject {
3
+ openapi: string;
4
+ info: InfoObject;
5
+ servers?: ServerObject[];
6
+ paths: PathsObject;
7
+ components?: ComponentsObject;
8
+ security?: SecurityRequirementObject[];
9
+ tags?: TagObject[];
10
+ externalDocs?: ExternalDocumentationObject;
11
+ swagger?: string;
12
+ webhooks?: PathsObject;
13
+ "x-webhooks"?: PathsObject;
14
+ "x-tagGroups"?: TagGroupObject[];
15
+ }
16
+ export interface OpenApiObjectWithRef {
17
+ openapi: string;
18
+ info: InfoObject;
19
+ servers?: ServerObject[];
20
+ paths: PathsObjectWithRef;
21
+ components?: ComponentsObjectWithRef;
22
+ security?: SecurityRequirementObject[];
23
+ tags?: TagObject[];
24
+ externalDocs?: ExternalDocumentationObject;
25
+ }
26
+ export interface InfoObject {
27
+ title: string;
28
+ description?: string;
29
+ termsOfService?: string;
30
+ contact?: ContactObject;
31
+ license?: LicenseObject;
32
+ version: string;
33
+ tags?: TagObject[];
34
+ "x-logo"?: LogoObject;
35
+ "x-dark-logo"?: LogoObject;
36
+ logo?: LogoObject;
37
+ darkLogo?: LogoObject;
38
+ }
39
+ export interface LogoObject {
40
+ url?: string;
41
+ }
42
+ export interface ContactObject {
43
+ name?: string;
44
+ url?: string;
45
+ email?: string;
46
+ }
47
+ export interface LicenseObject {
48
+ name: string;
49
+ url?: string;
50
+ }
51
+ export interface ServerObject {
52
+ url: string;
53
+ description?: string;
54
+ variables?: Record<string, ServerVariable>;
55
+ }
56
+ export interface ServerVariable {
57
+ enum?: string[];
58
+ default: string;
59
+ description?: string;
60
+ }
61
+ export interface ComponentsObject {
62
+ schemas?: Record<string, SchemaObject>;
63
+ responses?: Record<string, ResponseObject>;
64
+ parameters?: Record<string, ParameterObject>;
65
+ examples?: Record<string, ExampleObject>;
66
+ requestBodies?: Record<string, RequestBodyObject>;
67
+ headers?: Record<string, HeaderObject>;
68
+ securitySchemes?: Record<string, SecuritySchemeObject>;
69
+ links?: Record<string, LinkObject>;
70
+ callbacks?: Record<string, CallbackObject>;
71
+ }
72
+ export interface ComponentsObjectWithRef {
73
+ schemas?: Record<string, SchemaObjectWithRef | ReferenceObject>;
74
+ responses?: Record<string, ResponseObjectWithRef | ReferenceObject>;
75
+ parameters?: Record<string, ParameterObjectWithRef | ReferenceObject>;
76
+ examples?: Record<string, ExampleObject | ReferenceObject>;
77
+ requestBodies?: Record<string, RequestBodyObjectWithRef | ReferenceObject>;
78
+ headers?: Record<string, HeaderObjectWithRef | ReferenceObject>;
79
+ securitySchemes?: Record<string, SecuritySchemeObject | ReferenceObject>;
80
+ links?: Record<string, LinkObject | ReferenceObject>;
81
+ callbacks?: Record<string, CallbackObjectWithRef | ReferenceObject>;
82
+ }
83
+ export type PathsObject = Record<string, PathItemObject>;
84
+ export type PathsObjectWithRef = Record<string, PathItemObjectWithRef>;
85
+ export interface PathItemObject {
86
+ $ref?: string;
87
+ summary?: string;
88
+ description?: string;
89
+ get?: OperationObject;
90
+ put?: OperationObject;
91
+ post?: OperationObject;
92
+ delete?: OperationObject;
93
+ options?: OperationObject;
94
+ head?: OperationObject;
95
+ patch?: OperationObject;
96
+ trace?: OperationObject;
97
+ servers?: ServerObject[];
98
+ parameters?: ParameterObject[];
99
+ }
100
+ export interface PathItemObjectWithRef {
101
+ $ref?: string;
102
+ summary?: string;
103
+ description?: string;
104
+ get?: OperationObjectWithRef;
105
+ put?: OperationObjectWithRef;
106
+ post?: OperationObjectWithRef;
107
+ delete?: OperationObjectWithRef;
108
+ options?: OperationObjectWithRef;
109
+ head?: OperationObjectWithRef;
110
+ patch?: OperationObjectWithRef;
111
+ trace?: OperationObjectWithRef;
112
+ servers?: ServerObject[];
113
+ parameters?: (ParameterObjectWithRef | ReferenceObject)[];
114
+ }
115
+ export interface OperationObject {
116
+ tags?: string[];
117
+ summary?: string;
118
+ description?: string;
119
+ externalDocs?: ExternalDocumentationObject;
120
+ operationId?: string;
121
+ parameters?: ParameterObject[];
122
+ requestBody?: RequestBodyObject;
123
+ responses: ResponsesObject;
124
+ callbacks?: Record<string, CallbackObject>;
125
+ deprecated?: boolean;
126
+ security?: SecurityRequirementObject[];
127
+ servers?: ServerObject[];
128
+ "x-position"?: number;
129
+ "x-deprecated-description"?: string;
130
+ }
131
+ export interface OperationObjectWithRef {
132
+ tags?: string[];
133
+ summary?: string;
134
+ description?: string;
135
+ externalDocs?: ExternalDocumentationObject;
136
+ operationId?: string;
137
+ parameters?: (ParameterObjectWithRef | ReferenceObject)[];
138
+ requestBody?: RequestBodyObjectWithRef | ReferenceObject;
139
+ responses: ResponsesObjectWithRef;
140
+ callbacks?: Record<string, CallbackObjectWithRef | ReferenceObject>;
141
+ deprecated?: boolean;
142
+ security?: SecurityRequirementObject[];
143
+ servers?: ServerObject[];
144
+ "x-deprecated-description"?: string;
145
+ }
146
+ export interface ExternalDocumentationObject {
147
+ description?: string;
148
+ url: string;
149
+ }
150
+ export interface ParameterObject {
151
+ name: string;
152
+ in: "query" | "header" | "path" | "cookie";
153
+ description?: string;
154
+ required?: boolean;
155
+ deprecated?: boolean;
156
+ allowEmptyValue?: boolean;
157
+ style?: string;
158
+ explode?: string;
159
+ allowReserved?: boolean;
160
+ schema?: SchemaObject;
161
+ example?: any;
162
+ examples?: Record<string, ExampleObject>;
163
+ content?: Record<string, MediaTypeObject>;
164
+ param?: Object;
165
+ "x-enumDescriptions"?: Record<string, string>;
166
+ }
167
+ export interface ParameterObjectWithRef {
168
+ name: string;
169
+ in: string;
170
+ description?: string;
171
+ required?: boolean;
172
+ deprecated?: boolean;
173
+ allowEmptyValue?: boolean;
174
+ style?: string;
175
+ explode?: string;
176
+ allowReserved?: boolean;
177
+ schema?: SchemaObjectWithRef | ReferenceObject;
178
+ example?: any;
179
+ examples?: Record<string, ExampleObject | ReferenceObject>;
180
+ content?: Record<string, MediaTypeObjectWithRef>;
181
+ }
182
+ export interface RequestBodyObject {
183
+ description?: string;
184
+ content: Record<string, MediaTypeObject>;
185
+ required?: boolean;
186
+ }
187
+ export interface RequestBodyObjectWithRef {
188
+ description?: string;
189
+ content: Record<string, MediaTypeObjectWithRef>;
190
+ required?: boolean;
191
+ }
192
+ export interface MediaTypeObject {
193
+ schema?: SchemaObject;
194
+ example?: any;
195
+ examples?: Record<string, ExampleObject>;
196
+ encoding?: Record<string, EncodingObject>;
197
+ type?: any;
198
+ }
199
+ export interface MediaTypeObjectWithRef {
200
+ schema?: SchemaObjectWithRef | ReferenceObject;
201
+ example?: any;
202
+ examples?: Record<string, ExampleObject | ReferenceObject>;
203
+ encoding?: Record<string, EncodingObjectWithRef>;
204
+ }
205
+ export interface EncodingObject {
206
+ contentType?: string;
207
+ headers?: Record<string, HeaderObject>;
208
+ style?: string;
209
+ explode?: boolean;
210
+ allowReserved?: boolean;
211
+ }
212
+ export interface EncodingObjectWithRef {
213
+ contentType?: string;
214
+ headers?: Record<string, HeaderObjectWithRef | ReferenceObject>;
215
+ style?: string;
216
+ explode?: boolean;
217
+ allowReserved?: boolean;
218
+ }
219
+ export type ResponsesObject = Record<string, ResponseObject>;
220
+ export type ResponsesObjectWithRef = Record<string, ResponseObjectWithRef | ReferenceObject>;
221
+ export interface ResponseObject {
222
+ description: string;
223
+ headers?: Record<string, HeaderObject>;
224
+ content?: Record<string, MediaTypeObject>;
225
+ links?: Record<string, LinkObject>;
226
+ }
227
+ export interface ResponseObjectWithRef {
228
+ description: string;
229
+ headers?: Record<string, HeaderObjectWithRef | ReferenceObject>;
230
+ content?: Record<string, MediaTypeObjectWithRef>;
231
+ links?: Record<string, LinkObject | ReferenceObject>;
232
+ }
233
+ export type CallbackObject = Record<string, PathItemObject>;
234
+ export type CallbackObjectWithRef = Record<string, PathItemObjectWithRef>;
235
+ export interface ExampleObject {
236
+ summary?: string;
237
+ description?: string;
238
+ value?: any;
239
+ externalValue?: string;
240
+ }
241
+ export interface LinkObject {
242
+ operationRef?: string;
243
+ operationId?: string;
244
+ parameters?: Record<string, any>;
245
+ requestBody?: any;
246
+ description?: string;
247
+ server?: ServerObject;
248
+ }
249
+ export type HeaderObject = Omit<ParameterObject, "name" | "in">;
250
+ export type HeaderObjectWithRef = Omit<ParameterObjectWithRef, "name" | "in">;
251
+ export interface TagObject {
252
+ name?: string;
253
+ description?: string;
254
+ externalDocs?: ExternalDocumentationObject;
255
+ "x-displayName"?: string;
256
+ }
257
+ export interface TagGroupObject {
258
+ name: string;
259
+ tags: string[];
260
+ }
261
+ export interface ReferenceObject {
262
+ $ref: string;
263
+ }
264
+ export type JSONSchema = JSONSchema4 | JSONSchema6 | JSONSchema7;
265
+ export type SchemaType = JSONSchema7TypeName;
266
+ export type SchemaObject = Omit<JSONSchema, "type" | "allOf" | "oneOf" | "anyOf" | "not" | "items" | "properties" | "additionalProperties"> & {
267
+ type?: SchemaType;
268
+ allOf?: SchemaObject[];
269
+ oneOf?: SchemaObject[];
270
+ anyOf?: SchemaObject[];
271
+ not?: SchemaObject;
272
+ items?: SchemaObject;
273
+ properties?: Record<string, SchemaObject>;
274
+ additionalProperties?: boolean | SchemaObject;
275
+ nullable?: boolean;
276
+ discriminator?: DiscriminatorObject;
277
+ readOnly?: boolean;
278
+ writeOnly?: boolean;
279
+ xml?: XMLObject;
280
+ externalDocs?: ExternalDocumentationObject;
281
+ example?: any;
282
+ deprecated?: boolean;
283
+ "x-tags"?: string[];
284
+ "x-enumDescriptions"?: Record<string, string>;
285
+ };
286
+ export type SchemaObjectWithRef = Omit<JSONSchema, "type" | "allOf" | "oneOf" | "anyOf" | "not" | "items" | "properties" | "additionalProperties"> & {
287
+ type?: SchemaType;
288
+ allOf?: (SchemaObject | ReferenceObject)[];
289
+ oneOf?: (SchemaObject | ReferenceObject)[];
290
+ anyOf?: (SchemaObject | ReferenceObject)[];
291
+ not?: SchemaObject | ReferenceObject;
292
+ items?: SchemaObject | ReferenceObject;
293
+ properties?: Record<string, SchemaObject | ReferenceObject>;
294
+ additionalProperties?: boolean | SchemaObject | ReferenceObject;
295
+ nullable?: boolean;
296
+ discriminator?: DiscriminatorObject;
297
+ readOnly?: boolean;
298
+ writeOnly?: boolean;
299
+ xml?: XMLObject;
300
+ externalDocs?: ExternalDocumentationObject;
301
+ example?: any;
302
+ deprecated?: boolean;
303
+ };
304
+ export interface DiscriminatorObject {
305
+ propertyName: string;
306
+ mapping?: Record<string, string>;
307
+ }
308
+ export interface XMLObject {
309
+ name?: string;
310
+ namespace?: string;
311
+ prefix?: string;
312
+ attribute?: boolean;
313
+ wrapped?: boolean;
314
+ }
315
+ export type SecuritySchemeObject = ApiKeySecuritySchemeObject | HttpSecuritySchemeObject | Oauth2SecuritySchemeObject | OpenIdConnectSecuritySchemeObject;
316
+ export interface ApiKeySecuritySchemeObject {
317
+ type: "apiKey";
318
+ description?: string;
319
+ name: string;
320
+ in: "query" | "header" | "cookie";
321
+ }
322
+ export interface HttpSecuritySchemeObject {
323
+ type: "http";
324
+ description?: string;
325
+ scheme: string;
326
+ bearerFormat?: string;
327
+ name?: string;
328
+ in?: string;
329
+ }
330
+ export interface Oauth2SecuritySchemeObject {
331
+ type: "oauth2";
332
+ description?: string;
333
+ flows: OAuthFlowsObject;
334
+ }
335
+ export interface OpenIdConnectSecuritySchemeObject {
336
+ type: "openIdConnect";
337
+ description?: string;
338
+ openIdConnectUrl: string;
339
+ }
340
+ export interface OAuthFlowsObject {
341
+ implicit?: OAuthFlowObject;
342
+ password?: OAuthFlowObject;
343
+ clientCredentials?: OAuthFlowObject;
344
+ authorizationCode?: OAuthFlowObject;
345
+ }
346
+ export interface OAuthFlowObject {
347
+ authorizationUrl?: string;
348
+ tokenUrl?: string;
349
+ refreshUrl?: string;
350
+ scopes: Record<string, string>;
351
+ }
352
+ export type SecurityRequirementObject = Record<string, string[]>;
@@ -0,0 +1,8 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ export declare function convertSwagger2OpenAPI(spec: object): Promise<unknown>;
2
+ export declare function loadAndResolveSpec(specUrlOrObject: object | string): Promise<any>;
@@ -0,0 +1,153 @@
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.convertSwagger2OpenAPI = convertSwagger2OpenAPI;
13
+ exports.loadAndResolveSpec = loadAndResolveSpec;
14
+ const json_schema_ref_parser_1 = __importDefault(require("@apidevtools/json-schema-ref-parser"));
15
+ const openapi_core_1 = require("@redocly/openapi-core");
16
+ const chalk_1 = __importDefault(require("chalk"));
17
+ // @ts-ignore
18
+ const swagger2openapi_1 = require("swagger2openapi");
19
+ const OpenAPIParser_1 = require("./services/OpenAPIParser");
20
+ function serializer(replacer, cycleReplacer) {
21
+ var stack = [], keys = [];
22
+ if (cycleReplacer === undefined)
23
+ cycleReplacer = function (key, value) {
24
+ if (stack[0] === value)
25
+ return "circular()";
26
+ return value.title ? `circular(${value.title})` : "circular()";
27
+ };
28
+ return function (key, value) {
29
+ // Resolve discriminator ref pointers
30
+ if ((value === null || value === void 0 ? void 0 : value.discriminator) !== undefined) {
31
+ const parser = new OpenAPIParser_1.OpenAPIParser(stack[0]);
32
+ if (value.discriminator.mapping &&
33
+ typeof value.discriminator.mapping === "object") {
34
+ for (let [k, v] of Object.entries(value.discriminator.mapping)) {
35
+ const discriminator = k;
36
+ if (typeof v === "string" && v.charAt(0) === "#") {
37
+ const ref = v;
38
+ const resolvedRef = parser.byRef(ref);
39
+ value.discriminator.mapping[discriminator] = resolvedRef;
40
+ }
41
+ }
42
+ }
43
+ }
44
+ if (stack.length > 0) {
45
+ // @ts-ignore
46
+ var thisPos = stack.indexOf(this);
47
+ // @ts-ignore
48
+ ~thisPos ? stack.splice(thisPos + 1) : stack.push(this);
49
+ ~thisPos ? keys.splice(thisPos, Infinity, key) : keys.push(key);
50
+ // @ts-ignore
51
+ if (~stack.indexOf(value))
52
+ value = cycleReplacer.call(this, key, value);
53
+ }
54
+ else
55
+ stack.push(value);
56
+ // @ts-ignore
57
+ return replacer === undefined ? value : replacer.call(this, key, value);
58
+ };
59
+ }
60
+ function convertSwagger2OpenAPI(spec) {
61
+ console.warn("[ReDoc Compatibility mode]: Converting OpenAPI 2.0 to OpenAPI 3.0");
62
+ return new Promise((resolve, reject) => (0, swagger2openapi_1.convertObj)(spec, {
63
+ patch: true,
64
+ warnOnly: true,
65
+ text: "{}",
66
+ anchors: true,
67
+ resolveInternal: true,
68
+ }, (err, res) => {
69
+ // TODO: log any warnings
70
+ if (err) {
71
+ return reject(err);
72
+ }
73
+ resolve(res && res.openapi);
74
+ }));
75
+ }
76
+ async function resolveJsonRefs(specUrlOrObject) {
77
+ try {
78
+ let schema = await json_schema_ref_parser_1.default.dereference(specUrlOrObject, {
79
+ continueOnError: true,
80
+ resolve: {
81
+ file: true,
82
+ external: true,
83
+ http: {
84
+ timeout: 15000, // 15 sec timeout
85
+ },
86
+ },
87
+ dereference: {
88
+ circular: true,
89
+ },
90
+ });
91
+ return schema;
92
+ }
93
+ catch (err) {
94
+ let errorMsg = "";
95
+ if (err.errors[0] !== undefined) {
96
+ const error = err.errors[0];
97
+ errorMsg = `Error: [${error.message}] with footprint [${error.footprint}]`;
98
+ }
99
+ else {
100
+ errorMsg = err;
101
+ }
102
+ console.error(chalk_1.default.yellow(errorMsg));
103
+ return;
104
+ }
105
+ }
106
+ async function loadAndResolveSpec(specUrlOrObject) {
107
+ const config = new openapi_core_1.Config({});
108
+ const bundleOpts = {
109
+ config,
110
+ base: process.cwd(),
111
+ };
112
+ if (typeof specUrlOrObject === "object" && specUrlOrObject !== null) {
113
+ bundleOpts["doc"] = {
114
+ source: { absoluteRef: "" },
115
+ parsed: specUrlOrObject,
116
+ };
117
+ }
118
+ else {
119
+ bundleOpts["ref"] = specUrlOrObject;
120
+ }
121
+ // Force dereference ?
122
+ // bundleOpts["dereference"] = true;
123
+ const { bundle: { parsed }, } = await (0, openapi_core_1.bundle)(bundleOpts);
124
+ //Pre-processing before resolving JSON refs
125
+ if (parsed.components) {
126
+ for (let [component, type] of Object.entries(parsed.components)) {
127
+ if (component === "schemas") {
128
+ for (let [schemaKey, schemaValue] of Object.entries(type)) {
129
+ const title = schemaValue["title"];
130
+ if (!title) {
131
+ schemaValue.title = schemaKey;
132
+ }
133
+ }
134
+ }
135
+ }
136
+ }
137
+ const resolved = await resolveJsonRefs(parsed);
138
+ // Force serialization and replace circular $ref pointers
139
+ // @ts-ignore
140
+ const serialized = JSON.stringify(resolved, serializer());
141
+ let decycled;
142
+ try {
143
+ decycled = JSON.parse(serialized);
144
+ }
145
+ catch (err) {
146
+ console.error(chalk_1.default.yellow(err));
147
+ }
148
+ return decycled !== undefined && typeof decycled === "object"
149
+ ? decycled.swagger !== undefined
150
+ ? convertSwagger2OpenAPI(decycled)
151
+ : decycled
152
+ : resolved;
153
+ }