@gitbook/react-openapi 1.1.6 → 1.1.8

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 (51) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/dist/InteractiveSection.d.ts +0 -2
  3. package/dist/InteractiveSection.jsx +3 -4
  4. package/dist/OpenAPICodeSample.jsx +4 -4
  5. package/dist/OpenAPICodeSampleInteractive.d.ts +4 -3
  6. package/dist/OpenAPICodeSampleInteractive.jsx +22 -15
  7. package/dist/OpenAPICopyButton.d.ts +7 -0
  8. package/dist/OpenAPICopyButton.jsx +6 -6
  9. package/dist/OpenAPIOperation.jsx +21 -1
  10. package/dist/OpenAPIPath.jsx +2 -2
  11. package/dist/OpenAPIRequestBody.jsx +1 -1
  12. package/dist/OpenAPIResponse.jsx +1 -1
  13. package/dist/OpenAPIResponses.jsx +2 -2
  14. package/dist/OpenAPISchema.d.ts +5 -14
  15. package/dist/OpenAPISchema.jsx +79 -28
  16. package/dist/OpenAPISchemaName.jsx +1 -0
  17. package/dist/OpenAPISchemaServer.d.ts +12 -0
  18. package/dist/OpenAPISchemaServer.jsx +8 -0
  19. package/dist/OpenAPISpec.d.ts +0 -6
  20. package/dist/OpenAPISpec.jsx +5 -11
  21. package/dist/OpenAPITabs.jsx +3 -11
  22. package/dist/code-samples.js +44 -9
  23. package/dist/decycle.d.ts +2 -0
  24. package/dist/decycle.js +70 -0
  25. package/dist/schemas/OpenAPISchemas.d.ts +4 -0
  26. package/dist/schemas/OpenAPISchemas.jsx +6 -6
  27. package/dist/schemas/resolveOpenAPISchemas.d.ts +2 -6
  28. package/dist/schemas/resolveOpenAPISchemas.js +1 -21
  29. package/dist/tsconfig.build.tsbuildinfo +1 -1
  30. package/dist/types.d.ts +2 -5
  31. package/package.json +1 -1
  32. package/src/InteractiveSection.tsx +2 -6
  33. package/src/OpenAPICodeSample.tsx +16 -5
  34. package/src/OpenAPICodeSampleInteractive.tsx +53 -28
  35. package/src/OpenAPICopyButton.tsx +17 -4
  36. package/src/OpenAPIOperation.tsx +39 -2
  37. package/src/OpenAPIPath.tsx +2 -2
  38. package/src/OpenAPIRequestBody.tsx +1 -1
  39. package/src/OpenAPIResponse.tsx +4 -4
  40. package/src/OpenAPIResponses.tsx +1 -5
  41. package/src/OpenAPISchema.tsx +152 -58
  42. package/src/OpenAPISchemaName.tsx +3 -0
  43. package/src/OpenAPISchemaServer.tsx +34 -0
  44. package/src/OpenAPISpec.tsx +13 -11
  45. package/src/OpenAPITabs.tsx +3 -13
  46. package/src/code-samples.test.ts +69 -1
  47. package/src/code-samples.ts +45 -9
  48. package/src/decycle.ts +68 -0
  49. package/src/schemas/OpenAPISchemas.tsx +11 -6
  50. package/src/schemas/resolveOpenAPISchemas.ts +3 -31
  51. package/src/types.ts +6 -6
@@ -10,6 +10,7 @@ var __assign = (this && this.__assign) || function () {
10
10
  return __assign.apply(this, arguments);
11
11
  };
12
12
  import { isCSV, isFormData, isFormUrlEncoded, isGraphQL, isPDF, isPlainObject, isText, isXML, } from './contentTypeChecks';
13
+ import { json2xml } from './json2xml';
13
14
  import { stringifyOpenAPI } from './stringifyOpenAPI';
14
15
  export var codeSampleGenerators = [
15
16
  {
@@ -201,7 +202,11 @@ var BodyGenerators = {
201
202
  else if (isText(contentType)) {
202
203
  body = "--data '".concat(String(body).replace(/"/g, ''), "'");
203
204
  }
204
- else if (isXML(contentType) || isCSV(contentType)) {
205
+ else if (isXML(contentType)) {
206
+ // Convert to XML and ensure proper formatting
207
+ body = "--data-binary $'".concat(convertBodyToXML(body), "'");
208
+ }
209
+ else if (isCSV(contentType)) {
205
210
  // We use --data-binary to avoid cURL converting newlines to \r\n
206
211
  body = "--data-binary $'".concat(stringifyOpenAPI(body).replace(/"/g, '').replace(/\\n/g, '\n'), "'");
207
212
  }
@@ -285,7 +290,8 @@ var BodyGenerators = {
285
290
  }
286
291
  else if (isXML(contentType)) {
287
292
  code += 'const xml = `\n';
288
- code += indent(String(body), 4);
293
+ // Convert JSON to XML if needed
294
+ code += indent(convertBodyToXML(body), 4);
289
295
  code += '`;\n\n';
290
296
  body = 'xml';
291
297
  }
@@ -319,6 +325,10 @@ var BodyGenerators = {
319
325
  code += '}\n\n';
320
326
  body = 'files';
321
327
  }
328
+ if (isXML(contentType)) {
329
+ // Convert JSON to XML if needed
330
+ body = convertBodyToXML(body);
331
+ }
322
332
  return { body: body, code: code, headers: headers };
323
333
  },
324
334
  getHTTPBody: function (body, headers) {
@@ -332,14 +342,18 @@ var BodyGenerators = {
332
342
  ? Object.entries(body)
333
343
  .map(function (_a) {
334
344
  var key = _a[0], value = _a[1];
335
- return "".concat(key, "=").concat(String(value));
345
+ return "".concat(key, "=").concat(stringifyOpenAPI(value));
336
346
  })
337
347
  .join('&')
338
- : String(body);
339
- return "\"".concat(encoded, "\"");
348
+ : stringifyOpenAPI(body);
349
+ return "\"".concat(encoded.replace(/"/g, "'"), "\"");
340
350
  },
341
351
  text: function () { return "\"".concat(String(body), "\""); },
342
- xmlOrCsv: function () { return "\"".concat(stringifyOpenAPI(body).replace(/"/g, ''), "\""); },
352
+ xml: function () {
353
+ // Convert JSON to XML if needed
354
+ return "\"".concat(convertBodyToXML(body), "\"");
355
+ },
356
+ csv: function () { return "\"".concat(stringifyOpenAPI(body).replace(/"/g, ''), "\""); },
343
357
  default: function () { return "".concat(stringifyOpenAPI(body, null, 2)); },
344
358
  };
345
359
  if (isPDF(contentType))
@@ -348,9 +362,30 @@ var BodyGenerators = {
348
362
  return typeHandlers.formUrlEncoded();
349
363
  if (isText(contentType))
350
364
  return typeHandlers.text();
351
- if (isXML(contentType) || isCSV(contentType)) {
352
- return typeHandlers.xmlOrCsv();
353
- }
365
+ if (isXML(contentType))
366
+ return typeHandlers.xml();
367
+ if (isCSV(contentType))
368
+ return typeHandlers.csv();
354
369
  return typeHandlers.default();
355
370
  },
356
371
  };
372
+ /**
373
+ * Converts a body to XML format
374
+ */
375
+ function convertBodyToXML(body) {
376
+ // If body is already a string and looks like XML, return it as is
377
+ if (typeof body === 'string' && body.trim().startsWith('<')) {
378
+ return body;
379
+ }
380
+ // If body is not an object, try to parse it as JSON
381
+ if (typeof body !== 'object' || body === null) {
382
+ try {
383
+ body = JSON.parse(body);
384
+ }
385
+ catch (_a) {
386
+ // If parsing fails, return the original body
387
+ return body;
388
+ }
389
+ }
390
+ return json2xml(body).replace(/"/g, '').replace(/\\n/g, '\n').replace(/\\t/g, '\t');
391
+ }
@@ -0,0 +1,2 @@
1
+ export declare const decycle: () => (this: any, key: string | symbol, value: any) => any;
2
+ export declare function retrocycle(): (this: object, key: string | symbol, value: any) => any;
@@ -0,0 +1,70 @@
1
+ // Forked from: https://github.com/YChebotaev/json-decycle/blob/master/src/index.ts
2
+ // Replaced `$ref` with `$reference` to avoid conflicts with OpenAPI
3
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
4
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
5
+ if (ar || !(i in from)) {
6
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
7
+ ar[i] = from[i];
8
+ }
9
+ }
10
+ return to.concat(ar || Array.prototype.slice.call(from));
11
+ };
12
+ var isObject = function (value) {
13
+ return typeof value === 'object' &&
14
+ value != null &&
15
+ !(value instanceof Boolean) &&
16
+ !(value instanceof Date) &&
17
+ !(value instanceof Number) &&
18
+ !(value instanceof RegExp) &&
19
+ !(value instanceof String);
20
+ };
21
+ var toPointer = function (parts) {
22
+ return "#".concat(parts.map(function (part) { return String(part).replace(/~/g, '~0').replace(/\//g, '~1'); }).join('/'));
23
+ };
24
+ export var decycle = function () {
25
+ var paths = new WeakMap();
26
+ return function replacer(key, value) {
27
+ var _a;
28
+ if (key !== '$reference' && isObject(value)) {
29
+ var seen = paths.has(value);
30
+ if (seen) {
31
+ return { $reference: toPointer(paths.get(value)) };
32
+ }
33
+ paths.set(value, __spreadArray(__spreadArray([], ((_a = paths.get(this)) !== null && _a !== void 0 ? _a : []), true), [key], false));
34
+ }
35
+ return value;
36
+ };
37
+ };
38
+ export function retrocycle() {
39
+ var parents = new WeakMap();
40
+ var keys = new WeakMap();
41
+ var refs = new Set();
42
+ function dereference(ref) {
43
+ var _a;
44
+ var parts = ref.$reference.slice(1).split('/');
45
+ var key;
46
+ var value = this;
47
+ for (var i = 0; i < parts.length; i++) {
48
+ key = (_a = parts[i]) === null || _a === void 0 ? void 0 : _a.replace(/~1/g, '/').replace(/~0/g, '~');
49
+ value = value[key];
50
+ }
51
+ var parent = parents.get(ref);
52
+ parent[keys.get(ref)] = value;
53
+ }
54
+ return function reviver(key, value) {
55
+ if (key === '$reference') {
56
+ refs.add(this);
57
+ }
58
+ else if (isObject(value)) {
59
+ var isRoot = key === '' && Object.keys(this).length === 1;
60
+ if (isRoot) {
61
+ refs.forEach(dereference, this);
62
+ }
63
+ else {
64
+ parents.set(value, this);
65
+ keys.set(value, key);
66
+ }
67
+ }
68
+ return value;
69
+ };
70
+ }
@@ -7,5 +7,9 @@ export declare function OpenAPISchemas(props: {
7
7
  className?: string;
8
8
  data: OpenAPISchemasData;
9
9
  context: OpenAPISchemasContextProps;
10
+ /**
11
+ * Whether to show the schema directly if there is only one.
12
+ */
13
+ grouped?: boolean;
10
14
  }): import("react").JSX.Element | null;
11
15
  export {};
@@ -1,12 +1,12 @@
1
1
  import clsx from 'clsx';
2
2
  import { OpenAPIDisclosureGroup } from '../OpenAPIDisclosureGroup';
3
- import { OpenAPIRootSchema } from '../OpenAPISchema';
3
+ import { OpenAPIRootSchema } from '../OpenAPISchemaServer';
4
4
  import { Section, SectionBody } from '../StaticSection';
5
5
  /**
6
6
  * Display OpenAPI Schemas.
7
7
  */
8
8
  export function OpenAPISchemas(props) {
9
- var className = props.className, data = props.data, context = props.context;
9
+ var className = props.className, data = props.data, context = props.context, grouped = props.grouped;
10
10
  var schemas = data.schemas;
11
11
  var clientContext = {
12
12
  defaultInteractiveOpened: context.defaultInteractiveOpened,
@@ -17,7 +17,7 @@ export function OpenAPISchemas(props) {
17
17
  return null;
18
18
  }
19
19
  return (<div className={clsx('openapi-schemas', className)}>
20
- <OpenAPIRootSchemasSchema schemas={schemas} context={clientContext}/>
20
+ <OpenAPIRootSchemasSchema grouped={grouped} schemas={schemas} context={clientContext}/>
21
21
  </div>);
22
22
  }
23
23
  /**
@@ -26,9 +26,9 @@ export function OpenAPISchemas(props) {
26
26
  */
27
27
  function OpenAPIRootSchemasSchema(props) {
28
28
  var _a;
29
- var schemas = props.schemas, context = props.context;
30
- // If there is only one model, we show it directly.
31
- if (schemas.length === 1) {
29
+ var schemas = props.schemas, context = props.context, grouped = props.grouped;
30
+ // If there is only one model and we are not grouping, we show it directly.
31
+ if (schemas.length === 1 && !grouped) {
32
32
  var schema = (_a = schemas === null || schemas === void 0 ? void 0 : schemas[0]) === null || _a === void 0 ? void 0 : _a.schema;
33
33
  if (!schema) {
34
34
  return null;
@@ -1,5 +1,5 @@
1
- import { type Filesystem, type OpenAPIV3, type OpenAPIV3_1, type OpenAPIV3xDocument } from '@gitbook/openapi-parser';
2
- import type { OpenAPISchema, OpenAPISchemasData } from '../types';
1
+ import type { Filesystem, OpenAPIV3xDocument } from '@gitbook/openapi-parser';
2
+ import type { OpenAPISchemasData } from '../types';
3
3
  /**
4
4
  * Resolve an OpenAPI schemas from a file and compile it to a more usable format.
5
5
  * Schemas are extracted from the OpenAPI components.schemas
@@ -7,7 +7,3 @@ import type { OpenAPISchema, OpenAPISchemasData } from '../types';
7
7
  export declare function resolveOpenAPISchemas(filesystem: Filesystem<OpenAPIV3xDocument>, options: {
8
8
  schemas: string[];
9
9
  }): Promise<OpenAPISchemasData | null>;
10
- /**
11
- * Extract selected schemas from the OpenAPI document.
12
- */
13
- export declare function filterSelectedOpenAPISchemas(schema: OpenAPIV3.Document | OpenAPIV3_1.Document, selectedSchemas: string[]): OpenAPISchema[];
@@ -34,7 +34,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
34
34
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35
35
  }
36
36
  };
37
- import { shouldIgnoreEntity, } from '@gitbook/openapi-parser';
37
+ import { filterSelectedOpenAPISchemas } from '@gitbook/openapi-parser';
38
38
  import { dereferenceFilesystem } from '../dereference';
39
39
  //!!TODO: We should return only the schemas that are used in the block. Still a WIP awaiting future work.
40
40
  /**
@@ -60,23 +60,3 @@ export function resolveOpenAPISchemas(filesystem, options) {
60
60
  });
61
61
  });
62
62
  }
63
- /**
64
- * Extract selected schemas from the OpenAPI document.
65
- */
66
- export function filterSelectedOpenAPISchemas(schema, selectedSchemas) {
67
- var _a, _b;
68
- var componentsSchemas = (_b = (_a = schema.components) === null || _a === void 0 ? void 0 : _a.schemas) !== null && _b !== void 0 ? _b : {};
69
- // Preserve the order of the selected schemas
70
- return selectedSchemas
71
- .map(function (name) {
72
- var schema = componentsSchemas[name];
73
- if (schema && !shouldIgnoreEntity(schema)) {
74
- return {
75
- name: name,
76
- schema: schema,
77
- };
78
- }
79
- return null;
80
- })
81
- .filter(function (schema) { return !!schema; });
82
- }