docusaurus-plugin-openapi-docs 0.0.0-365 → 0.0.0-368

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.
package/lib/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  import type { LoadContext, Plugin } from "@docusaurus/types";
2
2
  import type { PluginOptions, LoadedContent } from "./types";
3
+ export declare function isURL(str: string): boolean;
3
4
  export default function pluginOpenAPI(context: LoadContext, options: PluginOptions): Plugin<LoadedContent>;
package/lib/index.js CHANGED
@@ -9,6 +9,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
9
9
  return (mod && mod.__esModule) ? mod : { "default": mod };
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.isURL = void 0;
12
13
  const fs_1 = __importDefault(require("fs"));
13
14
  const path_1 = __importDefault(require("path"));
14
15
  const utils_1 = require("@docusaurus/utils");
@@ -17,12 +18,18 @@ const mustache_1 = require("mustache");
17
18
  const markdown_1 = require("./markdown");
18
19
  const openapi_1 = require("./openapi");
19
20
  const sidebars_1 = __importDefault(require("./sidebars"));
21
+ function isURL(str) {
22
+ return /^(https?:)\/\//m.test(str);
23
+ }
24
+ exports.isURL = isURL;
20
25
  function pluginOpenAPI(context, options) {
21
26
  let { config } = options;
22
27
  let { siteDir } = context;
23
28
  async function generateApiDocs(options) {
24
29
  let { specPath, outputDir, template, sidebarOptions } = options;
25
- const contentPath = path_1.default.resolve(siteDir, specPath);
30
+ const contentPath = isURL(specPath)
31
+ ? specPath
32
+ : path_1.default.resolve(siteDir, specPath);
26
33
  try {
27
34
  const openapiFiles = await (0, openapi_1.readOpenapiFiles)(contentPath, {});
28
35
  const [loadedApi, tags] = await (0, openapi_1.processOpenapiFiles)(openapiFiles, sidebarOptions);
@@ -16,10 +16,11 @@ const openapi_to_postmanv2_1 = __importDefault(require("@paloaltonetworks/openap
16
16
  const postman_collection_1 = __importDefault(require("@paloaltonetworks/postman-collection"));
17
17
  const chalk_1 = __importDefault(require("chalk"));
18
18
  const fs_extra_1 = __importDefault(require("fs-extra"));
19
- const js_yaml_1 = __importDefault(require("js-yaml"));
20
19
  const json_refs_1 = __importDefault(require("json-refs"));
21
20
  const lodash_1 = require("lodash");
21
+ const index_1 = require("../index");
22
22
  const createExample_1 = require("./createExample");
23
+ const loadAndBundleSpec_1 = require("./utils/loadAndBundleSpec");
23
24
  /**
24
25
  * Finds any reference objects in the OpenAPI definition and resolves them to a finalized value.
25
26
  */
@@ -188,29 +189,30 @@ function bindCollectionToApiItems(items, postmanCollection) {
188
189
  });
189
190
  }
190
191
  async function readOpenapiFiles(openapiPath, _options) {
191
- const stat = await fs_extra_1.default.lstat(openapiPath);
192
- if (stat.isDirectory()) {
193
- console.warn(chalk_1.default.yellow("WARNING: Loading a directory of OpenAPI definitions is experimental and subject to unannounced breaking changes."));
194
- // TODO: Add config for inlcude/ignore
195
- const allFiles = await (0, utils_1.Globby)(["**/*.{json,yaml,yml}"], {
196
- cwd: openapiPath,
197
- ignore: utils_1.GlobExcludeDefault,
198
- });
199
- const sources = allFiles.filter((x) => !x.includes("_category_")); // todo: regex exclude?
200
- return Promise.all(sources.map(async (source) => {
201
- // TODO: make a function for this
202
- const fullPath = path_1.default.join(openapiPath, source);
203
- const openapiString = await fs_extra_1.default.readFile(fullPath, "utf-8");
204
- const data = js_yaml_1.default.load(openapiString);
205
- return {
206
- source: fullPath,
207
- sourceDirName: path_1.default.dirname(source),
208
- data,
209
- };
210
- }));
192
+ if (!(0, index_1.isURL)(openapiPath)) {
193
+ const stat = await fs_extra_1.default.lstat(openapiPath);
194
+ if (stat.isDirectory()) {
195
+ console.warn(chalk_1.default.yellow("WARNING: Loading a directory of OpenAPI definitions is experimental and subject to unannounced breaking changes."));
196
+ // TODO: Add config for inlcude/ignore
197
+ const allFiles = await (0, utils_1.Globby)(["**/*.{json,yaml,yml}"], {
198
+ cwd: openapiPath,
199
+ ignore: utils_1.GlobExcludeDefault,
200
+ deep: 1,
201
+ });
202
+ const sources = allFiles.filter((x) => !x.includes("_category_")); // todo: regex exclude?
203
+ return Promise.all(sources.map(async (source) => {
204
+ // TODO: make a function for this
205
+ const fullPath = path_1.default.join(openapiPath, source);
206
+ const data = (await (0, loadAndBundleSpec_1.loadAndBundleSpec)(fullPath));
207
+ return {
208
+ source: fullPath,
209
+ sourceDirName: path_1.default.dirname(source),
210
+ data,
211
+ };
212
+ }));
213
+ }
211
214
  }
212
- const openapiString = await fs_extra_1.default.readFile(openapiPath, "utf-8");
213
- const data = js_yaml_1.default.load(openapiString);
215
+ const data = (await (0, loadAndBundleSpec_1.loadAndBundleSpec)(openapiPath));
214
216
  return [
215
217
  {
216
218
  source: openapiPath,
@@ -22,12 +22,6 @@ describe("openapi", () => {
22
22
  const yaml = results.find((x) => x.source.endsWith("openapi.yaml"));
23
23
  expect(yaml).toBeTruthy();
24
24
  expect(yaml === null || yaml === void 0 ? void 0 : yaml.sourceDirName).toBe(".");
25
- const froyo = results.find((x) => x.source.endsWith("froyo.yaml"));
26
- expect(froyo).toBeTruthy();
27
- expect(froyo === null || froyo === void 0 ? void 0 : froyo.sourceDirName).toBe("yogurtstore");
28
- const nested = results.find((x) => x.source.endsWith("nested.yaml"));
29
- expect(nested).toBeTruthy();
30
- expect(nested === null || nested === void 0 ? void 0 : nested.sourceDirName).toBe("yogurtstore/nested");
31
25
  });
32
26
  });
33
27
  });
@@ -0,0 +1,3 @@
1
+ import { OpenAPISpec } from "./types";
2
+ export declare function loadAndBundleSpec(specUrlOrObject: object | string): Promise<OpenAPISpec>;
3
+ export declare function convertSwagger2OpenAPI(spec: any): Promise<OpenAPISpec>;
@@ -0,0 +1,44 @@
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 });
9
+ exports.convertSwagger2OpenAPI = exports.loadAndBundleSpec = void 0;
10
+ const bundle_1 = require("@redocly/openapi-core/lib/bundle");
11
+ const config_1 = require("@redocly/openapi-core/lib/config/config");
12
+ const swagger2openapi_1 = require("swagger2openapi");
13
+ async function loadAndBundleSpec(specUrlOrObject) {
14
+ const config = new config_1.Config({});
15
+ const bundleOpts = {
16
+ config,
17
+ base: process.cwd(),
18
+ };
19
+ if (typeof specUrlOrObject === "object" && specUrlOrObject !== null) {
20
+ bundleOpts["doc"] = {
21
+ source: { absoluteRef: "" },
22
+ parsed: specUrlOrObject,
23
+ };
24
+ }
25
+ else {
26
+ bundleOpts["ref"] = specUrlOrObject;
27
+ }
28
+ // Force dereference ?
29
+ // bundleOpts["dereference"] = true;
30
+ const { bundle: { parsed }, } = await (0, bundle_1.bundle)(bundleOpts);
31
+ return parsed.swagger !== undefined ? convertSwagger2OpenAPI(parsed) : parsed;
32
+ }
33
+ exports.loadAndBundleSpec = loadAndBundleSpec;
34
+ function convertSwagger2OpenAPI(spec) {
35
+ console.warn("[ReDoc Compatibility mode]: Converting OpenAPI 2.0 to OpenAPI 3.0");
36
+ return new Promise((resolve, reject) => (0, swagger2openapi_1.convertObj)(spec, { patch: true, warnOnly: true, text: "{}", anchors: true }, (err, res) => {
37
+ // TODO: log any warnings
38
+ if (err) {
39
+ return reject(err);
40
+ }
41
+ resolve(res && res.openapi);
42
+ }));
43
+ }
44
+ exports.convertSwagger2OpenAPI = convertSwagger2OpenAPI;
@@ -0,0 +1,306 @@
1
+ declare type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
2
+ export interface OpenAPISpec {
3
+ openapi: string;
4
+ info: OpenAPIInfo;
5
+ servers?: OpenAPIServer[];
6
+ paths: OpenAPIPaths;
7
+ components?: OpenAPIComponents;
8
+ security?: OpenAPISecurityRequirement[];
9
+ tags?: OpenAPITag[];
10
+ externalDocs?: OpenAPIExternalDocumentation;
11
+ "x-webhooks"?: OpenAPIPaths;
12
+ webhooks?: OpenAPIPaths;
13
+ }
14
+ export interface OpenAPIInfo {
15
+ title: string;
16
+ version: string;
17
+ description?: string;
18
+ summary?: string;
19
+ termsOfService?: string;
20
+ contact?: OpenAPIContact;
21
+ license?: OpenAPILicense;
22
+ }
23
+ export interface OpenAPIServer {
24
+ url: string;
25
+ description?: string;
26
+ variables?: {
27
+ [name: string]: OpenAPIServerVariable;
28
+ };
29
+ }
30
+ export interface OpenAPIServerVariable {
31
+ enum?: string[];
32
+ default: string;
33
+ description?: string;
34
+ }
35
+ export interface OpenAPIPaths {
36
+ [path: string]: OpenAPIPath;
37
+ }
38
+ export interface OpenAPIRef {
39
+ $ref: string;
40
+ }
41
+ export declare type Referenced<T> = OpenAPIRef | T;
42
+ export interface OpenAPIPath {
43
+ summary?: string;
44
+ description?: string;
45
+ get?: OpenAPIOperation;
46
+ put?: OpenAPIOperation;
47
+ post?: OpenAPIOperation;
48
+ delete?: OpenAPIOperation;
49
+ options?: OpenAPIOperation;
50
+ head?: OpenAPIOperation;
51
+ patch?: OpenAPIOperation;
52
+ trace?: OpenAPIOperation;
53
+ servers?: OpenAPIServer[];
54
+ parameters?: Array<Referenced<OpenAPIParameter>>;
55
+ $ref?: string;
56
+ }
57
+ export interface OpenAPIXCodeSample {
58
+ lang: string;
59
+ label?: string;
60
+ source: string;
61
+ }
62
+ export interface OpenAPIOperation {
63
+ tags?: string[];
64
+ summary?: string;
65
+ description?: string;
66
+ externalDocs?: OpenAPIExternalDocumentation;
67
+ operationId?: string;
68
+ parameters?: Array<Referenced<OpenAPIParameter>>;
69
+ requestBody?: Referenced<OpenAPIRequestBody>;
70
+ responses: OpenAPIResponses;
71
+ callbacks?: {
72
+ [name: string]: Referenced<OpenAPICallback>;
73
+ };
74
+ deprecated?: boolean;
75
+ security?: OpenAPISecurityRequirement[];
76
+ servers?: OpenAPIServer[];
77
+ "x-codeSamples"?: OpenAPIXCodeSample[];
78
+ "x-code-samples"?: OpenAPIXCodeSample[];
79
+ }
80
+ export interface OpenAPIParameter {
81
+ name: string;
82
+ in?: OpenAPIParameterLocation;
83
+ description?: string;
84
+ required?: boolean;
85
+ deprecated?: boolean;
86
+ allowEmptyValue?: boolean;
87
+ style?: OpenAPIParameterStyle;
88
+ explode?: boolean;
89
+ allowReserved?: boolean;
90
+ schema?: Referenced<OpenAPISchema>;
91
+ example?: any;
92
+ examples?: {
93
+ [media: string]: Referenced<OpenAPIExample>;
94
+ };
95
+ content?: {
96
+ [media: string]: OpenAPIMediaType;
97
+ };
98
+ encoding?: Record<string, OpenAPIEncoding>;
99
+ const?: any;
100
+ }
101
+ export interface OpenAPIExample {
102
+ value: any;
103
+ summary?: string;
104
+ description?: string;
105
+ externalValue?: string;
106
+ }
107
+ export interface OpenAPISchema {
108
+ $ref?: string;
109
+ type?: string | string[];
110
+ properties?: {
111
+ [name: string]: OpenAPISchema;
112
+ };
113
+ patternProperties?: {
114
+ [name: string]: OpenAPISchema;
115
+ };
116
+ additionalProperties?: boolean | OpenAPISchema;
117
+ unevaluatedProperties?: boolean | OpenAPISchema;
118
+ description?: string;
119
+ default?: any;
120
+ items?: OpenAPISchema | OpenAPISchema[] | boolean;
121
+ required?: string[];
122
+ readOnly?: boolean;
123
+ writeOnly?: boolean;
124
+ deprecated?: boolean;
125
+ format?: string;
126
+ externalDocs?: OpenAPIExternalDocumentation;
127
+ discriminator?: OpenAPIDiscriminator;
128
+ nullable?: boolean;
129
+ oneOf?: OpenAPISchema[];
130
+ anyOf?: OpenAPISchema[];
131
+ allOf?: OpenAPISchema[];
132
+ not?: OpenAPISchema;
133
+ title?: string;
134
+ multipleOf?: number;
135
+ maximum?: number;
136
+ exclusiveMaximum?: boolean | number;
137
+ minimum?: number;
138
+ exclusiveMinimum?: boolean | number;
139
+ maxLength?: number;
140
+ minLength?: number;
141
+ pattern?: string;
142
+ maxItems?: number;
143
+ minItems?: number;
144
+ uniqueItems?: boolean;
145
+ maxProperties?: number;
146
+ minProperties?: number;
147
+ enum?: any[];
148
+ example?: any;
149
+ if?: OpenAPISchema;
150
+ else?: OpenAPISchema;
151
+ then?: OpenAPISchema;
152
+ examples?: any[];
153
+ const?: string;
154
+ contentEncoding?: string;
155
+ contentMediaType?: string;
156
+ prefixItems?: OpenAPISchema[];
157
+ additionalItems?: OpenAPISchema | boolean;
158
+ }
159
+ export interface OpenAPIDiscriminator {
160
+ propertyName: string;
161
+ mapping?: {
162
+ [name: string]: string;
163
+ };
164
+ "x-explicitMappingOnly"?: boolean;
165
+ }
166
+ export interface OpenAPIMediaType {
167
+ schema?: Referenced<OpenAPISchema>;
168
+ example?: any;
169
+ examples?: {
170
+ [name: string]: Referenced<OpenAPIExample>;
171
+ };
172
+ encoding?: {
173
+ [field: string]: OpenAPIEncoding;
174
+ };
175
+ }
176
+ export interface OpenAPIEncoding {
177
+ contentType: string;
178
+ headers?: {
179
+ [name: string]: Referenced<OpenAPIHeader>;
180
+ };
181
+ style: OpenAPIParameterStyle;
182
+ explode: boolean;
183
+ allowReserved: boolean;
184
+ }
185
+ export declare type OpenAPIParameterLocation = "query" | "header" | "path" | "cookie";
186
+ export declare type OpenAPIParameterStyle = "matrix" | "label" | "form" | "simple" | "spaceDelimited" | "pipeDelimited" | "deepObject";
187
+ export interface OpenAPIRequestBody {
188
+ description?: string;
189
+ required?: boolean;
190
+ content: {
191
+ [mime: string]: OpenAPIMediaType;
192
+ };
193
+ "x-examples"?: {
194
+ [mime: string]: {
195
+ [name: string]: Referenced<OpenAPIExample>;
196
+ };
197
+ };
198
+ "x-example"?: {
199
+ [mime: string]: any;
200
+ };
201
+ }
202
+ export interface OpenAPIResponses {
203
+ [code: string]: Referenced<OpenAPIResponse>;
204
+ }
205
+ export interface OpenAPIResponse extends Pick<OpenAPIRequestBody, "description" | "x-examples" | "x-example"> {
206
+ headers?: {
207
+ [name: string]: Referenced<OpenAPIHeader>;
208
+ };
209
+ links?: {
210
+ [name: string]: Referenced<OpenAPILink>;
211
+ };
212
+ content?: {
213
+ [mime: string]: OpenAPIMediaType;
214
+ };
215
+ }
216
+ export interface OpenAPILink {
217
+ $ref?: string;
218
+ }
219
+ export declare type OpenAPIHeader = Omit<OpenAPIParameter, "in" | "name">;
220
+ export interface OpenAPICallback {
221
+ [name: string]: OpenAPIPath;
222
+ }
223
+ export interface OpenAPIComponents {
224
+ schemas?: {
225
+ [name: string]: Referenced<OpenAPISchema>;
226
+ };
227
+ responses?: {
228
+ [name: string]: Referenced<OpenAPIResponse>;
229
+ };
230
+ parameters?: {
231
+ [name: string]: Referenced<OpenAPIParameter>;
232
+ };
233
+ examples?: {
234
+ [name: string]: Referenced<OpenAPIExample>;
235
+ };
236
+ requestBodies?: {
237
+ [name: string]: Referenced<OpenAPIRequestBody>;
238
+ };
239
+ headers?: {
240
+ [name: string]: Referenced<OpenAPIHeader>;
241
+ };
242
+ securitySchemes?: {
243
+ [name: string]: Referenced<OpenAPISecurityScheme>;
244
+ };
245
+ links?: {
246
+ [name: string]: Referenced<OpenAPILink>;
247
+ };
248
+ callbacks?: {
249
+ [name: string]: Referenced<OpenAPICallback>;
250
+ };
251
+ }
252
+ export interface OpenAPISecurityRequirement {
253
+ [name: string]: string[];
254
+ }
255
+ export interface OpenAPISecurityScheme {
256
+ type: "apiKey" | "http" | "oauth2" | "openIdConnect";
257
+ description?: string;
258
+ name?: string;
259
+ in?: "query" | "header" | "cookie";
260
+ scheme?: string;
261
+ bearerFormat: string;
262
+ flows: {
263
+ implicit?: {
264
+ refreshUrl?: string;
265
+ scopes: Record<string, string>;
266
+ authorizationUrl: string;
267
+ };
268
+ password?: {
269
+ refreshUrl?: string;
270
+ scopes: Record<string, string>;
271
+ tokenUrl: string;
272
+ };
273
+ clientCredentials?: {
274
+ refreshUrl?: string;
275
+ scopes: Record<string, string>;
276
+ tokenUrl: string;
277
+ };
278
+ authorizationCode?: {
279
+ refreshUrl?: string;
280
+ scopes: Record<string, string>;
281
+ tokenUrl: string;
282
+ };
283
+ };
284
+ openIdConnectUrl?: string;
285
+ }
286
+ export interface OpenAPITag {
287
+ name: string;
288
+ description?: string;
289
+ externalDocs?: OpenAPIExternalDocumentation;
290
+ "x-displayName"?: string;
291
+ }
292
+ export interface OpenAPIExternalDocumentation {
293
+ description?: string;
294
+ url: string;
295
+ }
296
+ export interface OpenAPIContact {
297
+ name?: string;
298
+ url?: string;
299
+ email?: string;
300
+ }
301
+ export interface OpenAPILicense {
302
+ name: string;
303
+ url?: string;
304
+ identifier?: string;
305
+ }
306
+ export {};
@@ -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 });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "docusaurus-plugin-openapi-docs",
3
3
  "description": "OpenAPI plugin for Docusaurus.",
4
- "version": "0.0.0-365",
4
+ "version": "0.0.0-368",
5
5
  "license": "MIT",
6
6
  "keywords": [
7
7
  "openapi",
@@ -42,6 +42,7 @@
42
42
  "@docusaurus/utils-validation": "2.0.0-beta.21",
43
43
  "@paloaltonetworks/openapi-to-postmanv2": "3.1.0-hotfix.1",
44
44
  "@paloaltonetworks/postman-collection": "^4.1.0",
45
+ "@redocly/openapi-core": "^1.0.0-beta.100",
45
46
  "@types/js-yaml": "^4.0.5",
46
47
  "@types/mustache": "^4.1.2",
47
48
  "chalk": "^4.1.2",
@@ -52,6 +53,7 @@
52
53
  "json-schema-merge-allof": "^0.8.1",
53
54
  "lodash": "^4.17.20",
54
55
  "mustache": "^4.2.0",
56
+ "swagger2openapi": "^7.0.8",
55
57
  "webpack": "^5.61.0"
56
58
  },
57
59
  "peerDependencies": {
@@ -60,5 +62,5 @@
60
62
  "engines": {
61
63
  "node": ">=14"
62
64
  },
63
- "gitHead": "f9390cc5f7f74c629f45bb374c26dac169deb797"
65
+ "gitHead": "22c02a6463280203fe9437c891412b6fbf2cd274"
64
66
  }
package/src/index.ts CHANGED
@@ -18,6 +18,10 @@ import { readOpenapiFiles, processOpenapiFiles } from "./openapi";
18
18
  import generateSidebarSlice from "./sidebars";
19
19
  import type { PluginOptions, LoadedContent, APIOptions } from "./types";
20
20
 
21
+ export function isURL(str: string): boolean {
22
+ return /^(https?:)\/\//m.test(str);
23
+ }
24
+
21
25
  export default function pluginOpenAPI(
22
26
  context: LoadContext,
23
27
  options: PluginOptions
@@ -28,7 +32,9 @@ export default function pluginOpenAPI(
28
32
  async function generateApiDocs(options: APIOptions) {
29
33
  let { specPath, outputDir, template, sidebarOptions } = options;
30
34
 
31
- const contentPath = path.resolve(siteDir, specPath);
35
+ const contentPath = isURL(specPath)
36
+ ? specPath
37
+ : path.resolve(siteDir, specPath);
32
38
 
33
39
  try {
34
40
  const openapiFiles = await readOpenapiFiles(contentPath, {});
@@ -26,12 +26,6 @@ describe("openapi", () => {
26
26
  const yaml = results.find((x) => x.source.endsWith("openapi.yaml"));
27
27
  expect(yaml).toBeTruthy();
28
28
  expect(yaml?.sourceDirName).toBe(".");
29
- const froyo = results.find((x) => x.source.endsWith("froyo.yaml"));
30
- expect(froyo).toBeTruthy();
31
- expect(froyo?.sourceDirName).toBe("yogurtstore");
32
- const nested = results.find((x) => x.source.endsWith("nested.yaml"));
33
- expect(nested).toBeTruthy();
34
- expect(nested?.sourceDirName).toBe("yogurtstore/nested");
35
29
  });
36
30
  });
37
31
  });
@@ -13,10 +13,10 @@ import sdk from "@paloaltonetworks/postman-collection";
13
13
  import Collection from "@paloaltonetworks/postman-collection";
14
14
  import chalk from "chalk";
15
15
  import fs from "fs-extra";
16
- import yaml from "js-yaml";
17
16
  import JsonRefs from "json-refs";
18
17
  import { kebabCase } from "lodash";
19
18
 
19
+ import { isURL } from "../index";
20
20
  import {
21
21
  ApiMetadata,
22
22
  ApiPageMetadata,
@@ -26,6 +26,7 @@ import {
26
26
  } from "../types";
27
27
  import { sampleFromSchema } from "./createExample";
28
28
  import { OpenApiObject, OpenApiObjectWithRef, TagObject } from "./types";
29
+ import { loadAndBundleSpec } from "./utils/loadAndBundleSpec";
29
30
 
30
31
  /**
31
32
  * Finds any reference objects in the OpenAPI definition and resolves them to a finalized value.
@@ -248,36 +249,39 @@ export async function readOpenapiFiles(
248
249
  openapiPath: string,
249
250
  _options: {}
250
251
  ): Promise<OpenApiFiles[]> {
251
- const stat = await fs.lstat(openapiPath);
252
- if (stat.isDirectory()) {
253
- console.warn(
254
- chalk.yellow(
255
- "WARNING: Loading a directory of OpenAPI definitions is experimental and subject to unannounced breaking changes."
256
- )
257
- );
258
-
259
- // TODO: Add config for inlcude/ignore
260
- const allFiles = await Globby(["**/*.{json,yaml,yml}"], {
261
- cwd: openapiPath,
262
- ignore: GlobExcludeDefault,
263
- });
264
- const sources = allFiles.filter((x) => !x.includes("_category_")); // todo: regex exclude?
265
- return Promise.all(
266
- sources.map(async (source) => {
267
- // TODO: make a function for this
268
- const fullPath = path.join(openapiPath, source);
269
- const openapiString = await fs.readFile(fullPath, "utf-8");
270
- const data = yaml.load(openapiString) as OpenApiObjectWithRef;
271
- return {
272
- source: fullPath, // This will be aliased in process.
273
- sourceDirName: path.dirname(source),
274
- data,
275
- };
276
- })
277
- );
252
+ if (!isURL(openapiPath)) {
253
+ const stat = await fs.lstat(openapiPath);
254
+ if (stat.isDirectory()) {
255
+ console.warn(
256
+ chalk.yellow(
257
+ "WARNING: Loading a directory of OpenAPI definitions is experimental and subject to unannounced breaking changes."
258
+ )
259
+ );
260
+
261
+ // TODO: Add config for inlcude/ignore
262
+ const allFiles = await Globby(["**/*.{json,yaml,yml}"], {
263
+ cwd: openapiPath,
264
+ ignore: GlobExcludeDefault,
265
+ deep: 1,
266
+ });
267
+ const sources = allFiles.filter((x) => !x.includes("_category_")); // todo: regex exclude?
268
+ return Promise.all(
269
+ sources.map(async (source) => {
270
+ // TODO: make a function for this
271
+ const fullPath = path.join(openapiPath, source);
272
+ const data = (await loadAndBundleSpec(
273
+ fullPath
274
+ )) as OpenApiObjectWithRef;
275
+ return {
276
+ source: fullPath, // This will be aliased in process.
277
+ sourceDirName: path.dirname(source),
278
+ data,
279
+ };
280
+ })
281
+ );
282
+ }
278
283
  }
279
- const openapiString = await fs.readFile(openapiPath, "utf-8");
280
- const data = yaml.load(openapiString) as OpenApiObjectWithRef;
284
+ const data = (await loadAndBundleSpec(openapiPath)) as OpenApiObjectWithRef;
281
285
  return [
282
286
  {
283
287
  source: openapiPath, // This will be aliased in process.
@@ -0,0 +1,62 @@
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
+ // @ts-nocheck
9
+
10
+ import type { Source, Document } from "@redocly/openapi-core";
11
+ import { bundle } from "@redocly/openapi-core/lib/bundle";
12
+ import type { ResolvedConfig } from "@redocly/openapi-core/lib/config";
13
+ import { Config } from "@redocly/openapi-core/lib/config/config";
14
+ import { convertObj } from "swagger2openapi";
15
+
16
+ import { OpenAPISpec } from "./types";
17
+
18
+ export async function loadAndBundleSpec(
19
+ specUrlOrObject: object | string
20
+ ): Promise<OpenAPISpec> {
21
+ const config = new Config({} as ResolvedConfig);
22
+ const bundleOpts = {
23
+ config,
24
+ base: process.cwd(),
25
+ };
26
+
27
+ if (typeof specUrlOrObject === "object" && specUrlOrObject !== null) {
28
+ bundleOpts["doc"] = {
29
+ source: { absoluteRef: "" } as Source,
30
+ parsed: specUrlOrObject,
31
+ } as Document;
32
+ } else {
33
+ bundleOpts["ref"] = specUrlOrObject;
34
+ }
35
+
36
+ // Force dereference ?
37
+ // bundleOpts["dereference"] = true;
38
+
39
+ const {
40
+ bundle: { parsed },
41
+ } = await bundle(bundleOpts);
42
+ return parsed.swagger !== undefined ? convertSwagger2OpenAPI(parsed) : parsed;
43
+ }
44
+
45
+ export function convertSwagger2OpenAPI(spec: any): Promise<OpenAPISpec> {
46
+ console.warn(
47
+ "[ReDoc Compatibility mode]: Converting OpenAPI 2.0 to OpenAPI 3.0"
48
+ );
49
+ return new Promise<OpenAPISpec>((resolve, reject) =>
50
+ convertObj(
51
+ spec,
52
+ { patch: true, warnOnly: true, text: "{}", anchors: true },
53
+ (err, res) => {
54
+ // TODO: log any warnings
55
+ if (err) {
56
+ return reject(err);
57
+ }
58
+ resolve(res && (res.openapi as any));
59
+ }
60
+ )
61
+ );
62
+ }
@@ -0,0 +1,303 @@
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
+ type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
9
+
10
+ export interface OpenAPISpec {
11
+ openapi: string;
12
+ info: OpenAPIInfo;
13
+ servers?: OpenAPIServer[];
14
+ paths: OpenAPIPaths;
15
+ components?: OpenAPIComponents;
16
+ security?: OpenAPISecurityRequirement[];
17
+ tags?: OpenAPITag[];
18
+ externalDocs?: OpenAPIExternalDocumentation;
19
+ "x-webhooks"?: OpenAPIPaths;
20
+ webhooks?: OpenAPIPaths;
21
+ }
22
+
23
+ export interface OpenAPIInfo {
24
+ title: string;
25
+ version: string;
26
+
27
+ description?: string;
28
+ summary?: string;
29
+ termsOfService?: string;
30
+ contact?: OpenAPIContact;
31
+ license?: OpenAPILicense;
32
+ }
33
+
34
+ export interface OpenAPIServer {
35
+ url: string;
36
+ description?: string;
37
+ variables?: { [name: string]: OpenAPIServerVariable };
38
+ }
39
+
40
+ export interface OpenAPIServerVariable {
41
+ enum?: string[];
42
+ default: string;
43
+ description?: string;
44
+ }
45
+
46
+ export interface OpenAPIPaths {
47
+ [path: string]: OpenAPIPath;
48
+ }
49
+ export interface OpenAPIRef {
50
+ $ref: string;
51
+ }
52
+
53
+ export type Referenced<T> = OpenAPIRef | T;
54
+
55
+ export interface OpenAPIPath {
56
+ summary?: string;
57
+ description?: string;
58
+ get?: OpenAPIOperation;
59
+ put?: OpenAPIOperation;
60
+ post?: OpenAPIOperation;
61
+ delete?: OpenAPIOperation;
62
+ options?: OpenAPIOperation;
63
+ head?: OpenAPIOperation;
64
+ patch?: OpenAPIOperation;
65
+ trace?: OpenAPIOperation;
66
+ servers?: OpenAPIServer[];
67
+ parameters?: Array<Referenced<OpenAPIParameter>>;
68
+ $ref?: string;
69
+ }
70
+
71
+ export interface OpenAPIXCodeSample {
72
+ lang: string;
73
+ label?: string;
74
+ source: string;
75
+ }
76
+
77
+ export interface OpenAPIOperation {
78
+ tags?: string[];
79
+ summary?: string;
80
+ description?: string;
81
+ externalDocs?: OpenAPIExternalDocumentation;
82
+ operationId?: string;
83
+ parameters?: Array<Referenced<OpenAPIParameter>>;
84
+ requestBody?: Referenced<OpenAPIRequestBody>;
85
+ responses: OpenAPIResponses;
86
+ callbacks?: { [name: string]: Referenced<OpenAPICallback> };
87
+ deprecated?: boolean;
88
+ security?: OpenAPISecurityRequirement[];
89
+ servers?: OpenAPIServer[];
90
+ "x-codeSamples"?: OpenAPIXCodeSample[];
91
+ "x-code-samples"?: OpenAPIXCodeSample[]; // deprecated
92
+ }
93
+
94
+ export interface OpenAPIParameter {
95
+ name: string;
96
+ in?: OpenAPIParameterLocation;
97
+ description?: string;
98
+ required?: boolean;
99
+ deprecated?: boolean;
100
+ allowEmptyValue?: boolean;
101
+ style?: OpenAPIParameterStyle;
102
+ explode?: boolean;
103
+ allowReserved?: boolean;
104
+ schema?: Referenced<OpenAPISchema>;
105
+ example?: any;
106
+ examples?: { [media: string]: Referenced<OpenAPIExample> };
107
+ content?: { [media: string]: OpenAPIMediaType };
108
+ encoding?: Record<string, OpenAPIEncoding>;
109
+ const?: any;
110
+ }
111
+
112
+ export interface OpenAPIExample {
113
+ value: any;
114
+ summary?: string;
115
+ description?: string;
116
+ externalValue?: string;
117
+ }
118
+
119
+ export interface OpenAPISchema {
120
+ $ref?: string;
121
+ type?: string | string[];
122
+ properties?: { [name: string]: OpenAPISchema };
123
+ patternProperties?: { [name: string]: OpenAPISchema };
124
+ additionalProperties?: boolean | OpenAPISchema;
125
+ unevaluatedProperties?: boolean | OpenAPISchema;
126
+ description?: string;
127
+ default?: any;
128
+ items?: OpenAPISchema | OpenAPISchema[] | boolean;
129
+ required?: string[];
130
+ readOnly?: boolean;
131
+ writeOnly?: boolean;
132
+ deprecated?: boolean;
133
+ format?: string;
134
+ externalDocs?: OpenAPIExternalDocumentation;
135
+ discriminator?: OpenAPIDiscriminator;
136
+ nullable?: boolean;
137
+ oneOf?: OpenAPISchema[];
138
+ anyOf?: OpenAPISchema[];
139
+ allOf?: OpenAPISchema[];
140
+ not?: OpenAPISchema;
141
+
142
+ title?: string;
143
+ multipleOf?: number;
144
+ maximum?: number;
145
+ exclusiveMaximum?: boolean | number;
146
+ minimum?: number;
147
+ exclusiveMinimum?: boolean | number;
148
+ maxLength?: number;
149
+ minLength?: number;
150
+ pattern?: string;
151
+ maxItems?: number;
152
+ minItems?: number;
153
+ uniqueItems?: boolean;
154
+ maxProperties?: number;
155
+ minProperties?: number;
156
+ enum?: any[];
157
+ example?: any;
158
+
159
+ if?: OpenAPISchema;
160
+ else?: OpenAPISchema;
161
+ then?: OpenAPISchema;
162
+ examples?: any[];
163
+ const?: string;
164
+ contentEncoding?: string;
165
+ contentMediaType?: string;
166
+ prefixItems?: OpenAPISchema[];
167
+ additionalItems?: OpenAPISchema | boolean;
168
+ }
169
+
170
+ export interface OpenAPIDiscriminator {
171
+ propertyName: string;
172
+ mapping?: { [name: string]: string };
173
+ "x-explicitMappingOnly"?: boolean;
174
+ }
175
+
176
+ export interface OpenAPIMediaType {
177
+ schema?: Referenced<OpenAPISchema>;
178
+ example?: any;
179
+ examples?: { [name: string]: Referenced<OpenAPIExample> };
180
+ encoding?: { [field: string]: OpenAPIEncoding };
181
+ }
182
+
183
+ export interface OpenAPIEncoding {
184
+ contentType: string;
185
+ headers?: { [name: string]: Referenced<OpenAPIHeader> };
186
+ style: OpenAPIParameterStyle;
187
+ explode: boolean;
188
+ allowReserved: boolean;
189
+ }
190
+
191
+ export type OpenAPIParameterLocation = "query" | "header" | "path" | "cookie";
192
+ export type OpenAPIParameterStyle =
193
+ | "matrix"
194
+ | "label"
195
+ | "form"
196
+ | "simple"
197
+ | "spaceDelimited"
198
+ | "pipeDelimited"
199
+ | "deepObject";
200
+
201
+ export interface OpenAPIRequestBody {
202
+ description?: string;
203
+ required?: boolean;
204
+ content: { [mime: string]: OpenAPIMediaType };
205
+
206
+ "x-examples"?: {
207
+ [mime: string]: { [name: string]: Referenced<OpenAPIExample> };
208
+ };
209
+ "x-example"?: { [mime: string]: any };
210
+ }
211
+
212
+ export interface OpenAPIResponses {
213
+ [code: string]: Referenced<OpenAPIResponse>;
214
+ }
215
+
216
+ export interface OpenAPIResponse
217
+ extends Pick<OpenAPIRequestBody, "description" | "x-examples" | "x-example"> {
218
+ headers?: { [name: string]: Referenced<OpenAPIHeader> };
219
+ links?: { [name: string]: Referenced<OpenAPILink> };
220
+ content?: { [mime: string]: OpenAPIMediaType };
221
+ }
222
+
223
+ export interface OpenAPILink {
224
+ $ref?: string;
225
+ }
226
+
227
+ export type OpenAPIHeader = Omit<OpenAPIParameter, "in" | "name">;
228
+
229
+ export interface OpenAPICallback {
230
+ [name: string]: OpenAPIPath;
231
+ }
232
+
233
+ export interface OpenAPIComponents {
234
+ schemas?: { [name: string]: Referenced<OpenAPISchema> };
235
+ responses?: { [name: string]: Referenced<OpenAPIResponse> };
236
+ parameters?: { [name: string]: Referenced<OpenAPIParameter> };
237
+ examples?: { [name: string]: Referenced<OpenAPIExample> };
238
+ requestBodies?: { [name: string]: Referenced<OpenAPIRequestBody> };
239
+ headers?: { [name: string]: Referenced<OpenAPIHeader> };
240
+ securitySchemes?: { [name: string]: Referenced<OpenAPISecurityScheme> };
241
+ links?: { [name: string]: Referenced<OpenAPILink> };
242
+ callbacks?: { [name: string]: Referenced<OpenAPICallback> };
243
+ }
244
+
245
+ export interface OpenAPISecurityRequirement {
246
+ [name: string]: string[];
247
+ }
248
+
249
+ export interface OpenAPISecurityScheme {
250
+ type: "apiKey" | "http" | "oauth2" | "openIdConnect";
251
+ description?: string;
252
+ name?: string;
253
+ in?: "query" | "header" | "cookie";
254
+ scheme?: string;
255
+ bearerFormat: string;
256
+ flows: {
257
+ implicit?: {
258
+ refreshUrl?: string;
259
+ scopes: Record<string, string>;
260
+ authorizationUrl: string;
261
+ };
262
+ password?: {
263
+ refreshUrl?: string;
264
+ scopes: Record<string, string>;
265
+ tokenUrl: string;
266
+ };
267
+ clientCredentials?: {
268
+ refreshUrl?: string;
269
+ scopes: Record<string, string>;
270
+ tokenUrl: string;
271
+ };
272
+ authorizationCode?: {
273
+ refreshUrl?: string;
274
+ scopes: Record<string, string>;
275
+ tokenUrl: string;
276
+ };
277
+ };
278
+ openIdConnectUrl?: string;
279
+ }
280
+
281
+ export interface OpenAPITag {
282
+ name: string;
283
+ description?: string;
284
+ externalDocs?: OpenAPIExternalDocumentation;
285
+ "x-displayName"?: string;
286
+ }
287
+
288
+ export interface OpenAPIExternalDocumentation {
289
+ description?: string;
290
+ url: string;
291
+ }
292
+
293
+ export interface OpenAPIContact {
294
+ name?: string;
295
+ url?: string;
296
+ email?: string;
297
+ }
298
+
299
+ export interface OpenAPILicense {
300
+ name: string;
301
+ url?: string;
302
+ identifier?: string;
303
+ }
@@ -1,4 +0,0 @@
1
- {
2
- "label": "Foods",
3
- "collapsed": true
4
- }
@@ -1,13 +0,0 @@
1
- openapi: 3.0.3
2
- info:
3
- title: Frozen Yogurt Example
4
- version: 1.0.0
5
- description: Sample description.
6
- paths:
7
- /flavors:
8
- get:
9
- summary: List All Flavors
10
- description: Froyo's the best!
11
- responses:
12
- 200:
13
- description: OK
@@ -1,13 +0,0 @@
1
- openapi: 3.0.3
2
- info:
3
- title: Nested Folder Example
4
- version: 1.0.0
5
- description: Sample description.
6
- paths:
7
- /flavors:
8
- get:
9
- summary: List All
10
- description: All
11
- responses:
12
- 200:
13
- description: OK