@gitbook/react-openapi 1.0.2 → 1.0.4

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 (72) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/dist/OpenAPICodeSample.jsx +17 -16
  3. package/dist/OpenAPIDisclosure.d.ts +2 -1
  4. package/dist/OpenAPIDisclosure.jsx +1 -1
  5. package/dist/OpenAPIDisclosureGroup.d.ts +1 -1
  6. package/dist/OpenAPIDisclosureGroup.jsx +2 -2
  7. package/dist/OpenAPIOperation.jsx +21 -7
  8. package/dist/OpenAPIPath.d.ts +3 -2
  9. package/dist/OpenAPIPath.jsx +4 -15
  10. package/dist/OpenAPIRequestBody.jsx +1 -1
  11. package/dist/OpenAPIResponse.jsx +1 -1
  12. package/dist/OpenAPIResponseExample.jsx +6 -5
  13. package/dist/OpenAPIResponses.d.ts +1 -1
  14. package/dist/OpenAPIResponses.jsx +2 -2
  15. package/dist/OpenAPISchema.d.ts +5 -1
  16. package/dist/OpenAPISchema.jsx +72 -61
  17. package/dist/OpenAPISchemaName.d.ts +5 -3
  18. package/dist/OpenAPISchemaName.jsx +25 -4
  19. package/dist/OpenAPISecurities.jsx +2 -2
  20. package/dist/OpenAPITabs.d.ts +3 -3
  21. package/dist/OpenAPITabs.jsx +17 -14
  22. package/dist/ScalarApiButton.jsx +1 -1
  23. package/dist/code-samples.js +239 -17
  24. package/dist/contentTypeChecks.d.ts +9 -0
  25. package/dist/contentTypeChecks.js +27 -0
  26. package/dist/generateSchemaExample.js +2 -1
  27. package/dist/resolveOpenAPIOperation.d.ts +3 -3
  28. package/dist/resolveOpenAPIOperation.js +1 -1
  29. package/dist/stringifyOpenAPI.d.ts +1 -1
  30. package/dist/stringifyOpenAPI.js +8 -2
  31. package/dist/tsconfig.build.tsbuildinfo +1 -1
  32. package/dist/types.d.ts +14 -2
  33. package/dist/util/server.d.ts +9 -0
  34. package/dist/util/server.js +44 -0
  35. package/dist/utils.d.ts +2 -2
  36. package/dist/utils.js +7 -6
  37. package/package.json +3 -8
  38. package/src/InteractiveSection.tsx +4 -4
  39. package/src/OpenAPICodeSample.tsx +20 -19
  40. package/src/OpenAPIDisclosure.tsx +4 -3
  41. package/src/OpenAPIDisclosureGroup.tsx +5 -5
  42. package/src/OpenAPIOperation.tsx +32 -10
  43. package/src/OpenAPIOperationContext.tsx +1 -1
  44. package/src/OpenAPIPath.tsx +11 -10
  45. package/src/OpenAPIRequestBody.tsx +2 -2
  46. package/src/OpenAPIResponse.tsx +3 -3
  47. package/src/OpenAPIResponseExample.tsx +7 -6
  48. package/src/OpenAPIResponses.tsx +4 -4
  49. package/src/OpenAPISchema.test.ts +5 -5
  50. package/src/OpenAPISchema.tsx +134 -73
  51. package/src/OpenAPISchemaName.tsx +40 -7
  52. package/src/OpenAPISecurities.tsx +3 -3
  53. package/src/OpenAPITabs.tsx +23 -17
  54. package/src/ScalarApiButton.tsx +3 -3
  55. package/src/code-samples.test.ts +594 -2
  56. package/src/code-samples.ts +238 -17
  57. package/src/contentTypeChecks.ts +35 -0
  58. package/src/generateSchemaExample.ts +22 -18
  59. package/src/json2xml.test.ts +1 -1
  60. package/src/resolveOpenAPIOperation.test.ts +6 -6
  61. package/src/resolveOpenAPIOperation.ts +7 -7
  62. package/src/stringifyOpenAPI.ts +13 -2
  63. package/src/types.ts +11 -1
  64. package/src/util/server.test.ts +58 -0
  65. package/src/util/server.ts +47 -0
  66. package/src/utils.ts +9 -5
  67. package/dist/OpenAPIServerURL.d.ts +0 -11
  68. package/dist/OpenAPIServerURL.jsx +0 -67
  69. package/dist/OpenAPIServerURLVariable.d.ts +0 -8
  70. package/dist/OpenAPIServerURLVariable.jsx +0 -8
  71. package/src/OpenAPIServerURL.tsx +0 -73
  72. package/src/OpenAPIServerURLVariable.tsx +0 -14
package/src/utils.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import type { AnyObject, OpenAPIV3, OpenAPIV3_1 } from '@gitbook/openapi-parser';
2
2
 
3
3
  export function checkIsReference(
4
- input: unknown,
4
+ input: unknown
5
5
  ): input is OpenAPIV3.ReferenceObject | OpenAPIV3_1.ReferenceObject {
6
6
  return typeof input === 'object' && !!input && '$ref' in input;
7
7
  }
@@ -13,7 +13,11 @@ export function createStateKey(key: string, scope?: string) {
13
13
  /**
14
14
  * Resolve the description of an object.
15
15
  */
16
- export function resolveDescription(object: AnyObject) {
16
+ export function resolveDescription(object: OpenAPIV3.SchemaObject | AnyObject) {
17
+ if ('items' in object && object.items) {
18
+ return resolveDescription(object.items);
19
+ }
20
+
17
21
  return 'x-gitbook-description-html' in object &&
18
22
  typeof object['x-gitbook-description-html'] === 'string'
19
23
  ? object['x-gitbook-description-html'].trim()
@@ -28,7 +32,7 @@ export function resolveDescription(object: AnyObject) {
28
32
  export function extractDescriptions(object: AnyObject) {
29
33
  return {
30
34
  description: object.description,
31
- ['x-gitbook-description-html']:
35
+ 'x-gitbook-description-html':
32
36
  'x-gitbook-description-html' in object
33
37
  ? object['x-gitbook-description-html']
34
38
  : undefined,
@@ -57,7 +61,7 @@ export function resolveFirstExample(object: AnyObject) {
57
61
  * Extract the description, example and deprecated from parameter.
58
62
  */
59
63
  export function resolveParameterSchema(
60
- parameter: OpenAPIV3.ParameterBaseObject,
64
+ parameter: OpenAPIV3.ParameterBaseObject
61
65
  ): OpenAPIV3.SchemaObject {
62
66
  const schema = checkIsReference(parameter.schema) ? undefined : parameter.schema;
63
67
  return {
@@ -75,7 +79,7 @@ export function resolveParameterSchema(
75
79
  * Transform a parameter object to a property object.
76
80
  */
77
81
  export function parameterToProperty(
78
- parameter: OpenAPIV3.ParameterObject | OpenAPIV3.ReferenceObject | OpenAPIV3_1.ReferenceObject,
82
+ parameter: OpenAPIV3.ParameterObject | OpenAPIV3.ReferenceObject | OpenAPIV3_1.ReferenceObject
79
83
  ): {
80
84
  propertyName: string | undefined;
81
85
  schema: OpenAPIV3.SchemaObject;
@@ -1,11 +0,0 @@
1
- import type { OpenAPIV3 } from '@gitbook/openapi-parser';
2
- /**
3
- * Show the url of the server with variables replaced by their default values.
4
- */
5
- export declare function OpenAPIServerURL(props: {
6
- servers: OpenAPIV3.ServerObject[];
7
- }): import("react").JSX.Element | null;
8
- /**
9
- * Get the default URL for the server.
10
- */
11
- export declare function getServersURL(servers: OpenAPIV3.ServerObject[]): string;
@@ -1,67 +0,0 @@
1
- import { OpenAPIServerURLVariable } from './OpenAPIServerURLVariable';
2
- /**
3
- * Show the url of the server with variables replaced by their default values.
4
- */
5
- export function OpenAPIServerURL(props) {
6
- var _a;
7
- var servers = props.servers;
8
- var server = servers[0];
9
- if (!server) {
10
- return null;
11
- }
12
- var parts = parseServerURL((_a = server === null || server === void 0 ? void 0 : server.url) !== null && _a !== void 0 ? _a : '');
13
- return (<span>
14
- {parts.map(function (part, i) {
15
- var _a;
16
- if (part.kind === 'text') {
17
- return <span key={i}>{part.text}</span>;
18
- }
19
- else {
20
- var variable = (_a = server.variables) === null || _a === void 0 ? void 0 : _a[part.name];
21
- if (!variable) {
22
- return <span key={i}>{"{".concat(part.name, "}")}</span>;
23
- }
24
- return (<OpenAPIServerURLVariable key={i} name={part.name} variable={variable}/>);
25
- }
26
- })}
27
- </span>);
28
- }
29
- /**
30
- * Get the default URL for the server.
31
- */
32
- export function getServersURL(servers) {
33
- var _a;
34
- var server = servers[0];
35
- if (!server) {
36
- return '';
37
- }
38
- var parts = parseServerURL((_a = server === null || server === void 0 ? void 0 : server.url) !== null && _a !== void 0 ? _a : '');
39
- return parts
40
- .map(function (part) {
41
- var _a, _b, _c;
42
- if (part.kind === 'text') {
43
- return part.text;
44
- }
45
- else {
46
- return (_c = (_b = (_a = server.variables) === null || _a === void 0 ? void 0 : _a[part.name]) === null || _b === void 0 ? void 0 : _b.default) !== null && _c !== void 0 ? _c : "{".concat(part.name, "}");
47
- }
48
- })
49
- .join('');
50
- }
51
- function parseServerURL(url) {
52
- var parts = url.split(/{([^}]+)}/g);
53
- var result = [];
54
- for (var i = 0; i < parts.length; i++) {
55
- var part = parts[i];
56
- if (!part) {
57
- continue;
58
- }
59
- if (i % 2 === 0) {
60
- result.push({ kind: 'text', text: part });
61
- }
62
- else {
63
- result.push({ kind: 'variable', name: part });
64
- }
65
- }
66
- return result;
67
- }
@@ -1,8 +0,0 @@
1
- import type { OpenAPIV3 } from '@gitbook/openapi-parser';
2
- /**
3
- * Interactive component to show the value of a server variable and let the user change it.
4
- */
5
- export declare function OpenAPIServerURLVariable(props: {
6
- name: string;
7
- variable: OpenAPIV3.ServerVariableObject;
8
- }): import("react").JSX.Element;
@@ -1,8 +0,0 @@
1
- 'use client';
2
- /**
3
- * Interactive component to show the value of a server variable and let the user change it.
4
- */
5
- export function OpenAPIServerURLVariable(props) {
6
- var variable = props.variable;
7
- return <span className="openapi-url-var">{variable.default}</span>;
8
- }
@@ -1,73 +0,0 @@
1
- import type { OpenAPIV3 } from '@gitbook/openapi-parser';
2
- import { OpenAPIServerURLVariable } from './OpenAPIServerURLVariable';
3
-
4
- /**
5
- * Show the url of the server with variables replaced by their default values.
6
- */
7
- export function OpenAPIServerURL(props: { servers: OpenAPIV3.ServerObject[] }) {
8
- const { servers } = props;
9
- const server = servers[0];
10
-
11
- if (!server) {
12
- return null;
13
- }
14
-
15
- const parts = parseServerURL(server?.url ?? '');
16
-
17
- return (
18
- <span>
19
- {parts.map((part, i) => {
20
- if (part.kind === 'text') {
21
- return <span key={i}>{part.text}</span>;
22
- } else {
23
- const variable = server.variables?.[part.name];
24
- if (!variable) {
25
- return <span key={i}>{`{${part.name}}`}</span>;
26
- }
27
-
28
- return (
29
- <OpenAPIServerURLVariable key={i} name={part.name} variable={variable} />
30
- );
31
- }
32
- })}
33
- </span>
34
- );
35
- }
36
-
37
- /**
38
- * Get the default URL for the server.
39
- */
40
- export function getServersURL(servers: OpenAPIV3.ServerObject[]): string {
41
- const server = servers[0];
42
- if (!server) {
43
- return '';
44
- }
45
- const parts = parseServerURL(server?.url ?? '');
46
-
47
- return parts
48
- .map((part) => {
49
- if (part.kind === 'text') {
50
- return part.text;
51
- } else {
52
- return server.variables?.[part.name]?.default ?? `{${part.name}}`;
53
- }
54
- })
55
- .join('');
56
- }
57
-
58
- function parseServerURL(url: string) {
59
- const parts = url.split(/{([^}]+)}/g);
60
- const result: Array<{ kind: 'variable'; name: string } | { kind: 'text'; text: string }> = [];
61
- for (let i = 0; i < parts.length; i++) {
62
- const part = parts[i];
63
- if (!part) {
64
- continue;
65
- }
66
- if (i % 2 === 0) {
67
- result.push({ kind: 'text', text: part });
68
- } else {
69
- result.push({ kind: 'variable', name: part });
70
- }
71
- }
72
- return result;
73
- }
@@ -1,14 +0,0 @@
1
- 'use client';
2
-
3
- import type { OpenAPIV3 } from '@gitbook/openapi-parser';
4
-
5
- /**
6
- * Interactive component to show the value of a server variable and let the user change it.
7
- */
8
- export function OpenAPIServerURLVariable(props: {
9
- name: string;
10
- variable: OpenAPIV3.ServerVariableObject;
11
- }) {
12
- const { variable } = props;
13
- return <span className="openapi-url-var">{variable.default}</span>;
14
- }