@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,363 +1,348 @@
1
- import { schemaModel } from '../../helpers/schema-model.js';
2
- import { getServerVariableExamples } from '../../spec-getters/get-server-variable-examples.js';
3
- import { nanoidSchema } from '@scalar/types/utils';
4
- import { z } from 'zod';
5
- import { isDefined } from '../../helpers/is-defined.js';
6
- import { getObjectKeys } from '../../helpers/object.js';
7
- import { getRequestBodyFromOperation } from '../../spec-getters/get-request-body-from-operation.js';
8
-
9
- // ---------------------------------------------------------------------------
10
- // Example Parameters
11
- /**
12
- * TODO: Deprecate this.
13
- *
14
- * The request schema should be stored in the request and any
15
- * parameters should be validated against that
16
- */
17
- const requestExampleParametersSchema = z
18
- .object({
19
- key: z.string().default(''),
20
- value: z.coerce.string().default(''),
21
- enabled: z.boolean().default(true),
22
- file: z.any().optional(),
23
- description: z.string().optional(),
24
- required: z.boolean().optional(),
25
- enum: z.array(z.string()).optional(),
26
- examples: z.array(z.any()).optional(),
27
- type: z
28
- .union([
29
- // 'string'
30
- z.string(),
31
- // ['string', 'null']
32
- z.array(z.string()),
33
- ])
34
- .optional(),
35
- format: z.string().optional(),
36
- minimum: z.number().optional(),
37
- maximum: z.number().optional(),
38
- default: z.any().optional(),
39
- nullable: z.boolean().optional(),
40
- })
41
- // set nullable: to true if type is ['string', 'null']
42
- .transform((_data) => {
43
- const data = { ..._data };
44
- // type: ['string', 'null'] -> nullable: true
45
- if (Array.isArray(data.type) && data.type.includes('null')) {
46
- data.nullable = true;
47
- }
48
- // Hey, if it's just one value and 'null', we can make it a string and ditch the 'null'.
49
- if (Array.isArray(data.type) && data.type.length === 2 && data.type.includes('null')) {
50
- data.type = data.type.find((item) => item !== 'null');
51
- }
52
- return data;
1
+ import { schemaModel } from "../../helpers/schema-model.js";
2
+ import { getServerVariableExamples } from "../../spec-getters/get-server-variable-examples.js";
3
+ import { keysOf } from "@scalar/object-utils/arrays";
4
+ import { nanoidSchema } from "@scalar/types/utils";
5
+ import { z } from "zod";
6
+ import { isDefined } from "../../helpers/is-defined.js";
7
+ import { getObjectKeys } from "../../helpers/object.js";
8
+ import { getRequestBodyFromOperation } from "../../spec-getters/get-request-body-from-operation.js";
9
+ const requestExampleParametersSchema = z.object({
10
+ key: z.string().default(""),
11
+ value: z.coerce.string().default(""),
12
+ enabled: z.boolean().default(true),
13
+ file: z.any().optional(),
14
+ description: z.string().optional(),
15
+ required: z.boolean().optional(),
16
+ enum: z.array(z.string()).optional(),
17
+ examples: z.array(z.any()).optional(),
18
+ type: z.union([
19
+ // 'string'
20
+ z.string(),
21
+ // ['string', 'null']
22
+ z.array(z.string())
23
+ ]).optional(),
24
+ format: z.string().optional(),
25
+ minimum: z.number().optional(),
26
+ maximum: z.number().optional(),
27
+ default: z.any().optional(),
28
+ nullable: z.boolean().optional()
29
+ }).transform((_data) => {
30
+ const data = { ..._data };
31
+ if (Array.isArray(data.type) && data.type.includes("null")) {
32
+ data.nullable = true;
33
+ }
34
+ if (Array.isArray(data.type) && data.type.length === 2 && data.type.includes("null")) {
35
+ data.type = data.type.find((item) => item !== "null");
36
+ }
37
+ return data;
53
38
  });
54
- const xScalarFileValueSchema = z
55
- .object({
56
- url: z.string(),
57
- base64: z.string().optional(),
58
- })
59
- .nullable();
60
- /**
61
- * Schema for the OAS serialization of request example parameters
62
- *
63
- * File values can be optionally fetched on import OR inserted as a base64 encoded string
64
- */
65
- z.union([
66
- z.object({
67
- type: z.literal('string'),
68
- value: z.string(),
69
- }),
70
- z.object({
71
- type: z.literal('file'),
72
- file: xScalarFileValueSchema,
73
- }),
39
+ const parameterArrayToObject = (params) => params.reduce((map, param) => {
40
+ map[param.key] = param.value;
41
+ return map;
42
+ }, {});
43
+ const xScalarFileValueSchema = z.object({
44
+ url: z.string(),
45
+ base64: z.string().optional()
46
+ }).nullable();
47
+ const xScalarFormDataValue = z.union([
48
+ z.object({
49
+ type: z.literal("string"),
50
+ value: z.string()
51
+ }),
52
+ z.object({
53
+ type: z.literal("file"),
54
+ file: xScalarFileValueSchema
55
+ })
74
56
  ]);
75
- // ---------------------------------------------------------------------------
76
- // Example Body
77
- /**
78
- * Possible encodings for example request bodies when using text formats
79
- *
80
- * TODO: This list may not be comprehensive enough
81
- */
82
- const exampleRequestBodyEncoding = ['json', 'text', 'html', 'javascript', 'xml', 'yaml', 'edn'];
57
+ const exampleRequestBodyEncoding = ["json", "text", "html", "javascript", "xml", "yaml", "edn"];
83
58
  const exampleBodyMime = [
84
- 'application/json',
85
- 'text/plain',
86
- 'text/html',
87
- 'application/javascript',
88
- 'application/xml',
89
- 'application/yaml',
90
- 'application/edn',
91
- 'application/octet-stream',
92
- 'application/x-www-form-urlencoded',
93
- 'multipart/form-data',
94
- /** Used for direct files */
95
- 'binary',
59
+ "application/json",
60
+ "text/plain",
61
+ "text/html",
62
+ "application/javascript",
63
+ "application/xml",
64
+ "application/yaml",
65
+ "application/edn",
66
+ "application/octet-stream",
67
+ "application/x-www-form-urlencoded",
68
+ "multipart/form-data",
69
+ /** Used for direct files */
70
+ "binary"
96
71
  ];
97
- /**
98
- * TODO: Migrate away from this layout to the format used in the extension
99
- *
100
- * If a user changes the encoding of the body we expect the content to change as well
101
- */
72
+ const contentMapping = {
73
+ json: "application/json",
74
+ text: "text/plain",
75
+ html: "text/html",
76
+ javascript: "application/javascript",
77
+ xml: "application/xml",
78
+ yaml: "application/yaml",
79
+ edn: "application/edn"
80
+ };
102
81
  const exampleRequestBodySchema = z.object({
103
- raw: z
104
- .object({
105
- encoding: z.enum(exampleRequestBodyEncoding),
106
- value: z.string().default(''),
107
- mimeType: z.string().optional(),
108
- })
109
- .optional(),
110
- formData: z
111
- .object({
112
- encoding: z.union([z.literal('form-data'), z.literal('urlencoded')]).default('form-data'),
113
- value: requestExampleParametersSchema.array().default([]),
114
- })
115
- .optional(),
116
- binary: z.instanceof(Blob).optional(),
117
- activeBody: z.union([z.literal('raw'), z.literal('formData'), z.literal('binary')]).default('raw'),
82
+ raw: z.object({
83
+ encoding: z.enum(exampleRequestBodyEncoding),
84
+ value: z.string().default(""),
85
+ mimeType: z.string().optional()
86
+ }).optional(),
87
+ formData: z.object({
88
+ encoding: z.union([z.literal("form-data"), z.literal("urlencoded")]).default("form-data"),
89
+ value: requestExampleParametersSchema.array().default([])
90
+ }).optional(),
91
+ binary: z.instanceof(Blob).optional(),
92
+ activeBody: z.union([z.literal("raw"), z.literal("formData"), z.literal("binary")]).default("raw")
118
93
  });
119
- /** Schema for the OAS serialization of request example bodies */
120
94
  const xScalarExampleBodySchema = z.object({
121
- encoding: z.enum(exampleBodyMime),
122
- /**
123
- * Body content as an object with a separately specified encoding or a simple pre-encoded string value
124
- *
125
- * Ideally we would convert any objects into the proper encoding on import
126
- */
127
- content: z.union([z.record(z.string(), z.any()), z.string()]),
128
- /** When the encoding is `binary` this will be used to link to the file */
129
- file: xScalarFileValueSchema.optional(),
95
+ encoding: z.enum(exampleBodyMime),
96
+ /**
97
+ * Body content as an object with a separately specified encoding or a simple pre-encoded string value
98
+ *
99
+ * Ideally we would convert any objects into the proper encoding on import
100
+ */
101
+ content: z.union([z.record(z.string(), z.any()), z.string()]),
102
+ /** When the encoding is `binary` this will be used to link to the file */
103
+ file: xScalarFileValueSchema.optional()
130
104
  });
131
- // ---------------------------------------------------------------------------
132
- // Example Schema
133
105
  const requestExampleSchema = z.object({
134
- uid: nanoidSchema.brand(),
135
- type: z.literal('requestExample').optional().default('requestExample'),
136
- requestUid: z.string().brand().optional(),
137
- name: z.string().optional().default('Name'),
138
- body: exampleRequestBodySchema.optional().default({}),
139
- parameters: z
140
- .object({
141
- path: requestExampleParametersSchema.array().default([]),
142
- query: requestExampleParametersSchema.array().default([]),
143
- headers: requestExampleParametersSchema.array().default([{ key: 'Accept', value: '*/*', enabled: true }]),
144
- cookies: requestExampleParametersSchema.array().default([]),
145
- })
146
- .optional()
147
- .default({}),
148
- /** TODO: Should this be deprecated? */
149
- serverVariables: z.record(z.string(), z.array(z.string())).optional(),
106
+ uid: nanoidSchema.brand(),
107
+ type: z.literal("requestExample").optional().default("requestExample"),
108
+ requestUid: z.string().brand().optional(),
109
+ name: z.string().optional().default("Name"),
110
+ body: exampleRequestBodySchema.optional().default({}),
111
+ parameters: z.object({
112
+ path: requestExampleParametersSchema.array().default([]),
113
+ query: requestExampleParametersSchema.array().default([]),
114
+ headers: requestExampleParametersSchema.array().default([{ key: "Accept", value: "*/*", enabled: true }]),
115
+ cookies: requestExampleParametersSchema.array().default([])
116
+ }).optional().default({}),
117
+ /** TODO: Should this be deprecated? */
118
+ serverVariables: z.record(z.string(), z.array(z.string())).optional()
150
119
  });
151
- /** For OAS serialization we just store the simple key/value pairs */
152
120
  const xScalarExampleParameterSchema = z.record(z.string(), z.string()).optional();
153
- /** Schema for the OAS serialization of request examples */
154
121
  const xScalarExampleSchema = z.object({
155
- /** TODO: Should this be required? */
156
- name: z.string().optional(),
157
- body: xScalarExampleBodySchema.optional(),
158
- parameters: z.object({
159
- path: xScalarExampleParameterSchema,
160
- query: xScalarExampleParameterSchema,
161
- headers: xScalarExampleParameterSchema,
162
- cookies: xScalarExampleParameterSchema,
163
- }),
122
+ /** TODO: Should this be required? */
123
+ name: z.string().optional(),
124
+ body: xScalarExampleBodySchema.optional(),
125
+ parameters: z.object({
126
+ path: xScalarExampleParameterSchema,
127
+ query: xScalarExampleParameterSchema,
128
+ headers: xScalarExampleParameterSchema,
129
+ cookies: xScalarExampleParameterSchema
130
+ })
164
131
  });
165
- // ---------------------------------------------------------------------------
166
- // Example Helpers
167
- /** Create new instance parameter from a request parameter */
132
+ function convertExampleToXScalar(example) {
133
+ const active = example.body?.activeBody;
134
+ const xScalarBody = {
135
+ encoding: "text/plain",
136
+ content: ""
137
+ };
138
+ if (example.body?.activeBody === "binary") {
139
+ xScalarBody.encoding = "binary";
140
+ xScalarBody.file = null;
141
+ }
142
+ if (active === "formData" && example.body?.[active]) {
143
+ const body = example.body[active];
144
+ xScalarBody.encoding = body.encoding === "form-data" ? "multipart/form-data" : "application/x-www-form-urlencoded";
145
+ xScalarBody.content = body.value.reduce((map, param) => {
146
+ map[param.key] = param.file ? {
147
+ type: "file",
148
+ file: null
149
+ } : {
150
+ type: "string",
151
+ value: param.value
152
+ };
153
+ return map;
154
+ }, {});
155
+ }
156
+ if (example.body?.activeBody === "raw") {
157
+ xScalarBody.encoding = contentMapping[example.body.raw?.encoding ?? "text"] ?? "text/plain";
158
+ xScalarBody.content = example.body.raw?.value ?? "";
159
+ }
160
+ const parameters = {};
161
+ keysOf(example.parameters ?? {}).forEach((key) => {
162
+ if (example.parameters?.[key].length) {
163
+ parameters[key] = parameterArrayToObject(example.parameters[key]);
164
+ }
165
+ });
166
+ return xScalarExampleSchema.parse({
167
+ /** Only add the body if we have content or the body should be a file */
168
+ body: xScalarBody.content || xScalarBody.encoding === "binary" ? xScalarBody : void 0,
169
+ parameters
170
+ });
171
+ }
168
172
  function createParamInstance(param) {
169
- const schema = param.schema;
170
- const firstExample = (() => {
171
- if (param.examples && !Array.isArray(param.examples) && getObjectKeys(param.examples).length > 0) {
172
- const exampleValues = Object.entries(param.examples).map(([_, example]) => {
173
- // returns the external value if it exists
174
- if (example.externalValue) {
175
- return example.externalValue;
176
- }
177
- // returns the value if it exists and is defined
178
- // e.g. { examples: { foo: { value: 'bar' } } } would return ['bar']
179
- return example.value;
180
- });
181
- // returns the first example as selected value along other examples
182
- return { value: exampleValues[0], examples: exampleValues };
183
- }
184
- // param example e.g. { example: 'foo' }
185
- if (isDefined(param.example)) {
186
- return { value: param.example };
187
- }
188
- // param examples e.g. { examples: ['foo', 'bar'] }
189
- if (Array.isArray(param.examples) && param.examples.length > 0) {
190
- return { value: param.examples[0] };
173
+ const schema = param.schema;
174
+ const firstExample = (() => {
175
+ if (param.examples && !Array.isArray(param.examples) && getObjectKeys(param.examples).length > 0) {
176
+ const exampleValues = Object.entries(param.examples).map(([_, example2]) => {
177
+ if (example2.externalValue) {
178
+ return example2.externalValue;
191
179
  }
192
- // schema example e.g. { example: 'foo' } while being discouraged
193
- // see https://spec.openapis.org/oas/v3.1.1.html#fixed-fields-20
194
- if (isDefined(schema?.example)) {
195
- return { value: schema.example };
196
- }
197
- // schema examples e.g. { examples: ['foo', 'bar'] }
198
- if (Array.isArray(schema?.examples) && schema.examples.length > 0) {
199
- // For boolean type, default to false when using schema examples
200
- if (schema?.type === 'boolean') {
201
- return { value: schema.default ?? false };
202
- }
203
- return { value: schema.examples[0] };
204
- }
205
- // content examples e.g. { content: { 'application/json': { examples: { foo: { value: 'bar' } } } } }
206
- if (param.content) {
207
- const firstContentType = getObjectKeys(param.content)[0];
208
- if (firstContentType) {
209
- const content = param.content[firstContentType];
210
- if (content?.examples) {
211
- const firstExampleKey = Object.keys(content.examples)[0];
212
- if (firstExampleKey) {
213
- const example = content.examples[firstExampleKey];
214
- if (isDefined(example?.value)) {
215
- return { value: example.value };
216
- }
217
- }
218
- }
219
- // content example e.g. { example: 'foo' }
220
- if (isDefined(content?.example)) {
221
- return { value: content.example };
222
- }
180
+ return example2.value;
181
+ });
182
+ return { value: exampleValues[0], examples: exampleValues };
183
+ }
184
+ if (isDefined(param.example)) {
185
+ return { value: param.example };
186
+ }
187
+ if (Array.isArray(param.examples) && param.examples.length > 0) {
188
+ return { value: param.examples[0] };
189
+ }
190
+ if (isDefined(schema?.example)) {
191
+ return { value: schema.example };
192
+ }
193
+ if (Array.isArray(schema?.examples) && schema.examples.length > 0) {
194
+ if (schema?.type === "boolean") {
195
+ return { value: schema.default ?? false };
196
+ }
197
+ return { value: schema.examples[0] };
198
+ }
199
+ if (param.content) {
200
+ const firstContentType = getObjectKeys(param.content)[0];
201
+ if (firstContentType) {
202
+ const content = param.content[firstContentType];
203
+ if (content?.examples) {
204
+ const firstExampleKey = Object.keys(content.examples)[0];
205
+ if (firstExampleKey) {
206
+ const example2 = content.examples[firstExampleKey];
207
+ if (isDefined(example2?.value)) {
208
+ return { value: example2.value };
223
209
  }
210
+ }
224
211
  }
225
- return null;
226
- })();
227
- /**
228
- * TODO:
229
- * - Need better value defaulting here
230
- * - Need to handle non-string parameters much better
231
- * - Need to handle unions/array values for schema
232
- */
233
- const value = String(firstExample?.value ?? schema?.default ?? '');
234
- // Handle non-string enums and enums within items for array types
235
- const parseEnum = (() => {
236
- if (schema?.enum && schema?.type !== 'string') {
237
- return schema.enum?.map(String);
238
- }
239
- if (schema?.items?.enum && schema?.type === 'array') {
240
- return schema.items.enum.map(String);
212
+ if (isDefined(content?.example)) {
213
+ return { value: content.example };
241
214
  }
242
- return schema?.enum;
243
- })();
244
- // Handle parameter examples
245
- const examples = firstExample?.examples ||
246
- (schema?.examples && schema?.type !== 'string' ? schema.examples?.map(String) : schema?.examples);
247
- // safe parse the example
248
- const example = schemaModel({
249
- ...schema,
250
- key: param.name,
251
- value,
252
- description: param.description,
253
- required: param.required,
254
- /** Initialized all required properties to enabled */
255
- enabled: !!param.required,
256
- enum: parseEnum,
257
- examples,
258
- }, requestExampleParametersSchema, false);
259
- if (!example) {
260
- console.warn(`Example at ${param.name} is invalid.`);
261
- return requestExampleParametersSchema.parse({});
215
+ }
262
216
  }
263
- return example;
217
+ return null;
218
+ })();
219
+ const value = String(firstExample?.value ?? schema?.default ?? "");
220
+ const parseEnum = (() => {
221
+ if (schema?.enum && schema?.type !== "string") {
222
+ return schema.enum?.map(String);
223
+ }
224
+ if (schema?.items?.enum && schema?.type === "array") {
225
+ return schema.items.enum.map(String);
226
+ }
227
+ return schema?.enum;
228
+ })();
229
+ const examples = firstExample?.examples || (schema?.examples && schema?.type !== "string" ? schema.examples?.map(String) : schema?.examples);
230
+ const example = schemaModel(
231
+ {
232
+ ...schema,
233
+ key: param.name,
234
+ value,
235
+ description: param.description,
236
+ required: param.required,
237
+ /** Initialized all required properties to enabled */
238
+ enabled: !!param.required,
239
+ enum: parseEnum,
240
+ examples
241
+ },
242
+ requestExampleParametersSchema,
243
+ false
244
+ );
245
+ if (!example) {
246
+ console.warn(`Example at ${param.name} is invalid.`);
247
+ return requestExampleParametersSchema.parse({});
248
+ }
249
+ return example;
264
250
  }
265
- /**
266
- * Create new request example from a request
267
- * Iterates the name of the example if provided
268
- */
269
251
  function createExampleFromRequest(request, name, server) {
270
- // ---------------------------------------------------------------------------
271
- // Populate all parameters with an example value
272
- const parameters = {
273
- path: [],
274
- query: [],
275
- cookie: [],
276
- // deprecated TODO: add zod transform to remove
277
- header: [],
278
- headers: [{ key: 'Accept', value: '*/*', enabled: true }],
279
- };
280
- // Populated the separated params
281
- request.parameters?.forEach((p) => parameters[p.in].push(createParamInstance(p)));
282
- // TODO: add zod transform to remove header and only support headers
283
- if (parameters.header.length > 0) {
284
- parameters.headers = parameters.header;
285
- parameters.header = [];
252
+ const parameters = {
253
+ path: [],
254
+ query: [],
255
+ cookie: [],
256
+ // deprecated TODO: add zod transform to remove
257
+ header: [],
258
+ headers: [{ key: "Accept", value: "*/*", enabled: true }]
259
+ };
260
+ request.parameters?.forEach((p) => parameters[p.in].push(createParamInstance(p)));
261
+ if (parameters.header.length > 0) {
262
+ parameters.headers = parameters.header;
263
+ parameters.header = [];
264
+ }
265
+ const contentTypeHeader = parameters.headers.find((h) => h.key.toLowerCase() === "content-type");
266
+ const body = {
267
+ activeBody: "raw"
268
+ };
269
+ if (request.requestBody || contentTypeHeader?.value) {
270
+ const requestBody = getRequestBodyFromOperation({
271
+ path: request.path,
272
+ information: {
273
+ requestBody: request.requestBody
274
+ }
275
+ });
276
+ const contentType = request.requestBody ? requestBody?.mimeType : contentTypeHeader?.value;
277
+ if (contentType?.includes("/json") || contentType?.endsWith("+json")) {
278
+ body.activeBody = "raw";
279
+ body.raw = {
280
+ encoding: "json",
281
+ mimeType: contentType,
282
+ value: requestBody?.text ?? JSON.stringify({})
283
+ };
286
284
  }
287
- // Get content type header
288
- const contentTypeHeader = parameters.headers.find((h) => h.key.toLowerCase() === 'content-type');
289
- // ---------------------------------------------------------------------------
290
- // Handle request body defaulting for various content type encodings
291
- const body = {
292
- activeBody: 'raw',
293
- };
294
- // If we have a request body or a content type header
295
- if (request.requestBody || contentTypeHeader?.value) {
296
- const requestBody = getRequestBodyFromOperation({
297
- path: request.path,
298
- information: {
299
- requestBody: request.requestBody,
300
- },
301
- });
302
- const contentType = request.requestBody ? requestBody?.mimeType : contentTypeHeader?.value;
303
- // Handle JSON and JSON-like mimetypes
304
- if (contentType?.includes('/json') || contentType?.endsWith('+json')) {
305
- body.activeBody = 'raw';
306
- body.raw = {
307
- encoding: 'json',
308
- mimeType: contentType,
309
- value: requestBody?.text ?? JSON.stringify({}),
310
- };
311
- }
312
- if (contentType === 'application/xml') {
313
- body.activeBody = 'raw';
314
- body.raw = {
315
- encoding: 'xml',
316
- value: requestBody?.text ?? '',
317
- };
318
- }
319
- /**
320
- * TODO: Are we loading example files from somewhere based on the spec?
321
- * How are we handling the body values
322
- */
323
- if (contentType === 'application/octet-stream') {
324
- body.activeBody = 'binary';
325
- body.binary = undefined;
326
- }
327
- if (contentType === 'application/x-www-form-urlencoded' || contentType === 'multipart/form-data') {
328
- body.activeBody = 'formData';
329
- body.formData = {
330
- encoding: contentType === 'application/x-www-form-urlencoded' ? 'urlencoded' : 'form-data',
331
- value: (requestBody?.params || []).map((param) => ({
332
- key: param.name,
333
- value: param.value || '',
334
- enabled: true,
335
- })),
336
- };
337
- }
338
- // Add the content-type header if it doesn't exist
339
- if (requestBody?.mimeType && !contentTypeHeader) {
340
- parameters.headers.push({
341
- key: 'Content-Type',
342
- value: requestBody.mimeType,
343
- enabled: true,
344
- });
345
- }
285
+ if (contentType === "application/xml") {
286
+ body.activeBody = "raw";
287
+ body.raw = {
288
+ encoding: "xml",
289
+ value: requestBody?.text ?? ""
290
+ };
291
+ }
292
+ if (contentType === "application/octet-stream") {
293
+ body.activeBody = "binary";
294
+ body.binary = void 0;
295
+ }
296
+ if (contentType === "application/x-www-form-urlencoded" || contentType === "multipart/form-data") {
297
+ body.activeBody = "formData";
298
+ body.formData = {
299
+ encoding: contentType === "application/x-www-form-urlencoded" ? "urlencoded" : "form-data",
300
+ value: (requestBody?.params || []).map((param) => ({
301
+ key: param.name,
302
+ value: param.value || "",
303
+ enabled: true
304
+ }))
305
+ };
346
306
  }
347
- const serverVariables = server ? getServerVariableExamples(server) : {};
348
- // safe parse the example
349
- const example = schemaModel({
350
- requestUid: request.uid,
351
- parameters,
352
- name,
353
- body,
354
- serverVariables,
355
- }, requestExampleSchema, false);
356
- if (!example) {
357
- console.warn(`Example at ${request.uid} is invalid.`);
358
- return requestExampleSchema.parse({});
307
+ if (requestBody?.mimeType && !contentTypeHeader) {
308
+ parameters.headers.push({
309
+ key: "Content-Type",
310
+ value: requestBody.mimeType,
311
+ enabled: true
312
+ });
359
313
  }
360
- return example;
314
+ }
315
+ const serverVariables = server ? getServerVariableExamples(server) : {};
316
+ const example = schemaModel(
317
+ {
318
+ requestUid: request.uid,
319
+ parameters,
320
+ name,
321
+ body,
322
+ serverVariables
323
+ },
324
+ requestExampleSchema,
325
+ false
326
+ );
327
+ if (!example) {
328
+ console.warn(`Example at ${request.uid} is invalid.`);
329
+ return requestExampleSchema.parse({});
330
+ }
331
+ return example;
361
332
  }
362
-
363
- export { createExampleFromRequest, createParamInstance, exampleBodyMime, exampleRequestBodyEncoding, exampleRequestBodySchema, requestExampleParametersSchema, requestExampleSchema, xScalarExampleBodySchema, xScalarExampleSchema, xScalarFileValueSchema };
333
+ export {
334
+ convertExampleToXScalar,
335
+ createExampleFromRequest,
336
+ createParamInstance,
337
+ exampleBodyMime,
338
+ exampleRequestBodyEncoding,
339
+ exampleRequestBodySchema,
340
+ parameterArrayToObject,
341
+ requestExampleParametersSchema,
342
+ requestExampleSchema,
343
+ xScalarExampleBodySchema,
344
+ xScalarExampleSchema,
345
+ xScalarFileValueSchema,
346
+ xScalarFormDataValue
347
+ };
348
+ //# sourceMappingURL=request-examples.js.map