@gitbook/react-openapi 1.0.1 → 1.0.2

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,18 @@
1
+ // Bun Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`getUrlFromServerState indents correctly 1`] = `
4
+ "<?xml version="1.0"?>
5
+ <id>10</id>
6
+ <name>doggie</name>
7
+ <category>
8
+ <id>1</id>
9
+ <name>Dogs</name>
10
+ </category>
11
+ <photoUrls>string</photoUrls>
12
+ <tags>
13
+ <id>0</id>
14
+ <name>string</name>
15
+ </tags>
16
+ <status>available</status>
17
+ "
18
+ `;
@@ -3,18 +3,21 @@ import { getExampleFromSchema } from '@scalar/oas-utils/spec-getters';
3
3
 
4
4
  type JSONValue = string | number | boolean | null | JSONValue[] | { [key: string]: JSONValue };
5
5
 
6
+ type ScalarGetExampleFromSchemaOptions = NonNullable<Parameters<typeof getExampleFromSchema>[1]>;
7
+ type GenerateSchemaExampleOptions = Pick<
8
+ ScalarGetExampleFromSchemaOptions,
9
+ 'xml' | 'omitEmptyAndOptionalProperties' | 'mode'
10
+ >;
11
+
6
12
  /**
7
13
  * Generate a JSON example from a schema
8
14
  */
9
15
  export function generateSchemaExample(
10
16
  schema: OpenAPIV3.SchemaObject,
11
- options: {
12
- onlyRequired?: boolean;
13
- } = {},
17
+ options?: GenerateSchemaExampleOptions,
14
18
  ): JSONValue | undefined {
15
19
  return getExampleFromSchema(schema, {
16
20
  emptyString: 'text',
17
- omitEmptyAndOptionalProperties: options.onlyRequired,
18
21
  variables: {
19
22
  'date-time': new Date().toISOString(),
20
23
  date: new Date().toISOString().split('T')[0],
@@ -28,6 +31,7 @@ export function generateSchemaExample(
28
31
  byte: 'Ynl0ZXM=',
29
32
  password: 'password',
30
33
  },
34
+ ...options,
31
35
  });
32
36
  }
33
37
 
@@ -36,9 +40,7 @@ export function generateSchemaExample(
36
40
  */
37
41
  export function generateMediaTypeExample(
38
42
  mediaType: OpenAPIV3.MediaTypeObject,
39
- options: {
40
- onlyRequired?: boolean;
41
- } = {},
43
+ options?: GenerateSchemaExampleOptions,
42
44
  ): JSONValue | undefined {
43
45
  if (mediaType.example) {
44
46
  return mediaType.example;
@@ -0,0 +1,46 @@
1
+ import { describe, expect, it } from 'bun:test';
2
+
3
+ import { json2xml } from './json2xml';
4
+
5
+ describe('getUrlFromServerState', () => {
6
+ it('transforms JSON to xml', () => {
7
+ const xml = json2xml({
8
+ foo: 'bar',
9
+ });
10
+
11
+ expect(xml).toBe('<?xml version="1.0"?>\n<foo>bar</foo>\n');
12
+ });
13
+
14
+ it('wraps array items', () => {
15
+ const xml = json2xml({
16
+ urls: {
17
+ url: ['https://example.com', 'https://example.com'],
18
+ },
19
+ });
20
+
21
+ expect(xml).toBe(
22
+ '<?xml version="1.0"?>\n<urls>\n\t<url>https://example.com</url>\n\t<url>https://example.com</url>\n</urls>\n',
23
+ );
24
+ });
25
+
26
+ it('indents correctly', () => {
27
+ const xml = json2xml({
28
+ id: 10,
29
+ name: 'doggie',
30
+ category: {
31
+ id: 1,
32
+ name: 'Dogs',
33
+ },
34
+ photoUrls: ['string'],
35
+ tags: [
36
+ {
37
+ id: 0,
38
+ name: 'string',
39
+ },
40
+ ],
41
+ status: 'available',
42
+ });
43
+
44
+ expect(xml).toMatchSnapshot();
45
+ });
46
+ });
@@ -0,0 +1,8 @@
1
+ import { jsXml } from 'json-xml-parse';
2
+
3
+ /**
4
+ * This function converts an object to XML.
5
+ */
6
+ export function json2xml(data: Record<string, any>) {
7
+ return jsXml.toXmlString(data, { beautify: true });
8
+ }
@@ -9,7 +9,7 @@ async function fetchFilesystem(url: string) {
9
9
  const filesystem = await parseOpenAPI({ value: text, rootURL: url });
10
10
  const transformedFs = await traverse(filesystem, async (node) => {
11
11
  if ('description' in node && typeof node.description === 'string' && node.description) {
12
- node['x-description-html'] = node.description;
12
+ node['x-gitbook-description-html'] = node.description;
13
13
  }
14
14
  return node;
15
15
  });
package/src/types.ts CHANGED
@@ -6,6 +6,7 @@ import type {
6
6
 
7
7
  export interface OpenAPIContextProps extends OpenAPIClientContext {
8
8
  CodeBlock: React.ComponentType<{ code: string; syntax: string }>;
9
+ renderHeading: (props: { deprecated: boolean; title: string }) => React.ReactNode;
9
10
 
10
11
  /** Spec url for the Scalar Api Client */
11
12
  specUrl: string;
package/src/utils.ts CHANGED
@@ -1,6 +1,8 @@
1
- import type { AnyObject, OpenAPIV3 } from '@gitbook/openapi-parser';
1
+ import type { AnyObject, OpenAPIV3, OpenAPIV3_1 } from '@gitbook/openapi-parser';
2
2
 
3
- export function checkIsReference(input: unknown): input is OpenAPIV3.ReferenceObject {
3
+ export function checkIsReference(
4
+ input: unknown,
5
+ ): input is OpenAPIV3.ReferenceObject | OpenAPIV3_1.ReferenceObject {
4
6
  return typeof input === 'object' && !!input && '$ref' in input;
5
7
  }
6
8
 
@@ -12,9 +14,83 @@ export function createStateKey(key: string, scope?: string) {
12
14
  * Resolve the description of an object.
13
15
  */
14
16
  export function resolveDescription(object: AnyObject) {
15
- return 'x-description-html' in object && typeof object['x-description-html'] === 'string'
16
- ? object['x-description-html']
17
+ return 'x-gitbook-description-html' in object &&
18
+ typeof object['x-gitbook-description-html'] === 'string'
19
+ ? object['x-gitbook-description-html'].trim()
17
20
  : typeof object.description === 'string'
18
- ? object.description
21
+ ? object.description.trim()
19
22
  : undefined;
20
23
  }
24
+
25
+ /**
26
+ * Extract descriptions from an object.
27
+ */
28
+ export function extractDescriptions(object: AnyObject) {
29
+ return {
30
+ description: object.description,
31
+ ['x-gitbook-description-html']:
32
+ 'x-gitbook-description-html' in object
33
+ ? object['x-gitbook-description-html']
34
+ : undefined,
35
+ };
36
+ }
37
+
38
+ /**
39
+ * Resolve the first example from an object.
40
+ */
41
+ export function resolveFirstExample(object: AnyObject) {
42
+ if ('examples' in object && typeof object.examples === 'object' && object.examples) {
43
+ const keys = Object.keys(object.examples);
44
+ const firstKey = keys[0];
45
+ if (firstKey && object.examples[firstKey]) {
46
+ return object.examples[firstKey];
47
+ }
48
+ }
49
+ if ('example' in object && object.example !== undefined) {
50
+ return object.example;
51
+ }
52
+ return undefined;
53
+ }
54
+
55
+ /**
56
+ * Resolve the schema of a parameter.
57
+ * Extract the description, example and deprecated from parameter.
58
+ */
59
+ export function resolveParameterSchema(
60
+ parameter: OpenAPIV3.ParameterBaseObject,
61
+ ): OpenAPIV3.SchemaObject {
62
+ const schema = checkIsReference(parameter.schema) ? undefined : parameter.schema;
63
+ return {
64
+ // Description of the parameter is defined at the parameter level
65
+ // we use display it if the schema doesn't override it
66
+ ...extractDescriptions(parameter),
67
+ example: resolveFirstExample(parameter),
68
+ // Deprecated can be defined at the parameter level
69
+ deprecated: parameter.deprecated,
70
+ ...schema,
71
+ };
72
+ }
73
+
74
+ /**
75
+ * Transform a parameter object to a property object.
76
+ */
77
+ export function parameterToProperty(
78
+ parameter: OpenAPIV3.ParameterObject | OpenAPIV3.ReferenceObject | OpenAPIV3_1.ReferenceObject,
79
+ ): {
80
+ propertyName: string | undefined;
81
+ schema: OpenAPIV3.SchemaObject;
82
+ required: boolean | undefined;
83
+ } {
84
+ if (checkIsReference(parameter)) {
85
+ return {
86
+ propertyName: parameter.$ref ?? 'Unknown ref',
87
+ schema: {},
88
+ required: undefined,
89
+ };
90
+ }
91
+ return {
92
+ propertyName: parameter.name,
93
+ schema: resolveParameterSchema(parameter),
94
+ required: parameter.required,
95
+ };
96
+ }