@gitbook/react-openapi 1.4.3 → 1.5.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.
Files changed (263) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/dist/InteractiveSection.js +59 -0
  3. package/dist/Markdown.js +10 -0
  4. package/dist/OpenAPICodeSample.js +219 -0
  5. package/dist/OpenAPICodeSampleInteractive.js +66 -0
  6. package/dist/OpenAPICodeSampleSelector.js +45 -0
  7. package/dist/OpenAPICopyButton.js +39 -0
  8. package/dist/OpenAPIDisclosure.js +30 -0
  9. package/dist/OpenAPIDisclosureGroup.js +75 -0
  10. package/dist/OpenAPIExample.js +41 -0
  11. package/dist/OpenAPIMediaType.js +58 -0
  12. package/dist/OpenAPIOperation.d.ts +12 -7
  13. package/dist/OpenAPIOperation.js +30 -0
  14. package/dist/OpenAPIOperationContext.d.ts +10 -6
  15. package/dist/OpenAPIOperationContext.js +30 -0
  16. package/dist/OpenAPIPath.js +51 -0
  17. package/dist/OpenAPIPrefillContextProvider.d.ts +11 -7
  18. package/dist/OpenAPIPrefillContextProvider.js +25 -0
  19. package/dist/OpenAPIRequestBody.js +28 -0
  20. package/dist/OpenAPIRequestBodyHeaderType.js +23 -0
  21. package/dist/OpenAPIResponse.js +39 -0
  22. package/dist/OpenAPIResponseExample.js +75 -0
  23. package/dist/OpenAPIResponseExampleContent.js +61 -0
  24. package/dist/OpenAPIResponses.js +61 -0
  25. package/dist/OpenAPISchema.js +373 -0
  26. package/dist/OpenAPISchemaName.js +45 -0
  27. package/dist/OpenAPISchemaServer.js +13 -0
  28. package/dist/OpenAPISecurities.js +124 -0
  29. package/dist/OpenAPISelect.js +45 -0
  30. package/dist/OpenAPISpec.js +73 -0
  31. package/dist/OpenAPIWebhook.d.ts +12 -7
  32. package/dist/OpenAPIWebhook.js +28 -0
  33. package/dist/OpenAPIWebhookExample.js +40 -0
  34. package/dist/ScalarApiButton.js +87 -0
  35. package/dist/StaticSection.js +37 -0
  36. package/dist/code-samples.js +267 -419
  37. package/dist/common/OpenAPIColumnSpec.js +23 -0
  38. package/dist/common/OpenAPIOperationDescription.js +18 -0
  39. package/dist/common/OpenAPIStability.js +17 -0
  40. package/dist/common/OpenAPISummary.js +27 -0
  41. package/dist/contentTypeChecks.js +24 -20
  42. package/dist/context.d.ts +68 -72
  43. package/dist/context.js +25 -39
  44. package/dist/decycle.js +39 -68
  45. package/dist/dereference.js +20 -64
  46. package/dist/generateSchemaExample.js +188 -332
  47. package/dist/getDisclosureLabel.js +15 -16
  48. package/dist/getOrCreateStoreByKey.js +20 -17
  49. package/dist/index.d.ts +12 -10
  50. package/dist/index.js +11 -8
  51. package/dist/json2xml.js +10 -5
  52. package/dist/resolveOpenAPIOperation.d.ts +11 -7
  53. package/dist/resolveOpenAPIOperation.js +88 -159
  54. package/dist/resolveOpenAPIWebhook.d.ts +11 -7
  55. package/dist/resolveOpenAPIWebhook.js +41 -116
  56. package/dist/schemas/OpenAPISchemaItem.js +26 -0
  57. package/dist/schemas/OpenAPISchemas.d.ts +16 -11
  58. package/dist/schemas/OpenAPISchemas.js +57 -0
  59. package/dist/schemas/resolveOpenAPISchemas.d.ts +9 -4
  60. package/dist/schemas/resolveOpenAPISchemas.js +15 -59
  61. package/dist/stringifyOpenAPI.js +12 -13
  62. package/dist/translate.js +43 -0
  63. package/dist/translations/de.js +47 -42
  64. package/dist/translations/en.d.ts +46 -42
  65. package/dist/translations/en.js +47 -42
  66. package/dist/translations/es.js +47 -42
  67. package/dist/translations/fr.js +47 -42
  68. package/dist/translations/index.d.ts +404 -391
  69. package/dist/translations/index.js +28 -24
  70. package/dist/translations/ja.js +47 -42
  71. package/dist/translations/nl.js +47 -42
  72. package/dist/translations/no.js +47 -42
  73. package/dist/translations/pt-br.js +47 -42
  74. package/dist/translations/types.d.ts +7 -5
  75. package/dist/translations/zh.js +47 -42
  76. package/dist/types.d.ts +30 -24
  77. package/dist/util/example.js +84 -0
  78. package/dist/util/server.js +32 -38
  79. package/dist/util/tryit-prefill.js +135 -121
  80. package/dist/utils.js +135 -196
  81. package/package.json +18 -11
  82. package/dist/InteractiveSection.d.ts +0 -33
  83. package/dist/InteractiveSection.jsx +0 -61
  84. package/dist/Markdown.d.ts +0 -4
  85. package/dist/Markdown.jsx +0 -5
  86. package/dist/OpenAPICodeSample.d.ts +0 -19
  87. package/dist/OpenAPICodeSample.jsx +0 -230
  88. package/dist/OpenAPICodeSampleInteractive.d.ts +0 -14
  89. package/dist/OpenAPICodeSampleInteractive.jsx +0 -73
  90. package/dist/OpenAPICodeSampleSelector.d.ts +0 -14
  91. package/dist/OpenAPICodeSampleSelector.jsx +0 -44
  92. package/dist/OpenAPICopyButton.d.ts +0 -13
  93. package/dist/OpenAPICopyButton.jsx +0 -35
  94. package/dist/OpenAPIDisclosure.d.ts +0 -11
  95. package/dist/OpenAPIDisclosure.jsx +0 -30
  96. package/dist/OpenAPIDisclosureGroup.d.ts +0 -23
  97. package/dist/OpenAPIDisclosureGroup.jsx +0 -83
  98. package/dist/OpenAPIExample.d.ts +0 -16
  99. package/dist/OpenAPIExample.jsx +0 -36
  100. package/dist/OpenAPIMediaType.d.ts +0 -21
  101. package/dist/OpenAPIMediaType.jsx +0 -61
  102. package/dist/OpenAPIOperation.jsx +0 -25
  103. package/dist/OpenAPIOperationContext.jsx +0 -26
  104. package/dist/OpenAPIOperationDescription.d.ts +0 -9
  105. package/dist/OpenAPIOperationDescription.jsx +0 -22
  106. package/dist/OpenAPIOperationStability.d.ts +0 -9
  107. package/dist/OpenAPIOperationStability.jsx +0 -27
  108. package/dist/OpenAPIPath.d.ts +0 -18
  109. package/dist/OpenAPIPath.jsx +0 -55
  110. package/dist/OpenAPIPrefillContextProvider.jsx +0 -19
  111. package/dist/OpenAPIRequestBody.d.ts +0 -11
  112. package/dist/OpenAPIRequestBody.jsx +0 -28
  113. package/dist/OpenAPIRequestBodyHeaderType.d.ts +0 -8
  114. package/dist/OpenAPIRequestBodyHeaderType.jsx +0 -25
  115. package/dist/OpenAPIResponse.d.ts +0 -10
  116. package/dist/OpenAPIResponse.jsx +0 -57
  117. package/dist/OpenAPIResponseExample.d.ts +0 -9
  118. package/dist/OpenAPIResponseExample.jsx +0 -105
  119. package/dist/OpenAPIResponseExampleContent.d.ts +0 -22
  120. package/dist/OpenAPIResponseExampleContent.jsx +0 -60
  121. package/dist/OpenAPIResponses.d.ts +0 -9
  122. package/dist/OpenAPIResponses.jsx +0 -77
  123. package/dist/OpenAPISchema.d.ts +0 -27
  124. package/dist/OpenAPISchema.jsx +0 -400
  125. package/dist/OpenAPISchemaName.d.ts +0 -16
  126. package/dist/OpenAPISchemaName.jsx +0 -43
  127. package/dist/OpenAPISchemaServer.d.ts +0 -12
  128. package/dist/OpenAPISchemaServer.jsx +0 -8
  129. package/dist/OpenAPISecurities.d.ts +0 -9
  130. package/dist/OpenAPISecurities.jsx +0 -114
  131. package/dist/OpenAPISelect.d.ts +0 -22
  132. package/dist/OpenAPISelect.jsx +0 -44
  133. package/dist/OpenAPISpec.d.ts +0 -6
  134. package/dist/OpenAPISpec.jsx +0 -80
  135. package/dist/OpenAPITabs.d.ts +0 -26
  136. package/dist/OpenAPITabs.jsx +0 -109
  137. package/dist/OpenAPIWebhook.jsx +0 -23
  138. package/dist/OpenAPIWebhookExample.d.ts +0 -6
  139. package/dist/OpenAPIWebhookExample.jsx +0 -41
  140. package/dist/ScalarApiButton.d.ts +0 -14
  141. package/dist/ScalarApiButton.jsx +0 -81
  142. package/dist/StaticSection.d.ts +0 -13
  143. package/dist/StaticSection.jsx +0 -32
  144. package/dist/code-samples.d.ts +0 -17
  145. package/dist/common/OpenAPIColumnSpec.d.ts +0 -6
  146. package/dist/common/OpenAPIColumnSpec.jsx +0 -20
  147. package/dist/common/OpenAPIOperationDescription.d.ts +0 -6
  148. package/dist/common/OpenAPIOperationDescription.jsx +0 -19
  149. package/dist/common/OpenAPIStability.d.ts +0 -4
  150. package/dist/common/OpenAPIStability.jsx +0 -15
  151. package/dist/common/OpenAPISummary.d.ts +0 -6
  152. package/dist/common/OpenAPISummary.jsx +0 -30
  153. package/dist/contentTypeChecks.d.ts +0 -10
  154. package/dist/decycle.d.ts +0 -2
  155. package/dist/dereference.d.ts +0 -5
  156. package/dist/generateSchemaExample.d.ts +0 -45
  157. package/dist/getDisclosureLabel.d.ts +0 -7
  158. package/dist/getOrCreateStoreByKey.d.ts +0 -10
  159. package/dist/json2xml.d.ts +0 -4
  160. package/dist/schemas/OpenAPISchemaItem.d.ts +0 -7
  161. package/dist/schemas/OpenAPISchemaItem.jsx +0 -16
  162. package/dist/schemas/OpenAPISchemas.jsx +0 -59
  163. package/dist/schemas/index.d.ts +0 -2
  164. package/dist/schemas/index.js +0 -2
  165. package/dist/stringifyOpenAPI.d.ts +0 -4
  166. package/dist/translate.d.ts +0 -10
  167. package/dist/translate.jsx +0 -75
  168. package/dist/translations/de.d.ts +0 -43
  169. package/dist/translations/es.d.ts +0 -43
  170. package/dist/translations/fr.d.ts +0 -43
  171. package/dist/translations/ja.d.ts +0 -43
  172. package/dist/translations/nl.d.ts +0 -43
  173. package/dist/translations/no.d.ts +0 -43
  174. package/dist/translations/pt-br.d.ts +0 -43
  175. package/dist/translations/types.js +0 -1
  176. package/dist/translations/zh.d.ts +0 -43
  177. package/dist/tsconfig.build.tsbuildinfo +0 -1
  178. package/dist/types.js +0 -1
  179. package/dist/util/example.d.ts +0 -35
  180. package/dist/util/example.jsx +0 -103
  181. package/dist/util/server.d.ts +0 -9
  182. package/dist/util/tryit-prefill.d.ts +0 -20
  183. package/dist/utils.d.ts +0 -50
  184. package/src/InteractiveSection.tsx +0 -147
  185. package/src/Markdown.tsx +0 -12
  186. package/src/OpenAPICodeSample.tsx +0 -330
  187. package/src/OpenAPICodeSampleInteractive.tsx +0 -136
  188. package/src/OpenAPICodeSampleSelector.tsx +0 -94
  189. package/src/OpenAPICopyButton.tsx +0 -72
  190. package/src/OpenAPIDisclosure.tsx +0 -46
  191. package/src/OpenAPIDisclosureGroup.tsx +0 -158
  192. package/src/OpenAPIExample.tsx +0 -55
  193. package/src/OpenAPIMediaType.tsx +0 -139
  194. package/src/OpenAPIOperation.tsx +0 -35
  195. package/src/OpenAPIOperationContext.tsx +0 -45
  196. package/src/OpenAPIOperationDescription.tsx +0 -34
  197. package/src/OpenAPIOperationStability.tsx +0 -39
  198. package/src/OpenAPIPath.tsx +0 -90
  199. package/src/OpenAPIPrefillContextProvider.tsx +0 -40
  200. package/src/OpenAPIRequestBody.tsx +0 -54
  201. package/src/OpenAPIRequestBodyHeaderType.tsx +0 -36
  202. package/src/OpenAPIResponse.tsx +0 -82
  203. package/src/OpenAPIResponseExample.tsx +0 -151
  204. package/src/OpenAPIResponseExampleContent.tsx +0 -125
  205. package/src/OpenAPIResponses.tsx +0 -125
  206. package/src/OpenAPISchema.test.ts +0 -172
  207. package/src/OpenAPISchema.tsx +0 -654
  208. package/src/OpenAPISchemaName.tsx +0 -80
  209. package/src/OpenAPISchemaServer.tsx +0 -34
  210. package/src/OpenAPISecurities.tsx +0 -231
  211. package/src/OpenAPISelect.tsx +0 -96
  212. package/src/OpenAPISpec.tsx +0 -138
  213. package/src/OpenAPITabs.tsx +0 -147
  214. package/src/OpenAPIWebhook.tsx +0 -33
  215. package/src/OpenAPIWebhookExample.tsx +0 -60
  216. package/src/ScalarApiButton.tsx +0 -132
  217. package/src/StaticSection.tsx +0 -91
  218. package/src/__snapshots__/json2xml.test.ts.snap +0 -18
  219. package/src/code-samples.test.ts +0 -714
  220. package/src/code-samples.ts +0 -448
  221. package/src/common/OpenAPIColumnSpec.tsx +0 -31
  222. package/src/common/OpenAPIOperationDescription.tsx +0 -31
  223. package/src/common/OpenAPIStability.tsx +0 -23
  224. package/src/common/OpenAPISummary.tsx +0 -45
  225. package/src/contentTypeChecks.ts +0 -39
  226. package/src/context.ts +0 -99
  227. package/src/decycle.ts +0 -68
  228. package/src/dereference.ts +0 -29
  229. package/src/generateSchemaExample.test.ts +0 -1040
  230. package/src/generateSchemaExample.ts +0 -530
  231. package/src/getDisclosureLabel.ts +0 -25
  232. package/src/getOrCreateStoreByKey.ts +0 -33
  233. package/src/index.ts +0 -10
  234. package/src/json2xml.test.ts +0 -46
  235. package/src/json2xml.ts +0 -8
  236. package/src/resolveOpenAPIOperation.test.ts +0 -177
  237. package/src/resolveOpenAPIOperation.ts +0 -151
  238. package/src/resolveOpenAPIWebhook.ts +0 -99
  239. package/src/schemas/OpenAPISchemaItem.tsx +0 -34
  240. package/src/schemas/OpenAPISchemas.tsx +0 -98
  241. package/src/schemas/index.ts +0 -2
  242. package/src/schemas/resolveOpenAPISchemas.test.ts +0 -174
  243. package/src/schemas/resolveOpenAPISchemas.ts +0 -28
  244. package/src/stringifyOpenAPI.ts +0 -25
  245. package/src/translate.tsx +0 -80
  246. package/src/translations/de.ts +0 -43
  247. package/src/translations/en.ts +0 -43
  248. package/src/translations/es.ts +0 -43
  249. package/src/translations/fr.ts +0 -43
  250. package/src/translations/index.ts +0 -33
  251. package/src/translations/ja.ts +0 -43
  252. package/src/translations/nl.ts +0 -43
  253. package/src/translations/no.ts +0 -43
  254. package/src/translations/pt-br.ts +0 -43
  255. package/src/translations/types.ts +0 -7
  256. package/src/translations/zh.ts +0 -43
  257. package/src/types.ts +0 -46
  258. package/src/util/example.tsx +0 -129
  259. package/src/util/server.test.ts +0 -58
  260. package/src/util/server.ts +0 -47
  261. package/src/util/tryit-prefill.test.ts +0 -311
  262. package/src/util/tryit-prefill.ts +0 -160
  263. package/src/utils.ts +0 -255
@@ -1,654 +0,0 @@
1
- 'use client';
2
- // This component does not use any client feature but we don't want to
3
- // render it server-side because it has recursion.
4
-
5
- import type { OpenAPICustomOperationProperties, OpenAPIV3 } from '@gitbook/openapi-parser';
6
- import { useId } from 'react';
7
- import type { ComponentPropsWithoutRef } from 'react';
8
-
9
- import clsx from 'clsx';
10
- import { Markdown } from './Markdown';
11
- import { OpenAPICopyButton } from './OpenAPICopyButton';
12
- import { OpenAPIDisclosure } from './OpenAPIDisclosure';
13
- import { OpenAPISchemaName } from './OpenAPISchemaName';
14
- import type { OpenAPIClientContext } from './context';
15
- import { retrocycle } from './decycle';
16
- import { getDisclosureLabel } from './getDisclosureLabel';
17
- import { stringifyOpenAPI } from './stringifyOpenAPI';
18
- import { tString } from './translate';
19
- import { checkIsReference, getSchemaTitle, resolveDescription, resolveFirstExample } from './utils';
20
-
21
- type CircularRefsIds = Map<OpenAPIV3.SchemaObject, string>;
22
-
23
- export interface OpenAPISchemaPropertyEntry {
24
- propertyName?: string;
25
- required?: boolean | null;
26
- schema: OpenAPIV3.SchemaObject;
27
- }
28
-
29
- /**
30
- * Render a property of an OpenAPI schema.
31
- */
32
- function OpenAPISchemaProperty(
33
- props: {
34
- property: OpenAPISchemaPropertyEntry;
35
- context: OpenAPIClientContext;
36
- circularRefs: CircularRefsIds;
37
- className?: string;
38
- } & Omit<ComponentPropsWithoutRef<'div'>, 'property' | 'context' | 'circularRefs' | 'className'>
39
- ) {
40
- const { circularRefs: parentCircularRefs, context, className, property, ...rest } = props;
41
-
42
- const { schema } = property;
43
-
44
- const id = useId();
45
-
46
- const circularRefId = parentCircularRefs.get(schema);
47
- // Avoid recursing infinitely, and instead render a link to the parent schema
48
- if (circularRefId) {
49
- return <OpenAPISchemaCircularRef id={circularRefId} schema={schema} />;
50
- }
51
-
52
- const circularRefs = new Map(parentCircularRefs);
53
- circularRefs.set(schema, id);
54
-
55
- const properties = getSchemaProperties(schema);
56
-
57
- const ancestors = new Set(circularRefs.keys());
58
- const alternatives = getSchemaAlternatives(schema, ancestors);
59
-
60
- const header = <OpenAPISchemaPresentation context={context} property={property} />;
61
- const content = (() => {
62
- if (properties?.length) {
63
- return (
64
- <OpenAPISchemaProperties
65
- properties={properties}
66
- circularRefs={circularRefs}
67
- context={context}
68
- />
69
- );
70
- }
71
-
72
- if (alternatives) {
73
- return (
74
- <div className="openapi-schema-alternatives">
75
- {alternatives.map((alternativeSchema, index) => (
76
- <div key={index} className="openapi-schema-alternative">
77
- <OpenAPISchemaAlternative
78
- schema={alternativeSchema}
79
- circularRefs={circularRefs}
80
- context={context}
81
- />
82
- {index < alternatives.length - 1 ? (
83
- <OpenAPISchemaAlternativeSeparator
84
- schema={schema}
85
- context={context}
86
- />
87
- ) : null}
88
- </div>
89
- ))}
90
- </div>
91
- );
92
- }
93
-
94
- return null;
95
- })();
96
-
97
- if (properties?.length) {
98
- return (
99
- <OpenAPIDisclosure
100
- icon={context.icons.plus}
101
- className={clsx('openapi-schema', className)}
102
- header={header}
103
- label={(isExpanded) => getDisclosureLabel({ schema, isExpanded, context })}
104
- {...rest}
105
- >
106
- {content}
107
- </OpenAPIDisclosure>
108
- );
109
- }
110
-
111
- return (
112
- <div id={id} {...rest} className={clsx('openapi-schema', className)}>
113
- {header}
114
- {content}
115
- </div>
116
- );
117
- }
118
-
119
- /**
120
- * Render a set of properties of an OpenAPI schema.
121
- */
122
- function OpenAPISchemaProperties(props: {
123
- id?: string;
124
- properties: OpenAPISchemaPropertyEntry[];
125
- circularRefs?: CircularRefsIds;
126
- context: OpenAPIClientContext;
127
- }) {
128
- const {
129
- id,
130
- properties,
131
- circularRefs = new Map<OpenAPIV3.SchemaObject, string>(),
132
- context,
133
- } = props;
134
-
135
- return (
136
- <div id={id} className="openapi-schema-properties">
137
- {properties.map((property, index) => {
138
- return (
139
- <OpenAPISchemaProperty
140
- key={index}
141
- circularRefs={circularRefs}
142
- property={property}
143
- context={context}
144
- style={{ animationDelay: `${index * 0.02}s` }}
145
- />
146
- );
147
- })}
148
- </div>
149
- );
150
- }
151
-
152
- export function OpenAPISchemaPropertiesFromServer(props: {
153
- id?: string;
154
- properties: string;
155
- context: OpenAPIClientContext;
156
- }) {
157
- return (
158
- <OpenAPISchemaProperties
159
- id={props.id}
160
- properties={JSON.parse(props.properties, retrocycle())}
161
- context={props.context}
162
- />
163
- );
164
- }
165
-
166
- /**
167
- * Render a root schema (such as the request body or response body).
168
- */
169
- function OpenAPIRootSchema(props: {
170
- schema: OpenAPIV3.SchemaObject;
171
- context: OpenAPIClientContext;
172
- circularRefs?: CircularRefsIds;
173
- }) {
174
- const {
175
- schema,
176
- context,
177
- circularRefs: parentCircularRefs = new Map<OpenAPIV3.SchemaObject, string>(),
178
- } = props;
179
-
180
- const id = useId();
181
- const properties = getSchemaProperties(schema);
182
- const description = resolveDescription(schema);
183
-
184
- if (properties?.length) {
185
- const circularRefs = new Map(parentCircularRefs);
186
- circularRefs.set(schema, id);
187
-
188
- return (
189
- <>
190
- {description ? (
191
- <Markdown source={description} className="openapi-schema-root-description" />
192
- ) : null}
193
- <OpenAPISchemaProperties
194
- properties={properties}
195
- circularRefs={circularRefs}
196
- context={context}
197
- />
198
- </>
199
- );
200
- }
201
-
202
- return (
203
- <OpenAPISchemaProperty
204
- className="openapi-schema-root"
205
- property={{ schema }}
206
- context={context}
207
- circularRefs={parentCircularRefs}
208
- />
209
- );
210
- }
211
-
212
- export function OpenAPIRootSchemaFromServer(props: {
213
- schema: string;
214
- context: OpenAPIClientContext;
215
- }) {
216
- return (
217
- <OpenAPIRootSchema
218
- schema={JSON.parse(props.schema, retrocycle())}
219
- context={props.context}
220
- />
221
- );
222
- }
223
-
224
- /**
225
- * Render a tab for an alternative schema.
226
- * It renders directly the properties if relevant;
227
- * for primitives, it renders the schema itself.
228
- */
229
- function OpenAPISchemaAlternative(props: {
230
- schema: OpenAPIV3.SchemaObject;
231
- circularRefs: CircularRefsIds;
232
- context: OpenAPIClientContext;
233
- }) {
234
- const { schema, circularRefs, context } = props;
235
- const properties = getSchemaProperties(schema);
236
-
237
- return properties?.length ? (
238
- <OpenAPIDisclosure
239
- icon={context.icons.plus}
240
- header={<OpenAPISchemaPresentation property={{ schema }} context={context} />}
241
- label={(isExpanded) => getDisclosureLabel({ schema, isExpanded, context })}
242
- >
243
- <OpenAPISchemaProperties
244
- properties={properties}
245
- circularRefs={circularRefs}
246
- context={context}
247
- />
248
- </OpenAPIDisclosure>
249
- ) : (
250
- <OpenAPISchemaProperty
251
- property={{ schema }}
252
- circularRefs={circularRefs}
253
- context={context}
254
- />
255
- );
256
- }
257
-
258
- function OpenAPISchemaAlternativeSeparator(props: {
259
- schema: OpenAPIV3.SchemaObject;
260
- context: OpenAPIClientContext;
261
- }) {
262
- const { schema, context } = props;
263
-
264
- const anyOf = schema.anyOf || schema.items?.anyOf;
265
- const oneOf = schema.oneOf || schema.items?.oneOf;
266
- const allOf = schema.allOf || schema.items?.allOf;
267
-
268
- if (!anyOf && !oneOf && !allOf) {
269
- return null;
270
- }
271
-
272
- return (
273
- <span className="openapi-schema-alternative-separator">
274
- {(anyOf || oneOf) && tString(context.translation, 'or')}
275
- {allOf && tString(context.translation, 'and')}
276
- </span>
277
- );
278
- }
279
-
280
- /**
281
- * Render a circular reference to a schema.
282
- */
283
- function OpenAPISchemaCircularRef(props: { id: string; schema: OpenAPIV3.SchemaObject }) {
284
- const { id, schema } = props;
285
-
286
- return (
287
- <div className="openapi-schema-circular">
288
- Circular reference to <a href={`#${id}`}>{getSchemaTitle(schema)}</a>{' '}
289
- <span className="openapi-schema-circular-glyph">↩</span>
290
- </div>
291
- );
292
- }
293
-
294
- /**
295
- * Render the enum value for a schema.
296
- */
297
- function OpenAPISchemaEnum(props: {
298
- schema: OpenAPIV3.SchemaObject & OpenAPICustomOperationProperties;
299
- context: OpenAPIClientContext;
300
- }) {
301
- const { schema, context } = props;
302
-
303
- const enumValues = (() => {
304
- // Render x-gitbook-enum first, as it has a different format
305
- if (schema['x-gitbook-enum']) {
306
- return Object.entries(schema['x-gitbook-enum']).map(([name, { description }]) => {
307
- return {
308
- value: name,
309
- description,
310
- };
311
- });
312
- }
313
-
314
- if (schema['x-enumDescriptions']) {
315
- return Object.entries(schema['x-enumDescriptions']).map(([value, description]) => {
316
- return {
317
- value,
318
- description,
319
- };
320
- });
321
- }
322
-
323
- return schema.enum?.map((value) => {
324
- return {
325
- value,
326
- description: undefined,
327
- };
328
- });
329
- })();
330
-
331
- if (!enumValues?.length) {
332
- return null;
333
- }
334
-
335
- return (
336
- <span className="openapi-schema-enum">
337
- {tString(context.translation, 'possible_values')}:{' '}
338
- {enumValues.map((item, index) => (
339
- <span key={index} className="openapi-schema-enum-value">
340
- <OpenAPICopyButton
341
- value={item.value}
342
- label={item.description}
343
- withTooltip={!!item.description}
344
- context={context}
345
- >
346
- <code>{`${item.value}`}</code>
347
- </OpenAPICopyButton>
348
- </span>
349
- ))}
350
- </span>
351
- );
352
- }
353
-
354
- /**
355
- * Render the top row of a schema. e.g: name, type, and required status.
356
- */
357
- export function OpenAPISchemaPresentation(props: {
358
- property: OpenAPISchemaPropertyEntry;
359
- context: OpenAPIClientContext;
360
- }) {
361
- const {
362
- property: { schema, propertyName, required },
363
- context,
364
- } = props;
365
-
366
- const description = resolveDescription(schema);
367
- const example = resolveFirstExample(schema);
368
-
369
- return (
370
- <div className="openapi-schema-presentation">
371
- <OpenAPISchemaName
372
- schema={schema}
373
- type={getSchemaTitle(schema)}
374
- propertyName={propertyName}
375
- required={required}
376
- context={context}
377
- />
378
- {typeof schema['x-deprecated-sunset'] === 'string' ? (
379
- <div className="openapi-deprecated-sunset openapi-schema-description openapi-markdown">
380
- Sunset date:{' '}
381
- <span className="openapi-deprecated-sunset-date">
382
- {schema['x-deprecated-sunset']}
383
- </span>
384
- </div>
385
- ) : null}
386
- {description ? (
387
- <Markdown source={description} className="openapi-schema-description" />
388
- ) : null}
389
- {schema.default !== undefined ? (
390
- <span className="openapi-schema-default">
391
- Default:{' '}
392
- <code>
393
- {typeof schema.default === 'string' && schema.default
394
- ? schema.default
395
- : stringifyOpenAPI(schema.default)}
396
- </code>
397
- </span>
398
- ) : null}
399
- {typeof example === 'string' ? (
400
- <span className="openapi-schema-example">
401
- Example: <code>{example}</code>
402
- </span>
403
- ) : null}
404
- {schema.pattern ? (
405
- <span className="openapi-schema-pattern">
406
- Pattern: <code>{schema.pattern}</code>
407
- </span>
408
- ) : null}
409
- <OpenAPISchemaEnum schema={schema} context={context} />
410
- </div>
411
- );
412
- }
413
-
414
- /**
415
- * Get the sub-properties of a schema.
416
- */
417
- function getSchemaProperties(schema: OpenAPIV3.SchemaObject): null | OpenAPISchemaPropertyEntry[] {
418
- // check array AND schema.items as this is sometimes null despite what the type indicates
419
- if (schema.type === 'array' && schema.items && !checkIsReference(schema.items)) {
420
- const items = schema.items;
421
- const itemProperties = getSchemaProperties(items);
422
- if (itemProperties) {
423
- return itemProperties;
424
- }
425
-
426
- // If the items are a primitive type, we don't need to display them
427
- if (
428
- (items.type === 'string' ||
429
- items.type === 'number' ||
430
- items.type === 'boolean' ||
431
- items.type === 'integer') &&
432
- !items.enum
433
- ) {
434
- return null;
435
- }
436
-
437
- return [{ propertyName: 'items', schema: items }];
438
- }
439
-
440
- if (schema.type === 'object' || schema.properties) {
441
- const result: OpenAPISchemaPropertyEntry[] = [];
442
-
443
- if (schema.properties) {
444
- Object.entries(schema.properties).forEach(([propertyName, propertySchema]) => {
445
- if (checkIsReference(propertySchema)) {
446
- return;
447
- }
448
-
449
- result.push({
450
- propertyName,
451
- required: Array.isArray(schema.required)
452
- ? schema.required.includes(propertyName)
453
- : undefined,
454
- schema: propertySchema,
455
- });
456
- });
457
- }
458
-
459
- if (schema.additionalProperties && !checkIsReference(schema.additionalProperties)) {
460
- result.push({
461
- propertyName: 'Other properties',
462
- schema: schema.additionalProperties === true ? {} : schema.additionalProperties,
463
- });
464
- }
465
-
466
- return result;
467
- }
468
-
469
- return null;
470
- }
471
-
472
- type AlternativeType = 'oneOf' | 'allOf' | 'anyOf';
473
-
474
- /**
475
- * Get the alternatives to display for a schema.
476
- */
477
- export function getSchemaAlternatives(
478
- schema: OpenAPIV3.SchemaObject,
479
- ancestors: Set<OpenAPIV3.SchemaObject> = new Set()
480
- ): OpenAPIV3.SchemaObject[] | null {
481
- // Search for alternatives in the items property if it exists
482
- if (
483
- schema.items &&
484
- ('oneOf' in schema.items || 'allOf' in schema.items || 'anyOf' in schema.items)
485
- ) {
486
- return getSchemaAlternatives(schema.items, ancestors);
487
- }
488
-
489
- const alternatives:
490
- | [AlternativeType, (OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject)[]]
491
- | null = (() => {
492
- if (schema.anyOf) {
493
- return ['anyOf', schema.anyOf];
494
- }
495
-
496
- if (schema.oneOf) {
497
- return ['oneOf', schema.oneOf];
498
- }
499
-
500
- if (schema.allOf) {
501
- return ['allOf', schema.allOf];
502
- }
503
-
504
- return null;
505
- })();
506
-
507
- if (!alternatives) {
508
- return null;
509
- }
510
-
511
- const [type, schemas] = alternatives;
512
- return mergeAlternatives(
513
- type,
514
- flattenAlternatives(type, schemas, new Set(ancestors).add(schema))
515
- );
516
- }
517
-
518
- /**
519
- * Merge alternatives of the same type into a single schema.
520
- * - Merge string enums
521
- */
522
- function mergeAlternatives(
523
- alternativeType: AlternativeType,
524
- schemasOrRefs: OpenAPIV3.SchemaObject[]
525
- ): OpenAPIV3.SchemaObject[] | null {
526
- switch (alternativeType) {
527
- case 'oneOf': {
528
- return schemasOrRefs.reduce<OpenAPIV3.SchemaObject[]>((acc, schemaOrRef) => {
529
- const latest = acc.at(-1);
530
-
531
- if (
532
- latest &&
533
- latest.type === 'string' &&
534
- latest.enum &&
535
- schemaOrRef.type === 'string' &&
536
- schemaOrRef.enum
537
- ) {
538
- latest.enum = Array.from(new Set([...latest.enum, ...schemaOrRef.enum]));
539
- latest.nullable = latest.nullable || schemaOrRef.nullable;
540
- return acc;
541
- }
542
-
543
- acc.push(schemaOrRef);
544
- return acc;
545
- }, []);
546
- }
547
- case 'allOf': {
548
- return schemasOrRefs.reduce<OpenAPIV3.SchemaObject[]>((acc, schemaOrRef) => {
549
- const latest = acc.at(-1);
550
-
551
- if (
552
- latest &&
553
- latest.type === 'string' &&
554
- latest.enum &&
555
- schemaOrRef.type === 'string' &&
556
- schemaOrRef.enum
557
- ) {
558
- const keys = Object.keys(schemaOrRef);
559
- if (keys.every((key) => ['type', 'enum', 'nullable'].includes(key))) {
560
- latest.enum = Array.from(new Set([...latest.enum, ...schemaOrRef.enum]));
561
- latest.nullable = latest.nullable || schemaOrRef.nullable;
562
- return acc;
563
- }
564
- }
565
-
566
- if (latest && latest.type === 'object' && schemaOrRef.type === 'object') {
567
- const keys = Object.keys(schemaOrRef);
568
- if (
569
- keys.every((key) =>
570
- ['type', 'properties', 'required', 'nullable'].includes(key)
571
- )
572
- ) {
573
- latest.properties = {
574
- ...latest.properties,
575
- ...schemaOrRef.properties,
576
- };
577
- latest.required = Array.from(
578
- new Set([
579
- ...(Array.isArray(latest.required) ? latest.required : []),
580
- ...(Array.isArray(schemaOrRef.required)
581
- ? schemaOrRef.required
582
- : []),
583
- ])
584
- );
585
- latest.nullable = latest.nullable || schemaOrRef.nullable;
586
- return acc;
587
- }
588
- }
589
-
590
- acc.push(schemaOrRef);
591
- return acc;
592
- }, []);
593
- }
594
- default:
595
- return schemasOrRefs;
596
- }
597
- }
598
-
599
- function flattenAlternatives(
600
- alternativeType: AlternativeType,
601
- schemasOrRefs: (OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject)[],
602
- ancestors: Set<OpenAPIV3.SchemaObject>
603
- ): OpenAPIV3.SchemaObject[] {
604
- // Get the parent schema's required fields from the most recent ancestor
605
- const latestAncestor = Array.from(ancestors).pop();
606
-
607
- return schemasOrRefs.reduce<OpenAPIV3.SchemaObject[]>((acc, schemaOrRef) => {
608
- if (checkIsReference(schemaOrRef)) {
609
- return acc;
610
- }
611
-
612
- if (schemaOrRef[alternativeType] && !ancestors.has(schemaOrRef)) {
613
- const schemas = getSchemaAlternatives(schemaOrRef, ancestors);
614
- if (schemas) {
615
- acc.push(
616
- ...schemas.map((schema) => ({
617
- ...schema,
618
- required: mergeRequiredFields(schema, latestAncestor),
619
- }))
620
- );
621
- }
622
- return acc;
623
- }
624
-
625
- // For direct schemas, handle required fields
626
- const schema = {
627
- ...schemaOrRef,
628
- required: mergeRequiredFields(schemaOrRef, latestAncestor),
629
- };
630
-
631
- acc.push(schema);
632
- return acc;
633
- }, []);
634
- }
635
-
636
- /**
637
- * Merge the required fields of a schema with the required fields of its latest ancestor.
638
- */
639
- function mergeRequiredFields(
640
- schemaOrRef: OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject,
641
- latestAncestor: OpenAPIV3.SchemaObject | undefined
642
- ) {
643
- if (!schemaOrRef.required && !latestAncestor?.required) {
644
- return undefined;
645
- }
646
-
647
- if (checkIsReference(schemaOrRef)) {
648
- return latestAncestor?.required;
649
- }
650
-
651
- return Array.from(
652
- new Set([...(latestAncestor?.required || []), ...(schemaOrRef.required || [])])
653
- );
654
- }