@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,714 +0,0 @@
1
- import { describe, expect, it } from 'bun:test';
2
- import { type CodeSampleInput, codeSampleGenerators, parseHostAndPath } from './code-samples';
3
-
4
- it('should parse host and path on url strings properly', () => {
5
- const testUrls = [
6
- '//example.com/path',
7
- '//sub.example.com',
8
- '//example:8080/v1/test',
9
- 'ftp://domain.com',
10
- '//example.com/com.example',
11
- 'https://example.com/path.com/another.com',
12
- 'example.com/firstPath/secondPath',
13
- ];
14
-
15
- expect(testUrls.map(parseHostAndPath)).toEqual([
16
- {
17
- host: 'example.com',
18
- path: '/path',
19
- },
20
-
21
- {
22
- host: 'sub.example.com',
23
- path: '/',
24
- },
25
-
26
- {
27
- host: 'example:8080',
28
- path: '/v1/test',
29
- },
30
-
31
- {
32
- host: 'domain.com',
33
- path: '/',
34
- },
35
-
36
- {
37
- host: 'example.com',
38
- path: '/com.example',
39
- },
40
-
41
- {
42
- host: 'example.com',
43
- path: '/path.com/another.com',
44
- },
45
-
46
- {
47
- host: 'example.com',
48
- path: '/firstPath/secondPath',
49
- },
50
- ]);
51
- });
52
-
53
- describe('curL code sample generator', () => {
54
- const generator = codeSampleGenerators.find((g) => g.id === 'curl');
55
-
56
- expect(generator).toBeDefined();
57
-
58
- it('should format application/x-www-form-urlencoded body properly', () => {
59
- const input: CodeSampleInput = {
60
- method: 'GET',
61
- url: 'https://example.com/path',
62
- headers: {
63
- 'Content-Type': 'application/x-www-form-urlencoded',
64
- },
65
- body: {
66
- key: 'value',
67
- },
68
- };
69
-
70
- const output = generator?.generate(input);
71
-
72
- expect(output).toBe(
73
- "curl -L \\\n --url 'https://example.com/path' \\\n --header 'Content-Type: application/x-www-form-urlencoded' \\\n --data 'key=value'"
74
- );
75
- });
76
-
77
- it('should format application/json body properly', () => {
78
- const input: CodeSampleInput = {
79
- method: 'GET',
80
- url: 'https://example.com/path',
81
- headers: {
82
- 'Content-Type': 'application/json',
83
- },
84
- body: {
85
- key: 'value',
86
- },
87
- };
88
-
89
- const output = generator?.generate(input);
90
-
91
- expect(output).toBe(
92
- "curl -L \\\n --url 'https://example.com/path' \\\n --header 'Content-Type: application/json' \\\n --data '{\n \"key\": \"value\"\n }'"
93
- );
94
- });
95
-
96
- it('should format application/xml body properly', () => {
97
- const input: CodeSampleInput = {
98
- method: 'GET',
99
- url: 'https://example.com/path',
100
- headers: {
101
- 'Content-Type': 'application/xml',
102
- },
103
- body: '<key>value</key>',
104
- };
105
-
106
- const output = generator?.generate(input);
107
-
108
- expect(output).toBe(
109
- "curl -L \\\n --url 'https://example.com/path' \\\n --header 'Content-Type: application/xml' \\\n --data-binary $'<key>value</key>'"
110
- );
111
- });
112
-
113
- it('should convert json to xml body properly', () => {
114
- const input: CodeSampleInput = {
115
- method: 'GET',
116
- url: 'https://example.com/path',
117
- headers: {
118
- 'Content-Type': 'application/xml',
119
- },
120
- body: '{ "key": "value" }',
121
- };
122
-
123
- const output = generator?.generate(input);
124
-
125
- expect(output).toBe(
126
- "curl -L \\\n --url 'https://example.com/path' \\\n --header 'Content-Type: application/xml' \\\n --data-binary $'<?xml version=1.0?>\n <key>value</key>\n '"
127
- );
128
- });
129
-
130
- it('should format application/graphql body properly', () => {
131
- const input: CodeSampleInput = {
132
- method: 'GET',
133
- url: 'https://example.com/path',
134
- headers: {
135
- 'Content-Type': 'application/graphql',
136
- },
137
- body: '{ key }',
138
- };
139
-
140
- const output = generator?.generate(input);
141
-
142
- expect(output).toBe(
143
- "curl -L \\\n --url 'https://example.com/path' \\\n --header 'Content-Type: application/json' \\\n --data '\"{ key }\"'"
144
- );
145
- });
146
-
147
- it('should format text/csv body properly', () => {
148
- const input: CodeSampleInput = {
149
- method: 'GET',
150
- url: 'https://example.com/path',
151
- headers: {
152
- 'Content-Type': 'text/csv',
153
- },
154
- body: 'key,value',
155
- };
156
-
157
- const output = generator?.generate(input);
158
-
159
- expect(output).toBe(
160
- "curl -L \\\n --url 'https://example.com/path' \\\n --header 'Content-Type: text/csv' \\\n --data-binary $'key,value'"
161
- );
162
- });
163
-
164
- it('should format application/pdf body properly', () => {
165
- const input: CodeSampleInput = {
166
- method: 'GET',
167
- url: 'https://example.com/path',
168
- headers: {
169
- 'Content-Type': 'application/pdf',
170
- },
171
- body: 'file',
172
- };
173
-
174
- const output = generator?.generate(input);
175
-
176
- expect(output).toBe(
177
- "curl -L \\\n --url 'https://example.com/path' \\\n --header 'Content-Type: application/pdf' \\\n --data-binary '@file'"
178
- );
179
- });
180
-
181
- it('should format text/plain body properly', () => {
182
- const input: CodeSampleInput = {
183
- method: 'GET',
184
- url: 'https://example.com/path',
185
- headers: {
186
- 'Content-Type': 'text/plain',
187
- },
188
- body: 'value',
189
- };
190
-
191
- const output = generator?.generate(input);
192
-
193
- expect(output).toBe(
194
- "curl -L \\\n --url 'https://example.com/path' \\\n --header 'Content-Type: text/plain' \\\n --data 'value'"
195
- );
196
- });
197
-
198
- it('should format multipart/form-data body properly', () => {
199
- const input: CodeSampleInput = {
200
- method: 'GET',
201
- url: 'https://example.com/path',
202
- headers: {
203
- 'Content-Type': 'multipart/form-data',
204
- },
205
- body: {
206
- key: 'value',
207
- },
208
- };
209
-
210
- const output = generator?.generate(input);
211
-
212
- expect(output).toBe(
213
- "curl -L \\\n --url 'https://example.com/path' \\\n --header 'Content-Type: multipart/form-data' \\\n --form 'key=value'"
214
- );
215
- });
216
- });
217
-
218
- describe('javascript code sample generator', () => {
219
- const generator = codeSampleGenerators.find((g) => g.id === 'javascript');
220
-
221
- expect(generator).toBeDefined();
222
-
223
- it('should format application/x-www-form-urlencoded body properly', () => {
224
- const input: CodeSampleInput = {
225
- method: 'GET',
226
- url: 'https://example.com/path',
227
- headers: {
228
- 'Content-Type': 'application/x-www-form-urlencoded',
229
- },
230
- body: {
231
- key: 'value',
232
- },
233
- };
234
-
235
- const output = generator?.generate(input);
236
-
237
- expect(output).toBe(
238
- 'const params = new URLSearchParams();\n\nparams.append("key", "value");\n\nconst response = await fetch(\'https://example.com/path\', {\n method: \'GET\',\n headers: {\n "Content-Type": "application/x-www-form-urlencoded"\n },\n body: params.toString()\n});\n\nconst data = await response.json();'
239
- );
240
- });
241
-
242
- it('should format application/json body properly', () => {
243
- const input: CodeSampleInput = {
244
- method: 'GET',
245
- url: 'https://example.com/path',
246
- headers: {
247
- 'Content-Type': 'application/json',
248
- },
249
- body: {
250
- key: 'value',
251
- },
252
- };
253
-
254
- const output = generator?.generate(input);
255
-
256
- expect(output).toBe(
257
- 'const response = await fetch(\'https://example.com/path\', {\n method: \'GET\',\n headers: {\n "Content-Type": "application/json"\n },\n body: JSON.stringify({\n "key": "value"\n })\n});\n\nconst data = await response.json();'
258
- );
259
- });
260
-
261
- it('should format application/xml body properly', () => {
262
- const input: CodeSampleInput = {
263
- method: 'GET',
264
- url: 'https://example.com/path',
265
- headers: {
266
- 'Content-Type': 'application/xml',
267
- },
268
- body: '<key>value</key>',
269
- };
270
-
271
- const output = generator?.generate(input);
272
-
273
- expect(output).toBe(
274
- 'const xml = `\n <key>value</key>`;\n\nconst response = await fetch(\'https://example.com/path\', {\n method: \'GET\',\n headers: {\n "Content-Type": "application/xml"\n },\n body: xml\n});\n\nconst data = await response.json();'
275
- );
276
- });
277
-
278
- it('should convert json to xml body properly', () => {
279
- const input: CodeSampleInput = {
280
- method: 'GET',
281
- url: 'https://example.com/path',
282
- headers: {
283
- 'Content-Type': 'application/xml',
284
- },
285
- body: '{ "key": "value" }',
286
- };
287
-
288
- const output = generator?.generate(input);
289
-
290
- expect(output).toBe(
291
- 'const xml = `\n <?xml version=1.0?>\n <key>value</key>\n`;\n\nconst response = await fetch(\'https://example.com/path\', {\n method: \'GET\',\n headers: {\n "Content-Type": "application/xml"\n },\n body: xml\n});\n\nconst data = await response.json();'
292
- );
293
- });
294
-
295
- it('should format application/graphql body properly', () => {
296
- const input: CodeSampleInput = {
297
- method: 'GET',
298
- url: 'https://example.com/path',
299
- headers: {
300
- 'Content-Type': 'application/graphql',
301
- },
302
- body: '{ key }',
303
- };
304
-
305
- const output = generator?.generate(input);
306
-
307
- expect(output).toBe(
308
- 'const query = `\n { key }`;\n\nconst response = await fetch(\'https://example.com/path\', {\n method: \'GET\',\n headers: {\n "Content-Type": "application/graphql"\n },\n body: JSON.stringify(query)\n});\n\nconst data = await response.json();'
309
- );
310
- });
311
-
312
- it('should format text/csv body properly', () => {
313
- const input: CodeSampleInput = {
314
- method: 'GET',
315
- url: 'https://example.com/path',
316
- headers: {
317
- 'Content-Type': 'text/csv',
318
- },
319
- body: 'key,value',
320
- };
321
-
322
- const output = generator?.generate(input);
323
-
324
- expect(output).toBe(
325
- 'const csv = `\n key,value`;\n\nconst response = await fetch(\'https://example.com/path\', {\n method: \'GET\',\n headers: {\n "Content-Type": "text/csv"\n },\n body: csv\n});\n\nconst data = await response.json();'
326
- );
327
- });
328
-
329
- it('should format application/pdf body properly', () => {
330
- const input: CodeSampleInput = {
331
- method: 'GET',
332
- url: 'https://example.com/path',
333
- headers: {
334
- 'Content-Type': 'application/pdf',
335
- },
336
- body: 'file',
337
- };
338
-
339
- const output = generator?.generate(input);
340
-
341
- expect(output).toBe(
342
- 'const formData = new FormData();\n\nformData.append("file", "file");\n\nconst response = await fetch(\'https://example.com/path\', {\n method: \'GET\',\n headers: {\n "Content-Type": "application/pdf"\n },\n body: formData\n});\n\nconst data = await response.json();'
343
- );
344
- });
345
-
346
- it('should format text/plain body properly', () => {
347
- const input: CodeSampleInput = {
348
- method: 'GET',
349
- url: 'https://example.com/path',
350
- headers: {
351
- 'Content-Type': 'text/plain',
352
- },
353
- body: 'value',
354
- };
355
-
356
- const output = generator?.generate(input);
357
-
358
- expect(output).toBe(
359
- 'const response = await fetch(\'https://example.com/path\', {\n method: \'GET\',\n headers: {\n "Content-Type": "text/plain"\n },\n body: "value"\n});\n\nconst data = await response.json();'
360
- );
361
- });
362
-
363
- it('should format multipart/form-data body properly', () => {
364
- const input: CodeSampleInput = {
365
- method: 'GET',
366
- url: 'https://example.com/path',
367
- headers: {
368
- 'Content-Type': 'multipart/form-data',
369
- },
370
- body: {
371
- key: 'value',
372
- },
373
- };
374
-
375
- const output = generator?.generate(input);
376
-
377
- expect(output).toBe(
378
- 'const formData = new FormData();\n\nformData.append("key", "value");\n\nconst response = await fetch(\'https://example.com/path\', {\n method: \'GET\',\n headers: {\n "Content-Type": "multipart/form-data"\n },\n body: formData\n});\n\nconst data = await response.json();'
379
- );
380
- });
381
- });
382
-
383
- describe('python code sample generator', () => {
384
- const generator = codeSampleGenerators.find((g) => g.id === 'python');
385
-
386
- expect(generator).toBeDefined();
387
-
388
- it('should format application/x-www-form-urlencoded body properly', () => {
389
- const input: CodeSampleInput = {
390
- method: 'GET',
391
- url: 'https://example.com/path',
392
- headers: {
393
- 'Content-Type': 'application/x-www-form-urlencoded',
394
- },
395
- body: {
396
- key: 'value',
397
- },
398
- };
399
-
400
- const output = generator?.generate(input);
401
-
402
- expect(output).toBe(
403
- 'import requests\n\nresponse = requests.get(\n "https://example.com/path",\n headers={"Content-Type":"application/x-www-form-urlencoded"},\n data={\n "key": "value"\n }\n)\n\ndata = response.json()'
404
- );
405
- });
406
-
407
- it('should format application/json body properly', () => {
408
- const input: CodeSampleInput = {
409
- method: 'POST',
410
- url: 'https://example.com/path',
411
- headers: {
412
- 'Content-Type': 'application/json',
413
- },
414
- body: {
415
- key: 'value',
416
- truethy: true,
417
- falsey: false,
418
- nullish: null,
419
- },
420
- };
421
-
422
- const output = generator?.generate(input);
423
-
424
- expect(output).toBe(
425
- 'import json\nimport requests\n\nresponse = requests.post(\n "https://example.com/path",\n headers={"Content-Type":"application/json"},\n data=json.dumps({\n "key": "value",\n "truethy": True,\n "falsey": False,\n "nullish": None\n })\n)\n\ndata = response.json()'
426
- );
427
- });
428
-
429
- it('should format application/xml body properly', () => {
430
- const input: CodeSampleInput = {
431
- method: 'GET',
432
- url: 'https://example.com/path',
433
- headers: {
434
- 'Content-Type': 'application/xml',
435
- },
436
- body: '<key>value</key>',
437
- };
438
-
439
- const output = generator?.generate(input);
440
-
441
- expect(output).toBe(
442
- 'import requests\n\nresponse = requests.get(\n "https://example.com/path",\n headers={"Content-Type":"application/xml"},\n data="<key>value</key>"\n)\n\ndata = response.json()'
443
- );
444
- });
445
-
446
- it('should convert json to xml body properly', () => {
447
- const input: CodeSampleInput = {
448
- method: 'GET',
449
- url: 'https://example.com/path',
450
- headers: {
451
- 'Content-Type': 'application/xml',
452
- },
453
- body: '{ "key": "value" }',
454
- };
455
-
456
- const output = generator?.generate(input);
457
-
458
- expect(output).toBe(
459
- 'import requests\n\nresponse = requests.get(\n "https://example.com/path",\n headers={"Content-Type":"application/xml"},\n data="<?xml version=1.0?>\\n<key>value</key>\\n"\n)\n\ndata = response.json()'
460
- );
461
- });
462
-
463
- it('should format application/graphql body properly', () => {
464
- const input: CodeSampleInput = {
465
- method: 'GET',
466
- url: 'https://example.com/path',
467
- headers: {
468
- 'Content-Type': 'application/graphql',
469
- },
470
- body: '{ key }',
471
- };
472
-
473
- const output = generator?.generate(input);
474
-
475
- expect(output).toBe(
476
- 'import requests\n\nresponse = requests.get(\n "https://example.com/path",\n headers={"Content-Type":"application/graphql"},\n data="{ key }"\n)\n\ndata = response.json()'
477
- );
478
- });
479
-
480
- it('should format text/csv body properly', () => {
481
- const input: CodeSampleInput = {
482
- method: 'GET',
483
- url: 'https://example.com/path',
484
- headers: {
485
- 'Content-Type': 'text/csv',
486
- },
487
- body: 'key,value',
488
- };
489
-
490
- const output = generator?.generate(input);
491
-
492
- expect(output).toBe(
493
- 'import requests\n\nresponse = requests.get(\n "https://example.com/path",\n headers={"Content-Type":"text/csv"},\n data="key,value"\n)\n\ndata = response.json()'
494
- );
495
- });
496
-
497
- it('should format application/pdf body properly', () => {
498
- const input: CodeSampleInput = {
499
- method: 'GET',
500
- url: 'https://example.com/path',
501
- headers: {
502
- 'Content-Type': 'application/pdf',
503
- },
504
- body: 'file',
505
- };
506
-
507
- const output = generator?.generate(input);
508
-
509
- expect(output).toBe(
510
- 'import requests\n\nfiles = {\n "file": "file",\n}\n\nresponse = requests.get(\n "https://example.com/path",\n headers={"Content-Type":"application/pdf"},\n files=files\n)\n\ndata = response.json()'
511
- );
512
- });
513
-
514
- it('should format text/plain body properly', () => {
515
- const input: CodeSampleInput = {
516
- method: 'GET',
517
- url: 'https://example.com/path',
518
- headers: {
519
- 'Content-Type': 'text/plain',
520
- },
521
- body: 'value',
522
- };
523
-
524
- const output = generator?.generate(input);
525
-
526
- expect(output).toBe(
527
- 'import requests\n\nresponse = requests.get(\n "https://example.com/path",\n headers={"Content-Type":"text/plain"},\n data="value"\n)\n\ndata = response.json()'
528
- );
529
- });
530
-
531
- it('should format multipart/form-data body properly', () => {
532
- const input: CodeSampleInput = {
533
- method: 'GET',
534
- url: 'https://example.com/path',
535
- headers: {
536
- 'Content-Type': 'multipart/form-data',
537
- },
538
- body: {
539
- key: 'value',
540
- },
541
- };
542
-
543
- const output = generator?.generate(input);
544
-
545
- expect(output).toBe(
546
- 'import requests\n\nfiles = {\n "key": "value",\n}\n\nresponse = requests.get(\n "https://example.com/path",\n headers={"Content-Type":"multipart/form-data"},\n files=files\n)\n\ndata = response.json()'
547
- );
548
- });
549
- });
550
-
551
- describe('http code sample generator', () => {
552
- const generator = codeSampleGenerators.find((g) => g.id === 'http');
553
-
554
- expect(generator).toBeDefined();
555
-
556
- it('should format application/x-www-form-urlencoded body properly', () => {
557
- const input: CodeSampleInput = {
558
- method: 'GET',
559
- url: 'https://example.com/path',
560
- headers: {
561
- 'Content-Type': 'application/x-www-form-urlencoded',
562
- },
563
- body: {
564
- key: 'value',
565
- },
566
- };
567
-
568
- const output = generator?.generate(input);
569
-
570
- expect(output).toBe(
571
- 'GET /path HTTP/1.1\nHost: example.com\nContent-Type: application/x-www-form-urlencoded\nContent-Length: 15\nAccept: */*\n\n"key=\'value\'"'
572
- );
573
- });
574
-
575
- it('should format application/json body properly', () => {
576
- const input: CodeSampleInput = {
577
- method: 'GET',
578
- url: 'https://example.com/path',
579
- headers: {
580
- 'Content-Type': 'application/json',
581
- },
582
- body: {
583
- key: 'value',
584
- },
585
- };
586
-
587
- const output = generator?.generate(input);
588
-
589
- expect(output).toBe(
590
- 'GET /path HTTP/1.1\nHost: example.com\nContent-Type: application/json\nContent-Length: 15\nAccept: */*\n\n{\n "key": "value"\n}'
591
- );
592
- });
593
-
594
- it('should format application/xml body properly', () => {
595
- const input: CodeSampleInput = {
596
- method: 'GET',
597
- url: 'https://example.com/path',
598
- headers: {
599
- 'Content-Type': 'application/xml',
600
- },
601
- body: '<key>value</key>',
602
- };
603
-
604
- const output = generator?.generate(input);
605
-
606
- expect(output).toBe(
607
- 'GET /path HTTP/1.1\nHost: example.com\nContent-Type: application/xml\nContent-Length: 18\nAccept: */*\n\n"<key>value</key>"'
608
- );
609
- });
610
-
611
- it('should convert json to xml body properly', () => {
612
- const input: CodeSampleInput = {
613
- method: 'GET',
614
- url: 'https://example.com/path',
615
- headers: {
616
- 'Content-Type': 'application/xml',
617
- },
618
- body: '{ "key": "value" }',
619
- };
620
-
621
- const output = generator?.generate(input);
622
-
623
- expect(output).toBe(
624
- 'GET /path HTTP/1.1\nHost: example.com\nContent-Type: application/xml\nContent-Length: 24\nAccept: */*\n\n"<?xml version=1.0?>\n<key>value</key>\n"'
625
- );
626
- });
627
-
628
- it('should format application/graphql body properly', () => {
629
- const input: CodeSampleInput = {
630
- method: 'GET',
631
- url: 'https://example.com/path',
632
- headers: {
633
- 'Content-Type': 'application/graphql',
634
- },
635
- body: '{ key }',
636
- };
637
-
638
- const output = generator?.generate(input);
639
-
640
- expect(output).toBe(
641
- 'GET /path HTTP/1.1\nHost: example.com\nContent-Type: application/graphql\nContent-Length: 9\nAccept: */*\n\n"{ key }"'
642
- );
643
- });
644
-
645
- it('should format text/csv body properly', () => {
646
- const input: CodeSampleInput = {
647
- method: 'GET',
648
- url: 'https://example.com/path',
649
- headers: {
650
- 'Content-Type': 'text/csv',
651
- },
652
- body: 'key,value',
653
- };
654
-
655
- const output = generator?.generate(input);
656
-
657
- expect(output).toBe(
658
- 'GET /path HTTP/1.1\nHost: example.com\nContent-Type: text/csv\nContent-Length: 11\nAccept: */*\n\n"key,value"'
659
- );
660
- });
661
-
662
- it('should format application/pdf body properly', () => {
663
- const input: CodeSampleInput = {
664
- method: 'GET',
665
- url: 'https://example.com/path',
666
- headers: {
667
- 'Content-Type': 'application/pdf',
668
- },
669
- body: 'file',
670
- };
671
-
672
- const output = generator?.generate(input);
673
-
674
- expect(output).toBe(
675
- 'GET /path HTTP/1.1\nHost: example.com\nContent-Type: application/pdf\nContent-Length: 6\nAccept: */*\n\n"file"'
676
- );
677
- });
678
-
679
- it('should format text/plain body properly', () => {
680
- const input: CodeSampleInput = {
681
- method: 'GET',
682
- url: 'https://example.com/path',
683
- headers: {
684
- 'Content-Type': 'text/plain',
685
- },
686
- body: 'value',
687
- };
688
-
689
- const output = generator?.generate(input);
690
-
691
- expect(output).toBe(
692
- 'GET /path HTTP/1.1\nHost: example.com\nContent-Type: text/plain\nContent-Length: 7\nAccept: */*\n\n"value"'
693
- );
694
- });
695
-
696
- it('should format multipart/form-data body properly', () => {
697
- const input: CodeSampleInput = {
698
- method: 'GET',
699
- url: 'https://example.com/path',
700
- headers: {
701
- 'Content-Type': 'multipart/form-data',
702
- },
703
- body: {
704
- key: 'value',
705
- },
706
- };
707
-
708
- const output = generator?.generate(input);
709
-
710
- expect(output).toBe(
711
- 'GET /path HTTP/1.1\nHost: example.com\nContent-Type: multipart/form-data\nContent-Length: 15\nAccept: */*\n\n{\n "key": "value"\n}'
712
- );
713
- });
714
- });