docusaurus-plugin-openapi-docs 0.0.0-1000

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (163) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +361 -0
  3. package/lib/index.d.ts +9 -0
  4. package/lib/index.js +709 -0
  5. package/lib/markdown/createArrayBracket.d.ts +2 -0
  6. package/lib/markdown/createArrayBracket.js +36 -0
  7. package/lib/markdown/createAuthentication.d.ts +2 -0
  8. package/lib/markdown/createAuthentication.js +171 -0
  9. package/lib/markdown/createAuthorization.d.ts +1 -0
  10. package/lib/markdown/createAuthorization.js +15 -0
  11. package/lib/markdown/createCallbackMethodEndpoint.d.ts +1 -0
  12. package/lib/markdown/createCallbackMethodEndpoint.js +20 -0
  13. package/lib/markdown/createCallbacks.d.ts +6 -0
  14. package/lib/markdown/createCallbacks.js +77 -0
  15. package/lib/markdown/createContactInfo.d.ts +2 -0
  16. package/lib/markdown/createContactInfo.js +39 -0
  17. package/lib/markdown/createDeprecationNotice.d.ts +6 -0
  18. package/lib/markdown/createDeprecationNotice.js +20 -0
  19. package/lib/markdown/createDescription.d.ts +1 -0
  20. package/lib/markdown/createDescription.js +13 -0
  21. package/lib/markdown/createDetails.d.ts +2 -0
  22. package/lib/markdown/createDetails.js +17 -0
  23. package/lib/markdown/createDetailsSummary.d.ts +2 -0
  24. package/lib/markdown/createDetailsSummary.js +17 -0
  25. package/lib/markdown/createDownload.d.ts +1 -0
  26. package/lib/markdown/createDownload.js +16 -0
  27. package/lib/markdown/createHeading.d.ts +1 -0
  28. package/lib/markdown/createHeading.js +20 -0
  29. package/lib/markdown/createLicense.d.ts +2 -0
  30. package/lib/markdown/createLicense.js +32 -0
  31. package/lib/markdown/createLogo.d.ts +2 -0
  32. package/lib/markdown/createLogo.js +18 -0
  33. package/lib/markdown/createMethodEndpoint.d.ts +1 -0
  34. package/lib/markdown/createMethodEndpoint.js +20 -0
  35. package/lib/markdown/createParamsDetails.d.ts +6 -0
  36. package/lib/markdown/createParamsDetails.js +18 -0
  37. package/lib/markdown/createRequestBodyDetails.d.ts +13 -0
  38. package/lib/markdown/createRequestBodyDetails.js +13 -0
  39. package/lib/markdown/createRequestHeader.d.ts +1 -0
  40. package/lib/markdown/createRequestHeader.js +21 -0
  41. package/lib/markdown/createRequestSchema.d.ts +14 -0
  42. package/lib/markdown/createRequestSchema.js +20 -0
  43. package/lib/markdown/createResponseSchema.d.ts +14 -0
  44. package/lib/markdown/createResponseSchema.js +20 -0
  45. package/lib/markdown/createSchema.d.ts +9 -0
  46. package/lib/markdown/createSchema.js +668 -0
  47. package/lib/markdown/createSchema.test.d.ts +1 -0
  48. package/lib/markdown/createSchema.test.js +913 -0
  49. package/lib/markdown/createStatusCodes.d.ts +9 -0
  50. package/lib/markdown/createStatusCodes.js +63 -0
  51. package/lib/markdown/createTermsOfService.d.ts +1 -0
  52. package/lib/markdown/createTermsOfService.js +31 -0
  53. package/lib/markdown/createVendorExtensions.d.ts +1 -0
  54. package/lib/markdown/createVendorExtensions.js +24 -0
  55. package/lib/markdown/createVersionBadge.d.ts +1 -0
  56. package/lib/markdown/createVersionBadge.js +19 -0
  57. package/lib/markdown/index.d.ts +5 -0
  58. package/lib/markdown/index.js +92 -0
  59. package/lib/markdown/schema.d.ts +3 -0
  60. package/lib/markdown/schema.js +154 -0
  61. package/lib/markdown/schema.test.d.ts +1 -0
  62. package/lib/markdown/schema.test.js +181 -0
  63. package/lib/markdown/utils.d.ts +20 -0
  64. package/lib/markdown/utils.js +68 -0
  65. package/lib/openapi/createRequestExample.d.ts +2 -0
  66. package/lib/openapi/createRequestExample.js +191 -0
  67. package/lib/openapi/createResponseExample.d.ts +2 -0
  68. package/lib/openapi/createResponseExample.js +192 -0
  69. package/lib/openapi/index.d.ts +1 -0
  70. package/lib/openapi/index.js +12 -0
  71. package/lib/openapi/openapi.d.ts +12 -0
  72. package/lib/openapi/openapi.js +544 -0
  73. package/lib/openapi/openapi.test.d.ts +1 -0
  74. package/lib/openapi/openapi.test.js +33 -0
  75. package/lib/openapi/types.d.ts +354 -0
  76. package/lib/openapi/types.js +8 -0
  77. package/lib/openapi/utils/loadAndResolveSpec.d.ts +2 -0
  78. package/lib/openapi/utils/loadAndResolveSpec.js +153 -0
  79. package/lib/openapi/utils/services/OpenAPIParser.d.ts +52 -0
  80. package/lib/openapi/utils/services/OpenAPIParser.js +343 -0
  81. package/lib/openapi/utils/services/RedocNormalizedOptions.d.ts +100 -0
  82. package/lib/openapi/utils/services/RedocNormalizedOptions.js +170 -0
  83. package/lib/openapi/utils/types/index.d.ts +2 -0
  84. package/lib/openapi/utils/types/index.js +23 -0
  85. package/lib/openapi/utils/types/open-api.d.ts +305 -0
  86. package/lib/openapi/utils/types/open-api.js +8 -0
  87. package/lib/openapi/utils/types.d.ts +306 -0
  88. package/lib/openapi/utils/types.js +8 -0
  89. package/lib/openapi/utils/utils/JsonPointer.d.ts +51 -0
  90. package/lib/openapi/utils/utils/JsonPointer.js +95 -0
  91. package/lib/openapi/utils/utils/helpers.d.ts +43 -0
  92. package/lib/openapi/utils/utils/helpers.js +230 -0
  93. package/lib/openapi/utils/utils/index.d.ts +3 -0
  94. package/lib/openapi/utils/utils/index.js +25 -0
  95. package/lib/openapi/utils/utils/openapi.d.ts +40 -0
  96. package/lib/openapi/utils/utils/openapi.js +605 -0
  97. package/lib/options.d.ts +2 -0
  98. package/lib/options.js +69 -0
  99. package/lib/sidebars/index.d.ts +4 -0
  100. package/lib/sidebars/index.js +226 -0
  101. package/lib/sidebars/utils.d.ts +2 -0
  102. package/lib/sidebars/utils.js +30 -0
  103. package/lib/types.d.ts +135 -0
  104. package/lib/types.js +8 -0
  105. package/package.json +69 -0
  106. package/src/index.ts +945 -0
  107. package/src/markdown/__snapshots__/createSchema.test.ts.snap +1605 -0
  108. package/src/markdown/createArrayBracket.ts +35 -0
  109. package/src/markdown/createAuthentication.ts +201 -0
  110. package/src/markdown/createAuthorization.ts +13 -0
  111. package/src/markdown/createCallbackMethodEndpoint.ts +19 -0
  112. package/src/markdown/createCallbacks.ts +101 -0
  113. package/src/markdown/createContactInfo.ts +41 -0
  114. package/src/markdown/createDeprecationNotice.ts +31 -0
  115. package/src/markdown/createDescription.ts +12 -0
  116. package/src/markdown/createDetails.ts +16 -0
  117. package/src/markdown/createDetailsSummary.ts +16 -0
  118. package/src/markdown/createDownload.ts +15 -0
  119. package/src/markdown/createHeading.ts +23 -0
  120. package/src/markdown/createLicense.ts +34 -0
  121. package/src/markdown/createLogo.ts +21 -0
  122. package/src/markdown/createMethodEndpoint.ts +19 -0
  123. package/src/markdown/createParamsDetails.ts +22 -0
  124. package/src/markdown/createRequestBodyDetails.ts +24 -0
  125. package/src/markdown/createRequestHeader.ts +24 -0
  126. package/src/markdown/createRequestSchema.ts +32 -0
  127. package/src/markdown/createResponseSchema.ts +32 -0
  128. package/src/markdown/createSchema.test.ts +1075 -0
  129. package/src/markdown/createSchema.ts +862 -0
  130. package/src/markdown/createStatusCodes.ts +63 -0
  131. package/src/markdown/createTermsOfService.ts +30 -0
  132. package/src/markdown/createVendorExtensions.ts +22 -0
  133. package/src/markdown/createVersionBadge.ts +22 -0
  134. package/src/markdown/index.ts +144 -0
  135. package/src/markdown/schema.test.ts +208 -0
  136. package/src/markdown/schema.ts +185 -0
  137. package/src/markdown/utils.ts +89 -0
  138. package/src/openapi/__fixtures__/examples/openapi.yaml +49 -0
  139. package/src/openapi/createRequestExample.ts +235 -0
  140. package/src/openapi/createResponseExample.ts +238 -0
  141. package/src/openapi/index.ts +8 -0
  142. package/src/openapi/openapi.test.ts +40 -0
  143. package/src/openapi/openapi.ts +697 -0
  144. package/src/openapi/types.ts +455 -0
  145. package/src/openapi/utils/loadAndResolveSpec.ts +171 -0
  146. package/src/openapi/utils/services/OpenAPIParser.ts +434 -0
  147. package/src/openapi/utils/services/RedocNormalizedOptions.ts +330 -0
  148. package/src/openapi/utils/types/index.ts +10 -0
  149. package/src/openapi/utils/types/open-api.ts +303 -0
  150. package/src/openapi/utils/types.ts +303 -0
  151. package/src/openapi/utils/utils/JsonPointer.ts +99 -0
  152. package/src/openapi/utils/utils/helpers.ts +239 -0
  153. package/src/openapi/utils/utils/index.ts +11 -0
  154. package/src/openapi/utils/utils/openapi.ts +771 -0
  155. package/src/openapi-to-postmanv2.d.ts +10 -0
  156. package/src/options.ts +77 -0
  157. package/src/plugin-content-docs-types.d.ts +42 -0
  158. package/src/plugin-openapi.d.ts +87 -0
  159. package/src/postman-collection.d.ts +10 -0
  160. package/src/sidebars/index.ts +322 -0
  161. package/src/sidebars/utils.ts +29 -0
  162. package/src/types.ts +176 -0
  163. package/tsconfig.json +7 -0
@@ -0,0 +1,455 @@
1
+ /* ============================================================================
2
+ * Copyright (c) Palo Alto Networks
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ * ========================================================================== */
7
+
8
+ import type { JSONSchema4, JSONSchema6, JSONSchema7 } from "json-schema";
9
+
10
+ interface Map<T> {
11
+ [key: string]: T;
12
+ }
13
+
14
+ export interface OpenApiObject {
15
+ openapi: string;
16
+ info: InfoObject;
17
+ servers?: ServerObject[];
18
+ paths: PathsObject;
19
+ components?: ComponentsObject;
20
+ security?: SecurityRequirementObject[];
21
+ tags?: TagObject[];
22
+ externalDocs?: ExternalDocumentationObject;
23
+ swagger?: string;
24
+ webhooks?: PathsObject;
25
+ "x-webhooks"?: PathsObject;
26
+ "x-tagGroups"?: TagGroupObject[];
27
+ }
28
+
29
+ export interface OpenApiObjectWithRef {
30
+ openapi: string;
31
+ info: InfoObject;
32
+ servers?: ServerObject[];
33
+ paths: PathsObjectWithRef;
34
+ components?: ComponentsObjectWithRef;
35
+ security?: SecurityRequirementObject[];
36
+ tags?: TagObject[];
37
+ externalDocs?: ExternalDocumentationObject;
38
+ }
39
+
40
+ export interface InfoObject {
41
+ title: string;
42
+ description?: string;
43
+ termsOfService?: string;
44
+ contact?: ContactObject;
45
+ license?: LicenseObject;
46
+ version: string;
47
+ tags?: TagObject[];
48
+ "x-logo"?: LogoObject;
49
+ "x-dark-logo"?: LogoObject;
50
+ logo?: LogoObject;
51
+ darkLogo?: LogoObject;
52
+ }
53
+
54
+ export interface LogoObject {
55
+ url?: string;
56
+ }
57
+
58
+ export interface ContactObject {
59
+ name?: string;
60
+ url?: string;
61
+ email?: string;
62
+ }
63
+
64
+ export interface LicenseObject {
65
+ name: string;
66
+ url?: string;
67
+ }
68
+
69
+ export interface ServerObject {
70
+ url: string;
71
+ description?: string;
72
+ variables?: Map<ServerVariable>;
73
+ }
74
+
75
+ export interface ServerVariable {
76
+ enum?: string[];
77
+ default: string;
78
+ description?: string;
79
+ }
80
+
81
+ export interface ComponentsObject {
82
+ schemas?: Map<SchemaObject>;
83
+ responses?: Map<ResponseObject>;
84
+ parameters?: Map<ParameterObject>;
85
+ examples?: Map<ExampleObject>;
86
+ requestBodies?: Map<RequestBodyObject>;
87
+ headers?: Map<HeaderObject>;
88
+ securitySchemes?: Map<SecuritySchemeObject>;
89
+ links?: Map<LinkObject>;
90
+ callbacks?: Map<CallbackObject>;
91
+ }
92
+
93
+ export interface ComponentsObjectWithRef {
94
+ schemas?: Map<SchemaObjectWithRef | ReferenceObject>;
95
+ responses?: Map<ResponseObjectWithRef | ReferenceObject>;
96
+ parameters?: Map<ParameterObjectWithRef | ReferenceObject>;
97
+ examples?: Map<ExampleObject | ReferenceObject>;
98
+ requestBodies?: Map<RequestBodyObjectWithRef | ReferenceObject>;
99
+ headers?: Map<HeaderObjectWithRef | ReferenceObject>;
100
+ securitySchemes?: Map<SecuritySchemeObject | ReferenceObject>;
101
+ links?: Map<LinkObject | ReferenceObject>;
102
+ callbacks?: Map<CallbackObjectWithRef | ReferenceObject>;
103
+ }
104
+
105
+ export type PathsObject = Map<PathItemObject>;
106
+
107
+ export type PathsObjectWithRef = Map<PathItemObjectWithRef>;
108
+
109
+ export interface PathItemObject {
110
+ $ref?: string;
111
+ summary?: string;
112
+ description?: string;
113
+ get?: OperationObject;
114
+ put?: OperationObject;
115
+ post?: OperationObject;
116
+ delete?: OperationObject;
117
+ options?: OperationObject;
118
+ head?: OperationObject;
119
+ patch?: OperationObject;
120
+ trace?: OperationObject;
121
+ servers?: ServerObject[];
122
+ parameters?: ParameterObject[];
123
+ }
124
+
125
+ export interface PathItemObjectWithRef {
126
+ $ref?: string;
127
+ summary?: string;
128
+ description?: string;
129
+ get?: OperationObjectWithRef;
130
+ put?: OperationObjectWithRef;
131
+ post?: OperationObjectWithRef;
132
+ delete?: OperationObjectWithRef;
133
+ options?: OperationObjectWithRef;
134
+ head?: OperationObjectWithRef;
135
+ patch?: OperationObjectWithRef;
136
+ trace?: OperationObjectWithRef;
137
+ servers?: ServerObject[];
138
+ parameters?: (ParameterObjectWithRef | ReferenceObject)[];
139
+ }
140
+
141
+ export interface OperationObject {
142
+ tags?: string[];
143
+ summary?: string;
144
+ description?: string;
145
+ externalDocs?: ExternalDocumentationObject;
146
+ operationId?: string;
147
+ parameters?: ParameterObject[];
148
+ requestBody?: RequestBodyObject;
149
+ responses: ResponsesObject;
150
+ callbacks?: Map<CallbackObject>;
151
+ deprecated?: boolean;
152
+ security?: SecurityRequirementObject[];
153
+ servers?: ServerObject[];
154
+
155
+ // extensions
156
+ "x-deprecated-description"?: string;
157
+ }
158
+
159
+ export interface OperationObjectWithRef {
160
+ tags?: string[];
161
+ summary?: string;
162
+ description?: string;
163
+ externalDocs?: ExternalDocumentationObject;
164
+ operationId?: string;
165
+ parameters?: (ParameterObjectWithRef | ReferenceObject)[];
166
+ requestBody?: RequestBodyObjectWithRef | ReferenceObject;
167
+ responses: ResponsesObjectWithRef;
168
+ callbacks?: Map<CallbackObjectWithRef | ReferenceObject>;
169
+ deprecated?: boolean;
170
+ security?: SecurityRequirementObject[];
171
+ servers?: ServerObject[];
172
+
173
+ // extensions
174
+ "x-deprecated-description"?: string;
175
+ }
176
+
177
+ export interface ExternalDocumentationObject {
178
+ description?: string;
179
+ url: string;
180
+ }
181
+
182
+ export interface ParameterObject {
183
+ name: string;
184
+ in: "query" | "header" | "path" | "cookie";
185
+ description?: string;
186
+ required?: boolean;
187
+ deprecated?: boolean;
188
+ allowEmptyValue?: boolean;
189
+ //
190
+ style?: string;
191
+ explode?: string;
192
+ allowReserved?: boolean;
193
+ schema?: SchemaObject;
194
+ example?: any;
195
+ examples?: Map<ExampleObject>;
196
+ //
197
+ content?: Map<MediaTypeObject>;
198
+ param?: Object;
199
+ // ignoring stylings: matrix, label, form, simple, spaceDelimited,
200
+ // pipeDelimited and deepObject
201
+ "x-enumDescriptions"?: Record<string, string>;
202
+ }
203
+
204
+ export interface ParameterObjectWithRef {
205
+ name: string;
206
+ in: string;
207
+ description?: string;
208
+ required?: boolean;
209
+ deprecated?: boolean;
210
+ allowEmptyValue?: boolean;
211
+ //
212
+ style?: string;
213
+ explode?: string;
214
+ allowReserved?: boolean;
215
+ schema?: SchemaObjectWithRef | ReferenceObject;
216
+ example?: any;
217
+ examples?: Map<ExampleObject | ReferenceObject>;
218
+ //
219
+ content?: Map<MediaTypeObjectWithRef>;
220
+ // ignoring stylings: matrix, label, form, simple, spaceDelimited,
221
+ // pipeDelimited and deepObject
222
+ }
223
+
224
+ export interface RequestBodyObject {
225
+ description?: string;
226
+ content: Map<MediaTypeObject>;
227
+ required?: boolean;
228
+ }
229
+
230
+ export interface RequestBodyObjectWithRef {
231
+ description?: string;
232
+ content: Map<MediaTypeObjectWithRef>;
233
+ required?: boolean;
234
+ }
235
+
236
+ export interface MediaTypeObject {
237
+ schema?: SchemaObject;
238
+ example?: any;
239
+ examples?: Map<ExampleObject>;
240
+ encoding?: Map<EncodingObject>;
241
+ type?: any;
242
+ }
243
+
244
+ export interface MediaTypeObjectWithRef {
245
+ schema?: SchemaObjectWithRef | ReferenceObject;
246
+ example?: any;
247
+ examples?: Map<ExampleObject | ReferenceObject>;
248
+ encoding?: Map<EncodingObjectWithRef>;
249
+ }
250
+
251
+ export interface EncodingObject {
252
+ contentType?: string;
253
+ headers?: Map<HeaderObject>;
254
+ style?: string;
255
+ explode?: boolean;
256
+ allowReserved?: boolean;
257
+ }
258
+
259
+ export interface EncodingObjectWithRef {
260
+ contentType?: string;
261
+ headers?: Map<HeaderObjectWithRef | ReferenceObject>;
262
+ style?: string;
263
+ explode?: boolean;
264
+ allowReserved?: boolean;
265
+ }
266
+
267
+ export type ResponsesObject = Map<ResponseObject>;
268
+
269
+ export type ResponsesObjectWithRef = Map<
270
+ ResponseObjectWithRef | ReferenceObject
271
+ >;
272
+
273
+ export interface ResponseObject {
274
+ description: string;
275
+ headers?: Map<HeaderObject>;
276
+ content?: Map<MediaTypeObject>;
277
+ links?: Map<LinkObject>;
278
+ }
279
+
280
+ export interface ResponseObjectWithRef {
281
+ description: string;
282
+ headers?: Map<HeaderObjectWithRef | ReferenceObject>;
283
+ content?: Map<MediaTypeObjectWithRef>;
284
+ links?: Map<LinkObject | ReferenceObject>;
285
+ }
286
+
287
+ export type CallbackObject = Map<PathItemObject>;
288
+
289
+ export type CallbackObjectWithRef = Map<PathItemObjectWithRef>;
290
+
291
+ export interface ExampleObject {
292
+ summary?: string;
293
+ description?: string;
294
+ value?: any;
295
+ externalValue?: string;
296
+ }
297
+
298
+ export interface LinkObject {
299
+ operationRef?: string;
300
+ operationId?: string;
301
+ parameters?: Map<any>;
302
+ requestBody?: any;
303
+ description?: string;
304
+ server?: ServerObject;
305
+ }
306
+
307
+ export type HeaderObject = Omit<ParameterObject, "name" | "in">;
308
+
309
+ export type HeaderObjectWithRef = Omit<ParameterObjectWithRef, "name" | "in">;
310
+
311
+ export interface TagObject {
312
+ name?: string;
313
+ description?: string;
314
+ externalDocs?: ExternalDocumentationObject;
315
+ "x-displayName"?: string;
316
+ }
317
+
318
+ export interface TagGroupObject {
319
+ name: string;
320
+ tags: string[];
321
+ }
322
+
323
+ export interface ReferenceObject {
324
+ $ref: string;
325
+ }
326
+
327
+ export type JSONSchema = JSONSchema4 | JSONSchema6 | JSONSchema7;
328
+ export type SchemaObject = Omit<
329
+ JSONSchema,
330
+ | "type"
331
+ | "allOf"
332
+ | "oneOf"
333
+ | "anyOf"
334
+ | "not"
335
+ | "items"
336
+ | "properties"
337
+ | "additionalProperties"
338
+ > & {
339
+ // OpenAPI specific overrides
340
+ type?: "string" | "number" | "integer" | "boolean" | "object" | "array";
341
+ allOf?: SchemaObject[];
342
+ oneOf?: SchemaObject[];
343
+ anyOf?: SchemaObject[];
344
+ not?: SchemaObject;
345
+ items?: SchemaObject;
346
+ properties?: Map<SchemaObject>;
347
+ additionalProperties?: boolean | SchemaObject;
348
+
349
+ // OpenAPI additions
350
+ nullable?: boolean;
351
+ discriminator?: DiscriminatorObject;
352
+ readOnly?: boolean;
353
+ writeOnly?: boolean;
354
+ xml?: XMLObject;
355
+ externalDocs?: ExternalDocumentationObject;
356
+ example?: any;
357
+ deprecated?: boolean;
358
+ "x-tags"?: string[];
359
+ "x-enumDescriptions"?: Record<string, string>;
360
+ };
361
+
362
+ export type SchemaObjectWithRef = Omit<
363
+ JSONSchema,
364
+ | "type"
365
+ | "allOf"
366
+ | "oneOf"
367
+ | "anyOf"
368
+ | "not"
369
+ | "items"
370
+ | "properties"
371
+ | "additionalProperties"
372
+ > & {
373
+ // OpenAPI specific overrides
374
+ type?: "string" | "number" | "integer" | "boolean" | "object" | "array";
375
+ allOf?: (SchemaObject | ReferenceObject)[];
376
+ oneOf?: (SchemaObject | ReferenceObject)[];
377
+ anyOf?: (SchemaObject | ReferenceObject)[];
378
+ not?: SchemaObject | ReferenceObject;
379
+ items?: SchemaObject | ReferenceObject;
380
+ properties?: Map<SchemaObject | ReferenceObject>;
381
+ additionalProperties?: boolean | SchemaObject | ReferenceObject;
382
+
383
+ // OpenAPI additions
384
+ nullable?: boolean;
385
+ discriminator?: DiscriminatorObject;
386
+ readOnly?: boolean;
387
+ writeOnly?: boolean;
388
+ xml?: XMLObject;
389
+ externalDocs?: ExternalDocumentationObject;
390
+ example?: any;
391
+ deprecated?: boolean;
392
+ };
393
+
394
+ export interface DiscriminatorObject {
395
+ propertyName: string;
396
+ mapping?: Map<string>;
397
+ }
398
+
399
+ export interface XMLObject {
400
+ name?: string;
401
+ namespace?: string;
402
+ prefix?: string;
403
+ attribute?: boolean;
404
+ wrapped?: boolean;
405
+ }
406
+
407
+ export type SecuritySchemeObject =
408
+ | ApiKeySecuritySchemeObject
409
+ | HttpSecuritySchemeObject
410
+ | Oauth2SecuritySchemeObject
411
+ | OpenIdConnectSecuritySchemeObject;
412
+
413
+ export interface ApiKeySecuritySchemeObject {
414
+ type: "apiKey";
415
+ description?: string;
416
+ name: string;
417
+ in: "query" | "header" | "cookie";
418
+ }
419
+
420
+ export interface HttpSecuritySchemeObject {
421
+ type: "http";
422
+ description?: string;
423
+ scheme: string;
424
+ bearerFormat?: string;
425
+ name?: string;
426
+ in?: string;
427
+ }
428
+
429
+ export interface Oauth2SecuritySchemeObject {
430
+ type: "oauth2";
431
+ description?: string;
432
+ flows: OAuthFlowsObject;
433
+ }
434
+
435
+ export interface OpenIdConnectSecuritySchemeObject {
436
+ type: "openIdConnect";
437
+ description?: string;
438
+ openIdConnectUrl: string;
439
+ }
440
+
441
+ export interface OAuthFlowsObject {
442
+ implicit?: OAuthFlowObject;
443
+ password?: OAuthFlowObject;
444
+ clientCredentials?: OAuthFlowObject;
445
+ authorizationCode?: OAuthFlowObject;
446
+ }
447
+
448
+ export interface OAuthFlowObject {
449
+ authorizationUrl?: string; // required for some
450
+ tokenUrl?: string; // required for some
451
+ refreshUrl?: string;
452
+ scopes: Map<string>;
453
+ }
454
+
455
+ export type SecurityRequirementObject = Map<string[]>;
@@ -0,0 +1,171 @@
1
+ /* ============================================================================
2
+ * Copyright (c) Palo Alto Networks
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ * ========================================================================== */
7
+
8
+ import $RefParser from "@apidevtools/json-schema-ref-parser";
9
+ import { bundle, Config } from "@redocly/openapi-core";
10
+ import type { Source, Document } from "@redocly/openapi-core";
11
+ import { ResolvedConfig } from "@redocly/openapi-core/lib/config";
12
+ import chalk from "chalk";
13
+ // @ts-ignore
14
+ import { convertObj } from "swagger2openapi";
15
+
16
+ import { OpenApiObject } from "../types";
17
+ import { OpenAPIParser } from "./services/OpenAPIParser";
18
+
19
+ function serializer(replacer: any, cycleReplacer: any) {
20
+ var stack: any = [],
21
+ keys: any = [];
22
+
23
+ if (cycleReplacer === undefined)
24
+ cycleReplacer = function (key: any, value: any) {
25
+ if (stack[0] === value) return "circular()";
26
+ return value.title ? `circular(${value.title})` : "circular()";
27
+ };
28
+
29
+ return function (key: any, value: any) {
30
+ // Resolve discriminator ref pointers
31
+ if (value?.discriminator !== undefined) {
32
+ const parser = new OpenAPIParser(stack[0]);
33
+ if (
34
+ value.discriminator.mapping &&
35
+ typeof value.discriminator.mapping === "object"
36
+ ) {
37
+ for (let [k, v] of Object.entries(value.discriminator.mapping)) {
38
+ const discriminator = k as string;
39
+ if (typeof v === "string" && v.charAt(0) === "#") {
40
+ const ref = v as string;
41
+ const resolvedRef = parser.byRef(ref);
42
+ value.discriminator.mapping[discriminator] = resolvedRef;
43
+ }
44
+ }
45
+ }
46
+ }
47
+ if (stack.length > 0) {
48
+ // @ts-ignore
49
+ var thisPos = stack.indexOf(this);
50
+ // @ts-ignore
51
+ ~thisPos ? stack.splice(thisPos + 1) : stack.push(this);
52
+ ~thisPos ? keys.splice(thisPos, Infinity, key) : keys.push(key);
53
+ // @ts-ignore
54
+ if (~stack.indexOf(value)) value = cycleReplacer.call(this, key, value);
55
+ } else stack.push(value);
56
+
57
+ // @ts-ignore
58
+ return replacer === undefined ? value : replacer.call(this, key, value);
59
+ };
60
+ }
61
+
62
+ export function convertSwagger2OpenAPI(spec: object) {
63
+ console.warn(
64
+ "[ReDoc Compatibility mode]: Converting OpenAPI 2.0 to OpenAPI 3.0"
65
+ );
66
+ return new Promise((resolve, reject) =>
67
+ convertObj(
68
+ spec,
69
+ {
70
+ patch: true,
71
+ warnOnly: true,
72
+ text: "{}",
73
+ anchors: true,
74
+ resolveInternal: true,
75
+ },
76
+ (err: any, res: any) => {
77
+ // TODO: log any warnings
78
+ if (err) {
79
+ return reject(err);
80
+ }
81
+ resolve(res && res.openapi);
82
+ }
83
+ )
84
+ );
85
+ }
86
+
87
+ async function resolveJsonRefs(specUrlOrObject: object | string) {
88
+ try {
89
+ let schema = await $RefParser.dereference(specUrlOrObject, {
90
+ continueOnError: true,
91
+ resolve: {
92
+ file: true,
93
+ external: true,
94
+ http: {
95
+ timeout: 15000, // 15 sec timeout
96
+ },
97
+ },
98
+ dereference: {
99
+ circular: true,
100
+ },
101
+ });
102
+ return schema as OpenApiObject;
103
+ } catch (err: any) {
104
+ let errorMsg = "";
105
+
106
+ if (err.errors[0] !== undefined) {
107
+ const error = err.errors[0];
108
+ errorMsg = `Error: [${error.message}] with footprint [${error.footprint}]`;
109
+ } else {
110
+ errorMsg = err;
111
+ }
112
+
113
+ console.error(chalk.yellow(errorMsg));
114
+ return;
115
+ }
116
+ }
117
+
118
+ export async function loadAndResolveSpec(specUrlOrObject: object | string) {
119
+ const config = new Config({} as ResolvedConfig);
120
+ const bundleOpts = {
121
+ config,
122
+ base: process.cwd(),
123
+ } as any;
124
+
125
+ if (typeof specUrlOrObject === "object" && specUrlOrObject !== null) {
126
+ bundleOpts["doc"] = {
127
+ source: { absoluteRef: "" } as Source,
128
+ parsed: specUrlOrObject,
129
+ } as Document;
130
+ } else {
131
+ bundleOpts["ref"] = specUrlOrObject;
132
+ }
133
+
134
+ // Force dereference ?
135
+ // bundleOpts["dereference"] = true;
136
+
137
+ const {
138
+ bundle: { parsed },
139
+ } = await bundle(bundleOpts);
140
+
141
+ //Pre-processing before resolving JSON refs
142
+ if (parsed.components) {
143
+ for (let [component, type] of Object.entries(parsed.components) as any) {
144
+ if (component === "schemas") {
145
+ for (let [schemaKey, schemaValue] of Object.entries(type) as any) {
146
+ const title: string | undefined = schemaValue["title"];
147
+ if (!title) {
148
+ schemaValue.title = schemaKey;
149
+ }
150
+ }
151
+ }
152
+ }
153
+ }
154
+
155
+ const resolved = await resolveJsonRefs(parsed);
156
+
157
+ // Force serialization and replace circular $ref pointers
158
+ // @ts-ignore
159
+ const serialized = JSON.stringify(resolved, serializer());
160
+ let decycled;
161
+ try {
162
+ decycled = JSON.parse(serialized);
163
+ } catch (err: any) {
164
+ console.error(chalk.yellow(err));
165
+ }
166
+ return decycled !== undefined && typeof decycled === "object"
167
+ ? decycled.swagger !== undefined
168
+ ? convertSwagger2OpenAPI(decycled)
169
+ : decycled
170
+ : resolved;
171
+ }