docusaurus-plugin-openapi-docs 1.5.0 → 1.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.
@@ -13,7 +13,9 @@ function createDescription(description) {
13
13
  return "";
14
14
  }
15
15
  return `\n\n${description
16
- .replace(utils_1.lessThan, "<")
17
- .replace(utils_1.greaterThan, ">")}\n\n`;
16
+ .replace(utils_1.greaterThan, "\\>")
17
+ .replace(utils_1.codeFence, function (match) {
18
+ return match.replace(/\\>/g, ">");
19
+ })}\n\n`;
18
20
  }
19
21
  exports.createDescription = createDescription;
@@ -28,8 +28,8 @@ function mergeAllOf(allOf) {
28
28
  "x-examples": function () {
29
29
  return true;
30
30
  },
31
- ignoreAdditionalProperties: true,
32
31
  },
32
+ ignoreAdditionalProperties: true,
33
33
  });
34
34
  const required = allOf.reduce((acc, cur) => {
35
35
  if (Array.isArray(cur.required)) {
@@ -7,3 +7,4 @@ export declare function guard<T>(value: T | undefined, cb: (value: T) => Childre
7
7
  export declare function render(children: Children): string;
8
8
  export declare const lessThan: RegExp;
9
9
  export declare const greaterThan: RegExp;
10
+ export declare const codeFence: RegExp;
@@ -6,7 +6,7 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  * ========================================================================== */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.greaterThan = exports.lessThan = exports.render = exports.guard = exports.create = void 0;
9
+ exports.codeFence = exports.greaterThan = exports.lessThan = exports.render = exports.guard = exports.create = void 0;
10
10
  function create(tag, props) {
11
11
  const { children, ...rest } = props;
12
12
  let propString = "";
@@ -35,5 +35,6 @@ function render(children) {
35
35
  }
36
36
  exports.render = render;
37
37
  // Regex to selectively URL-encode '>' and '<' chars
38
- exports.lessThan = /<(?!(=|button|\s?\/button|details|\s?\/details|summary|\s?\/summary|hr|\s?\/hr|br|\s?\/br|span|\s?\/span|strong|\s?\/strong|small|\s?\/small|table|\s?\/table|td|\s?\/td|tr|\s?\/tr|th|\s?\/th|h1|\s?\/h1|h2|\s?\/h2|h3|\s?\/h3|h4|\s?\/h4|h5|\s?\/h5|h6|\s?\/h6|title|\s?\/title|p|\s?\/p|em|\s?\/em|b|\s?\/b|i|\s?\/i|u|\s?\/u|strike|\s?\/strike|a|\s?\/a|li|\s?\/li|ol|\s?\/ol|ul|\s?\/ul|img|\s?\/img|div|\s?\/div|center|\s?\/center))/giu;
39
- exports.greaterThan = /(?<!(button|details|summary|hr|br|span|strong|small|table|td|tr|th|h1|h2|h3|h4|h5|h6|title|p|em|b|i|u|strike|a|tag|li|ol|ul|img|div|center|\/|\s|"|'))>?!=/giu;
38
+ exports.lessThan = /<(?!(=|button|\s?\/button|details|\s?\/details|summary|\s?\/summary|hr|\s?\/hr|br|\s?\/br|span|\s?\/span|strong|\s?\/strong|small|\s?\/small|table|\s?\/table|td|\s?\/td|tr|\s?\/tr|th|\s?\/th|h1|\s?\/h1|h2|\s?\/h2|h3|\s?\/h3|h4|\s?\/h4|h5|\s?\/h5|h6|\s?\/h6|title|\s?\/title|p|\s?\/p|em|\s?\/em|b|\s?\/b|i|\s?\/i|u|\s?\/u|strike|\s?\/strike|a|\s?\/a|li|\s?\/li|ol|\s?\/ol|ul|\s?\/ul|img|\s?\/img|div|\s?\/div|center|\s?\/center))/gu;
39
+ exports.greaterThan = /(?<!(button|details|summary|hr|br|span|strong|small|table|td|tr|th|h1|h2|h3|h4|h5|h6|title|p|em|b|i|u|strike|a|tag|li|ol|ul|img|div|center|\/|\s|"|'))>/gu;
40
+ exports.codeFence = /`{1,3}[\s\S]*?`{1,3}/g;
@@ -61,7 +61,7 @@ async function createPostmanCollection(openapiData) {
61
61
  return await jsonToCollection(data);
62
62
  }
63
63
  function createItems(openapiData, options, sidebarOptions) {
64
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
64
+ 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;
65
65
  // TODO: Find a better way to handle this
66
66
  let items = [];
67
67
  const infoIdSpaces = openapiData.info.title.replace(" ", "-").toLowerCase();
@@ -189,9 +189,100 @@ function createItems(openapiData, options, sidebarOptions) {
189
189
  items.push(apiPage);
190
190
  }
191
191
  }
192
+ // Gather x-webhooks endpoints
193
+ for (let [path, pathObject] of Object.entries((_q = openapiData["x-webhooks"]) !== null && _q !== void 0 ? _q : {})) {
194
+ path = "webhook";
195
+ const { $ref, description, parameters, servers, summary, ...rest } = pathObject;
196
+ for (let [method, operationObject] of Object.entries({ ...rest })) {
197
+ method = "event";
198
+ const title = (_s = (_r = operationObject.summary) !== null && _r !== void 0 ? _r : operationObject.operationId) !== null && _s !== void 0 ? _s : "Missing summary";
199
+ if (operationObject.description === undefined) {
200
+ operationObject.description =
201
+ (_u = (_t = operationObject.summary) !== null && _t !== void 0 ? _t : operationObject.operationId) !== null && _u !== void 0 ? _u : "";
202
+ }
203
+ const baseId = operationObject.operationId
204
+ ? (0, kebabCase_1.default)(operationObject.operationId)
205
+ : (0, kebabCase_1.default)(operationObject.summary);
206
+ const servers = (_w = (_v = operationObject.servers) !== null && _v !== void 0 ? _v : pathObject.servers) !== null && _w !== void 0 ? _w : openapiData.servers;
207
+ const security = (_x = operationObject.security) !== null && _x !== void 0 ? _x : openapiData.security;
208
+ // Add security schemes so we know how to handle security.
209
+ const securitySchemes = (_y = openapiData.components) === null || _y === void 0 ? void 0 : _y.securitySchemes;
210
+ // Make sure schemes are lowercase. See: https://github.com/cloud-annotations/docusaurus-plugin-openapi/issues/79
211
+ if (securitySchemes) {
212
+ for (let securityScheme of Object.values(securitySchemes)) {
213
+ if (securityScheme.type === "http") {
214
+ securityScheme.scheme = securityScheme.scheme.toLowerCase();
215
+ }
216
+ }
217
+ }
218
+ let jsonRequestBodyExample;
219
+ const body = (_0 = (_z = operationObject.requestBody) === null || _z === void 0 ? void 0 : _z.content) === null || _0 === void 0 ? void 0 : _0["application/json"];
220
+ if (body === null || body === void 0 ? void 0 : body.schema) {
221
+ jsonRequestBodyExample = (0, createRequestExample_1.sampleRequestFromSchema)(body.schema);
222
+ }
223
+ // Handle vendor JSON media types
224
+ const bodyContent = (_1 = operationObject.requestBody) === null || _1 === void 0 ? void 0 : _1.content;
225
+ if (bodyContent) {
226
+ const firstBodyContentKey = Object.keys(bodyContent)[0];
227
+ if (firstBodyContentKey.endsWith("+json")) {
228
+ const firstBody = bodyContent[firstBodyContentKey];
229
+ if (firstBody === null || firstBody === void 0 ? void 0 : firstBody.schema) {
230
+ jsonRequestBodyExample = (0, createRequestExample_1.sampleRequestFromSchema)(firstBody.schema);
231
+ }
232
+ }
233
+ }
234
+ // TODO: Don't include summary temporarilly
235
+ const { summary, ...defaults } = operationObject;
236
+ // Merge common parameters with operation parameters
237
+ // Operation params take precendence over common params
238
+ if (parameters !== undefined) {
239
+ if (operationObject.parameters !== undefined) {
240
+ defaults.parameters = (0, unionBy_1.default)(operationObject.parameters, parameters, "name");
241
+ }
242
+ else {
243
+ defaults.parameters = parameters;
244
+ }
245
+ }
246
+ const opDescription = operationObject.description;
247
+ let splitDescription;
248
+ if (opDescription) {
249
+ splitDescription = opDescription.match(/[^\r\n]+/g);
250
+ }
251
+ const apiPage = {
252
+ type: "api",
253
+ id: baseId,
254
+ infoId: infoId !== null && infoId !== void 0 ? infoId : "",
255
+ unversionedId: baseId,
256
+ title: title ? title.replace(/((?:^|[^\\])(?:\\{2})*)"/g, "$1'") : "",
257
+ description: operationObject.description
258
+ ? operationObject.description.replace(/((?:^|[^\\])(?:\\{2})*)"/g, "$1'")
259
+ : "",
260
+ frontMatter: {
261
+ description: splitDescription
262
+ ? splitDescription[0]
263
+ .replace(/((?:^|[^\\])(?:\\{2})*)"/g, "$1'")
264
+ .replace(/\s+$/, "")
265
+ : "",
266
+ ...((options === null || options === void 0 ? void 0 : options.proxy) && { proxy: options.proxy }),
267
+ },
268
+ api: {
269
+ ...defaults,
270
+ tags: operationObject.tags,
271
+ method,
272
+ path,
273
+ servers,
274
+ security,
275
+ securitySchemes,
276
+ jsonRequestBodyExample,
277
+ info: openapiData.info,
278
+ },
279
+ };
280
+ items.push(apiPage);
281
+ }
282
+ }
192
283
  if ((sidebarOptions === null || sidebarOptions === void 0 ? void 0 : sidebarOptions.categoryLinkSource) === "tag") {
193
284
  // Get global tags
194
- const tags = (_q = openapiData.tags) !== null && _q !== void 0 ? _q : [];
285
+ const tags = (_2 = openapiData.tags) !== null && _2 !== void 0 ? _2 : [];
195
286
  // Get operation tags
196
287
  const apiItems = items.filter((item) => {
197
288
  return item.type === "api";
@@ -12,6 +12,7 @@ export interface OpenApiObject {
12
12
  tags?: TagObject[];
13
13
  externalDocs?: ExternalDocumentationObject;
14
14
  swagger?: string;
15
+ "x-webhooks"?: PathsObject;
15
16
  }
16
17
  export interface OpenApiObjectWithRef {
17
18
  openapi: string;
@@ -58,7 +58,13 @@ function serializer(replacer, cycleReplacer) {
58
58
  }
59
59
  function convertSwagger2OpenAPI(spec) {
60
60
  console.warn("[ReDoc Compatibility mode]: Converting OpenAPI 2.0 to OpenAPI 3.0");
61
- return new Promise((resolve, reject) => (0, swagger2openapi_1.convertObj)(spec, { patch: true, warnOnly: true, text: "{}", anchors: true }, (err, res) => {
61
+ return new Promise((resolve, reject) => (0, swagger2openapi_1.convertObj)(spec, {
62
+ patch: true,
63
+ warnOnly: true,
64
+ text: "{}",
65
+ anchors: true,
66
+ resolveInternal: true,
67
+ }, (err, res) => {
62
68
  // TODO: log any warnings
63
69
  if (err) {
64
70
  return reject(err);
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": "1.5.0",
4
+ "version": "1.5.1",
5
5
  "license": "MIT",
6
6
  "keywords": [
7
7
  "openapi",
@@ -68,5 +68,5 @@
68
68
  "engines": {
69
69
  "node": ">=14"
70
70
  },
71
- "gitHead": "7ed68875703f5481aaf58f936ce58feea36e6596"
71
+ "gitHead": "11d2c2fdb2bc81048097b05bc65698777d54e456"
72
72
  }
@@ -5,13 +5,15 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  * ========================================================================== */
7
7
 
8
- import { greaterThan, lessThan } from "./utils";
8
+ import { greaterThan, codeFence } from "./utils";
9
9
 
10
10
  export function createDescription(description: string | undefined) {
11
11
  if (!description) {
12
12
  return "";
13
13
  }
14
14
  return `\n\n${description
15
- .replace(lessThan, "&lt;")
16
- .replace(greaterThan, "&gt;")}\n\n`;
15
+ .replace(greaterThan, "\\>")
16
+ .replace(codeFence, function (match) {
17
+ return match.replace(/\\>/g, ">");
18
+ })}\n\n`;
17
19
  }
@@ -29,8 +29,8 @@ export function mergeAllOf(allOf: SchemaObject[]) {
29
29
  "x-examples": function () {
30
30
  return true;
31
31
  },
32
- ignoreAdditionalProperties: true,
33
32
  },
33
+ ignoreAdditionalProperties: true,
34
34
  });
35
35
 
36
36
  const required = allOf.reduce((acc, cur) => {
@@ -43,6 +43,7 @@ export function render(children: Children): string {
43
43
 
44
44
  // Regex to selectively URL-encode '>' and '<' chars
45
45
  export const lessThan =
46
- /<(?!(=|button|\s?\/button|details|\s?\/details|summary|\s?\/summary|hr|\s?\/hr|br|\s?\/br|span|\s?\/span|strong|\s?\/strong|small|\s?\/small|table|\s?\/table|td|\s?\/td|tr|\s?\/tr|th|\s?\/th|h1|\s?\/h1|h2|\s?\/h2|h3|\s?\/h3|h4|\s?\/h4|h5|\s?\/h5|h6|\s?\/h6|title|\s?\/title|p|\s?\/p|em|\s?\/em|b|\s?\/b|i|\s?\/i|u|\s?\/u|strike|\s?\/strike|a|\s?\/a|li|\s?\/li|ol|\s?\/ol|ul|\s?\/ul|img|\s?\/img|div|\s?\/div|center|\s?\/center))/giu;
46
+ /<(?!(=|button|\s?\/button|details|\s?\/details|summary|\s?\/summary|hr|\s?\/hr|br|\s?\/br|span|\s?\/span|strong|\s?\/strong|small|\s?\/small|table|\s?\/table|td|\s?\/td|tr|\s?\/tr|th|\s?\/th|h1|\s?\/h1|h2|\s?\/h2|h3|\s?\/h3|h4|\s?\/h4|h5|\s?\/h5|h6|\s?\/h6|title|\s?\/title|p|\s?\/p|em|\s?\/em|b|\s?\/b|i|\s?\/i|u|\s?\/u|strike|\s?\/strike|a|\s?\/a|li|\s?\/li|ol|\s?\/ol|ul|\s?\/ul|img|\s?\/img|div|\s?\/div|center|\s?\/center))/gu;
47
47
  export const greaterThan =
48
- /(?<!(button|details|summary|hr|br|span|strong|small|table|td|tr|th|h1|h2|h3|h4|h5|h6|title|p|em|b|i|u|strike|a|tag|li|ol|ul|img|div|center|\/|\s|"|'))>?!=/giu;
48
+ /(?<!(button|details|summary|hr|br|span|strong|small|table|td|tr|th|h1|h2|h3|h4|h5|h6|title|p|em|b|i|u|strike|a|tag|li|ol|ul|img|div|center|\/|\s|"|'))>/gu;
49
+ export const codeFence = /`{1,3}[\s\S]*?`{1,3}/g;
@@ -239,6 +239,122 @@ function createItems(
239
239
  }
240
240
  }
241
241
 
242
+ // Gather x-webhooks endpoints
243
+ for (let [path, pathObject] of Object.entries(
244
+ openapiData["x-webhooks"] ?? {}
245
+ )) {
246
+ path = "webhook";
247
+ const { $ref, description, parameters, servers, summary, ...rest } =
248
+ pathObject;
249
+ for (let [method, operationObject] of Object.entries({ ...rest })) {
250
+ method = "event";
251
+ const title =
252
+ operationObject.summary ??
253
+ operationObject.operationId ??
254
+ "Missing summary";
255
+ if (operationObject.description === undefined) {
256
+ operationObject.description =
257
+ operationObject.summary ?? operationObject.operationId ?? "";
258
+ }
259
+
260
+ const baseId = operationObject.operationId
261
+ ? kebabCase(operationObject.operationId)
262
+ : kebabCase(operationObject.summary);
263
+
264
+ const servers =
265
+ operationObject.servers ?? pathObject.servers ?? openapiData.servers;
266
+
267
+ const security = operationObject.security ?? openapiData.security;
268
+
269
+ // Add security schemes so we know how to handle security.
270
+ const securitySchemes = openapiData.components?.securitySchemes;
271
+
272
+ // Make sure schemes are lowercase. See: https://github.com/cloud-annotations/docusaurus-plugin-openapi/issues/79
273
+ if (securitySchemes) {
274
+ for (let securityScheme of Object.values(securitySchemes)) {
275
+ if (securityScheme.type === "http") {
276
+ securityScheme.scheme = securityScheme.scheme.toLowerCase();
277
+ }
278
+ }
279
+ }
280
+
281
+ let jsonRequestBodyExample;
282
+ const body = operationObject.requestBody?.content?.["application/json"];
283
+ if (body?.schema) {
284
+ jsonRequestBodyExample = sampleRequestFromSchema(body.schema);
285
+ }
286
+
287
+ // Handle vendor JSON media types
288
+ const bodyContent = operationObject.requestBody?.content;
289
+ if (bodyContent) {
290
+ const firstBodyContentKey = Object.keys(bodyContent)[0];
291
+ if (firstBodyContentKey.endsWith("+json")) {
292
+ const firstBody = bodyContent[firstBodyContentKey];
293
+ if (firstBody?.schema) {
294
+ jsonRequestBodyExample = sampleRequestFromSchema(firstBody.schema);
295
+ }
296
+ }
297
+ }
298
+
299
+ // TODO: Don't include summary temporarilly
300
+ const { summary, ...defaults } = operationObject;
301
+
302
+ // Merge common parameters with operation parameters
303
+ // Operation params take precendence over common params
304
+ if (parameters !== undefined) {
305
+ if (operationObject.parameters !== undefined) {
306
+ defaults.parameters = unionBy(
307
+ operationObject.parameters,
308
+ parameters,
309
+ "name"
310
+ );
311
+ } else {
312
+ defaults.parameters = parameters;
313
+ }
314
+ }
315
+
316
+ const opDescription = operationObject.description;
317
+ let splitDescription: any;
318
+ if (opDescription) {
319
+ splitDescription = opDescription.match(/[^\r\n]+/g);
320
+ }
321
+
322
+ const apiPage: PartialPage<ApiPageMetadata> = {
323
+ type: "api",
324
+ id: baseId,
325
+ infoId: infoId ?? "",
326
+ unversionedId: baseId,
327
+ title: title ? title.replace(/((?:^|[^\\])(?:\\{2})*)"/g, "$1'") : "",
328
+ description: operationObject.description
329
+ ? operationObject.description.replace(
330
+ /((?:^|[^\\])(?:\\{2})*)"/g,
331
+ "$1'"
332
+ )
333
+ : "",
334
+ frontMatter: {
335
+ description: splitDescription
336
+ ? splitDescription[0]
337
+ .replace(/((?:^|[^\\])(?:\\{2})*)"/g, "$1'")
338
+ .replace(/\s+$/, "")
339
+ : "",
340
+ ...(options?.proxy && { proxy: options.proxy }),
341
+ },
342
+ api: {
343
+ ...defaults,
344
+ tags: operationObject.tags,
345
+ method,
346
+ path,
347
+ servers,
348
+ security,
349
+ securitySchemes,
350
+ jsonRequestBodyExample,
351
+ info: openapiData.info,
352
+ },
353
+ };
354
+ items.push(apiPage);
355
+ }
356
+ }
357
+
242
358
  if (sidebarOptions?.categoryLinkSource === "tag") {
243
359
  // Get global tags
244
360
  const tags: TagObject[] = openapiData.tags ?? [];
@@ -21,6 +21,7 @@ export interface OpenApiObject {
21
21
  tags?: TagObject[];
22
22
  externalDocs?: ExternalDocumentationObject;
23
23
  swagger?: string;
24
+ "x-webhooks"?: PathsObject;
24
25
  }
25
26
 
26
27
  export interface OpenApiObjectWithRef {
@@ -66,7 +66,13 @@ export function convertSwagger2OpenAPI(spec: object) {
66
66
  return new Promise((resolve, reject) =>
67
67
  convertObj(
68
68
  spec,
69
- { patch: true, warnOnly: true, text: "{}", anchors: true },
69
+ {
70
+ patch: true,
71
+ warnOnly: true,
72
+ text: "{}",
73
+ anchors: true,
74
+ resolveInternal: true,
75
+ },
70
76
  (err: any, res: any) => {
71
77
  // TODO: log any warnings
72
78
  if (err) {