@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,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
- }