@gitbook/react-openapi 1.4.2 → 1.5.1

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 (225) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/dist/index.d.ts +663 -10
  3. package/dist/index.js +3873 -8
  4. package/package.json +11 -7
  5. package/dist/InteractiveSection.d.ts +0 -33
  6. package/dist/InteractiveSection.jsx +0 -61
  7. package/dist/Markdown.d.ts +0 -4
  8. package/dist/Markdown.jsx +0 -5
  9. package/dist/OpenAPICodeSample.d.ts +0 -19
  10. package/dist/OpenAPICodeSample.jsx +0 -230
  11. package/dist/OpenAPICodeSampleInteractive.d.ts +0 -14
  12. package/dist/OpenAPICodeSampleInteractive.jsx +0 -73
  13. package/dist/OpenAPICodeSampleSelector.d.ts +0 -14
  14. package/dist/OpenAPICodeSampleSelector.jsx +0 -44
  15. package/dist/OpenAPICopyButton.d.ts +0 -13
  16. package/dist/OpenAPICopyButton.jsx +0 -35
  17. package/dist/OpenAPIDisclosure.d.ts +0 -11
  18. package/dist/OpenAPIDisclosure.jsx +0 -30
  19. package/dist/OpenAPIDisclosureGroup.d.ts +0 -23
  20. package/dist/OpenAPIDisclosureGroup.jsx +0 -83
  21. package/dist/OpenAPIExample.d.ts +0 -16
  22. package/dist/OpenAPIExample.jsx +0 -36
  23. package/dist/OpenAPIMediaType.d.ts +0 -21
  24. package/dist/OpenAPIMediaType.jsx +0 -61
  25. package/dist/OpenAPIOperation.d.ts +0 -10
  26. package/dist/OpenAPIOperation.jsx +0 -25
  27. package/dist/OpenAPIOperationContext.d.ts +0 -16
  28. package/dist/OpenAPIOperationContext.jsx +0 -26
  29. package/dist/OpenAPIOperationDescription.d.ts +0 -9
  30. package/dist/OpenAPIOperationDescription.jsx +0 -22
  31. package/dist/OpenAPIOperationStability.d.ts +0 -9
  32. package/dist/OpenAPIOperationStability.jsx +0 -27
  33. package/dist/OpenAPIPath.d.ts +0 -18
  34. package/dist/OpenAPIPath.jsx +0 -55
  35. package/dist/OpenAPIPrefillContextProvider.d.ts +0 -22
  36. package/dist/OpenAPIPrefillContextProvider.jsx +0 -19
  37. package/dist/OpenAPIRequestBody.d.ts +0 -11
  38. package/dist/OpenAPIRequestBody.jsx +0 -28
  39. package/dist/OpenAPIRequestBodyHeaderType.d.ts +0 -8
  40. package/dist/OpenAPIRequestBodyHeaderType.jsx +0 -25
  41. package/dist/OpenAPIResponse.d.ts +0 -10
  42. package/dist/OpenAPIResponse.jsx +0 -57
  43. package/dist/OpenAPIResponseExample.d.ts +0 -9
  44. package/dist/OpenAPIResponseExample.jsx +0 -105
  45. package/dist/OpenAPIResponseExampleContent.d.ts +0 -22
  46. package/dist/OpenAPIResponseExampleContent.jsx +0 -60
  47. package/dist/OpenAPIResponses.d.ts +0 -9
  48. package/dist/OpenAPIResponses.jsx +0 -77
  49. package/dist/OpenAPISchema.d.ts +0 -27
  50. package/dist/OpenAPISchema.jsx +0 -400
  51. package/dist/OpenAPISchemaName.d.ts +0 -16
  52. package/dist/OpenAPISchemaName.jsx +0 -43
  53. package/dist/OpenAPISchemaServer.d.ts +0 -12
  54. package/dist/OpenAPISchemaServer.jsx +0 -8
  55. package/dist/OpenAPISecurities.d.ts +0 -9
  56. package/dist/OpenAPISecurities.jsx +0 -114
  57. package/dist/OpenAPISelect.d.ts +0 -22
  58. package/dist/OpenAPISelect.jsx +0 -44
  59. package/dist/OpenAPISpec.d.ts +0 -6
  60. package/dist/OpenAPISpec.jsx +0 -80
  61. package/dist/OpenAPITabs.d.ts +0 -26
  62. package/dist/OpenAPITabs.jsx +0 -109
  63. package/dist/OpenAPIWebhook.d.ts +0 -10
  64. package/dist/OpenAPIWebhook.jsx +0 -23
  65. package/dist/OpenAPIWebhookExample.d.ts +0 -6
  66. package/dist/OpenAPIWebhookExample.jsx +0 -41
  67. package/dist/ScalarApiButton.d.ts +0 -14
  68. package/dist/ScalarApiButton.jsx +0 -81
  69. package/dist/StaticSection.d.ts +0 -13
  70. package/dist/StaticSection.jsx +0 -32
  71. package/dist/code-samples.d.ts +0 -17
  72. package/dist/code-samples.js +0 -427
  73. package/dist/common/OpenAPIColumnSpec.d.ts +0 -6
  74. package/dist/common/OpenAPIColumnSpec.jsx +0 -20
  75. package/dist/common/OpenAPIOperationDescription.d.ts +0 -6
  76. package/dist/common/OpenAPIOperationDescription.jsx +0 -19
  77. package/dist/common/OpenAPIStability.d.ts +0 -4
  78. package/dist/common/OpenAPIStability.jsx +0 -15
  79. package/dist/common/OpenAPISummary.d.ts +0 -6
  80. package/dist/common/OpenAPISummary.jsx +0 -30
  81. package/dist/contentTypeChecks.d.ts +0 -10
  82. package/dist/contentTypeChecks.js +0 -30
  83. package/dist/context.d.ts +0 -75
  84. package/dist/context.js +0 -43
  85. package/dist/decycle.d.ts +0 -2
  86. package/dist/decycle.js +0 -70
  87. package/dist/dereference.d.ts +0 -5
  88. package/dist/dereference.js +0 -68
  89. package/dist/generateSchemaExample.d.ts +0 -45
  90. package/dist/generateSchemaExample.js +0 -342
  91. package/dist/getDisclosureLabel.d.ts +0 -7
  92. package/dist/getDisclosureLabel.js +0 -18
  93. package/dist/getOrCreateStoreByKey.d.ts +0 -10
  94. package/dist/getOrCreateStoreByKey.js +0 -19
  95. package/dist/json2xml.d.ts +0 -4
  96. package/dist/json2xml.js +0 -7
  97. package/dist/resolveOpenAPIOperation.d.ts +0 -11
  98. package/dist/resolveOpenAPIOperation.js +0 -173
  99. package/dist/resolveOpenAPIWebhook.d.ts +0 -11
  100. package/dist/resolveOpenAPIWebhook.js +0 -127
  101. package/dist/schemas/OpenAPISchemaItem.d.ts +0 -7
  102. package/dist/schemas/OpenAPISchemaItem.jsx +0 -16
  103. package/dist/schemas/OpenAPISchemas.d.ts +0 -14
  104. package/dist/schemas/OpenAPISchemas.jsx +0 -59
  105. package/dist/schemas/index.d.ts +0 -2
  106. package/dist/schemas/index.js +0 -2
  107. package/dist/schemas/resolveOpenAPISchemas.d.ts +0 -10
  108. package/dist/schemas/resolveOpenAPISchemas.js +0 -61
  109. package/dist/stringifyOpenAPI.d.ts +0 -4
  110. package/dist/stringifyOpenAPI.js +0 -15
  111. package/dist/translate.d.ts +0 -10
  112. package/dist/translate.jsx +0 -75
  113. package/dist/translations/de.d.ts +0 -43
  114. package/dist/translations/de.js +0 -43
  115. package/dist/translations/en.d.ts +0 -43
  116. package/dist/translations/en.js +0 -43
  117. package/dist/translations/es.d.ts +0 -43
  118. package/dist/translations/es.js +0 -43
  119. package/dist/translations/fr.d.ts +0 -43
  120. package/dist/translations/fr.js +0 -43
  121. package/dist/translations/index.d.ts +0 -395
  122. package/dist/translations/index.js +0 -27
  123. package/dist/translations/ja.d.ts +0 -43
  124. package/dist/translations/ja.js +0 -43
  125. package/dist/translations/nl.d.ts +0 -43
  126. package/dist/translations/nl.js +0 -43
  127. package/dist/translations/no.d.ts +0 -43
  128. package/dist/translations/no.js +0 -43
  129. package/dist/translations/pt-br.d.ts +0 -43
  130. package/dist/translations/pt-br.js +0 -43
  131. package/dist/translations/types.d.ts +0 -5
  132. package/dist/translations/types.js +0 -1
  133. package/dist/translations/zh.d.ts +0 -43
  134. package/dist/translations/zh.js +0 -43
  135. package/dist/tsconfig.build.tsbuildinfo +0 -1
  136. package/dist/types.d.ts +0 -31
  137. package/dist/types.js +0 -1
  138. package/dist/util/example.d.ts +0 -35
  139. package/dist/util/example.jsx +0 -103
  140. package/dist/util/server.d.ts +0 -9
  141. package/dist/util/server.js +0 -44
  142. package/dist/util/tryit-prefill.d.ts +0 -20
  143. package/dist/util/tryit-prefill.js +0 -129
  144. package/dist/utils.d.ts +0 -50
  145. package/dist/utils.js +0 -224
  146. package/src/InteractiveSection.tsx +0 -147
  147. package/src/Markdown.tsx +0 -12
  148. package/src/OpenAPICodeSample.tsx +0 -330
  149. package/src/OpenAPICodeSampleInteractive.tsx +0 -136
  150. package/src/OpenAPICodeSampleSelector.tsx +0 -94
  151. package/src/OpenAPICopyButton.tsx +0 -72
  152. package/src/OpenAPIDisclosure.tsx +0 -46
  153. package/src/OpenAPIDisclosureGroup.tsx +0 -158
  154. package/src/OpenAPIExample.tsx +0 -55
  155. package/src/OpenAPIMediaType.tsx +0 -139
  156. package/src/OpenAPIOperation.tsx +0 -35
  157. package/src/OpenAPIOperationContext.tsx +0 -45
  158. package/src/OpenAPIOperationDescription.tsx +0 -34
  159. package/src/OpenAPIOperationStability.tsx +0 -39
  160. package/src/OpenAPIPath.tsx +0 -90
  161. package/src/OpenAPIPrefillContextProvider.tsx +0 -40
  162. package/src/OpenAPIRequestBody.tsx +0 -54
  163. package/src/OpenAPIRequestBodyHeaderType.tsx +0 -36
  164. package/src/OpenAPIResponse.tsx +0 -82
  165. package/src/OpenAPIResponseExample.tsx +0 -151
  166. package/src/OpenAPIResponseExampleContent.tsx +0 -125
  167. package/src/OpenAPIResponses.tsx +0 -125
  168. package/src/OpenAPISchema.test.ts +0 -172
  169. package/src/OpenAPISchema.tsx +0 -654
  170. package/src/OpenAPISchemaName.tsx +0 -80
  171. package/src/OpenAPISchemaServer.tsx +0 -34
  172. package/src/OpenAPISecurities.tsx +0 -231
  173. package/src/OpenAPISelect.tsx +0 -96
  174. package/src/OpenAPISpec.tsx +0 -138
  175. package/src/OpenAPITabs.tsx +0 -147
  176. package/src/OpenAPIWebhook.tsx +0 -33
  177. package/src/OpenAPIWebhookExample.tsx +0 -60
  178. package/src/ScalarApiButton.tsx +0 -132
  179. package/src/StaticSection.tsx +0 -91
  180. package/src/__snapshots__/json2xml.test.ts.snap +0 -18
  181. package/src/code-samples.test.ts +0 -714
  182. package/src/code-samples.ts +0 -448
  183. package/src/common/OpenAPIColumnSpec.tsx +0 -31
  184. package/src/common/OpenAPIOperationDescription.tsx +0 -31
  185. package/src/common/OpenAPIStability.tsx +0 -23
  186. package/src/common/OpenAPISummary.tsx +0 -45
  187. package/src/contentTypeChecks.ts +0 -39
  188. package/src/context.ts +0 -99
  189. package/src/decycle.ts +0 -68
  190. package/src/dereference.ts +0 -29
  191. package/src/generateSchemaExample.test.ts +0 -1040
  192. package/src/generateSchemaExample.ts +0 -530
  193. package/src/getDisclosureLabel.ts +0 -25
  194. package/src/getOrCreateStoreByKey.ts +0 -33
  195. package/src/index.ts +0 -10
  196. package/src/json2xml.test.ts +0 -46
  197. package/src/json2xml.ts +0 -8
  198. package/src/resolveOpenAPIOperation.test.ts +0 -177
  199. package/src/resolveOpenAPIOperation.ts +0 -151
  200. package/src/resolveOpenAPIWebhook.ts +0 -99
  201. package/src/schemas/OpenAPISchemaItem.tsx +0 -34
  202. package/src/schemas/OpenAPISchemas.tsx +0 -98
  203. package/src/schemas/index.ts +0 -2
  204. package/src/schemas/resolveOpenAPISchemas.test.ts +0 -174
  205. package/src/schemas/resolveOpenAPISchemas.ts +0 -28
  206. package/src/stringifyOpenAPI.ts +0 -25
  207. package/src/translate.tsx +0 -80
  208. package/src/translations/de.ts +0 -43
  209. package/src/translations/en.ts +0 -43
  210. package/src/translations/es.ts +0 -43
  211. package/src/translations/fr.ts +0 -43
  212. package/src/translations/index.ts +0 -33
  213. package/src/translations/ja.ts +0 -43
  214. package/src/translations/nl.ts +0 -43
  215. package/src/translations/no.ts +0 -43
  216. package/src/translations/pt-br.ts +0 -43
  217. package/src/translations/types.ts +0 -7
  218. package/src/translations/zh.ts +0 -43
  219. package/src/types.ts +0 -46
  220. package/src/util/example.tsx +0 -129
  221. package/src/util/server.test.ts +0 -58
  222. package/src/util/server.ts +0 -47
  223. package/src/util/tryit-prefill.test.ts +0 -311
  224. package/src/util/tryit-prefill.ts +0 -160
  225. package/src/utils.ts +0 -255
@@ -1,530 +0,0 @@
1
- import type { OpenAPIV3 } from '@gitbook/openapi-parser';
2
- import { checkIsReference } from './utils';
3
-
4
- type JSONValue = string | number | boolean | null | JSONValue[] | { [key: string]: JSONValue };
5
-
6
- type ScalarGetExampleFromSchemaOptions = NonNullable<Parameters<typeof getExampleFromSchema>[1]>;
7
- type GenerateSchemaExampleOptions = Pick<
8
- ScalarGetExampleFromSchemaOptions,
9
- 'xml' | 'omitEmptyAndOptionalProperties' | 'mode'
10
- >;
11
-
12
- /**
13
- * Generate a JSON example from a schema
14
- */
15
- export function generateSchemaExample(
16
- schema: OpenAPIV3.SchemaObject,
17
- options?: GenerateSchemaExampleOptions
18
- ): JSONValue | undefined {
19
- return getExampleFromSchema(schema, {
20
- emptyString: 'text',
21
- ...options,
22
- });
23
- }
24
-
25
- /**
26
- * Generate an example for a media type.
27
- */
28
- export function generateMediaTypeExamples(
29
- mediaType: OpenAPIV3.MediaTypeObject,
30
- options?: GenerateSchemaExampleOptions
31
- ): OpenAPIV3.ExampleObject[] {
32
- if (mediaType.example) {
33
- return [{ summary: 'default', value: mediaType.example }];
34
- }
35
-
36
- if (mediaType.examples) {
37
- const { examples } = mediaType;
38
- const keys = Object.keys(examples);
39
- if (keys.length > 0) {
40
- return keys.reduce<OpenAPIV3.ExampleObject[]>((result, key) => {
41
- const example = examples[key];
42
- if (!example || checkIsReference(example)) {
43
- return result;
44
- }
45
- result.push({
46
- summary: example.summary || key,
47
- value: example.value,
48
- description: example.description,
49
- externalValue: example.externalValue,
50
- });
51
- return result;
52
- }, []);
53
- }
54
- }
55
-
56
- if (mediaType.schema) {
57
- return [{ summary: 'default', value: generateSchemaExample(mediaType.schema, options) }];
58
- }
59
-
60
- return [];
61
- }
62
-
63
- /** Hard limit for rendering circular references */
64
- const MAX_LEVELS_DEEP = 5;
65
-
66
- const genericExampleValues: Record<string, string> = {
67
- 'date-time': new Date().toISOString(),
68
- date: new Date().toISOString().split('T')[0] ?? '1970-01-01',
69
- email: 'name@gmail.com',
70
- hostname: 'example.com',
71
- ipv4: '0.0.0.0',
72
- ipv6: '2001:0db8:85a3:0000:0000:8a2e:0370:7334',
73
- uri: 'https://example.com',
74
- uuid: '123e4567-e89b-12d3-a456-426614174000',
75
- binary: 'binary',
76
- byte: 'Ynl0ZXM=',
77
- password: 'password',
78
- 'idn-email': 'jane.doe@example.com',
79
- 'idn-hostname': 'example.com',
80
- 'iri-reference': '/entitiy/1',
81
- // https://tools.ietf.org/html/rfc3987
82
- iri: 'https://example.com/entity/123',
83
- 'json-pointer': '/nested/objects',
84
- regex: '/[a-z]/',
85
- // https://tools.ietf.org/html/draft-handrews-relative-json-pointer-01
86
- 'relative-json-pointer': '1/nested/objects',
87
- // full-time in https://tools.ietf.org/html/rfc3339#section-5.6
88
- time: new Date().toISOString().split('T')[1]?.split('.')[0] ?? '00:00:00Z',
89
- // either a URI or relative-reference https://tools.ietf.org/html/rfc3986#section-4.1
90
- 'uri-reference': '../folder',
91
- 'uri-template': 'https://example.com/{id}',
92
- 'object-id': '6592008029c8c3e4dc76256c',
93
- };
94
-
95
- /**
96
- * We can use the `format` to generate some random values.
97
- */
98
- function guessFromFormat(schema: Record<string, any>, fallback = '') {
99
- return genericExampleValues[schema.format] ?? fallback;
100
- }
101
-
102
- /**
103
- * This function takes an OpenAPI schema and generates an example from it
104
- * Forked from : https://github.com/scalar/scalar/blob/main/packages/oas-utils/src/spec-getters/getExampleFromSchema.ts
105
- */
106
- const getExampleFromSchema = (
107
- schema: Record<string, any>,
108
- options?: {
109
- /**
110
- * The fallback string for empty string values.
111
- * @default ''
112
- */
113
- emptyString?: string;
114
- /**
115
- * Whether to use the XML tag names as keys
116
- * @default false
117
- */
118
- xml?: boolean;
119
- /**
120
- * Whether to show read-only/write-only properties. Otherwise all properties are shown.
121
- * @default undefined
122
- */
123
- mode?: 'read' | 'write';
124
- /**
125
- * Dynamic values to add to the example.
126
- */
127
- variables?: Record<string, any>;
128
- /**
129
- * Whether to omit empty and optional properties.
130
- * @default false
131
- */
132
- omitEmptyAndOptionalProperties?: boolean;
133
- },
134
- level = 0,
135
- parentSchema?: Record<string, any>,
136
- name?: string,
137
- resultCache = new WeakMap<Record<string, any>, any>()
138
- ): any => {
139
- // Store result in the cache, and return the result
140
- function cache(schema: Record<string, any>, result: unknown) {
141
- // Avoid unnecessary WeakMap operations for primitive values
142
- if (typeof result !== 'object' || result === null) {
143
- return result;
144
- }
145
-
146
- resultCache.set(schema, result);
147
- return result;
148
- }
149
-
150
- // Check if the result is already cached
151
- if (resultCache.has(schema)) {
152
- return resultCache.get(schema);
153
- }
154
-
155
- // Check whether it’s a circular reference
156
- if (level === MAX_LEVELS_DEEP + 1) {
157
- try {
158
- // Fails if it contains a circular reference
159
- JSON.stringify(schema);
160
- } catch {
161
- return '[Circular Reference]';
162
- }
163
- }
164
-
165
- // Sometimes, we just want the structure and no values.
166
- // But if `emptyString` is set, we do want to see some values.
167
- const makeUpRandomData = !!options?.emptyString;
168
-
169
- // If the property is deprecated we don't show it in examples.
170
- if (schema.deprecated || (schema.type === 'array' && schema.items?.deprecated)) {
171
- return undefined;
172
- }
173
-
174
- // Check if the property is read-only/write-only
175
- if (
176
- (options?.mode === 'write' && schema.readOnly) ||
177
- (options?.mode === 'read' && schema.writeOnly)
178
- ) {
179
- return undefined;
180
- }
181
-
182
- // Use given variables as values
183
- if (schema['x-variable']) {
184
- const value = options?.variables?.[schema['x-variable']];
185
-
186
- // Return the value if it’s defined
187
- if (value !== undefined) {
188
- // Type-casting
189
- if (schema.type === 'number' || schema.type === 'integer') {
190
- return Number.parseInt(value, 10);
191
- }
192
-
193
- return cache(schema, value);
194
- }
195
- }
196
-
197
- // Use the first example, if there’s an array
198
- if (Array.isArray(schema.examples) && schema.examples.length > 0) {
199
- return cache(schema, schema.examples[0]);
200
- }
201
-
202
- // Use an example, if there’s one
203
- if (schema.example !== undefined) {
204
- return cache(schema, schema.example);
205
- }
206
-
207
- // Use a default value, if there’s one and it’s a string or number
208
- if (
209
- schema.default !== undefined &&
210
- ['string', 'number', 'boolean'].includes(typeof schema.default)
211
- ) {
212
- return cache(schema, schema.default);
213
- }
214
-
215
- // enum: [ 'available', 'pending', 'sold' ]
216
- if (Array.isArray(schema.enum) && schema.enum.length > 0) {
217
- return cache(schema, schema.enum[0]);
218
- }
219
-
220
- // Check if the property is required
221
- const isObjectOrArray =
222
- schema.type === 'object' ||
223
- schema.type === 'array' ||
224
- !!schema.allOf?.at?.(0) ||
225
- !!schema.anyOf?.at?.(0) ||
226
- !!schema.oneOf?.at?.(0);
227
- if (!isObjectOrArray && options?.omitEmptyAndOptionalProperties === true) {
228
- const isRequired =
229
- schema.required === true ||
230
- parentSchema?.required === true ||
231
- parentSchema?.required?.includes(name ?? schema.name);
232
-
233
- if (!isRequired) {
234
- return undefined;
235
- }
236
- }
237
-
238
- // Object
239
- if (schema.type === 'object' || schema.properties !== undefined) {
240
- const response: Record<string, any> = {};
241
-
242
- // Regular properties
243
- if (schema.properties !== undefined) {
244
- for (const propertyName in schema.properties) {
245
- if (Object.prototype.hasOwnProperty.call(schema.properties, propertyName)) {
246
- const property = schema.properties[propertyName];
247
- const propertyXmlTagName = options?.xml ? property.xml?.name : undefined;
248
-
249
- response[propertyXmlTagName ?? propertyName] = getExampleFromSchema(
250
- property,
251
- options,
252
- level + 1,
253
- schema,
254
- propertyName,
255
- resultCache
256
- );
257
-
258
- if (typeof response[propertyXmlTagName ?? propertyName] === 'undefined') {
259
- delete response[propertyXmlTagName ?? propertyName];
260
- }
261
- }
262
- }
263
- }
264
-
265
- // Pattern properties (regex)
266
- if (schema.patternProperties !== undefined) {
267
- for (const pattern in schema.patternProperties) {
268
- if (Object.prototype.hasOwnProperty.call(schema.patternProperties, pattern)) {
269
- const property = schema.patternProperties[pattern];
270
-
271
- // Use the regex pattern as an example key
272
- const exampleKey = pattern;
273
-
274
- response[exampleKey] = getExampleFromSchema(
275
- property,
276
- options,
277
- level + 1,
278
- schema,
279
- exampleKey,
280
- resultCache
281
- );
282
- }
283
- }
284
- }
285
-
286
- // Additional properties
287
- if (schema.additionalProperties !== undefined) {
288
- const anyTypeIsValid =
289
- // true
290
- schema.additionalProperties === true ||
291
- // or an empty object {}
292
- (typeof schema.additionalProperties === 'object' &&
293
- !Object.keys(schema.additionalProperties).length);
294
-
295
- if (anyTypeIsValid) {
296
- response.ANY_ADDITIONAL_PROPERTY = 'anything';
297
- } else if (schema.additionalProperties !== false) {
298
- response.ANY_ADDITIONAL_PROPERTY = getExampleFromSchema(
299
- schema.additionalProperties,
300
- options,
301
- level + 1,
302
- undefined,
303
- undefined,
304
- resultCache
305
- );
306
- }
307
- }
308
-
309
- if (schema.anyOf !== undefined) {
310
- Object.assign(
311
- response,
312
- getExampleFromSchema(
313
- schema.anyOf[0],
314
- options,
315
- level + 1,
316
- undefined,
317
- undefined,
318
- resultCache
319
- )
320
- );
321
- } else if (schema.oneOf !== undefined) {
322
- Object.assign(
323
- response,
324
- getExampleFromSchema(
325
- schema.oneOf[0],
326
- options,
327
- level + 1,
328
- undefined,
329
- undefined,
330
- resultCache
331
- )
332
- );
333
- } else if (schema.allOf !== undefined) {
334
- Object.assign(
335
- response,
336
- ...schema.allOf
337
- .map((item: Record<string, any>) =>
338
- getExampleFromSchema(
339
- item,
340
- options,
341
- level + 1,
342
- schema,
343
- undefined,
344
- resultCache
345
- )
346
- )
347
- .filter((item: any) => item !== undefined)
348
- );
349
- }
350
-
351
- return cache(schema, response);
352
- }
353
-
354
- // Array
355
- if (schema.type === 'array' || schema.items !== undefined) {
356
- const itemsXmlTagName = schema?.items?.xml?.name;
357
- const wrapItems = !!(options?.xml && schema.xml?.wrapped && itemsXmlTagName);
358
-
359
- if (schema.example !== undefined) {
360
- return cache(
361
- schema,
362
- wrapItems ? { [itemsXmlTagName]: schema.example } : schema.example
363
- );
364
- }
365
-
366
- // Check whether the array has a anyOf, oneOf, or allOf rule
367
- if (schema.items) {
368
- // First handle allOf separately since it needs special handling
369
- if (schema.items.allOf) {
370
- // If the first item is an object type, merge all schemas
371
- if (schema.items.allOf[0].type === 'object') {
372
- const mergedExample = getExampleFromSchema(
373
- { type: 'object', allOf: schema.items.allOf },
374
- options,
375
- level + 1,
376
- schema,
377
- undefined,
378
- resultCache
379
- );
380
-
381
- return cache(
382
- schema,
383
- wrapItems ? [{ [itemsXmlTagName]: mergedExample }] : [mergedExample]
384
- );
385
- }
386
- // For non-objects (like strings), collect all examples
387
- const examples = schema.items.allOf
388
- .map((item: Record<string, any>) =>
389
- getExampleFromSchema(
390
- item,
391
- options,
392
- level + 1,
393
- schema,
394
- undefined,
395
- resultCache
396
- )
397
- )
398
- .filter((item: any) => item !== undefined);
399
-
400
- return cache(
401
- schema,
402
- wrapItems
403
- ? examples.map((example: any) => ({ [itemsXmlTagName]: example }))
404
- : examples
405
- );
406
- }
407
-
408
- // Handle other rules (anyOf, oneOf)
409
- const rules = ['anyOf', 'oneOf'];
410
- for (const rule of rules) {
411
- if (!schema.items[rule]) {
412
- continue;
413
- }
414
-
415
- const schemas = schema.items[rule].slice(0, 1);
416
- const exampleFromRule = schemas
417
- .map((item: Record<string, any>) =>
418
- getExampleFromSchema(
419
- item,
420
- options,
421
- level + 1,
422
- schema,
423
- undefined,
424
- resultCache
425
- )
426
- )
427
- .filter((item: any) => item !== undefined);
428
-
429
- return cache(
430
- schema,
431
- wrapItems ? [{ [itemsXmlTagName]: exampleFromRule }] : exampleFromRule
432
- );
433
- }
434
- }
435
-
436
- if (schema.items?.type) {
437
- const exampleFromSchema = getExampleFromSchema(
438
- schema.items,
439
- options,
440
- level + 1,
441
- undefined,
442
- undefined,
443
- resultCache
444
- );
445
-
446
- return wrapItems ? [{ [itemsXmlTagName]: exampleFromSchema }] : [exampleFromSchema];
447
- }
448
-
449
- return [];
450
- }
451
-
452
- const exampleValues: Record<any, any> = {
453
- string: makeUpRandomData ? guessFromFormat(schema, options?.emptyString) : '',
454
- boolean: true,
455
- integer: schema.min ?? 1,
456
- number: schema.min ?? 1,
457
- array: [],
458
- };
459
-
460
- if (schema.type !== undefined && exampleValues[schema.type] !== undefined) {
461
- return cache(schema, exampleValues[schema.type]);
462
- }
463
-
464
- const discriminateSchema = schema.oneOf || schema.anyOf;
465
- // Check if property has the `oneOf` | `anyOf` key
466
- if (Array.isArray(discriminateSchema) && discriminateSchema.length > 0) {
467
- // Get the first item from the `oneOf` | `anyOf` array
468
- const firstOneOfItem = discriminateSchema[0];
469
-
470
- // Return an example for the first item
471
- return getExampleFromSchema(
472
- firstOneOfItem,
473
- options,
474
- level + 1,
475
- undefined,
476
- undefined,
477
- resultCache
478
- );
479
- }
480
-
481
- // Check if schema has the `allOf` key
482
- if (Array.isArray(schema.allOf)) {
483
- let example: any = null;
484
-
485
- // Loop through all `allOf` schemas
486
- schema.allOf.forEach((allOfItem: Record<string, any>) => {
487
- // Return an example from the schema
488
- const newExample = getExampleFromSchema(
489
- allOfItem,
490
- options,
491
- level + 1,
492
- undefined,
493
- undefined,
494
- resultCache
495
- );
496
-
497
- // Merge or overwrite the example
498
- example =
499
- typeof newExample === 'object' && typeof example === 'object'
500
- ? {
501
- ...(example ?? {}),
502
- ...newExample,
503
- }
504
- : Array.isArray(newExample) && Array.isArray(example)
505
- ? [...(example ?? {}), ...newExample]
506
- : newExample;
507
- });
508
-
509
- return cache(schema, example);
510
- }
511
-
512
- // Check if schema is a union type
513
- if (Array.isArray(schema.type)) {
514
- // Return null if the type is nullable
515
- if (schema.type.includes('null')) {
516
- return null;
517
- }
518
- // Return an example for the first type in the union
519
- const exampleValue = exampleValues[schema.type[0]];
520
- if (exampleValue !== undefined) {
521
- return cache(schema, exampleValue);
522
- }
523
- }
524
-
525
- // Warn if the type is unknown …
526
- // console.warn(`[getExampleFromSchema] Unknown property type "${schema.type}".`)
527
-
528
- // … and just return null for now.
529
- return null;
530
- };
@@ -1,25 +0,0 @@
1
- 'use client';
2
-
3
- import type { OpenAPIV3 } from '@gitbook/openapi-parser';
4
- import type { OpenAPIClientContext } from './context';
5
- import { tString } from './translate';
6
-
7
- export function getDisclosureLabel(props: {
8
- schema: OpenAPIV3.SchemaObject;
9
- isExpanded: boolean;
10
- context: OpenAPIClientContext;
11
- }) {
12
- const { schema, isExpanded, context } = props;
13
- let label: string;
14
- if (schema.type === 'array' && !!schema.items) {
15
- if (schema.items.oneOf) {
16
- label = tString(context.translation, 'available_items').toLowerCase();
17
- } else {
18
- label = tString(context.translation, 'properties').toLowerCase();
19
- }
20
- } else {
21
- label = tString(context.translation, 'properties').toLowerCase();
22
- }
23
-
24
- return tString(context.translation, isExpanded ? 'hide' : 'show', label);
25
- }
@@ -1,33 +0,0 @@
1
- import { createStore } from 'zustand';
2
-
3
- type Key = string | number;
4
-
5
- type State = {
6
- key: Key | null;
7
- };
8
-
9
- type Actions = { setKey: (key: Key | null) => void };
10
-
11
- export type Store = State & Actions;
12
-
13
- const createStateStore = (initial?: Key) => {
14
- return createStore<Store>()((set) => ({
15
- key: initial ?? null,
16
- setKey: (key) => {
17
- set(() => ({ key }));
18
- },
19
- }));
20
- };
21
-
22
- const defaultStores = new Map<string, ReturnType<typeof createStateStore>>();
23
-
24
- const createStateStoreFactory = (stores: typeof defaultStores) => {
25
- return (storeKey: string, initialKey?: Key) => {
26
- if (!stores.has(storeKey)) {
27
- stores.set(storeKey, createStateStore(initialKey));
28
- }
29
- return stores.get(storeKey)!;
30
- };
31
- };
32
-
33
- export const getOrCreateStoreByKey = createStateStoreFactory(defaultStores);
package/src/index.ts DELETED
@@ -1,10 +0,0 @@
1
- export * from './schemas';
2
- export * from './OpenAPIOperation';
3
- export * from './OpenAPIWebhook';
4
- export * from './OpenAPIOperationContext';
5
- export * from './OpenAPIPrefillContextProvider';
6
- export * from './resolveOpenAPIOperation';
7
- export * from './resolveOpenAPIWebhook';
8
- export type { OpenAPIOperationData, OpenAPIWebhookData } from './types';
9
- export type { OpenAPIContextInput } from './context';
10
- export { checkIsValidLocale } from './translations';
@@ -1,46 +0,0 @@
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
- });
package/src/json2xml.ts DELETED
@@ -1,8 +0,0 @@
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
- }