@gitbook/react-openapi 1.1.5 → 1.1.7

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 (59) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/dist/InteractiveSection.d.ts +0 -2
  3. package/dist/InteractiveSection.jsx +3 -4
  4. package/dist/OpenAPICodeSample.d.ts +9 -0
  5. package/dist/OpenAPICodeSample.jsx +117 -58
  6. package/dist/OpenAPICodeSampleInteractive.d.ts +11 -0
  7. package/dist/OpenAPICodeSampleInteractive.jsx +85 -0
  8. package/dist/OpenAPICopyButton.d.ts +7 -0
  9. package/dist/OpenAPICopyButton.jsx +6 -6
  10. package/dist/OpenAPIOperation.jsx +21 -1
  11. package/dist/OpenAPIPath.jsx +2 -2
  12. package/dist/OpenAPIRequestBody.jsx +1 -1
  13. package/dist/OpenAPIResponse.jsx +1 -1
  14. package/dist/OpenAPIResponses.jsx +2 -2
  15. package/dist/OpenAPISchema.d.ts +5 -14
  16. package/dist/OpenAPISchema.jsx +79 -28
  17. package/dist/OpenAPISchemaName.jsx +8 -6
  18. package/dist/OpenAPISchemaServer.d.ts +12 -0
  19. package/dist/OpenAPISchemaServer.jsx +8 -0
  20. package/dist/OpenAPISpec.d.ts +0 -6
  21. package/dist/OpenAPISpec.jsx +5 -11
  22. package/dist/OpenAPITabs.jsx +3 -11
  23. package/dist/code-samples.d.ts +1 -2
  24. package/dist/code-samples.js +46 -11
  25. package/dist/decycle.d.ts +2 -0
  26. package/dist/decycle.js +70 -0
  27. package/dist/generateSchemaExample.d.ts +31 -2
  28. package/dist/generateSchemaExample.js +307 -24
  29. package/dist/schemas/OpenAPISchemas.jsx +1 -1
  30. package/dist/schemas/resolveOpenAPISchemas.d.ts +2 -6
  31. package/dist/schemas/resolveOpenAPISchemas.js +1 -21
  32. package/dist/tsconfig.build.tsbuildinfo +1 -1
  33. package/dist/types.d.ts +2 -5
  34. package/dist/utils.d.ts +1 -1
  35. package/dist/utils.js +11 -7
  36. package/package.json +3 -3
  37. package/src/InteractiveSection.tsx +2 -6
  38. package/src/OpenAPICodeSample.tsx +187 -78
  39. package/src/OpenAPICodeSampleInteractive.tsx +139 -0
  40. package/src/OpenAPICopyButton.tsx +17 -4
  41. package/src/OpenAPIOperation.tsx +39 -2
  42. package/src/OpenAPIPath.tsx +2 -2
  43. package/src/OpenAPIRequestBody.tsx +1 -1
  44. package/src/OpenAPIResponse.tsx +4 -4
  45. package/src/OpenAPIResponses.tsx +1 -5
  46. package/src/OpenAPISchema.tsx +152 -58
  47. package/src/OpenAPISchemaName.tsx +14 -6
  48. package/src/OpenAPISchemaServer.tsx +34 -0
  49. package/src/OpenAPISpec.tsx +13 -11
  50. package/src/OpenAPITabs.tsx +3 -13
  51. package/src/code-samples.test.ts +69 -1
  52. package/src/code-samples.ts +48 -12
  53. package/src/decycle.ts +68 -0
  54. package/src/generateSchemaExample.ts +412 -25
  55. package/src/resolveOpenAPIOperation.test.ts +6 -6
  56. package/src/schemas/OpenAPISchemas.tsx +1 -1
  57. package/src/schemas/resolveOpenAPISchemas.ts +3 -31
  58. package/src/types.ts +6 -6
  59. package/src/utils.ts +13 -10
@@ -1,55 +1,74 @@
1
+ 'use client';
1
2
  import { useId } from 'react';
2
3
  import clsx from 'clsx';
3
4
  import { Markdown } from './Markdown';
5
+ import { OpenAPICopyButton } from './OpenAPICopyButton';
4
6
  import { OpenAPIDisclosure } from './OpenAPIDisclosure';
5
7
  import { OpenAPISchemaName } from './OpenAPISchemaName';
8
+ import { retrocycle } from './decycle';
6
9
  import { checkIsReference, resolveDescription, resolveFirstExample } from './utils';
7
10
  /**
8
11
  * Render a property of an OpenAPI schema.
9
12
  */
10
13
  function OpenAPISchemaProperty(props) {
11
- var property = props.property, _a = props.circularRefs, parentCircularRefs = _a === void 0 ? new Map() : _a, context = props.context, className = props.className;
14
+ var parentCircularRefs = props.circularRefs, context = props.context, className = props.className, property = props.property;
12
15
  var schema = property.schema;
13
16
  var id = useId();
14
17
  return (<div id={id} className={clsx('openapi-schema', className)}>
15
18
  <OpenAPISchemaPresentation property={property}/>
16
19
  {(function () {
17
- var parentCircularRef = parentCircularRefs.get(schema);
20
+ var circularRefId = parentCircularRefs.get(schema);
18
21
  // Avoid recursing infinitely, and instead render a link to the parent schema
19
- if (parentCircularRef) {
20
- return <OpenAPISchemaCircularRef id={parentCircularRef} schema={schema}/>;
22
+ if (circularRefId) {
23
+ return <OpenAPISchemaCircularRef id={circularRefId} schema={schema}/>;
21
24
  }
22
- var circularRefs = parentCircularRefs.set(schema, id);
25
+ var circularRefs = new Map(parentCircularRefs);
26
+ circularRefs.set(schema, id);
23
27
  var properties = getSchemaProperties(schema);
24
- var alternatives = getSchemaAlternatives(schema, new Set(circularRefs.keys()));
25
- return (<>
26
- {alternatives === null || alternatives === void 0 ? void 0 : alternatives.map(function (schema, index) { return (<OpenAPISchemaAlternative key={index} schema={schema} circularRefs={circularRefs} context={context}/>); })}
27
- {(properties === null || properties === void 0 ? void 0 : properties.length) ? (<OpenAPIDisclosure context={context} label={getDisclosureLabel(schema)}>
28
- <OpenAPISchemaProperties properties={properties} circularRefs={circularRefs} context={context}/>
29
- </OpenAPIDisclosure>) : null}
30
- </>);
28
+ if (properties === null || properties === void 0 ? void 0 : properties.length) {
29
+ return (<OpenAPIDisclosure context={context} label={getDisclosureLabel(schema)}>
30
+ <OpenAPISchemaProperties properties={properties} circularRefs={circularRefs} context={context}/>
31
+ </OpenAPIDisclosure>);
32
+ }
33
+ var ancestors = new Set(circularRefs.keys());
34
+ var alternatives = getSchemaAlternatives(schema, ancestors);
35
+ if (alternatives) {
36
+ return alternatives.map(function (schema, index) { return (<OpenAPISchemaAlternative key={index} schema={schema} circularRefs={circularRefs} context={context}/>); });
37
+ }
38
+ return null;
31
39
  })()}
32
40
  </div>);
33
41
  }
34
42
  /**
35
43
  * Render a set of properties of an OpenAPI schema.
36
44
  */
37
- export function OpenAPISchemaProperties(props) {
38
- var id = props.id, properties = props.properties, circularRefs = props.circularRefs, context = props.context;
45
+ function OpenAPISchemaProperties(props) {
46
+ var id = props.id, properties = props.properties, _a = props.circularRefs, circularRefs = _a === void 0 ? new Map() : _a, context = props.context;
39
47
  return (<div id={id} className="openapi-schema-properties">
40
- {properties.map(function (property, index) { return (<OpenAPISchemaProperty key={index} circularRefs={circularRefs} property={property} context={context}/>); })}
48
+ {properties.map(function (property, index) {
49
+ return (<OpenAPISchemaProperty key={index} circularRefs={circularRefs} property={property} context={context}/>);
50
+ })}
41
51
  </div>);
42
52
  }
53
+ export function OpenAPISchemaPropertiesFromServer(props) {
54
+ return (<OpenAPISchemaProperties id={props.id} properties={JSON.parse(props.properties, retrocycle())} context={props.context}/>);
55
+ }
43
56
  /**
44
57
  * Render a root schema (such as the request body or response body).
45
58
  */
46
- export function OpenAPIRootSchema(props) {
47
- var schema = props.schema, context = props.context;
59
+ function OpenAPIRootSchema(props) {
60
+ var schema = props.schema, context = props.context, _a = props.circularRefs, parentCircularRefs = _a === void 0 ? new Map() : _a;
61
+ var id = useId();
48
62
  var properties = getSchemaProperties(schema);
49
63
  if (properties === null || properties === void 0 ? void 0 : properties.length) {
50
- return <OpenAPISchemaProperties properties={properties} context={context}/>;
64
+ var circularRefs = new Map(parentCircularRefs);
65
+ circularRefs.set(schema, id);
66
+ return (<OpenAPISchemaProperties properties={properties} circularRefs={circularRefs} context={context}/>);
51
67
  }
52
- return (<OpenAPISchemaProperty className="openapi-schema-root" property={{ schema: schema }} context={context}/>);
68
+ return (<OpenAPISchemaProperty className="openapi-schema-root" property={{ schema: schema }} context={context} circularRefs={parentCircularRefs}/>);
69
+ }
70
+ export function OpenAPIRootSchemaFromServer(props) {
71
+ return (<OpenAPIRootSchema schema={JSON.parse(props.schema, retrocycle())} context={props.context}/>);
53
72
  }
54
73
  /**
55
74
  * Render a tab for an alternative schema.
@@ -81,15 +100,47 @@ function OpenAPISchemaCircularRef(props) {
81
100
  * Render the enum value for a schema.
82
101
  */
83
102
  function OpenAPISchemaEnum(props) {
84
- var enumValues = props.enumValues;
103
+ var schema = props.schema;
104
+ var enumValues = (function () {
105
+ var _a;
106
+ // Render x-gitbook-enum first, as it has a different format
107
+ if (schema['x-gitbook-enum']) {
108
+ return Object.entries(schema['x-gitbook-enum']).map(function (_a) {
109
+ var name = _a[0], description = _a[1].description;
110
+ return {
111
+ value: name,
112
+ description: description,
113
+ };
114
+ });
115
+ }
116
+ if (schema['x-enumDescriptions']) {
117
+ return Object.entries(schema['x-enumDescriptions']).map(function (_a) {
118
+ var value = _a[0], description = _a[1];
119
+ return {
120
+ value: value,
121
+ description: description,
122
+ };
123
+ });
124
+ }
125
+ return (_a = schema.enum) === null || _a === void 0 ? void 0 : _a.map(function (value) {
126
+ return {
127
+ value: value,
128
+ description: undefined,
129
+ };
130
+ });
131
+ })();
132
+ if (!(enumValues === null || enumValues === void 0 ? void 0 : enumValues.length)) {
133
+ return null;
134
+ }
85
135
  return (<div className="openapi-schema-enum">
86
- <span>
87
- Options:{' '}
88
- {enumValues.map(function (value, index) { return (<span key={index} className="openapi-schema-enum-value">
89
- <code>{"".concat(value)}</code>
90
- {index < enumValues.length - 1 ? ', ' : ''}
136
+ <span>Available options:</span>
137
+ <div className="openapi-schema-enum-list">
138
+ {enumValues.map(function (item, index) { return (<span key={index} className="openapi-schema-enum-value">
139
+ <OpenAPICopyButton value={item.value} label={item.description} withTooltip={!!item.description}>
140
+ <code>{"".concat(item.value)}</code>
141
+ </OpenAPICopyButton>
91
142
  </span>); })}
92
- </span>
143
+ </div>
93
144
  </div>);
94
145
  }
95
146
  /**
@@ -114,7 +165,7 @@ function OpenAPISchemaPresentation(props) {
114
165
  {schema.pattern ? (<div className="openapi-schema-pattern">
115
166
  Pattern: <code>{schema.pattern}</code>
116
167
  </div>) : null}
117
- {schema.enum && schema.enum.length > 0 ? (<OpenAPISchemaEnum enumValues={schema.enum}/>) : null}
168
+ <OpenAPISchemaEnum schema={schema}/>
118
169
  </div>);
119
170
  }
120
171
  /**
@@ -208,7 +259,7 @@ function getSchemaTitle(schema) {
208
259
  var _a;
209
260
  // Otherwise try to infer a nice title
210
261
  var type = 'any';
211
- if (schema.enum) {
262
+ if (schema.enum || schema['x-enumDescriptions'] || schema['x-gitbook-enum']) {
212
263
  type = "".concat(schema.type, " \u00B7 enum");
213
264
  // check array AND schema.items as this is sometimes null despite what the type indicates
214
265
  }
@@ -1,3 +1,4 @@
1
+ import { stringifyOpenAPI } from './stringifyOpenAPI';
1
2
  /**
2
3
  * Display the schema name row.
3
4
  * It includes the property name, type, required and deprecated status.
@@ -14,21 +15,22 @@ export function OpenAPISchemaName(props) {
14
15
  {additionalItems ? (<span className="openapi-schema-type">{additionalItems}</span>) : null}
15
16
  </span>
16
17
  {(schema === null || schema === void 0 ? void 0 : schema.readOnly) ? <span className="openapi-schema-readonly">read-only</span> : null}
17
- {required ? <span className="openapi-schema-required">required</span> : null}
18
+ {(schema === null || schema === void 0 ? void 0 : schema.writeOnly) ? (<span className="openapi-schema-writeonly">write-only</span>) : null}
19
+ {required ? (<span className="openapi-schema-required">required</span>) : (<span className="openapi-schema-optional">optional</span>)}
18
20
  {(schema === null || schema === void 0 ? void 0 : schema.deprecated) ? <span className="openapi-deprecated">Deprecated</span> : null}
19
21
  </div>);
20
22
  }
21
23
  function getAdditionalItems(schema) {
22
24
  var additionalItems = '';
23
- if (schema.minimum || schema.minLength) {
24
- additionalItems += " \u00B7 min: ".concat(schema.minimum || schema.minLength);
25
+ if (schema.minimum || schema.minLength || schema.minItems) {
26
+ additionalItems += " \u00B7 min: ".concat(schema.minimum || schema.minLength || schema.minItems);
25
27
  }
26
- if (schema.maximum || schema.maxLength) {
27
- additionalItems += " \u00B7 max: ".concat(schema.maximum || schema.maxLength);
28
+ if (schema.maximum || schema.maxLength || schema.maxItems) {
29
+ additionalItems += " \u00B7 max: ".concat(schema.maximum || schema.maxLength || schema.maxItems);
28
30
  }
29
31
  // If the schema has a default value, we display it
30
32
  if (typeof schema.default !== 'undefined') {
31
- additionalItems += " \u00B7 default: ".concat(schema.default);
33
+ additionalItems += " \u00B7 default: ".concat(stringifyOpenAPI(schema.default));
32
34
  }
33
35
  if (schema.nullable) {
34
36
  additionalItems = ' | nullable';
@@ -0,0 +1,12 @@
1
+ import type { OpenAPIV3 } from '@gitbook/openapi-parser';
2
+ import { type OpenAPISchemaPropertyEntry } from './OpenAPISchema';
3
+ import type { OpenAPIClientContext } from './types';
4
+ export declare function OpenAPISchemaProperties(props: {
5
+ id?: string;
6
+ properties: OpenAPISchemaPropertyEntry[];
7
+ context: OpenAPIClientContext;
8
+ }): import("react").JSX.Element;
9
+ export declare function OpenAPIRootSchema(props: {
10
+ schema: OpenAPIV3.SchemaObject;
11
+ context: OpenAPIClientContext;
12
+ }): import("react").JSX.Element;
@@ -0,0 +1,8 @@
1
+ import { OpenAPIRootSchemaFromServer, OpenAPISchemaPropertiesFromServer, } from './OpenAPISchema';
2
+ import { decycle } from './decycle';
3
+ export function OpenAPISchemaProperties(props) {
4
+ return (<OpenAPISchemaPropertiesFromServer id={props.id} properties={JSON.stringify(props.properties, decycle())} context={props.context}/>);
5
+ }
6
+ export function OpenAPIRootSchema(props) {
7
+ return (<OpenAPIRootSchemaFromServer schema={JSON.stringify(props.schema, decycle())} context={props.context}/>);
8
+ }
@@ -1,10 +1,4 @@
1
1
  import type { OpenAPIClientContext, OpenAPIOperationData } from './types';
2
- /**
3
- * Client component to render the spec for the request and response.
4
- *
5
- * We use a client component as rendering recursive JSON schema in the server is expensive
6
- * (the entire schema is rendered at once, while the client component only renders the visible part)
7
- */
8
2
  export declare function OpenAPISpec(props: {
9
3
  data: OpenAPIOperationData;
10
4
  context: OpenAPIClientContext;
@@ -1,15 +1,9 @@
1
1
  import { OpenAPIRequestBody } from './OpenAPIRequestBody';
2
2
  import { OpenAPIResponses } from './OpenAPIResponses';
3
- import { OpenAPISchemaProperties } from './OpenAPISchema';
3
+ import { OpenAPISchemaProperties } from './OpenAPISchemaServer';
4
4
  import { OpenAPISecurities } from './OpenAPISecurities';
5
5
  import { StaticSection } from './StaticSection';
6
6
  import { parameterToProperty } from './utils';
7
- /**
8
- * Client component to render the spec for the request and response.
9
- *
10
- * We use a client component as rendering recursive JSON schema in the server is expensive
11
- * (the entire schema is rendered at once, while the client component only renders the visible part)
12
- */
13
7
  export function OpenAPISpec(props) {
14
8
  var _a;
15
9
  var data = props.data, context = props.context;
@@ -17,16 +11,16 @@ export function OpenAPISpec(props) {
17
11
  var parameters = (_a = operation.parameters) !== null && _a !== void 0 ? _a : [];
18
12
  var parameterGroups = groupParameters(parameters);
19
13
  return (<>
20
- {securities.length > 0 ? (<OpenAPISecurities securities={securities} context={context}/>) : null}
14
+ {securities.length > 0 ? (<OpenAPISecurities key="securities" securities={securities} context={context}/>) : null}
21
15
 
22
16
  {parameterGroups.map(function (group) {
23
- return (<StaticSection key={group.key} className="openapi-parameters" header={group.label}>
17
+ return (<StaticSection key={"parameter-".concat(group.key)} className="openapi-parameters" header={group.label}>
24
18
  <OpenAPISchemaProperties properties={group.parameters.map(parameterToProperty)} context={context}/>
25
19
  </StaticSection>);
26
20
  })}
27
21
 
28
- {operation.requestBody ? (<OpenAPIRequestBody requestBody={operation.requestBody} context={context}/>) : null}
29
- {operation.responses ? (<OpenAPIResponses responses={operation.responses} context={context}/>) : null}
22
+ {operation.requestBody ? (<OpenAPIRequestBody key="body" requestBody={operation.requestBody} context={context}/>) : null}
23
+ {operation.responses ? (<OpenAPIResponses key="responses" responses={operation.responses} context={context}/>) : null}
30
24
  </>);
31
25
  }
32
26
  function groupParameters(parameters) {
@@ -102,16 +102,8 @@ export function OpenAPITabsPanels() {
102
102
  return null;
103
103
  }
104
104
  var key = selectedTab.key.toString();
105
- return (<TabPanel key={key} id={key} className="openapi-tabs-panel">
106
- {selectedTab.body}
107
- {selectedTab.footer ? (<OpenAPITabsPanelFooter>{selectedTab.footer}</OpenAPITabsPanelFooter>) : null}
105
+ return (<TabPanel id={key} className="openapi-tabs-panel">
106
+ <div className="openapi-tabs-body">{selectedTab.body}</div>
107
+ {selectedTab.footer ? (<div className="openapi-tabs-footer">{selectedTab.footer}</div>) : null}
108
108
  </TabPanel>);
109
109
  }
110
- /**
111
- * The OpenAPI Tabs panel footer component.
112
- * This component should be used as a child of the OpenAPITabs component.
113
- */
114
- function OpenAPITabsPanelFooter(props) {
115
- var children = props.children;
116
- return <div className="openapi-tabs-footer">{children}</div>;
117
- }
@@ -4,7 +4,7 @@ export interface CodeSampleInput {
4
4
  headers?: Record<string, string>;
5
5
  body?: any;
6
6
  }
7
- interface CodeSampleGenerator {
7
+ export interface CodeSampleGenerator {
8
8
  id: string;
9
9
  label: string;
10
10
  syntax: string;
@@ -15,4 +15,3 @@ export declare function parseHostAndPath(url: string): {
15
15
  host: string | undefined;
16
16
  path: string;
17
17
  };
18
- export {};
@@ -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,9 +202,13 @@ 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
- body = "--data-binary $'".concat(stringifyOpenAPI(body).replace(/"/g, ''), "'");
211
+ body = "--data-binary $'".concat(stringifyOpenAPI(body).replace(/"/g, '').replace(/\\n/g, '\n'), "'");
207
212
  }
208
213
  else if (isGraphQL(contentType)) {
209
214
  body = "--data '".concat(stringifyOpenAPI(body), "'");
@@ -215,7 +220,7 @@ var BodyGenerators = {
215
220
  body = "--data-binary '@".concat(String(body), "'");
216
221
  }
217
222
  else {
218
- body = "--data '".concat(stringifyOpenAPI(body, null, 2), "'");
223
+ body = "--data '".concat(stringifyOpenAPI(body, null, 2).replace(/\\n/g, '\n'), "'");
219
224
  }
220
225
  return {
221
226
  body: body,
@@ -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
+ }
@@ -1,5 +1,4 @@
1
1
  import type { OpenAPIV3 } from '@gitbook/openapi-parser';
2
- import { getExampleFromSchema } from '@scalar/oas-utils/spec-getters';
3
2
  type JSONValue = string | number | boolean | null | JSONValue[] | {
4
3
  [key: string]: JSONValue;
5
4
  };
@@ -12,5 +11,35 @@ export declare function generateSchemaExample(schema: OpenAPIV3.SchemaObject, op
12
11
  /**
13
12
  * Generate an example for a media type.
14
13
  */
15
- export declare function generateMediaTypeExample(mediaType: OpenAPIV3.MediaTypeObject, options?: GenerateSchemaExampleOptions): JSONValue | undefined;
14
+ export declare function generateMediaTypeExamples(mediaType: OpenAPIV3.MediaTypeObject, options?: GenerateSchemaExampleOptions): OpenAPIV3.ExampleObject[];
15
+ /**
16
+ * This function takes an OpenAPI schema and generates an example from it
17
+ * Forked from : https://github.com/scalar/scalar/blob/main/packages/oas-utils/src/spec-getters/getExampleFromSchema.ts
18
+ */
19
+ declare const getExampleFromSchema: (schema: Record<string, any>, options?: {
20
+ /**
21
+ * The fallback string for empty string values.
22
+ * @default ''
23
+ */
24
+ emptyString?: string;
25
+ /**
26
+ * Whether to use the XML tag names as keys
27
+ * @default false
28
+ */
29
+ xml?: boolean;
30
+ /**
31
+ * Whether to show read-only/write-only properties. Otherwise all properties are shown.
32
+ * @default undefined
33
+ */
34
+ mode?: "read" | "write";
35
+ /**
36
+ * Dynamic values to add to the example.
37
+ */
38
+ variables?: Record<string, any>;
39
+ /**
40
+ * Whether to omit empty and optional properties.
41
+ * @default false
42
+ */
43
+ omitEmptyAndOptionalProperties?: boolean;
44
+ }, level?: number, parentSchema?: Record<string, any>, name?: string) => any;
16
45
  export {};