@scalar/oas-utils 0.2.140 → 0.2.141

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 (280) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/entities/cookie/cookie.js +14 -13
  3. package/dist/entities/cookie/cookie.js.map +7 -0
  4. package/dist/entities/cookie/index.d.ts +1 -1
  5. package/dist/entities/cookie/index.d.ts.map +1 -1
  6. package/dist/entities/cookie/index.js +5 -1
  7. package/dist/entities/cookie/index.js.map +7 -0
  8. package/dist/entities/environment/environment.js +10 -9
  9. package/dist/entities/environment/environment.js.map +7 -0
  10. package/dist/entities/environment/index.d.ts +1 -1
  11. package/dist/entities/environment/index.d.ts.map +1 -1
  12. package/dist/entities/environment/index.js +5 -1
  13. package/dist/entities/environment/index.js.map +7 -0
  14. package/dist/entities/hotkeys/hotkeys.js +109 -116
  15. package/dist/entities/hotkeys/hotkeys.js.map +7 -0
  16. package/dist/entities/hotkeys/index.d.ts +1 -1
  17. package/dist/entities/hotkeys/index.d.ts.map +1 -1
  18. package/dist/entities/hotkeys/index.js +6 -1
  19. package/dist/entities/hotkeys/index.js.map +7 -0
  20. package/dist/entities/shared/index.d.ts +1 -1
  21. package/dist/entities/shared/index.d.ts.map +1 -1
  22. package/dist/entities/shared/index.js +7 -1
  23. package/dist/entities/shared/index.js.map +7 -0
  24. package/dist/entities/shared/utility.js +9 -12
  25. package/dist/entities/shared/utility.js.map +7 -0
  26. package/dist/entities/spec/collection.d.ts.map +1 -1
  27. package/dist/entities/spec/collection.js +91 -94
  28. package/dist/entities/spec/collection.js.map +7 -0
  29. package/dist/entities/spec/index.d.ts +8 -8
  30. package/dist/entities/spec/index.d.ts.map +1 -1
  31. package/dist/entities/spec/index.js +59 -9
  32. package/dist/entities/spec/index.js.map +7 -0
  33. package/dist/entities/spec/operation.d.ts +1 -1
  34. package/dist/entities/spec/operation.d.ts.map +1 -1
  35. package/dist/entities/spec/operation.js +5 -8
  36. package/dist/entities/spec/operation.js.map +7 -0
  37. package/dist/entities/spec/operation.test.js +43 -0
  38. package/dist/entities/spec/operation.test.js.map +7 -0
  39. package/dist/entities/spec/parameters.d.ts +1 -1
  40. package/dist/entities/spec/parameters.d.ts.map +1 -1
  41. package/dist/entities/spec/parameters.js +40 -38
  42. package/dist/entities/spec/parameters.js.map +7 -0
  43. package/dist/entities/spec/parameters.test.js +71 -0
  44. package/dist/entities/spec/parameters.test.js.map +7 -0
  45. package/dist/entities/spec/request-example.test.js +637 -0
  46. package/dist/entities/spec/request-example.test.js.map +7 -0
  47. package/dist/entities/spec/request-examples.d.ts +3 -3
  48. package/dist/entities/spec/request-examples.d.ts.map +1 -1
  49. package/dist/entities/spec/request-examples.js +323 -338
  50. package/dist/entities/spec/request-examples.js.map +7 -0
  51. package/dist/entities/spec/requests.d.ts +6 -8
  52. package/dist/entities/spec/requests.d.ts.map +1 -1
  53. package/dist/entities/spec/requests.js +80 -101
  54. package/dist/entities/spec/requests.js.map +7 -0
  55. package/dist/entities/spec/server.js +47 -63
  56. package/dist/entities/spec/server.js.map +7 -0
  57. package/dist/entities/spec/server.test.js +120 -0
  58. package/dist/entities/spec/server.test.js.map +7 -0
  59. package/dist/entities/spec/spec-objects.d.ts.map +1 -1
  60. package/dist/entities/spec/spec-objects.js +77 -121
  61. package/dist/entities/spec/spec-objects.js.map +7 -0
  62. package/dist/entities/spec/spec-objects.test.js +205 -0
  63. package/dist/entities/spec/spec-objects.test.js.map +7 -0
  64. package/dist/entities/spec/x-scalar-environments.js +21 -18
  65. package/dist/entities/spec/x-scalar-environments.js.map +7 -0
  66. package/dist/entities/spec/x-scalar-environments.test.js +11 -0
  67. package/dist/entities/spec/x-scalar-environments.test.js.map +7 -0
  68. package/dist/entities/spec/x-scalar-secrets.js +8 -6
  69. package/dist/entities/spec/x-scalar-secrets.js.map +7 -0
  70. package/dist/entities/spec/x-scalar-secrets.test.js +11 -0
  71. package/dist/entities/spec/x-scalar-secrets.test.js.map +7 -0
  72. package/dist/entities/workspace/index.d.ts +1 -1
  73. package/dist/entities/workspace/index.d.ts.map +1 -1
  74. package/dist/entities/workspace/index.js +7 -1
  75. package/dist/entities/workspace/index.js.map +7 -0
  76. package/dist/entities/workspace/workspace.js +46 -50
  77. package/dist/entities/workspace/workspace.js.map +7 -0
  78. package/dist/helpers/create-hash.js +15 -18
  79. package/dist/helpers/create-hash.js.map +7 -0
  80. package/dist/helpers/ensure-protocol.js +9 -10
  81. package/dist/helpers/ensure-protocol.js.map +7 -0
  82. package/dist/helpers/ensure-protocol.test.js +70 -0
  83. package/dist/helpers/ensure-protocol.test.js.map +7 -0
  84. package/dist/helpers/fetch-document.js +24 -32
  85. package/dist/helpers/fetch-document.js.map +7 -0
  86. package/dist/helpers/fetch-document.test.js +63 -0
  87. package/dist/helpers/fetch-document.test.js.map +7 -0
  88. package/dist/helpers/fetch-with-proxy-fallback.js +20 -28
  89. package/dist/helpers/fetch-with-proxy-fallback.js.map +7 -0
  90. package/dist/helpers/find-variables.d.ts +1 -1
  91. package/dist/helpers/find-variables.d.ts.map +1 -1
  92. package/dist/helpers/find-variables.js +6 -8
  93. package/dist/helpers/find-variables.js.map +7 -0
  94. package/dist/helpers/find-variables.test.js +20 -0
  95. package/dist/helpers/find-variables.test.js.map +7 -0
  96. package/dist/helpers/http-methods.d.ts +1 -1
  97. package/dist/helpers/http-methods.d.ts.map +1 -1
  98. package/dist/helpers/http-methods.js +60 -66
  99. package/dist/helpers/http-methods.js.map +7 -0
  100. package/dist/helpers/http-status-codes.js +319 -317
  101. package/dist/helpers/http-status-codes.js.map +7 -0
  102. package/dist/helpers/index.d.ts +27 -27
  103. package/dist/helpers/index.d.ts.map +1 -1
  104. package/dist/helpers/index.js +73 -27
  105. package/dist/helpers/index.js.map +7 -0
  106. package/dist/helpers/is-defined.js +5 -14
  107. package/dist/helpers/is-defined.js.map +7 -0
  108. package/dist/helpers/is-defined.test.js +37 -0
  109. package/dist/helpers/is-defined.test.js.map +7 -0
  110. package/dist/helpers/is-local-url.js +11 -16
  111. package/dist/helpers/is-local-url.js.map +7 -0
  112. package/dist/helpers/is-local-url.test.js +40 -0
  113. package/dist/helpers/is-local-url.test.js.map +7 -0
  114. package/dist/helpers/is-valid-url.js +9 -18
  115. package/dist/helpers/is-valid-url.js.map +7 -0
  116. package/dist/helpers/is-valid-url.test.js +17 -0
  117. package/dist/helpers/is-valid-url.test.js.map +7 -0
  118. package/dist/helpers/iterate-title.js +11 -15
  119. package/dist/helpers/iterate-title.js.map +7 -0
  120. package/dist/helpers/json2xml.js +40 -47
  121. package/dist/helpers/json2xml.js.map +7 -0
  122. package/dist/helpers/json2xml.test.js +19 -0
  123. package/dist/helpers/json2xml.test.js.map +7 -0
  124. package/dist/helpers/local-storage.js +13 -15
  125. package/dist/helpers/local-storage.js.map +7 -0
  126. package/dist/helpers/make-url-absolute.js +11 -17
  127. package/dist/helpers/make-url-absolute.js.map +7 -0
  128. package/dist/helpers/make-url-absolute.test.js +61 -0
  129. package/dist/helpers/make-url-absolute.test.js.map +7 -0
  130. package/dist/helpers/merge-urls.js +50 -75
  131. package/dist/helpers/merge-urls.js.map +7 -0
  132. package/dist/helpers/merge-urls.test.js +339 -0
  133. package/dist/helpers/merge-urls.test.js.map +7 -0
  134. package/dist/helpers/normalize-mime-type-object.js +21 -31
  135. package/dist/helpers/normalize-mime-type-object.js.map +7 -0
  136. package/dist/helpers/normalize-mime-type-object.test.js +53 -0
  137. package/dist/helpers/normalize-mime-type-object.test.js.map +7 -0
  138. package/dist/helpers/normalize-mime-type.js +8 -18
  139. package/dist/helpers/normalize-mime-type.js.map +7 -0
  140. package/dist/helpers/normalize-mime-type.test.js +33 -0
  141. package/dist/helpers/normalize-mime-type.test.js.map +7 -0
  142. package/dist/helpers/object.js +12 -18
  143. package/dist/helpers/object.js.map +7 -0
  144. package/dist/helpers/omit-undefined-values.js +17 -19
  145. package/dist/helpers/omit-undefined-values.js.map +7 -0
  146. package/dist/helpers/omit-undefined-values.test.js +89 -0
  147. package/dist/helpers/omit-undefined-values.test.js.map +7 -0
  148. package/dist/helpers/parse.js +71 -83
  149. package/dist/helpers/parse.js.map +7 -0
  150. package/dist/helpers/parse.test.js +45 -0
  151. package/dist/helpers/parse.test.js.map +7 -0
  152. package/dist/helpers/pretty-print-json.js +31 -35
  153. package/dist/helpers/pretty-print-json.js.map +7 -0
  154. package/dist/helpers/pretty-print-json.test.js +28 -0
  155. package/dist/helpers/pretty-print-json.test.js.map +7 -0
  156. package/dist/helpers/redirect-to-proxy.js +46 -83
  157. package/dist/helpers/redirect-to-proxy.js.map +7 -0
  158. package/dist/helpers/redirect-to-proxy.test.js +54 -0
  159. package/dist/helpers/redirect-to-proxy.test.js.map +7 -0
  160. package/dist/helpers/regex-helpers.js +11 -9
  161. package/dist/helpers/regex-helpers.js.map +7 -0
  162. package/dist/helpers/regex-helpers.test.js +154 -0
  163. package/dist/helpers/regex-helpers.test.js.map +7 -0
  164. package/dist/helpers/replace-variables.js +13 -16
  165. package/dist/helpers/replace-variables.js.map +7 -0
  166. package/dist/helpers/replace-variables.test.js +30 -0
  167. package/dist/helpers/replace-variables.test.js.map +7 -0
  168. package/dist/helpers/schema-model.js +15 -14
  169. package/dist/helpers/schema-model.js.map +7 -0
  170. package/dist/helpers/security/get-schemes.d.ts +1 -1
  171. package/dist/helpers/security/get-schemes.d.ts.map +1 -1
  172. package/dist/helpers/security/get-schemes.js +7 -9
  173. package/dist/helpers/security/get-schemes.js.map +7 -0
  174. package/dist/helpers/security/get-schemes.test.js +71 -0
  175. package/dist/helpers/security/get-schemes.test.js.map +7 -0
  176. package/dist/helpers/security/has-token.js +18 -19
  177. package/dist/helpers/security/has-token.js.map +7 -0
  178. package/dist/helpers/security/has-token.test.js +157 -0
  179. package/dist/helpers/security/has-token.test.js.map +7 -0
  180. package/dist/helpers/security/index.d.ts +2 -2
  181. package/dist/helpers/security/index.d.ts.map +1 -1
  182. package/dist/helpers/security/index.js +7 -2
  183. package/dist/helpers/security/index.js.map +7 -0
  184. package/dist/helpers/should-ignore-entity.d.ts +2 -2
  185. package/dist/helpers/should-ignore-entity.d.ts.map +1 -1
  186. package/dist/helpers/should-ignore-entity.js +5 -6
  187. package/dist/helpers/should-ignore-entity.js.map +7 -0
  188. package/dist/helpers/ssr-state.js +6 -6
  189. package/dist/helpers/ssr-state.js.map +7 -0
  190. package/dist/helpers/string.js +6 -18
  191. package/dist/helpers/string.js.map +7 -0
  192. package/dist/migrations/data-version.js +7 -17
  193. package/dist/migrations/data-version.js.map +7 -0
  194. package/dist/migrations/generate-types.js +56 -0
  195. package/dist/migrations/generate-types.js.map +7 -0
  196. package/dist/migrations/index.d.ts +3 -3
  197. package/dist/migrations/index.d.ts.map +1 -1
  198. package/dist/migrations/index.js +10 -3
  199. package/dist/migrations/index.js.map +7 -0
  200. package/dist/migrations/local-storage.d.ts +1 -1
  201. package/dist/migrations/local-storage.d.ts.map +1 -1
  202. package/dist/migrations/local-storage.js +27 -32
  203. package/dist/migrations/local-storage.js.map +7 -0
  204. package/dist/migrations/migrator.d.ts +1 -1
  205. package/dist/migrations/migrator.d.ts.map +1 -1
  206. package/dist/migrations/migrator.js +51 -58
  207. package/dist/migrations/migrator.js.map +7 -0
  208. package/dist/migrations/semver.js +23 -25
  209. package/dist/migrations/semver.js.map +7 -0
  210. package/dist/migrations/semver.test.js +21 -0
  211. package/dist/migrations/semver.test.js.map +7 -0
  212. package/dist/migrations/v-0.0.0/types.generated.js +1 -0
  213. package/dist/migrations/v-0.0.0/types.generated.js.map +7 -0
  214. package/dist/migrations/v-2.1.0/migration.d.ts +2 -2
  215. package/dist/migrations/v-2.1.0/migration.d.ts.map +1 -1
  216. package/dist/migrations/v-2.1.0/migration.js +256 -273
  217. package/dist/migrations/v-2.1.0/migration.js.map +7 -0
  218. package/dist/migrations/v-2.1.0/types.generated.js +1 -0
  219. package/dist/migrations/v-2.1.0/types.generated.js.map +7 -0
  220. package/dist/migrations/v-2.2.0/migration.d.ts +2 -2
  221. package/dist/migrations/v-2.2.0/migration.d.ts.map +1 -1
  222. package/dist/migrations/v-2.2.0/migration.js +95 -100
  223. package/dist/migrations/v-2.2.0/migration.js.map +7 -0
  224. package/dist/migrations/v-2.2.0/types.generated.js +1 -0
  225. package/dist/migrations/v-2.2.0/types.generated.js.map +7 -0
  226. package/dist/migrations/v-2.3.0/migration.d.ts +2 -2
  227. package/dist/migrations/v-2.3.0/migration.d.ts.map +1 -1
  228. package/dist/migrations/v-2.3.0/migration.js +47 -46
  229. package/dist/migrations/v-2.3.0/migration.js.map +7 -0
  230. package/dist/migrations/v-2.3.0/types.generated.js +1 -0
  231. package/dist/migrations/v-2.3.0/types.generated.js.map +7 -0
  232. package/dist/migrations/v-2.4.0/migration.d.ts +2 -2
  233. package/dist/migrations/v-2.4.0/migration.d.ts.map +1 -1
  234. package/dist/migrations/v-2.4.0/migration.js +24 -26
  235. package/dist/migrations/v-2.4.0/migration.js.map +7 -0
  236. package/dist/migrations/v-2.4.0/migration.test.js +90 -0
  237. package/dist/migrations/v-2.4.0/migration.test.js.map +7 -0
  238. package/dist/migrations/v-2.4.0/types.generated.js +1 -0
  239. package/dist/migrations/v-2.4.0/types.generated.js.map +7 -0
  240. package/dist/migrations/v-2.5.0/migration.d.ts +2 -2
  241. package/dist/migrations/v-2.5.0/migration.d.ts.map +1 -1
  242. package/dist/migrations/v-2.5.0/migration.js +135 -119
  243. package/dist/migrations/v-2.5.0/migration.js.map +7 -0
  244. package/dist/migrations/v-2.5.0/migration.test.js +108 -0
  245. package/dist/migrations/v-2.5.0/migration.test.js.map +7 -0
  246. package/dist/migrations/v-2.5.0/types.generated.d.ts +8 -8
  247. package/dist/migrations/v-2.5.0/types.generated.d.ts.map +1 -1
  248. package/dist/migrations/v-2.5.0/types.generated.js +1 -0
  249. package/dist/migrations/v-2.5.0/types.generated.js.map +7 -0
  250. package/dist/spec-getters/get-example-from-schema.js +206 -268
  251. package/dist/spec-getters/get-example-from-schema.js.map +7 -0
  252. package/dist/spec-getters/get-example-from-schema.test.js +1092 -0
  253. package/dist/spec-getters/get-example-from-schema.test.js.map +7 -0
  254. package/dist/spec-getters/get-parameters-from-operation.js +22 -42
  255. package/dist/spec-getters/get-parameters-from-operation.js.map +7 -0
  256. package/dist/spec-getters/get-parameters-from-operation.test.js +178 -0
  257. package/dist/spec-getters/get-parameters-from-operation.test.js.map +7 -0
  258. package/dist/spec-getters/get-request-body-from-operation.js +110 -162
  259. package/dist/spec-getters/get-request-body-from-operation.js.map +7 -0
  260. package/dist/spec-getters/get-request-body-from-operation.test.js +289 -0
  261. package/dist/spec-getters/get-request-body-from-operation.test.js.map +7 -0
  262. package/dist/spec-getters/get-server-variable-examples.d.ts +1 -1
  263. package/dist/spec-getters/get-server-variable-examples.d.ts.map +1 -1
  264. package/dist/spec-getters/get-server-variable-examples.js +12 -11
  265. package/dist/spec-getters/get-server-variable-examples.js.map +7 -0
  266. package/dist/spec-getters/index.d.ts +4 -4
  267. package/dist/spec-getters/index.d.ts.map +1 -1
  268. package/dist/spec-getters/index.js +11 -4
  269. package/dist/spec-getters/index.js.map +7 -0
  270. package/dist/transforms/import-spec.d.ts +6 -6
  271. package/dist/transforms/import-spec.d.ts.map +1 -1
  272. package/dist/transforms/import-spec.js +313 -425
  273. package/dist/transforms/import-spec.js.map +7 -0
  274. package/dist/transforms/import-spec.test.js +1124 -0
  275. package/dist/transforms/import-spec.test.js.map +7 -0
  276. package/dist/transforms/index.d.ts +1 -1
  277. package/dist/transforms/index.d.ts.map +1 -1
  278. package/dist/transforms/index.js +7 -1
  279. package/dist/transforms/index.js.map +7 -0
  280. package/package.json +9 -10
@@ -1,168 +1,116 @@
1
- import { json2xml } from '../helpers/json2xml.js';
2
- import { normalizeMimeTypeObject } from '../helpers/normalize-mime-type-object.js';
3
- import { prettyPrintJson } from '../helpers/pretty-print-json.js';
4
- import { getExampleFromSchema } from './get-example-from-schema.js';
5
- import { getParametersFromOperation } from './get-parameters-from-operation.js';
6
-
7
- /**
8
- * Transform the object into a nested array of objects
9
- * that represent the key-value pairs of the object.
10
- */
11
- function getParamsFromObject(obj, prefix = '') {
12
- return Object.entries(obj).flatMap(([key, value]) => {
13
- const newKey = prefix ? `${prefix}[${key}]` : key;
14
- if (typeof value === 'object' && value !== null) {
15
- return getParamsFromObject(value, newKey);
16
- }
17
- return [{ name: newKey, value }];
18
- });
1
+ import { json2xml } from "../helpers/json2xml.js";
2
+ import { normalizeMimeTypeObject } from "../helpers/normalize-mime-type-object.js";
3
+ import { prettyPrintJson } from "../helpers/pretty-print-json.js";
4
+ import { getExampleFromSchema } from "./get-example-from-schema.js";
5
+ import { getParametersFromOperation } from "./get-parameters-from-operation.js";
6
+ function getParamsFromObject(obj, prefix = "") {
7
+ return Object.entries(obj).flatMap(([key, value]) => {
8
+ const newKey = prefix ? `${prefix}[${key}]` : key;
9
+ if (typeof value === "object" && value !== null) {
10
+ return getParamsFromObject(value, newKey);
11
+ }
12
+ return [{ name: newKey, value }];
13
+ });
19
14
  }
20
- // Define preferred standard mime types (order indicates preference)
21
15
  const standardMimeTypes = [
22
- 'application/json',
23
- 'application/octet-stream',
24
- 'application/x-www-form-urlencoded',
25
- 'application/xml',
26
- 'multipart/form-data',
27
- 'text/plain',
16
+ "application/json",
17
+ "application/octet-stream",
18
+ "application/x-www-form-urlencoded",
19
+ "application/xml",
20
+ "multipart/form-data",
21
+ "text/plain"
28
22
  ];
29
- /**
30
- * Get the request body from the operation.
31
- */
32
23
  function getRequestBodyFromOperation(operation, selectedExampleKey, omitEmptyAndOptionalProperties) {
33
- const originalContent = operation.information?.requestBody?.content;
34
- const content = normalizeMimeTypeObject(originalContent);
35
- // First try to find a standard mime type
36
- const mimeType = standardMimeTypes.find((currentMimeType) => !!content?.[currentMimeType]) ??
37
- (Object.keys(content ?? {})[0] || 'application/json');
38
- // Handle JSON-like content types (e.g., application/vnd.github+json)
39
- const isJsonLike = mimeType.includes('json') || mimeType.endsWith('+json');
40
- /** Examples */
41
- const examples = content?.[mimeType]?.examples ?? content?.['application/json']?.examples;
42
- // Let’s use the first example
43
- const selectedExample = examples?.[selectedExampleKey ?? Object.keys(examples ?? {})[0] ?? ''];
44
- if (selectedExample) {
45
- return {
46
- mimeType,
47
- text: prettyPrintJson(selectedExample?.value),
48
- };
49
- }
50
- /**
51
- * Body Parameters (Swagger 2.0)
52
- *
53
- * ”The payload that's appended to the HTTP request. Since there can only be one payload, there can only
54
- * be one body parameter. The name of the body parameter has no effect on the parameter itself and is used
55
- * for documentation purposes only. Since Form parameters are also in the payload, body and form
56
- * parameters cannot exist together for the same operation.”
57
- */
58
- const bodyParameters = getParametersFromOperation(operation, 'body', false);
59
- if (bodyParameters.length > 0) {
60
- return {
61
- mimeType: 'application/json',
62
- text: prettyPrintJson(bodyParameters[0]?.value ?? ''),
63
- };
64
- }
65
- /**
66
- * FormData Parameters (Swagger 2.0)
67
- *
68
- * ”Form - Used to describe the payload of an HTTP request when either application/x-www-form-urlencoded,
69
- * multipart/form-data or both are used as the content type of the request (in Swagger's definition, the
70
- * consumes property of an operation). This is the only parameter type that can be used to send files,
71
- * thus supporting the file type. Since form parameters are sent in the payload, they cannot be declared
72
- * together with a body parameter for the same operation. Form parameters have a different format based on
73
- * the content-type used (for further details, consult http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4):
74
- * - application/x-www-form-urlencoded - Similar to the format of Query parameters but as a payload.
75
- * For example, foo=1&bar=swagger - both foo and bar are form parameters. This is normally used for simple
76
- * parameters that are being transferred.
77
- * - multipart/form-data - each parameter takes a section in the payload with an internal header.
78
- * For example, for the header Content-Disposition: form-data; name="submit-name" the name of the parameter is
79
- * submit-name. This type of form parameters is more commonly used for file transfers.”
80
- */
81
- const formDataParameters = getParametersFromOperation(operation, 'formData', false);
82
- if (formDataParameters.length > 0) {
83
- return {
84
- mimeType: 'application/x-www-form-urlencoded',
85
- params: formDataParameters.map((parameter) => ({
86
- name: parameter.name,
87
- /**
88
- * TODO: This value MUST be a string
89
- * Figure out why this is not always a string
90
- *
91
- * JSON.stringify is a TEMPORARY fix
92
- */
93
- value: typeof parameter.value === 'string' ? parameter.value : JSON.stringify(parameter.value),
94
- })),
95
- };
96
- }
97
- // If no mime type is supported, exit early
98
- if (!mimeType) {
99
- return null;
100
- }
101
- // Get the request body object for the mime type
102
- const requestBodyObject = content?.[mimeType];
103
- // Get example from operation
104
- const example = requestBodyObject?.example ? requestBodyObject?.example : undefined;
105
- // Update the JSON handling section
106
- if (isJsonLike) {
107
- const exampleFromSchema = requestBodyObject?.schema
108
- ? getExampleFromSchema(requestBodyObject?.schema, {
109
- mode: 'write',
110
- omitEmptyAndOptionalProperties: omitEmptyAndOptionalProperties ?? false,
111
- })
112
- : null;
113
- const body = example ?? exampleFromSchema;
114
- return {
115
- mimeType,
116
- text: body ? (typeof body === 'string' ? body : JSON.stringify(body, null, 2)) : undefined,
117
- };
118
- }
119
- // XML
120
- if (mimeType === 'application/xml') {
121
- const exampleFromSchema = requestBodyObject?.schema
122
- ? getExampleFromSchema(requestBodyObject?.schema, {
123
- xml: true,
124
- mode: 'write',
125
- })
126
- : null;
127
- return {
128
- mimeType,
129
- text: example ?? json2xml(exampleFromSchema, ' '),
130
- };
131
- }
132
- // Binary data
133
- if (mimeType === 'application/octet-stream') {
134
- return {
135
- mimeType,
136
- text: 'BINARY',
137
- };
138
- }
139
- // Plain text
140
- if (mimeType === 'text/plain') {
141
- const exampleFromSchema = requestBodyObject?.schema
142
- ? getExampleFromSchema(requestBodyObject?.schema, {
143
- xml: true,
144
- mode: 'write',
145
- })
146
- : null;
147
- return {
148
- mimeType,
149
- text: example ?? exampleFromSchema ?? '',
150
- };
151
- }
152
- // URL encoded data
153
- if (mimeType === 'multipart/form-data' || mimeType === 'application/x-www-form-urlencoded') {
154
- const exampleFromSchema = requestBodyObject?.schema
155
- ? getExampleFromSchema(requestBodyObject?.schema, {
156
- xml: true,
157
- mode: 'write',
158
- })
159
- : null;
160
- return {
161
- mimeType,
162
- params: getParamsFromObject(example ?? exampleFromSchema ?? {}),
163
- };
164
- }
24
+ const originalContent = operation.information?.requestBody?.content;
25
+ const content = normalizeMimeTypeObject(originalContent);
26
+ const mimeType = standardMimeTypes.find((currentMimeType) => !!content?.[currentMimeType]) ?? (Object.keys(content ?? {})[0] || "application/json");
27
+ const isJsonLike = mimeType.includes("json") || mimeType.endsWith("+json");
28
+ const examples = content?.[mimeType]?.examples ?? content?.["application/json"]?.examples;
29
+ const selectedExample = examples?.[selectedExampleKey ?? Object.keys(examples ?? {})[0] ?? ""];
30
+ if (selectedExample) {
31
+ return {
32
+ mimeType,
33
+ text: prettyPrintJson(selectedExample?.value)
34
+ };
35
+ }
36
+ const bodyParameters = getParametersFromOperation(operation, "body", false);
37
+ if (bodyParameters.length > 0) {
38
+ return {
39
+ mimeType: "application/json",
40
+ text: prettyPrintJson(bodyParameters[0]?.value ?? "")
41
+ };
42
+ }
43
+ const formDataParameters = getParametersFromOperation(operation, "formData", false);
44
+ if (formDataParameters.length > 0) {
45
+ return {
46
+ mimeType: "application/x-www-form-urlencoded",
47
+ params: formDataParameters.map((parameter) => ({
48
+ name: parameter.name,
49
+ /**
50
+ * TODO: This value MUST be a string
51
+ * Figure out why this is not always a string
52
+ *
53
+ * JSON.stringify is a TEMPORARY fix
54
+ */
55
+ value: typeof parameter.value === "string" ? parameter.value : JSON.stringify(parameter.value)
56
+ }))
57
+ };
58
+ }
59
+ if (!mimeType) {
165
60
  return null;
61
+ }
62
+ const requestBodyObject = content?.[mimeType];
63
+ const example = requestBodyObject?.example ? requestBodyObject?.example : void 0;
64
+ if (isJsonLike) {
65
+ const exampleFromSchema = requestBodyObject?.schema ? getExampleFromSchema(requestBodyObject?.schema, {
66
+ mode: "write",
67
+ omitEmptyAndOptionalProperties: omitEmptyAndOptionalProperties ?? false
68
+ }) : null;
69
+ const body = example ?? exampleFromSchema;
70
+ return {
71
+ mimeType,
72
+ text: body ? typeof body === "string" ? body : JSON.stringify(body, null, 2) : void 0
73
+ };
74
+ }
75
+ if (mimeType === "application/xml") {
76
+ const exampleFromSchema = requestBodyObject?.schema ? getExampleFromSchema(requestBodyObject?.schema, {
77
+ xml: true,
78
+ mode: "write"
79
+ }) : null;
80
+ return {
81
+ mimeType,
82
+ text: example ?? json2xml(exampleFromSchema, " ")
83
+ };
84
+ }
85
+ if (mimeType === "application/octet-stream") {
86
+ return {
87
+ mimeType,
88
+ text: "BINARY"
89
+ };
90
+ }
91
+ if (mimeType === "text/plain") {
92
+ const exampleFromSchema = requestBodyObject?.schema ? getExampleFromSchema(requestBodyObject?.schema, {
93
+ xml: true,
94
+ mode: "write"
95
+ }) : null;
96
+ return {
97
+ mimeType,
98
+ text: example ?? exampleFromSchema ?? ""
99
+ };
100
+ }
101
+ if (mimeType === "multipart/form-data" || mimeType === "application/x-www-form-urlencoded") {
102
+ const exampleFromSchema = requestBodyObject?.schema ? getExampleFromSchema(requestBodyObject?.schema, {
103
+ xml: true,
104
+ mode: "write"
105
+ }) : null;
106
+ return {
107
+ mimeType,
108
+ params: getParamsFromObject(example ?? exampleFromSchema ?? {})
109
+ };
110
+ }
111
+ return null;
166
112
  }
167
-
168
- export { getRequestBodyFromOperation };
113
+ export {
114
+ getRequestBodyFromOperation
115
+ };
116
+ //# sourceMappingURL=get-request-body-from-operation.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/spec-getters/get-request-body-from-operation.ts"],
4
+ "sourcesContent": ["import type { ContentType, TransformedOperation } from '@scalar/types/legacy'\n\nimport { json2xml } from '@/helpers/json2xml'\nimport { normalizeMimeTypeObject } from '@/helpers/normalize-mime-type-object'\nimport { prettyPrintJson } from '@/helpers/pretty-print-json'\nimport { getExampleFromSchema } from './get-example-from-schema'\nimport { getParametersFromOperation } from './get-parameters-from-operation'\n\ntype AnyObject = Record<string, any>\n\n/**\n * Transform the object into a nested array of objects\n * that represent the key-value pairs of the object.\n */\nfunction getParamsFromObject(\n obj: AnyObject,\n prefix = '',\n): {\n name: string\n value: any\n}[] {\n return Object.entries(obj).flatMap(([key, value]) => {\n const newKey = prefix ? `${prefix}[${key}]` : key\n\n if (typeof value === 'object' && value !== null) {\n return getParamsFromObject(value, newKey)\n }\n\n return [{ name: newKey, value }]\n })\n}\n// Define preferred standard mime types (order indicates preference)\nconst standardMimeTypes: ContentType[] = [\n 'application/json',\n 'application/octet-stream',\n 'application/x-www-form-urlencoded',\n 'application/xml',\n 'multipart/form-data',\n 'text/plain',\n]\n\n/**\n * Get the request body from the operation.\n */\nexport function getRequestBodyFromOperation(\n operation: Omit<TransformedOperation, 'httpVerb'>,\n selectedExampleKey?: string | number,\n omitEmptyAndOptionalProperties?: boolean,\n): {\n mimeType: ContentType\n text?: string | undefined\n params?: {\n name: string\n value?: string\n }[]\n} | null {\n const originalContent = operation.information?.requestBody?.content\n const content = normalizeMimeTypeObject(originalContent)\n\n // First try to find a standard mime type\n const mimeType =\n standardMimeTypes.find((currentMimeType) => !!content?.[currentMimeType]) ??\n ((Object.keys(content ?? {})[0] || 'application/json') as ContentType)\n\n // Handle JSON-like content types (e.g., application/vnd.github+json)\n const isJsonLike = mimeType.includes('json') || mimeType.endsWith('+json')\n\n /** Examples */\n const examples = content?.[mimeType]?.examples ?? content?.['application/json']?.examples\n\n // Let\u2019s use the first example\n const selectedExample = examples?.[selectedExampleKey ?? Object.keys(examples ?? {})[0] ?? '']\n\n if (selectedExample) {\n return {\n mimeType,\n text: prettyPrintJson(selectedExample?.value),\n }\n }\n\n /**\n * Body Parameters (Swagger 2.0)\n *\n * \u201DThe payload that's appended to the HTTP request. Since there can only be one payload, there can only\n * be one body parameter. The name of the body parameter has no effect on the parameter itself and is used\n * for documentation purposes only. Since Form parameters are also in the payload, body and form\n * parameters cannot exist together for the same operation.\u201D\n */\n const bodyParameters = getParametersFromOperation(operation, 'body', false)\n\n if (bodyParameters.length > 0) {\n return {\n mimeType: 'application/json',\n text: prettyPrintJson(bodyParameters[0]?.value ?? ''),\n }\n }\n\n /**\n * FormData Parameters (Swagger 2.0)\n *\n * \u201DForm - Used to describe the payload of an HTTP request when either application/x-www-form-urlencoded,\n * multipart/form-data or both are used as the content type of the request (in Swagger's definition, the\n * consumes property of an operation). This is the only parameter type that can be used to send files,\n * thus supporting the file type. Since form parameters are sent in the payload, they cannot be declared\n * together with a body parameter for the same operation. Form parameters have a different format based on\n * the content-type used (for further details, consult http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4):\n * - application/x-www-form-urlencoded - Similar to the format of Query parameters but as a payload.\n * For example, foo=1&bar=swagger - both foo and bar are form parameters. This is normally used for simple\n * parameters that are being transferred.\n * - multipart/form-data - each parameter takes a section in the payload with an internal header.\n * For example, for the header Content-Disposition: form-data; name=\"submit-name\" the name of the parameter is\n * submit-name. This type of form parameters is more commonly used for file transfers.\u201D\n */\n\n const formDataParameters = getParametersFromOperation(operation, 'formData', false)\n\n if (formDataParameters.length > 0) {\n return {\n mimeType: 'application/x-www-form-urlencoded',\n params: formDataParameters.map((parameter) => ({\n name: parameter.name,\n /**\n * TODO: This value MUST be a string\n * Figure out why this is not always a string\n *\n * JSON.stringify is a TEMPORARY fix\n */\n value: typeof parameter.value === 'string' ? parameter.value : JSON.stringify(parameter.value),\n })),\n }\n }\n\n // If no mime type is supported, exit early\n if (!mimeType) {\n return null\n }\n\n // Get the request body object for the mime type\n const requestBodyObject = content?.[mimeType]\n\n // Get example from operation\n const example = requestBodyObject?.example ? requestBodyObject?.example : undefined\n\n // Update the JSON handling section\n if (isJsonLike) {\n const exampleFromSchema = requestBodyObject?.schema\n ? getExampleFromSchema(requestBodyObject?.schema, {\n mode: 'write',\n omitEmptyAndOptionalProperties: omitEmptyAndOptionalProperties ?? false,\n })\n : null\n\n const body = example ?? exampleFromSchema\n\n return {\n mimeType,\n text: body ? (typeof body === 'string' ? body : JSON.stringify(body, null, 2)) : undefined,\n }\n }\n\n // XML\n if (mimeType === 'application/xml') {\n const exampleFromSchema = requestBodyObject?.schema\n ? getExampleFromSchema(requestBodyObject?.schema, {\n xml: true,\n mode: 'write',\n })\n : null\n\n return {\n mimeType,\n text: example ?? json2xml(exampleFromSchema, ' '),\n }\n }\n\n // Binary data\n if (mimeType === 'application/octet-stream') {\n return {\n mimeType,\n text: 'BINARY',\n }\n }\n\n // Plain text\n if (mimeType === 'text/plain') {\n const exampleFromSchema = requestBodyObject?.schema\n ? getExampleFromSchema(requestBodyObject?.schema, {\n xml: true,\n mode: 'write',\n })\n : null\n\n return {\n mimeType,\n text: example ?? exampleFromSchema ?? '',\n }\n }\n\n // URL encoded data\n if (mimeType === 'multipart/form-data' || mimeType === 'application/x-www-form-urlencoded') {\n const exampleFromSchema = requestBodyObject?.schema\n ? getExampleFromSchema(requestBodyObject?.schema, {\n xml: true,\n mode: 'write',\n })\n : null\n\n return {\n mimeType,\n params: getParamsFromObject(example ?? exampleFromSchema ?? {}),\n }\n }\n\n return null\n}\n"],
5
+ "mappings": "AAEA,SAAS,gBAAgB;AACzB,SAAS,+BAA+B;AACxC,SAAS,uBAAuB;AAChC,SAAS,4BAA4B;AACrC,SAAS,kCAAkC;AAQ3C,SAAS,oBACP,KACA,SAAS,IAIP;AACF,SAAO,OAAO,QAAQ,GAAG,EAAE,QAAQ,CAAC,CAAC,KAAK,KAAK,MAAM;AACnD,UAAM,SAAS,SAAS,GAAG,MAAM,IAAI,GAAG,MAAM;AAE9C,QAAI,OAAO,UAAU,YAAY,UAAU,MAAM;AAC/C,aAAO,oBAAoB,OAAO,MAAM;AAAA,IAC1C;AAEA,WAAO,CAAC,EAAE,MAAM,QAAQ,MAAM,CAAC;AAAA,EACjC,CAAC;AACH;AAEA,MAAM,oBAAmC;AAAA,EACvC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAKO,SAAS,4BACd,WACA,oBACA,gCAQO;AACP,QAAM,kBAAkB,UAAU,aAAa,aAAa;AAC5D,QAAM,UAAU,wBAAwB,eAAe;AAGvD,QAAM,WACJ,kBAAkB,KAAK,CAAC,oBAAoB,CAAC,CAAC,UAAU,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A,MACL;AAAA,MACA,MAAM,OAAQ,OAAO,SAAS,WAAW,OAAO,KAAK,UAAU,MAAM,MAAM,CAAC,IAAK;AAAA,IACnF;AAAA,EACF;AAGA,MAAI,aAAa,mBAAmB;AAClC,UAAM,oBAAoB,mBAAmB,SACzC,qBAAqB,mBAAmB,QAAQ;AAAA,MAC9C,KAAK;AAAA,MACL,MAAM;AAAA,IACR,CAAC,IACD;AAEJ,WAAO;AAAA,MACL;AAAA,MACA,MAAM,WAAW,SAAS,mBAAmB,IAAI;AAAA,IACnD;AAAA,EACF;AAGA,MAAI,aAAa,4BAA4B;AAC3C,WAAO;AAAA,MACL;AAAA,MACA,MAAM;AAAA,IACR;AAAA,EACF;AAGA,MAAI,aAAa,cAAc;AAC7B,UAAM,oBAAoB,mBAAmB,SACzC,qBAAqB,mBAAmB,QAAQ;AAAA,MAC9C,KAAK;AAAA,MACL,MAAM;AAAA,IACR,CAAC,IACD;AAEJ,WAAO;AAAA,MACL;AAAA,MACA,MAAM,WAAW,qBAAqB;AAAA,IACxC;AAAA,EACF;AAGA,MAAI,aAAa,yBAAyB,aAAa,qCAAqC;AAC1F,UAAM,oBAAoB,mBAAmB,SACzC,qBAAqB,mBAAmB,QAAQ;AAAA,MAC9C,KAAK;AAAA,MACL,MAAM;AAAA,IACR,CAAC,IACD;AAEJ,WAAO;AAAA,MACL;AAAA,MACA,QAAQ,oBAAoB,WAAW,qBAAqB,CAAC,CAAC;AAAA,IAChE;AAAA,EACF;AAEA,SAAO;AACT;",
6
+ "names": []
7
+ }
@@ -0,0 +1,289 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { getRequestBodyFromOperation } from "./get-request-body-from-operation.js";
3
+ describe("getRequestBodyFromOperation", () => {
4
+ it("creates a JSON body from a requestBody schema", () => {
5
+ const body = getRequestBodyFromOperation({
6
+ path: "/foobar",
7
+ information: {
8
+ requestBody: {
9
+ content: {
10
+ "application/json": {
11
+ schema: {
12
+ type: "object",
13
+ properties: {
14
+ id: {
15
+ type: "integer",
16
+ example: 1
17
+ }
18
+ }
19
+ }
20
+ }
21
+ }
22
+ }
23
+ }
24
+ });
25
+ const expectedResult = {
26
+ id: 1
27
+ };
28
+ expect(body).toMatchObject({
29
+ mimeType: "application/json",
30
+ text: JSON.stringify(expectedResult, null, 2)
31
+ });
32
+ });
33
+ it("ignores charset in mimetypes", () => {
34
+ const body = getRequestBodyFromOperation({
35
+ path: "/foobar",
36
+ information: {
37
+ requestBody: {
38
+ content: {
39
+ "application/json; charset=utf-8": {
40
+ schema: {
41
+ type: "object",
42
+ properties: {
43
+ id: {
44
+ type: "integer",
45
+ example: 1
46
+ }
47
+ }
48
+ }
49
+ }
50
+ }
51
+ }
52
+ }
53
+ });
54
+ const expectedResult = {
55
+ id: 1
56
+ };
57
+ expect(body).toMatchObject({
58
+ mimeType: "application/json",
59
+ text: JSON.stringify(expectedResult, null, 2)
60
+ });
61
+ });
62
+ it("creates a JSON body from body parameters", () => {
63
+ const body = getRequestBodyFromOperation({
64
+ path: "/foobar",
65
+ information: {
66
+ parameters: [
67
+ {
68
+ name: "body",
69
+ in: "body",
70
+ schema: {
71
+ type: "object",
72
+ properties: {
73
+ id: {
74
+ name: "id",
75
+ type: "integer",
76
+ example: 1
77
+ }
78
+ }
79
+ }
80
+ }
81
+ ]
82
+ }
83
+ });
84
+ const expectedResult = {
85
+ id: 1
86
+ };
87
+ expect(body).toMatchObject({
88
+ mimeType: "application/json",
89
+ text: JSON.stringify(expectedResult, null, 2)
90
+ });
91
+ });
92
+ it("uses example", () => {
93
+ const body = getRequestBodyFromOperation({
94
+ path: "/foobar",
95
+ information: {
96
+ requestBody: {
97
+ description: "Sample request body",
98
+ required: false,
99
+ content: {
100
+ "application/json": {
101
+ example: {
102
+ someObject: {
103
+ someAttribute: "attribute1"
104
+ }
105
+ },
106
+ schema: {
107
+ $ref: "#/components/schemas/PutDocumentRequest"
108
+ }
109
+ }
110
+ }
111
+ }
112
+ }
113
+ });
114
+ const expectedResult = {
115
+ someObject: {
116
+ someAttribute: "attribute1"
117
+ }
118
+ };
119
+ expect(body).toMatchObject({
120
+ mimeType: "application/json",
121
+ text: JSON.stringify(expectedResult, null, 2)
122
+ });
123
+ });
124
+ it("uses examples", () => {
125
+ const body = getRequestBodyFromOperation({
126
+ path: "/foobar",
127
+ information: {
128
+ requestBody: {
129
+ description: "Sample request body",
130
+ required: false,
131
+ content: {
132
+ "application/json": {
133
+ examples: {
134
+ "request-example-1": {
135
+ summary: "an example of a request",
136
+ description: "a longer string than the summary",
137
+ value: {
138
+ someObject: {
139
+ someAttribute: "attribute1"
140
+ }
141
+ }
142
+ }
143
+ },
144
+ schema: {
145
+ $ref: "#/components/schemas/PutDocumentRequest"
146
+ }
147
+ }
148
+ }
149
+ }
150
+ }
151
+ });
152
+ const expectedResult = {
153
+ someObject: {
154
+ someAttribute: "attribute1"
155
+ }
156
+ };
157
+ expect(body).toMatchObject({
158
+ mimeType: "application/json",
159
+ text: JSON.stringify(expectedResult, null, 2)
160
+ });
161
+ });
162
+ it("creates key-value pair examples from object schema", () => {
163
+ const body = getRequestBodyFromOperation({
164
+ path: "/foobar",
165
+ information: {
166
+ requestBody: {
167
+ description: "Sample request body",
168
+ content: {
169
+ "application/json": {
170
+ schema: {
171
+ type: "object",
172
+ required: [
173
+ "recordString",
174
+ "recordInteger",
175
+ "recordArray",
176
+ "recordBoolean",
177
+ "recordNullable",
178
+ "recordObject",
179
+ "recordWithoutAdditionalProperties"
180
+ ],
181
+ properties: {
182
+ recordString: {
183
+ type: "string"
184
+ },
185
+ recordInteger: {
186
+ type: "integer"
187
+ },
188
+ recordArray: {
189
+ type: "array"
190
+ },
191
+ recordBoolean: {
192
+ type: "boolean"
193
+ },
194
+ recordNullable: {
195
+ nullable: "true"
196
+ },
197
+ recordObject: {
198
+ type: "object"
199
+ }
200
+ }
201
+ }
202
+ }
203
+ }
204
+ }
205
+ }
206
+ });
207
+ const expectedResult = {
208
+ recordString: "",
209
+ recordInteger: 1,
210
+ recordArray: [],
211
+ recordBoolean: true,
212
+ recordNullable: null,
213
+ recordObject: {}
214
+ };
215
+ expect(body).toMatchObject({
216
+ mimeType: "application/json",
217
+ text: JSON.stringify(expectedResult, null, 2)
218
+ });
219
+ });
220
+ it("adds parameters from a requestBody schema", () => {
221
+ const body = getRequestBodyFromOperation({
222
+ path: "/foobar",
223
+ information: {
224
+ requestBody: {
225
+ content: {
226
+ "application/x-www-form-urlencoded": {
227
+ schema: {
228
+ type: "object",
229
+ properties: {
230
+ id: {
231
+ type: "integer",
232
+ example: 1
233
+ },
234
+ name: {
235
+ type: "string",
236
+ example: "foobar"
237
+ }
238
+ }
239
+ }
240
+ }
241
+ }
242
+ }
243
+ }
244
+ });
245
+ expect(body).toMatchObject({
246
+ mimeType: "application/x-www-form-urlencoded",
247
+ params: [
248
+ {
249
+ name: "id",
250
+ value: 1
251
+ },
252
+ {
253
+ name: "name",
254
+ value: "foobar"
255
+ }
256
+ ]
257
+ });
258
+ });
259
+ it("handles vendor-specific MIME types", () => {
260
+ const body = getRequestBodyFromOperation({
261
+ path: "/foobar",
262
+ information: {
263
+ requestBody: {
264
+ content: {
265
+ "application/vnd.github+json": {
266
+ schema: {
267
+ type: "object",
268
+ properties: {
269
+ id: {
270
+ type: "integer",
271
+ example: 1
272
+ }
273
+ }
274
+ }
275
+ }
276
+ }
277
+ }
278
+ }
279
+ });
280
+ const expectedResult = {
281
+ id: 1
282
+ };
283
+ expect(body).toMatchObject({
284
+ mimeType: "application/vnd.github+json",
285
+ text: JSON.stringify(expectedResult, null, 2)
286
+ });
287
+ });
288
+ });
289
+ //# sourceMappingURL=get-request-body-from-operation.test.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/spec-getters/get-request-body-from-operation.test.ts"],
4
+ "sourcesContent": ["import { describe, expect, it } from 'vitest'\n\nimport { getRequestBodyFromOperation } from './get-request-body-from-operation'\n\ndescribe('getRequestBodyFromOperation', () => {\n it('creates a JSON body from a requestBody schema', () => {\n const body = getRequestBodyFromOperation({\n path: '/foobar',\n information: {\n requestBody: {\n content: {\n 'application/json': {\n schema: {\n type: 'object',\n properties: {\n id: {\n type: 'integer',\n example: 1,\n },\n },\n },\n },\n },\n },\n },\n })\n\n const expectedResult = {\n id: 1,\n }\n\n expect(body).toMatchObject({\n mimeType: 'application/json',\n text: JSON.stringify(expectedResult, null, 2),\n })\n })\n\n it('ignores charset in mimetypes', () => {\n const body = getRequestBodyFromOperation({\n path: '/foobar',\n information: {\n requestBody: {\n content: {\n 'application/json; charset=utf-8': {\n schema: {\n type: 'object',\n properties: {\n id: {\n type: 'integer',\n example: 1,\n },\n },\n },\n },\n },\n },\n },\n })\n\n const expectedResult = {\n id: 1,\n }\n\n expect(body).toMatchObject({\n mimeType: 'application/json',\n text: JSON.stringify(expectedResult, null, 2),\n })\n })\n\n it('creates a JSON body from body parameters', () => {\n const body = getRequestBodyFromOperation({\n path: '/foobar',\n information: {\n parameters: [\n {\n name: 'body',\n in: 'body',\n schema: {\n type: 'object',\n properties: {\n id: {\n name: 'id',\n type: 'integer',\n example: 1,\n },\n },\n },\n },\n ],\n },\n })\n\n const expectedResult = {\n id: 1,\n }\n\n expect(body).toMatchObject({\n mimeType: 'application/json',\n text: JSON.stringify(expectedResult, null, 2),\n })\n })\n\n it('uses example', () => {\n const body = getRequestBodyFromOperation({\n path: '/foobar',\n information: {\n requestBody: {\n description: 'Sample request body',\n required: false,\n content: {\n 'application/json': {\n example: {\n someObject: {\n someAttribute: 'attribute1',\n },\n },\n schema: {\n $ref: '#/components/schemas/PutDocumentRequest',\n },\n },\n },\n },\n },\n })\n\n const expectedResult = {\n someObject: {\n someAttribute: 'attribute1',\n },\n }\n\n expect(body).toMatchObject({\n mimeType: 'application/json',\n text: JSON.stringify(expectedResult, null, 2),\n })\n })\n\n it('uses examples', () => {\n const body = getRequestBodyFromOperation({\n path: '/foobar',\n information: {\n requestBody: {\n description: 'Sample request body',\n required: false,\n content: {\n 'application/json': {\n examples: {\n 'request-example-1': {\n summary: 'an example of a request',\n description: 'a longer string than the summary',\n value: {\n someObject: {\n someAttribute: 'attribute1',\n },\n },\n },\n },\n schema: {\n $ref: '#/components/schemas/PutDocumentRequest',\n },\n },\n },\n },\n },\n })\n\n const expectedResult = {\n someObject: {\n someAttribute: 'attribute1',\n },\n }\n\n expect(body).toMatchObject({\n mimeType: 'application/json',\n text: JSON.stringify(expectedResult, null, 2),\n })\n })\n\n it('creates key-value pair examples from object schema', () => {\n const body = getRequestBodyFromOperation({\n path: '/foobar',\n information: {\n requestBody: {\n description: 'Sample request body',\n content: {\n 'application/json': {\n schema: {\n type: 'object',\n required: [\n 'recordString',\n 'recordInteger',\n 'recordArray',\n 'recordBoolean',\n 'recordNullable',\n 'recordObject',\n 'recordWithoutAdditionalProperties',\n ],\n properties: {\n recordString: {\n type: 'string',\n },\n recordInteger: {\n type: 'integer',\n },\n recordArray: {\n type: 'array',\n },\n recordBoolean: {\n type: 'boolean',\n },\n recordNullable: {\n nullable: 'true',\n },\n recordObject: {\n type: 'object',\n },\n },\n },\n },\n },\n },\n },\n })\n\n const expectedResult = {\n recordString: '',\n recordInteger: 1,\n recordArray: [],\n recordBoolean: true,\n recordNullable: null,\n recordObject: {},\n }\n\n expect(body).toMatchObject({\n mimeType: 'application/json',\n text: JSON.stringify(expectedResult, null, 2),\n })\n })\n\n it('adds parameters from a requestBody schema', () => {\n const body = getRequestBodyFromOperation({\n path: '/foobar',\n information: {\n requestBody: {\n content: {\n 'application/x-www-form-urlencoded': {\n schema: {\n type: 'object',\n properties: {\n id: {\n type: 'integer',\n example: 1,\n },\n name: {\n type: 'string',\n example: 'foobar',\n },\n },\n },\n },\n },\n },\n },\n })\n\n expect(body).toMatchObject({\n mimeType: 'application/x-www-form-urlencoded',\n params: [\n {\n name: 'id',\n value: 1,\n },\n {\n name: 'name',\n value: 'foobar',\n },\n ],\n })\n })\n\n it('handles vendor-specific MIME types', () => {\n const body = getRequestBodyFromOperation({\n path: '/foobar',\n information: {\n requestBody: {\n content: {\n 'application/vnd.github+json': {\n schema: {\n type: 'object',\n properties: {\n id: {\n type: 'integer',\n example: 1,\n },\n },\n },\n },\n },\n },\n },\n })\n\n const expectedResult = {\n id: 1,\n }\n\n expect(body).toMatchObject({\n mimeType: 'application/vnd.github+json',\n text: JSON.stringify(expectedResult, null, 2),\n })\n })\n})\n"],
5
+ "mappings": "AAAA,SAAS,UAAU,QAAQ,UAAU;AAErC,SAAS,mCAAmC;AAE5C,SAAS,+BAA+B,MAAM;AAC5C,KAAG,iDAAiD,MAAM;AACxD,UAAM,OAAO,4BAA4B;AAAA,MACvC,MAAM;AAAA,MACN,aAAa;AAAA,QACX,aAAa;AAAA,UACX,SAAS;AAAA,YACP,oBAAoB;AAAA,cAClB,QAAQ;AAAA,gBACN,MAAM;AAAA,gBACN,YAAY;AAAA,kBACV,IAAI;AAAA,oBACF,MAAM;AAAA,oBACN,SAAS;AAAA,kBACX;AAAA,gBACF;AAAA,cACF;AAAA,YACF;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF,CAAC;AAED,UAAM,iBAAiB;AAAA,MACrB,IAAI;AAAA,IACN;AAEA,WAAO,IAAI,EAAE,cAAc;AAAA,MACzB,UAAU;AAAA,MACV,MAAM,KAAK,UAAU,gBAAgB,MAAM,CAAC;AAAA,IAC9C,CAAC;AAAA,EACH,CAAC;AAED,KAAG,gCAAgC,MAAM;AACvC,UAAM,OAAO,4BAA4B;AAAA,MACvC,MAAM;AAAA,MACN,aAAa;AAAA,QACX,aAAa;AAAA,UACX,SAAS;AAAA,YACP,mCAAmC;AAAA,cACjC,QAAQ;AAAA,gBACN,MAAM;AAAA,gBACN,YAAY;AAAA,kBACV,IAAI;AAAA,oBACF,MAAM;AAAA,oBACN,SAAS;AAAA,kBACX;AAAA,gBACF;AAAA,cACF;AAAA,YACF;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF,CAAC;AAED,UAAM,iBAAiB;AAAA,MACrB,IAAI;AA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;AAAA,gBACN,MAAM;AAAA,gBACN,YAAY;AAAA,kBACV,IAAI;AAAA,oBACF,MAAM;AAAA,oBACN,SAAS;AAAA,kBACX;AAAA,gBACF;AAAA,cACF;AAAA,YACF;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF,CAAC;AAED,UAAM,iBAAiB;AAAA,MACrB,IAAI;AAAA,IACN;AAEA,WAAO,IAAI,EAAE,cAAc;AAAA,MACzB,UAAU;AAAA,MACV,MAAM,KAAK,UAAU,gBAAgB,MAAM,CAAC;AAAA,IAC9C,CAAC;AAAA,EACH,CAAC;AACH,CAAC;",
6
+ "names": []
7
+ }
@@ -1,4 +1,4 @@
1
- import type { Server } from '../entities/spec/server.ts';
1
+ import type { Server } from '../entities/spec/server.js';
2
2
  /** Retrieve example values for server variables */
3
3
  export declare const getServerVariableExamples: (server: Server) => Record<string, string[]>;
4
4
  //# sourceMappingURL=get-server-variable-examples.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"get-server-variable-examples.d.ts","sourceRoot":"","sources":["../../src/spec-getters/get-server-variable-examples.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AAEvD,mDAAmD;AACnD,eAAO,MAAM,yBAAyB,WAAY,MAAM,6BAUvD,CAAA"}
1
+ {"version":3,"file":"get-server-variable-examples.d.ts","sourceRoot":"","sources":["../../src/spec-getters/get-server-variable-examples.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAA;AAEpD,mDAAmD;AACnD,eAAO,MAAM,yBAAyB,WAAY,MAAM,6BAUvD,CAAA"}