@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.
Files changed (58) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/InteractiveSection.jsx +10 -9
  3. package/dist/OpenAPICodeSample.jsx +3 -3
  4. package/dist/OpenAPIDisclosure.d.ts +5 -9
  5. package/dist/OpenAPIDisclosure.jsx +25 -27
  6. package/dist/OpenAPIDisclosureGroup.d.ts +2 -2
  7. package/dist/OpenAPIDisclosureGroup.jsx +6 -5
  8. package/dist/OpenAPIPath.jsx +5 -1
  9. package/dist/OpenAPIResponseExample.jsx +8 -8
  10. package/dist/OpenAPIResponses.jsx +3 -3
  11. package/dist/OpenAPISchema.d.ts +3 -26
  12. package/dist/OpenAPISchema.jsx +80 -131
  13. package/dist/OpenAPISpec.jsx +3 -4
  14. package/dist/OpenAPITabs.jsx +51 -47
  15. package/dist/ScalarApiButton.d.ts +3 -2
  16. package/dist/ScalarApiButton.jsx +22 -18
  17. package/dist/StaticSection.d.ts +10 -0
  18. package/dist/StaticSection.jsx +23 -0
  19. package/dist/dereference.d.ts +5 -0
  20. package/dist/dereference.js +68 -0
  21. package/dist/index.d.ts +3 -2
  22. package/dist/index.js +2 -1
  23. package/dist/models/OpenAPIModels.d.ts +9 -0
  24. package/dist/models/OpenAPIModels.jsx +62 -0
  25. package/dist/models/index.d.ts +2 -0
  26. package/dist/models/index.js +2 -0
  27. package/dist/models/resolveOpenAPIModels.d.ts +7 -0
  28. package/dist/models/resolveOpenAPIModels.js +73 -0
  29. package/dist/resolveOpenAPIOperation.d.ts +2 -2
  30. package/dist/resolveOpenAPIOperation.js +3 -34
  31. package/dist/tsconfig.build.tsbuildinfo +1 -1
  32. package/dist/types.d.ts +8 -0
  33. package/dist/useSyncedTabsGlobalState.d.ts +10 -1
  34. package/dist/useSyncedTabsGlobalState.js +19 -15
  35. package/dist/utils.js +42 -3
  36. package/package.json +3 -3
  37. package/src/InteractiveSection.tsx +10 -18
  38. package/src/OpenAPICodeSample.tsx +3 -3
  39. package/src/OpenAPIDisclosure.tsx +35 -42
  40. package/src/OpenAPIDisclosureGroup.tsx +13 -11
  41. package/src/OpenAPIPath.tsx +7 -1
  42. package/src/OpenAPIResponseExample.tsx +8 -15
  43. package/src/OpenAPIResponses.tsx +3 -3
  44. package/src/OpenAPISchema.test.ts +26 -35
  45. package/src/OpenAPISchema.tsx +138 -227
  46. package/src/OpenAPISpec.tsx +3 -5
  47. package/src/OpenAPITabs.tsx +52 -63
  48. package/src/ScalarApiButton.tsx +26 -28
  49. package/src/StaticSection.tsx +59 -0
  50. package/src/dereference.ts +29 -0
  51. package/src/index.ts +3 -2
  52. package/src/models/OpenAPIModels.tsx +89 -0
  53. package/src/models/index.ts +2 -0
  54. package/src/models/resolveOpenAPIModels.ts +35 -0
  55. package/src/resolveOpenAPIOperation.ts +8 -36
  56. package/src/types.ts +10 -0
  57. package/src/useSyncedTabsGlobalState.ts +33 -21
  58. 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
- if ('items' in object && object.items) {
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
- if ('example' in object && object.example !== undefined) {
54
- return object.example;
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
+ }