@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,448 +0,0 @@
1
- import yaml from 'js-yaml';
2
- import {
3
- isCSV,
4
- isFormData,
5
- isFormUrlEncoded,
6
- isGraphQL,
7
- isJSON,
8
- isPDF,
9
- isPlainObject,
10
- isText,
11
- isXML,
12
- isYAML,
13
- } from './contentTypeChecks';
14
- import { json2xml } from './json2xml';
15
- import { stringifyOpenAPI } from './stringifyOpenAPI';
16
-
17
- export interface CodeSampleInput {
18
- method: string;
19
- url: string;
20
- headers?: Record<string, string>;
21
- body?: any;
22
- }
23
-
24
- export interface CodeSampleGenerator {
25
- id: string;
26
- label: string;
27
- syntax: string;
28
- generate: (operation: CodeSampleInput) => string;
29
- }
30
-
31
- export const codeSampleGenerators: CodeSampleGenerator[] = [
32
- {
33
- id: 'http',
34
- label: 'HTTP',
35
- syntax: 'http',
36
- generate: ({ method, url, headers = {}, body }: CodeSampleInput) => {
37
- const { host, path } = parseHostAndPath(url);
38
-
39
- if (body) {
40
- // if we had a body add a content length header
41
- const bodyContent = body ? stringifyOpenAPI(body) : '';
42
- // handle unicode chars with a text encoder
43
- const encoder = new TextEncoder();
44
-
45
- const bodyString = BodyGenerators.getHTTPBody(body, headers);
46
-
47
- if (bodyString) {
48
- body = bodyString;
49
- }
50
-
51
- headers = {
52
- ...headers,
53
- 'Content-Length': encoder.encode(bodyContent).length.toString(),
54
- };
55
- }
56
-
57
- if (!headers.hasOwnProperty('Accept')) {
58
- headers.Accept = '*/*';
59
- }
60
-
61
- const headerString = headers
62
- ? `${Object.entries(headers)
63
- .map(([key, value]) =>
64
- key.toLowerCase() !== 'host' ? `${key}: ${value}` : ''
65
- )
66
- .join('\n')}\n`
67
- : '';
68
-
69
- const bodyString = body ? `\n${body}` : '';
70
-
71
- const httpRequest = `${method.toUpperCase()} ${decodeURI(path)} HTTP/1.1
72
- Host: ${host}
73
- ${headerString}${bodyString}`;
74
-
75
- return httpRequest;
76
- },
77
- },
78
- {
79
- id: 'curl',
80
- label: 'cURL',
81
- syntax: 'bash',
82
- generate: ({ method, url, headers, body }) => {
83
- const separator = ' \\\n';
84
-
85
- const lines: string[] = ['curl -L'];
86
-
87
- if (method.toUpperCase() !== 'GET') {
88
- lines.push(`--request ${method.toUpperCase()}`);
89
- }
90
-
91
- lines.push(`--url '${url}'`);
92
-
93
- if (body) {
94
- const bodyContent = BodyGenerators.getCurlBody(body, headers);
95
-
96
- if (bodyContent) {
97
- body = bodyContent.body;
98
- headers = bodyContent.headers;
99
- }
100
- }
101
-
102
- if (headers && Object.keys(headers).length > 0) {
103
- Object.entries(headers).forEach(([key, value]) => {
104
- lines.push(`--header '${key}: ${value}'`);
105
- });
106
- }
107
-
108
- if (body) {
109
- if (Array.isArray(body)) {
110
- lines.push(...body);
111
- } else {
112
- lines.push(body);
113
- }
114
- }
115
-
116
- return lines.map((line, index) => (index > 0 ? indent(line, 2) : line)).join(separator);
117
- },
118
- },
119
- {
120
- id: 'javascript',
121
- label: 'JavaScript',
122
- syntax: 'javascript',
123
- generate: ({ method, url, headers, body }) => {
124
- let code = '';
125
-
126
- if (body) {
127
- const lines = BodyGenerators.getJavaScriptBody(body, headers);
128
-
129
- if (lines) {
130
- // add the generated code to the top
131
- code += lines.code;
132
- body = lines.body;
133
- headers = lines.headers;
134
- }
135
- }
136
-
137
- code += `const response = await fetch('${url}', {
138
- method: '${method.toUpperCase()}',\n`;
139
-
140
- if (headers && Object.keys(headers).length > 0) {
141
- code += indent(`headers: ${stringifyOpenAPI(headers, null, 2)},\n`, 4);
142
- }
143
-
144
- if (body) {
145
- code += indent(`body: ${body}\n`, 4);
146
- }
147
-
148
- code += '});\n\n';
149
- code += 'const data = await response.json();';
150
-
151
- return code;
152
- },
153
- },
154
- {
155
- id: 'python',
156
- label: 'Python',
157
- syntax: 'python',
158
- generate: ({ method, url, headers, body }) => {
159
- const contentType = headers?.['Content-Type'];
160
- let code = `${isJSON(contentType) ? 'import json\n' : ''}import requests\n\n`;
161
-
162
- if (body) {
163
- const lines = BodyGenerators.getPythonBody(body, headers);
164
-
165
- // add the generated code to the top
166
- if (lines) {
167
- code += lines.code;
168
- body = lines.body;
169
- headers = lines.headers;
170
- }
171
- }
172
-
173
- code += `response = requests.${method.toLowerCase()}(\n`;
174
- code += indent(`"${url}",\n`, 4);
175
-
176
- if (headers && Object.keys(headers).length > 0) {
177
- code += indent(`headers=${stringifyOpenAPI(headers)},\n`, 4);
178
- }
179
-
180
- if (body) {
181
- if (body === 'files') {
182
- code += indent(`files=${body}\n`, 4);
183
- } else if (isJSON(contentType)) {
184
- code += indent(`data=json.dumps(${body})\n`, 4);
185
- } else {
186
- code += indent(`data=${body}\n`, 4);
187
- }
188
- }
189
-
190
- code += ')\n\n';
191
- code += 'data = response.json()';
192
- return code;
193
- },
194
- },
195
- ];
196
-
197
- function indent(code: string, spaces: number) {
198
- const indent = ' '.repeat(spaces);
199
- return code
200
- .split('\n')
201
- .map((line) => (line ? indent + line : ''))
202
- .join('\n');
203
- }
204
-
205
- export function parseHostAndPath(url: string) {
206
- try {
207
- const urlObj = new URL(url);
208
- const path = urlObj.pathname || '/';
209
- return { host: urlObj.host, path };
210
- } catch (_e) {
211
- // If the URL was invalid do our best to parse the URL.
212
- // Check for the protocol part and pull it off to grab the host
213
- const splitted = url.split('//');
214
- const fullUrl = splitted[1] ? splitted[1] : url;
215
-
216
- // separate paths from the first element (host)
217
- const parts = fullUrl.split('/');
218
- // pull off the host (mutates)
219
- const host = parts.shift();
220
- // add a leading slash and join the paths again
221
- const path = `/${parts.join('/')}`;
222
-
223
- return { host, path };
224
- }
225
- }
226
-
227
- // Body Generators
228
- const BodyGenerators = {
229
- getCurlBody(body: any, headers?: Record<string, string>) {
230
- if (!body || !headers) return undefined;
231
-
232
- // Copy headers to avoid mutating the original object
233
- const headersCopy = { ...headers };
234
- const contentType: string = headersCopy['Content-Type'] || '';
235
-
236
- if (isFormData(contentType)) {
237
- body = isPlainObject(body)
238
- ? Object.entries(body).map(([key, value]) => `--form '${key}=${String(value)}'`)
239
- : `--form 'file=@${body}'`;
240
- } else if (isFormUrlEncoded(contentType)) {
241
- body = isPlainObject(body)
242
- ? `--data '${Object.entries(body)
243
- .map(([key, value]) => `${key}=${String(value)}`)
244
- .join('&')}'`
245
- : String(body);
246
- } else if (isText(contentType)) {
247
- body = `--data '${String(body).replace(/"/g, '')}'`;
248
- } else if (isXML(contentType)) {
249
- // Convert to XML and ensure proper formatting
250
- body = `--data-binary $'${convertBodyToXML(body)}'`;
251
- } else if (isCSV(contentType)) {
252
- // We use --data-binary to avoid cURL converting newlines to \r\n
253
- body = `--data-binary $'${stringifyOpenAPI(body).replace(/"/g, '').replace(/\\n/g, '\n')}'`;
254
- } else if (isGraphQL(contentType)) {
255
- body = `--data '${stringifyOpenAPI(body)}'`;
256
- // Set Content-Type to application/json for GraphQL, recommended by GraphQL spec
257
- headersCopy['Content-Type'] = 'application/json';
258
- } else if (isPDF(contentType)) {
259
- // We use --data-binary to avoid cURL converting newlines to \r\n
260
- body = `--data-binary '@${String(body)}'`;
261
- } else if (isYAML(contentType)) {
262
- body = `--data-binary $'${yaml.dump(body).replace(/'/g, '').replace(/\\n/g, '\n')}'`;
263
- } else {
264
- body = `--data '${stringifyOpenAPI(body, null, 2).replace(/\\n/g, '\n')}'`;
265
- }
266
-
267
- return {
268
- body,
269
- headers: headersCopy,
270
- };
271
- },
272
- getJavaScriptBody: (body: any, headers?: Record<string, string>) => {
273
- if (!body || !headers) return;
274
-
275
- let code = '';
276
-
277
- // Copy headers to avoid mutating the original object
278
- const headersCopy = { ...headers };
279
- const contentType: string = headersCopy['Content-Type'] || '';
280
-
281
- // Use FormData for file uploads
282
- if (isFormData(contentType)) {
283
- code += 'const formData = new FormData();\n\n';
284
- if (isPlainObject(body)) {
285
- Object.entries(body).forEach(([key, value]) => {
286
- code += `formData.append("${key}", "${String(value)}");\n`;
287
- });
288
- } else if (typeof body === 'string') {
289
- code += `formData.append("file", "${body}");\n`;
290
- }
291
- code += '\n';
292
- body = 'formData';
293
- } else if (isFormUrlEncoded(contentType)) {
294
- // Use URLSearchParams for form-urlencoded data
295
- code += 'const params = new URLSearchParams();\n\n';
296
- if (isPlainObject(body)) {
297
- Object.entries(body).forEach(([key, value]) => {
298
- code += `params.append("${key}", "${String(value)}");\n`;
299
- });
300
- }
301
- code += '\n';
302
- body = 'params.toString()';
303
- } else if (isGraphQL(contentType)) {
304
- if (isPlainObject(body)) {
305
- Object.entries(body).forEach(([key, value]) => {
306
- code += `const ${key} = \`\n${indent(String(value), 4)}\`;\n\n`;
307
- });
308
- body = `JSON.stringify({ ${Object.keys(body).join(', ')} })`;
309
- // Set Content-Type to application/json for GraphQL, recommended by GraphQL spec
310
- headersCopy['Content-Type'] = 'application/json';
311
- } else {
312
- code += `const query = \`\n${indent(String(body), 4)}\`;\n\n`;
313
- body = 'JSON.stringify(query)';
314
- }
315
- } else if (isCSV(contentType)) {
316
- code += 'const csv = `\n';
317
- code += indent(String(body), 4);
318
- code += '`;\n\n';
319
- body = 'csv';
320
- } else if (isPDF(contentType)) {
321
- // Use FormData to upload PDF files
322
- code += 'const formData = new FormData();\n\n';
323
- code += `formData.append("file", "${body}");\n\n`;
324
- body = 'formData';
325
- } else if (isXML(contentType)) {
326
- code += 'const xml = `\n';
327
-
328
- // Convert JSON to XML if needed
329
- code += indent(convertBodyToXML(body), 4);
330
- code += '`;\n\n';
331
- body = 'xml';
332
- } else if (isYAML(contentType)) {
333
- code += `const yamlBody = \`\n${indent(yaml.dump(body), 4)}\`;\n\n`;
334
- body = 'yamlBody';
335
- } else if (isText(contentType)) {
336
- body = stringifyOpenAPI(body, null, 2);
337
- } else {
338
- body = `JSON.stringify(${stringifyOpenAPI(body, null, 2)})`;
339
- }
340
-
341
- return { body, code, headers: headersCopy };
342
- },
343
- getPythonBody: (body: any, headers?: Record<string, string>) => {
344
- if (!body || !headers) return;
345
- let code = '';
346
- const contentType: string = headers['Content-Type'] || '';
347
-
348
- if (isFormData(contentType)) {
349
- code += 'files = {\n';
350
- if (isPlainObject(body)) {
351
- Object.entries(body).forEach(([key, value]) => {
352
- code += `${indent(`"${key}": "${String(value)}",`, 4)}\n`;
353
- });
354
- }
355
- code += '}\n\n';
356
- body = 'files';
357
- } else if (isPDF(contentType)) {
358
- code += 'files = {\n';
359
- code += `${indent(`"file": "${body}",`, 4)}\n`;
360
- code += '}\n\n';
361
- body = 'files';
362
- } else if (isXML(contentType)) {
363
- // Convert JSON to XML if needed
364
- body = JSON.stringify(convertBodyToXML(body));
365
- } else if (isYAML(contentType)) {
366
- code += `yamlBody = \"\"\"\n${indent(yaml.dump(body), 4)}\"\"\"\n\n`;
367
- body = 'yamlBody';
368
- } else {
369
- body = stringifyOpenAPI(
370
- body,
371
- (_key, value) => {
372
- switch (value) {
373
- case true:
374
- return '$$__TRUE__$$';
375
- case false:
376
- return '$$__FALSE__$$';
377
- case null:
378
- return '$$__NULL__$$';
379
- default:
380
- return value;
381
- }
382
- },
383
- 2
384
- )
385
- .replaceAll('"$$__TRUE__$$"', 'True')
386
- .replaceAll('"$$__FALSE__$$"', 'False')
387
- .replaceAll('"$$__NULL__$$"', 'None');
388
- }
389
-
390
- return { body, code, headers };
391
- },
392
- getHTTPBody: (body: any, headers?: Record<string, string>) => {
393
- if (!body || !headers) return undefined;
394
-
395
- const contentType: string = headers['Content-Type'] || '';
396
-
397
- const typeHandlers = {
398
- pdf: () => `${stringifyOpenAPI(body, null, 2)}`,
399
- formUrlEncoded: () => {
400
- const encoded = isPlainObject(body)
401
- ? Object.entries(body)
402
- .map(([key, value]) => `${key}=${stringifyOpenAPI(value)}`)
403
- .join('&')
404
- : stringifyOpenAPI(body);
405
- return `"${encoded.replace(/"/g, "'")}"`;
406
- },
407
- text: () => `"${String(body)}"`,
408
- xml: () => {
409
- // Convert JSON to XML if needed
410
- return `"${convertBodyToXML(body)}"`;
411
- },
412
- yaml: () => `"${yaml.dump(body).replace(/"/g, '\\"')}"`,
413
- csv: () => `"${stringifyOpenAPI(body).replace(/"/g, '')}"`,
414
- default: () => `${stringifyOpenAPI(body, null, 2)}`,
415
- };
416
-
417
- if (isPDF(contentType)) return typeHandlers.pdf();
418
- if (isFormUrlEncoded(contentType)) return typeHandlers.formUrlEncoded();
419
- if (isText(contentType)) return typeHandlers.text();
420
- if (isXML(contentType)) return typeHandlers.xml();
421
- if (isYAML(contentType)) return typeHandlers.yaml();
422
- if (isCSV(contentType)) return typeHandlers.csv();
423
-
424
- return typeHandlers.default();
425
- },
426
- };
427
-
428
- /**
429
- * Converts a body to XML format
430
- */
431
- function convertBodyToXML(body: any): string {
432
- // If body is already a string and looks like XML, return it as is
433
- if (typeof body === 'string' && body.trim().startsWith('<')) {
434
- return body;
435
- }
436
-
437
- // If body is not an object, try to parse it as JSON
438
- if (typeof body !== 'object' || body === null) {
439
- try {
440
- body = JSON.parse(body);
441
- } catch {
442
- // If parsing fails, return the original body
443
- return body;
444
- }
445
- }
446
-
447
- return json2xml(body).replace(/"/g, '').replace(/\\n/g, '\n').replace(/\\t/g, '\t');
448
- }
@@ -1,31 +0,0 @@
1
- import { OpenAPISpec } from '../OpenAPISpec';
2
- import { type OpenAPIContext, getOpenAPIClientContext } from '../context';
3
- import { t } from '../translate';
4
- import type { OpenAPIOperationData, OpenAPIWebhookData } from '../types';
5
- import { OpenAPIOperationDescription } from './OpenAPIOperationDescription';
6
-
7
- export function OpenAPIColumnSpec(props: {
8
- data: OpenAPIOperationData | OpenAPIWebhookData;
9
- context: OpenAPIContext;
10
- }) {
11
- const { data, context } = props;
12
- const { operation } = data;
13
-
14
- const clientContext = getOpenAPIClientContext(context);
15
-
16
- return (
17
- <div className="openapi-column-spec">
18
- {operation['x-deprecated-sunset'] ? (
19
- <div className="openapi-deprecated-sunset openapi-description openapi-markdown">
20
- {t(context.translation, 'deprecated_and_sunset_on', [
21
- <span key="date" className="openapi-deprecated-sunset-date">
22
- {operation['x-deprecated-sunset']}
23
- </span>,
24
- ])}
25
- </div>
26
- ) : null}
27
- <OpenAPIOperationDescription operation={operation} context={context} />
28
- <OpenAPISpec data={data} context={clientContext} />
29
- </div>
30
- );
31
- }
@@ -1,31 +0,0 @@
1
- import type { OpenAPICustomOperationProperties, OpenAPIV3 } from '@gitbook/openapi-parser';
2
- import { Markdown } from '../Markdown';
3
- import type { OpenAPIContext } from '../context';
4
- import { resolveDescription } from '../utils';
5
-
6
- export function OpenAPIOperationDescription(props: {
7
- operation: OpenAPIV3.OperationObject<OpenAPICustomOperationProperties>;
8
- context: OpenAPIContext;
9
- }) {
10
- const { operation } = props;
11
- if (operation['x-gitbook-description-document']) {
12
- return (
13
- <div className="openapi-intro">
14
- {props.context.renderDocument({
15
- document: operation['x-gitbook-description-document'],
16
- })}
17
- </div>
18
- );
19
- }
20
-
21
- const description = resolveDescription(operation);
22
- if (!description) {
23
- return null;
24
- }
25
-
26
- return (
27
- <div className="openapi-intro">
28
- <Markdown className="openapi-description" source={description} />
29
- </div>
30
- );
31
- }
@@ -1,23 +0,0 @@
1
- import type { OpenAPIStability as OpenAPIStabilityType } from '@gitbook/openapi-parser';
2
-
3
- const stabilityEnum: Record<OpenAPIStabilityType, string> = {
4
- experimental: 'Experimental',
5
- alpha: 'Alpha',
6
- beta: 'Beta',
7
- } as const;
8
-
9
- export function OpenAPIStability(props: { stability: OpenAPIStabilityType }) {
10
- const { stability } = props;
11
-
12
- const foundStability = stabilityEnum[stability];
13
-
14
- if (!foundStability) {
15
- return null;
16
- }
17
-
18
- return (
19
- <div className={`openapi-stability openapi-stability-${foundStability.toLowerCase()}`}>
20
- {foundStability}
21
- </div>
22
- );
23
- }
@@ -1,45 +0,0 @@
1
- import { OpenAPIPath } from '../OpenAPIPath';
2
- import type { OpenAPIContext } from '../context';
3
- import type { OpenAPIOperationData, OpenAPIWebhookData } from '../types';
4
- import { OpenAPIStability } from './OpenAPIStability';
5
-
6
- export function OpenAPISummary(props: {
7
- data: OpenAPIOperationData | OpenAPIWebhookData;
8
- context: OpenAPIContext;
9
- }) {
10
- const { data, context } = props;
11
- const { operation } = data;
12
-
13
- const title = (() => {
14
- if (operation.summary) {
15
- return operation.summary;
16
- }
17
-
18
- if ('name' in data) {
19
- return data.name;
20
- }
21
-
22
- return undefined;
23
- })();
24
-
25
- return (
26
- <div className="openapi-summary" id={operation.summary ? undefined : context.id}>
27
- {(operation.deprecated || operation['x-stability']) && (
28
- <div className="openapi-summary-tags">
29
- {operation.deprecated && <div className="openapi-deprecated">Deprecated</div>}
30
- {operation['x-stability'] && (
31
- <OpenAPIStability stability={operation['x-stability']} />
32
- )}
33
- </div>
34
- )}
35
- {title
36
- ? context.renderHeading({
37
- deprecated: operation.deprecated ?? false,
38
- stability: operation['x-stability'],
39
- title,
40
- })
41
- : null}
42
- {'path' in data ? <OpenAPIPath data={data} context={context} /> : null}
43
- </div>
44
- );
45
- }
@@ -1,39 +0,0 @@
1
- export function isJSON(contentType?: string): boolean {
2
- return contentType?.toLowerCase().includes('application/json') || false;
3
- }
4
-
5
- export function isXML(contentType?: string): boolean {
6
- return contentType?.toLowerCase().includes('application/xml') || false;
7
- }
8
-
9
- export function isYAML(contentType?: string): boolean {
10
- return contentType?.toLowerCase().includes('application/yaml') || false;
11
- }
12
-
13
- export function isGraphQL(contentType?: string): boolean {
14
- return contentType?.toLowerCase().includes('application/graphql') || false;
15
- }
16
-
17
- export function isCSV(contentType?: string): boolean {
18
- return contentType?.toLowerCase().includes('text/csv') || false;
19
- }
20
-
21
- export function isPDF(contentType?: string): boolean {
22
- return contentType?.toLowerCase().includes('application/pdf') || false;
23
- }
24
-
25
- export function isText(contentType?: string): boolean {
26
- return contentType?.toLowerCase().includes('text/plain') || false;
27
- }
28
-
29
- export function isFormUrlEncoded(contentType?: string): boolean {
30
- return contentType?.toLowerCase().includes('application/x-www-form-urlencoded') || false;
31
- }
32
-
33
- export function isFormData(contentType?: string): boolean {
34
- return !!contentType && contentType.toLowerCase().includes('multipart/form-data');
35
- }
36
-
37
- export function isPlainObject(value: unknown): boolean {
38
- return typeof value === 'object' && value !== null && !Array.isArray(value);
39
- }