docusaurus-plugin-openapi-docs 4.5.0 → 4.6.0

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.
@@ -0,0 +1,48 @@
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
+ const createSchemaExample_1 = require("./createSchemaExample");
10
+ describe("sampleFromSchema", () => {
11
+ describe("const support", () => {
12
+ it("should return default string value when const is not present", () => {
13
+ const schema = {
14
+ type: "string",
15
+ };
16
+ const context = { type: "request" };
17
+ const result = (0, createSchemaExample_1.sampleFromSchema)(schema, context);
18
+ expect(result).toBe("string");
19
+ });
20
+ it("should return const value when const is present", () => {
21
+ const schema = {
22
+ type: "string",
23
+ const: "example",
24
+ };
25
+ const context = { type: "request" };
26
+ const result = (0, createSchemaExample_1.sampleFromSchema)(schema, context);
27
+ expect(result).toBe("example");
28
+ });
29
+ it("should handle anyOf with const values", () => {
30
+ const schema = {
31
+ type: "string",
32
+ anyOf: [
33
+ {
34
+ type: "string",
35
+ const: "dog",
36
+ },
37
+ {
38
+ type: "string",
39
+ const: "cat",
40
+ },
41
+ ],
42
+ };
43
+ const context = { type: "request" };
44
+ const result = (0, createSchemaExample_1.sampleFromSchema)(schema, context);
45
+ expect(result).toBe("dog");
46
+ });
47
+ });
48
+ });
@@ -65,11 +65,16 @@ const loadAndResolveSpec_1 = require("./utils/loadAndResolveSpec");
65
65
  */
66
66
  function jsonToCollection(data) {
67
67
  return new Promise((resolve, reject) => {
68
+ var _a, _b;
68
69
  let schemaPack = new openapi_to_postmanv2_1.default.SchemaPack({ type: "json", data }, { schemaFaker: false });
69
70
  schemaPack.computedOptions.schemaFaker = false;
71
+ // Make sure the schema was properly validated or reject with error
72
+ if (!((_a = schemaPack.validationResult) === null || _a === void 0 ? void 0 : _a.result)) {
73
+ return reject((_b = schemaPack.validationResult) === null || _b === void 0 ? void 0 : _b.reason);
74
+ }
70
75
  schemaPack.convert((_err, conversionResult) => {
71
- if (!conversionResult.result) {
72
- return reject(conversionResult.reason);
76
+ if (_err || !conversionResult.result) {
77
+ return reject(_err || conversionResult.reason);
73
78
  }
74
79
  return resolve(new sdk.Collection(conversionResult.output[0].data));
75
80
  });
@@ -98,13 +103,15 @@ async function createPostmanCollection(openapiData) {
98
103
  return await jsonToCollection(data);
99
104
  }
100
105
  function createItems(openapiData, options, sidebarOptions) {
101
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7;
106
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8;
102
107
  // TODO: Find a better way to handle this
103
108
  let items = [];
104
109
  const infoIdSpaces = openapiData.info.title.replace(" ", "-").toLowerCase();
105
110
  const infoId = (0, kebabCase_1.default)(infoIdSpaces);
106
- if (openapiData.info.description || openapiData.info.title) {
107
- // Only create an info page if we have a description.
111
+ const schemasOnly = (options === null || options === void 0 ? void 0 : options.schemasOnly) === true;
112
+ // Only create an info page if we have a description/title AND showInfoPage is not false
113
+ if ((openapiData.info.description || openapiData.info.title) &&
114
+ (options === null || options === void 0 ? void 0 : options.showInfoPage) !== false) {
108
115
  const infoDescription = (_a = openapiData.info) === null || _a === void 0 ? void 0 : _a.description;
109
116
  let splitDescription;
110
117
  if (infoDescription) {
@@ -231,6 +238,9 @@ function createItems(openapiData, options, sidebarOptions) {
231
238
  ...((options === null || options === void 0 ? void 0 : options.showExtensions) && {
232
239
  show_extensions: options.showExtensions,
233
240
  }),
241
+ ...((options === null || options === void 0 ? void 0 : options.maskCredentials) === false && {
242
+ mask_credentials_disabled: true,
243
+ }),
234
244
  },
235
245
  api: {
236
246
  ...defaults,
@@ -251,10 +261,15 @@ function createItems(openapiData, options, sidebarOptions) {
251
261
  }
252
262
  // Gather x-webhooks endpoints
253
263
  for (let [path, pathObject] of Object.entries((_q = (_p = openapiData["x-webhooks"]) !== null && _p !== void 0 ? _p : openapiData["webhooks"]) !== null && _q !== void 0 ? _q : {})) {
264
+ const eventName = path;
254
265
  path = "webhook";
255
266
  const { $ref, description, parameters, servers, summary, ...rest } = pathObject;
256
267
  for (let [method, operationObject] of Object.entries({ ...rest })) {
257
268
  method = "event";
269
+ if (operationObject.summary === undefined &&
270
+ operationObject.operationId === undefined) {
271
+ operationObject.summary = eventName;
272
+ }
258
273
  const title = (_s = (_r = operationObject.summary) !== null && _r !== void 0 ? _r : operationObject.operationId) !== null && _s !== void 0 ? _s : "Missing summary";
259
274
  if (operationObject.description === undefined) {
260
275
  operationObject.description =
@@ -262,7 +277,7 @@ function createItems(openapiData, options, sidebarOptions) {
262
277
  }
263
278
  const baseId = operationObject.operationId
264
279
  ? (0, kebabCase_1.default)(operationObject.operationId)
265
- : (0, kebabCase_1.default)(operationObject.summary);
280
+ : (0, kebabCase_1.default)((_v = operationObject.summary) !== null && _v !== void 0 ? _v : eventName);
266
281
  const extensions = [];
267
282
  const commonExtensions = ["x-codeSamples"];
268
283
  for (const [key, value] of Object.entries(operationObject)) {
@@ -270,10 +285,10 @@ function createItems(openapiData, options, sidebarOptions) {
270
285
  extensions.push({ key, value });
271
286
  }
272
287
  }
273
- const servers = (_w = (_v = operationObject.servers) !== null && _v !== void 0 ? _v : pathObject.servers) !== null && _w !== void 0 ? _w : openapiData.servers;
274
- const security = (_x = operationObject.security) !== null && _x !== void 0 ? _x : openapiData.security;
288
+ const servers = (_x = (_w = operationObject.servers) !== null && _w !== void 0 ? _w : pathObject.servers) !== null && _x !== void 0 ? _x : openapiData.servers;
289
+ const security = (_y = operationObject.security) !== null && _y !== void 0 ? _y : openapiData.security;
275
290
  // Add security schemes so we know how to handle security.
276
- const securitySchemes = (_y = openapiData.components) === null || _y === void 0 ? void 0 : _y.securitySchemes;
291
+ const securitySchemes = (_z = openapiData.components) === null || _z === void 0 ? void 0 : _z.securitySchemes;
277
292
  // Make sure schemes are lowercase. See: https://github.com/cloud-annotations/docusaurus-plugin-openapi/issues/79
278
293
  if (securitySchemes) {
279
294
  for (let securityScheme of Object.values(securitySchemes)) {
@@ -283,7 +298,7 @@ function createItems(openapiData, options, sidebarOptions) {
283
298
  }
284
299
  }
285
300
  let jsonRequestBodyExample;
286
- const content = (_z = operationObject.requestBody) === null || _z === void 0 ? void 0 : _z.content;
301
+ const content = (_0 = operationObject.requestBody) === null || _0 === void 0 ? void 0 : _0.content;
287
302
  let body;
288
303
  for (let key in content) {
289
304
  if (key.toLowerCase() === "application/json" ||
@@ -296,7 +311,7 @@ function createItems(openapiData, options, sidebarOptions) {
296
311
  jsonRequestBodyExample = (0, createRequestExample_1.sampleRequestFromSchema)(body.schema);
297
312
  }
298
313
  // Handle vendor JSON media types
299
- const bodyContent = (_0 = operationObject.requestBody) === null || _0 === void 0 ? void 0 : _0.content;
314
+ const bodyContent = (_1 = operationObject.requestBody) === null || _1 === void 0 ? void 0 : _1.content;
300
315
  if (bodyContent) {
301
316
  const firstBodyContentKey = Object.keys(bodyContent)[0];
302
317
  if (firstBodyContentKey.endsWith("+json")) {
@@ -345,6 +360,9 @@ function createItems(openapiData, options, sidebarOptions) {
345
360
  ...((options === null || options === void 0 ? void 0 : options.showExtensions) && {
346
361
  show_extensions: options.showExtensions,
347
362
  }),
363
+ ...((options === null || options === void 0 ? void 0 : options.maskCredentials) === false && {
364
+ mask_credentials_disabled: true,
365
+ }),
348
366
  },
349
367
  api: {
350
368
  ...defaults,
@@ -362,14 +380,17 @@ function createItems(openapiData, options, sidebarOptions) {
362
380
  items.push(apiPage);
363
381
  }
364
382
  }
365
- if ((options === null || options === void 0 ? void 0 : options.showSchemas) === true ||
366
- Object.entries((_2 = (_1 = openapiData === null || openapiData === void 0 ? void 0 : openapiData.components) === null || _1 === void 0 ? void 0 : _1.schemas) !== null && _2 !== void 0 ? _2 : {})
383
+ if (schemasOnly ||
384
+ (options === null || options === void 0 ? void 0 : options.showSchemas) === true ||
385
+ Object.entries((_3 = (_2 = openapiData === null || openapiData === void 0 ? void 0 : openapiData.components) === null || _2 === void 0 ? void 0 : _2.schemas) !== null && _3 !== void 0 ? _3 : {})
367
386
  .flatMap(([_, s]) => s["x-tags"])
368
387
  .filter((item) => !!item).length > 0) {
369
388
  // Gather schemas
370
- for (let [schema, schemaObject] of Object.entries((_4 = (_3 = openapiData === null || openapiData === void 0 ? void 0 : openapiData.components) === null || _3 === void 0 ? void 0 : _3.schemas) !== null && _4 !== void 0 ? _4 : {})) {
371
- if ((options === null || options === void 0 ? void 0 : options.showSchemas) === true || schemaObject["x-tags"]) {
372
- const baseIdSpaces = (_6 = (_5 = schemaObject === null || schemaObject === void 0 ? void 0 : schemaObject.title) === null || _5 === void 0 ? void 0 : _5.replace(" ", "-").toLowerCase()) !== null && _6 !== void 0 ? _6 : "";
389
+ for (let [schema, schemaObject] of Object.entries((_5 = (_4 = openapiData === null || openapiData === void 0 ? void 0 : openapiData.components) === null || _4 === void 0 ? void 0 : _4.schemas) !== null && _5 !== void 0 ? _5 : {})) {
390
+ if (schemasOnly ||
391
+ (options === null || options === void 0 ? void 0 : options.showSchemas) === true ||
392
+ schemaObject["x-tags"]) {
393
+ const baseIdSpaces = (_7 = (_6 = schemaObject === null || schemaObject === void 0 ? void 0 : schemaObject.title) === null || _6 === void 0 ? void 0 : _6.replace(" ", "-").toLowerCase()) !== null && _7 !== void 0 ? _7 : "";
373
394
  const baseId = (0, kebabCase_1.default)(baseIdSpaces);
374
395
  const schemaDescription = schemaObject.description;
375
396
  let splitDescription;
@@ -403,7 +424,7 @@ function createItems(openapiData, options, sidebarOptions) {
403
424
  }
404
425
  if ((sidebarOptions === null || sidebarOptions === void 0 ? void 0 : sidebarOptions.categoryLinkSource) === "tag") {
405
426
  // Get global tags
406
- const tags = (_7 = openapiData.tags) !== null && _7 !== void 0 ? _7 : [];
427
+ const tags = (_8 = openapiData.tags) !== null && _8 !== void 0 ? _8 : [];
407
428
  // Get operation tags
408
429
  const apiItems = items.filter((item) => {
409
430
  return item.type === "api";
@@ -13,6 +13,7 @@ const path_1 = __importDefault(require("path"));
13
13
  // eslint-disable-next-line import/no-extraneous-dependencies
14
14
  const utils_1 = require("@docusaurus/utils");
15
15
  const _1 = require(".");
16
+ const openapi_1 = require("./openapi");
16
17
  // npx jest packages/docusaurus-plugin-openapi/src/openapi/openapi.test.ts --watch
17
18
  describe("openapi", () => {
18
19
  describe("readOpenapiFiles", () => {
@@ -30,4 +31,51 @@ describe("openapi", () => {
30
31
  expect((_b = (_a = yaml === null || yaml === void 0 ? void 0 : yaml.data.components) === null || _a === void 0 ? void 0 : _a.schemas) === null || _b === void 0 ? void 0 : _b.HelloString["x-tags"]).toBeDefined();
31
32
  });
32
33
  });
34
+ describe("schemasOnly", () => {
35
+ it("includes schema metadata when showSchemas is disabled", async () => {
36
+ const openapiData = {
37
+ openapi: "3.0.0",
38
+ info: {
39
+ title: "Schema Only",
40
+ version: "1.0.0",
41
+ },
42
+ paths: {
43
+ "/ping": {
44
+ get: {
45
+ summary: "Ping",
46
+ responses: {
47
+ "200": {
48
+ description: "OK",
49
+ },
50
+ },
51
+ },
52
+ },
53
+ },
54
+ components: {
55
+ schemas: {
56
+ WithoutTags: {
57
+ title: "Without Tags",
58
+ type: "object",
59
+ properties: {
60
+ value: {
61
+ type: "string",
62
+ },
63
+ },
64
+ },
65
+ },
66
+ },
67
+ };
68
+ const options = {
69
+ specPath: "dummy", // required by the type but unused in this context
70
+ outputDir: "build",
71
+ showSchemas: false,
72
+ schemasOnly: true,
73
+ };
74
+ const sidebarOptions = {};
75
+ const [items] = await (0, openapi_1.processOpenapiFile)(openapiData, options, sidebarOptions);
76
+ const schemaItems = items.filter((item) => item.type === "schema");
77
+ expect(schemaItems).toHaveLength(1);
78
+ expect(schemaItems[0].id).toBe("without-tags");
79
+ });
80
+ });
33
81
  });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ /* ============================================================================
3
+ * Copyright (c) Palo Alto Networks
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ * ========================================================================== */
8
+ var __importDefault = (this && this.__importDefault) || function (mod) {
9
+ return (mod && mod.__esModule) ? mod : { "default": mod };
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const path_1 = __importDefault(require("path"));
13
+ // eslint-disable-next-line import/no-extraneous-dependencies
14
+ const utils_1 = require("@docusaurus/utils");
15
+ const _1 = require(".");
16
+ describe("webhooks", () => {
17
+ it("uses event name when summary and operationId are missing", async () => {
18
+ const files = await (0, _1.readOpenapiFiles)((0, utils_1.posixPath)(path_1.default.join(__dirname, "__fixtures__/webhook/openapi.yaml")));
19
+ const [items] = await (0, _1.processOpenapiFiles)(files, { specPath: "", outputDir: "" }, {});
20
+ const webhookItem = items.find((item) => item.type === "api");
21
+ expect(webhookItem === null || webhookItem === void 0 ? void 0 : webhookItem.id).toBe("order-created");
22
+ });
23
+ });
package/lib/options.js CHANGED
@@ -44,7 +44,10 @@ exports.OptionsSchema = utils_validation_1.Joi.object({
44
44
  sidebarOptions: sidebarOptions,
45
45
  markdownGenerators: markdownGenerators,
46
46
  showSchemas: utils_validation_1.Joi.boolean(),
47
+ showInfoPage: utils_validation_1.Joi.boolean(),
48
+ schemasOnly: utils_validation_1.Joi.boolean(),
47
49
  disableCompression: utils_validation_1.Joi.boolean(),
50
+ maskCredentials: utils_validation_1.Joi.boolean(),
48
51
  version: utils_validation_1.Joi.string().when("versions", {
49
52
  is: utils_validation_1.Joi.exist(),
50
53
  then: utils_validation_1.Joi.required(),
@@ -81,9 +81,18 @@ function groupByTags(items, sidebarOptions, options, tags, docPath) {
81
81
  if (sidebarOptions.groupPathsBy !== "tagGroup") {
82
82
  apiTags = (0, uniq_1.default)(apiTags.concat(operationTags, schemaTags));
83
83
  }
84
- const basePath = docPath
85
- ? outputDir.split(docPath)[1].replace(/^\/+/g, "")
86
- : outputDir.slice(outputDir.indexOf("/", 1)).replace(/^\/+/g, "");
84
+ // Extract base path from outputDir, handling cases where docPath may not be in outputDir
85
+ const getBasePathFromOutput = (output, doc) => {
86
+ var _a, _b;
87
+ if (doc && output.includes(doc)) {
88
+ return (_b = (_a = output.split(doc)[1]) === null || _a === void 0 ? void 0 : _a.replace(/^\/+/g, "")) !== null && _b !== void 0 ? _b : "";
89
+ }
90
+ const slashIndex = output.indexOf("/", 1);
91
+ return slashIndex === -1
92
+ ? ""
93
+ : output.slice(slashIndex).replace(/^\/+/g, "");
94
+ };
95
+ const basePath = getBasePathFromOutput(outputDir, docPath);
87
96
  const createDocItemFnContext = {
88
97
  sidebarOptions,
89
98
  basePath,
package/lib/types.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { SidebarItemDoc } from "@docusaurus/plugin-content-docs/src/sidebars/types";
1
+ import type { SidebarItemDoc } from "@docusaurus/plugin-content-docs/lib/sidebars/types";
2
2
  import { InfoObject, OperationObject, SchemaObject, SecuritySchemeObject, TagObject } from "./openapi/types";
3
- export type { PropSidebarItemCategory, SidebarItemLink, PropSidebar, PropSidebarItem, } from "@docusaurus/plugin-content-docs-types";
3
+ export type { PropSidebarItemCategory, SidebarItemLink, PropSidebar, PropSidebarItem, } from "@docusaurus/plugin-content-docs/lib/sidebars/types";
4
4
  export interface PluginOptions {
5
5
  id?: string;
6
6
  docsPlugin?: string;
@@ -29,7 +29,10 @@ export interface APIOptions {
29
29
  proxy?: string;
30
30
  markdownGenerators?: MarkdownGenerator;
31
31
  showSchemas?: boolean;
32
+ showInfoPage?: boolean;
33
+ schemasOnly?: boolean;
32
34
  disableCompression?: boolean;
35
+ maskCredentials?: boolean;
33
36
  }
34
37
  export interface MarkdownGenerator {
35
38
  createApiPageMD?: (pageData: ApiPageMetadata) => string;
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": "4.5.0",
4
+ "version": "4.6.0",
5
5
  "license": "MIT",
6
6
  "keywords": [
7
7
  "openapi",
@@ -32,29 +32,29 @@
32
32
  "@docusaurus/types": "^3.5.0",
33
33
  "@docusaurus/utils": "^3.5.0",
34
34
  "@docusaurus/utils-validation": "^3.5.0",
35
- "@types/fs-extra": "^9.0.13",
36
- "@types/json-pointer": "^1.0.31",
37
- "@types/json-schema": "^7.0.9",
38
- "@types/lodash": "^4.14.176",
39
- "@types/mustache": "^4.1.2",
40
- "eslint-plugin-prettier": "^5.0.1"
35
+ "@types/fs-extra": "^11.0.4",
36
+ "@types/json-pointer": "^1.0.34",
37
+ "@types/json-schema": "^7.0.15",
38
+ "@types/lodash": "^4.17.20",
39
+ "@types/mustache": "^4.2.6",
40
+ "eslint-plugin-prettier": "^5.5.1"
41
41
  },
42
42
  "dependencies": {
43
43
  "@apidevtools/json-schema-ref-parser": "^11.5.4",
44
- "@redocly/openapi-core": "^1.10.5",
44
+ "@redocly/openapi-core": "^1.34.3",
45
45
  "allof-merge": "^0.6.6",
46
46
  "chalk": "^4.1.2",
47
- "clsx": "^1.1.1",
48
- "fs-extra": "^9.0.1",
47
+ "clsx": "^2.1.1",
48
+ "fs-extra": "^11.3.0",
49
49
  "json-pointer": "^0.6.2",
50
50
  "json5": "^2.2.3",
51
- "lodash": "^4.17.20",
51
+ "lodash": "^4.17.21",
52
52
  "mustache": "^4.2.0",
53
- "openapi-to-postmanv2": "^4.21.0",
54
- "postman-collection": "^4.4.0",
55
- "slugify": "^1.6.5",
53
+ "openapi-to-postmanv2": "^5.0.0",
54
+ "postman-collection": "^5.0.2",
55
+ "slugify": "^1.6.6",
56
56
  "swagger2openapi": "^7.0.8",
57
- "xml-formatter": "^2.6.1"
57
+ "xml-formatter": "^3.6.6"
58
58
  },
59
59
  "peerDependencies": {
60
60
  "@docusaurus/plugin-content-docs": "^3.5.0",
@@ -65,5 +65,5 @@
65
65
  "engines": {
66
66
  "node": ">=14"
67
67
  },
68
- "gitHead": "cb0ed6e02d7a963d51073e2bfd78d944fbb7ee34"
68
+ "gitHead": "c7c5ca9934031d166faa0740746ef1a39e72982d"
69
69
  }