@gitbook/react-openapi 1.0.4 → 1.1.0
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.
- package/CHANGELOG.md +22 -0
- package/dist/InteractiveSection.jsx +10 -9
- package/dist/OpenAPICodeSample.jsx +3 -3
- package/dist/OpenAPIDisclosure.d.ts +5 -9
- package/dist/OpenAPIDisclosure.jsx +25 -27
- package/dist/OpenAPIDisclosureGroup.d.ts +2 -2
- package/dist/OpenAPIDisclosureGroup.jsx +6 -5
- package/dist/OpenAPIPath.jsx +5 -1
- package/dist/OpenAPIResponseExample.jsx +8 -8
- package/dist/OpenAPIResponses.jsx +3 -3
- package/dist/OpenAPISchema.d.ts +3 -26
- package/dist/OpenAPISchema.jsx +80 -131
- package/dist/OpenAPISpec.jsx +3 -4
- package/dist/OpenAPITabs.jsx +51 -47
- package/dist/ScalarApiButton.d.ts +3 -2
- package/dist/ScalarApiButton.jsx +22 -18
- package/dist/StaticSection.d.ts +10 -0
- package/dist/StaticSection.jsx +23 -0
- package/dist/dereference.d.ts +5 -0
- package/dist/dereference.js +68 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +2 -1
- package/dist/models/OpenAPIModels.d.ts +9 -0
- package/dist/models/OpenAPIModels.jsx +62 -0
- package/dist/models/index.d.ts +2 -0
- package/dist/models/index.js +2 -0
- package/dist/models/resolveOpenAPIModels.d.ts +7 -0
- package/dist/models/resolveOpenAPIModels.js +73 -0
- package/dist/resolveOpenAPIOperation.d.ts +2 -2
- package/dist/resolveOpenAPIOperation.js +3 -34
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types.d.ts +8 -0
- package/dist/useSyncedTabsGlobalState.d.ts +10 -1
- package/dist/useSyncedTabsGlobalState.js +19 -15
- package/dist/utils.js +42 -3
- package/package.json +3 -3
- package/src/InteractiveSection.tsx +10 -18
- package/src/OpenAPICodeSample.tsx +3 -3
- package/src/OpenAPIDisclosure.tsx +35 -42
- package/src/OpenAPIDisclosureGroup.tsx +13 -11
- package/src/OpenAPIPath.tsx +7 -1
- package/src/OpenAPIResponseExample.tsx +8 -15
- package/src/OpenAPIResponses.tsx +3 -3
- package/src/OpenAPISchema.test.ts +26 -35
- package/src/OpenAPISchema.tsx +138 -227
- package/src/OpenAPISpec.tsx +3 -5
- package/src/OpenAPITabs.tsx +52 -63
- package/src/ScalarApiButton.tsx +26 -28
- package/src/StaticSection.tsx +59 -0
- package/src/dereference.ts +29 -0
- package/src/index.ts +3 -2
- package/src/models/OpenAPIModels.tsx +89 -0
- package/src/models/index.ts +2 -0
- package/src/models/resolveOpenAPIModels.ts +35 -0
- package/src/resolveOpenAPIOperation.ts +8 -36
- package/src/types.ts +10 -0
- package/src/useSyncedTabsGlobalState.ts +33 -21
- package/src/utils.ts +51 -3
package/src/utils.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { AnyObject, OpenAPIV3, OpenAPIV3_1 } from '@gitbook/openapi-parser';
|
|
2
|
+
import { stringifyOpenAPI } from './stringifyOpenAPI';
|
|
2
3
|
|
|
3
4
|
export function checkIsReference(
|
|
4
5
|
input: unknown
|
|
@@ -10,11 +11,19 @@ export function createStateKey(key: string, scope?: string) {
|
|
|
10
11
|
return scope ? `${scope}_${key}` : key;
|
|
11
12
|
}
|
|
12
13
|
|
|
14
|
+
/**
|
|
15
|
+
* Check if an object has a description. Either at the root level or in items.
|
|
16
|
+
*/
|
|
17
|
+
function hasDescription(object: AnyObject) {
|
|
18
|
+
return 'description' in object || 'x-gitbook-description-html' in object;
|
|
19
|
+
}
|
|
20
|
+
|
|
13
21
|
/**
|
|
14
22
|
* Resolve the description of an object.
|
|
15
23
|
*/
|
|
16
24
|
export function resolveDescription(object: OpenAPIV3.SchemaObject | AnyObject) {
|
|
17
|
-
|
|
25
|
+
// If the object has items and has a description, we resolve the description from items
|
|
26
|
+
if ('items' in object && typeof object.items === 'object' && hasDescription(object.items)) {
|
|
18
27
|
return resolveDescription(object.items);
|
|
19
28
|
}
|
|
20
29
|
|
|
@@ -50,9 +59,17 @@ export function resolveFirstExample(object: AnyObject) {
|
|
|
50
59
|
return object.examples[firstKey];
|
|
51
60
|
}
|
|
52
61
|
}
|
|
53
|
-
|
|
54
|
-
|
|
62
|
+
|
|
63
|
+
// Resolve top level example first
|
|
64
|
+
if (shouldDisplayExample(object)) {
|
|
65
|
+
return formatExample(object.example);
|
|
55
66
|
}
|
|
67
|
+
|
|
68
|
+
// Resolve example from items if it exists
|
|
69
|
+
if (object.items && typeof object.items === 'object') {
|
|
70
|
+
return formatExample(object.items.example);
|
|
71
|
+
}
|
|
72
|
+
|
|
56
73
|
return undefined;
|
|
57
74
|
}
|
|
58
75
|
|
|
@@ -98,3 +115,34 @@ export function parameterToProperty(
|
|
|
98
115
|
required: parameter.required,
|
|
99
116
|
};
|
|
100
117
|
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Format the example of a schema.
|
|
121
|
+
*/
|
|
122
|
+
function formatExample(example: unknown): string {
|
|
123
|
+
if (typeof example === 'string') {
|
|
124
|
+
return example
|
|
125
|
+
.replace(/\n/g, ' ') // Replace newlines with spaces
|
|
126
|
+
.replace(/\s+/g, ' ') // Collapse multiple spaces/newlines into a single space
|
|
127
|
+
.replace(/([\{\}:,])\s+/g, '$1 ') // Ensure a space after {, }, :, and ,
|
|
128
|
+
.replace(/\s+([\{\}:,])/g, ' $1') // Ensure a space before {, }, :, and ,
|
|
129
|
+
.trim();
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return stringifyOpenAPI(example);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Check if an example should be displayed.
|
|
137
|
+
*/
|
|
138
|
+
function shouldDisplayExample(schema: OpenAPIV3.SchemaObject): boolean {
|
|
139
|
+
return (
|
|
140
|
+
(typeof schema.example === 'string' && !!schema.example) ||
|
|
141
|
+
typeof schema.example === 'number' ||
|
|
142
|
+
typeof schema.example === 'boolean' ||
|
|
143
|
+
(Array.isArray(schema.example) && schema.example.length > 0) ||
|
|
144
|
+
(typeof schema.example === 'object' &&
|
|
145
|
+
schema.example !== null &&
|
|
146
|
+
Object.keys(schema.example).length > 0)
|
|
147
|
+
);
|
|
148
|
+
}
|