@scalar/oas-utils 0.2.140 → 0.2.142

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 +18 -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 +13 -1
  279. package/dist/transforms/index.js.map +7 -0
  280. package/package.json +10 -11
@@ -1,92 +1,55 @@
1
- import { isLocalUrl } from './is-local-url.js';
2
- import { REGEX } from './regex-helpers.js';
3
-
4
- /**
5
- * Redirects the request to a proxy server with a given URL. But not for:
6
- *
7
- * - Relative URLs
8
- * - URLs that seem to point to a local IP (except the proxy is on the same domain)
9
- * - URLs that don’t look like a domain
10
- **/
1
+ import { isLocalUrl } from "./is-local-url.js";
2
+ import { REGEX } from "./regex-helpers.js";
11
3
  function redirectToProxy(proxyUrl, url) {
12
- try {
13
- if (!shouldUseProxy(proxyUrl, url)) {
14
- console.log('should not use proxy', proxyUrl, url);
15
- return url ?? '';
16
- }
17
- // Create new URL object from url
18
- const newUrl = new URL(url);
19
- // Add temporary domain for relative proxy URLs
20
- //
21
- // Q: Why isn’t proxyUrl type guarded?
22
- // A: Type guarding works for one parameter only (as of now).
23
- //
24
- // Q: Why do we need to add http://localhost to relative proxy URLs?
25
- // A: Because the `new URL()` would otherwise fail.
26
- //
27
- const temporaryProxyUrl = isRelativePath(proxyUrl) ? `http://localhost${proxyUrl}` : proxyUrl;
28
- // Rewrite the URL with the proxy
29
- newUrl.href = temporaryProxyUrl;
30
- // Add the original URL as a query parameter
31
- newUrl.searchParams.append('scalar_url', url);
32
- // Remove the temporary domain if we added it, but only from the start of the URL
33
- const result = isRelativePath(proxyUrl)
34
- ? newUrl.toString().replace(/^http:\/\/localhost/, '')
35
- : newUrl.toString();
36
- return result;
37
- }
38
- catch {
39
- return url ?? '';
4
+ try {
5
+ if (!shouldUseProxy(proxyUrl, url)) {
6
+ console.log("should not use proxy", proxyUrl, url);
7
+ return url ?? "";
40
8
  }
9
+ const newUrl = new URL(url);
10
+ const temporaryProxyUrl = isRelativePath(proxyUrl) ? `http://localhost${proxyUrl}` : proxyUrl;
11
+ newUrl.href = temporaryProxyUrl;
12
+ newUrl.searchParams.append("scalar_url", url);
13
+ const result = isRelativePath(proxyUrl) ? newUrl.toString().replace(/^http:\/\/localhost/, "") : newUrl.toString();
14
+ return result;
15
+ } catch {
16
+ return url ?? "";
17
+ }
41
18
  }
42
- /**
43
- * Check if the URL is relative or if it's a domain without protocol
44
- **/
45
19
  const isRelativePath = (url) => {
46
- // Allow http:// https:// and other protocols such as file://
47
- if (REGEX.PROTOCOL.test(url)) {
48
- return false;
49
- }
50
- // Check if it looks like a domain (contains dots and no spaces)
51
- // This catches cases like "galaxy.scalar.com/planets"
52
- if (/^[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+(\/|$)/.test(url)) {
53
- return false;
54
- }
55
- return true;
20
+ if (REGEX.PROTOCOL.test(url)) {
21
+ return false;
22
+ }
23
+ if (/^[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+(\/|$)/.test(url)) {
24
+ return false;
25
+ }
26
+ return true;
56
27
  };
57
- /**
58
- * Returns false for requests to localhost, relative URLs, if no proxy is defined …
59
- **/
60
28
  function shouldUseProxy(proxyUrl, url) {
61
- try {
62
- // We don’t have a proxy URL or the URL
63
- if (!proxyUrl || !url) {
64
- return false;
65
- }
66
- // ❌ Request to relative URLs (won’t be blocked by CORS anyway)
67
- if (isRelativePath(url)) {
68
- return false;
69
- }
70
- // ✅ Proxy URL is on the same domain (e.g. /proxy)
71
- // It’s more likely (not guaranteed, though) that the proxy has access to local domains.
72
- if (isRelativePath(proxyUrl)) {
73
- return true;
74
- }
75
- // ✅ Proxy URL is local
76
- if (isLocalUrl(proxyUrl)) {
77
- return true;
78
- }
79
- // ❌ Requests to localhost
80
- // We won’t reach them from a (likely remote) proxy.
81
- if (isLocalUrl(url)) {
82
- return false;
83
- }
84
- // ✅ Seems fine (e.g. remote proxy + remote URL)
85
- return true;
29
+ try {
30
+ if (!proxyUrl || !url) {
31
+ return false;
32
+ }
33
+ if (isRelativePath(url)) {
34
+ return false;
86
35
  }
87
- catch {
88
- return false;
36
+ if (isRelativePath(proxyUrl)) {
37
+ return true;
89
38
  }
39
+ if (isLocalUrl(proxyUrl)) {
40
+ return true;
41
+ }
42
+ if (isLocalUrl(url)) {
43
+ return false;
44
+ }
45
+ return true;
46
+ } catch {
47
+ return false;
48
+ }
90
49
  }
91
-
92
- export { isRelativePath, redirectToProxy, shouldUseProxy };
50
+ export {
51
+ isRelativePath,
52
+ redirectToProxy,
53
+ shouldUseProxy
54
+ };
55
+ //# sourceMappingURL=redirect-to-proxy.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/helpers/redirect-to-proxy.ts"],
4
+ "sourcesContent": ["import { isLocalUrl } from './is-local-url'\nimport { REGEX } from './regex-helpers'\n\n/**\n * Redirects the request to a proxy server with a given URL. But not for:\n *\n * - Relative URLs\n * - URLs that seem to point to a local IP (except the proxy is on the same domain)\n * - URLs that don\u2019t look like a domain\n **/\nexport function redirectToProxy(proxyUrl?: string, url?: string): string {\n try {\n if (!shouldUseProxy(proxyUrl, url)) {\n console.log('should not use proxy', proxyUrl, url)\n return url ?? ''\n }\n\n // Create new URL object from url\n const newUrl = new URL(url)\n\n // Add temporary domain for relative proxy URLs\n //\n // Q: Why isn\u2019t proxyUrl type guarded?\n // A: Type guarding works for one parameter only (as of now).\n //\n // Q: Why do we need to add http://localhost to relative proxy URLs?\n // A: Because the `new URL()` would otherwise fail.\n //\n const temporaryProxyUrl = isRelativePath(proxyUrl as string) ? `http://localhost${proxyUrl}` : (proxyUrl as string)\n\n // Rewrite the URL with the proxy\n newUrl.href = temporaryProxyUrl\n\n // Add the original URL as a query parameter\n newUrl.searchParams.append('scalar_url', url)\n\n // Remove the temporary domain if we added it, but only from the start of the URL\n const result = isRelativePath(proxyUrl as string)\n ? newUrl.toString().replace(/^http:\\/\\/localhost/, '')\n : newUrl.toString()\n\n return result\n } catch {\n return url ?? ''\n }\n}\n\n/**\n * Check if the URL is relative or if it's a domain without protocol\n **/\nexport const isRelativePath = (url: string) => {\n // Allow http:// https:// and other protocols such as file://\n if (REGEX.PROTOCOL.test(url)) {\n return false\n }\n\n // Check if it looks like a domain (contains dots and no spaces)\n // This catches cases like \"galaxy.scalar.com/planets\"\n if (/^[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+(\\/|$)/.test(url)) {\n return false\n }\n\n return true\n}\n\n/**\n * Returns false for requests to localhost, relative URLs, if no proxy is defined \u2026\n **/\nexport function shouldUseProxy(proxyUrl?: string, url?: string): url is string {\n try {\n // \u274C We don\u2019t have a proxy URL or the URL\n if (!proxyUrl || !url) {\n return false\n }\n\n // \u274C Request to relative URLs (won\u2019t be blocked by CORS anyway)\n if (isRelativePath(url)) {\n return false\n }\n\n // \u2705 Proxy URL is on the same domain (e.g. /proxy)\n // It\u2019s more likely (not guaranteed, though) that the proxy has access to local domains.\n if (isRelativePath(proxyUrl)) {\n return true\n }\n\n // \u2705 Proxy URL is local\n if (isLocalUrl(proxyUrl)) {\n return true\n }\n\n // \u274C Requests to localhost\n // We won\u2019t reach them from a (likely remote) proxy.\n if (isLocalUrl(url)) {\n return false\n }\n\n // \u2705 Seems fine (e.g. remote proxy + remote URL)\n return true\n } catch {\n return false\n }\n}\n"],
5
+ "mappings": "AAAA,SAAS,kBAAkB;AAC3B,SAAS,aAAa;AASf,SAAS,gBAAgB,UAAmB,KAAsB;AACvE,MAAI;AACF,QAAI,CAAC,eAAe,UAAU,GAAG,GAAG;AAClC,cAAQ,IAAI,wBAAwB,UAAU,GAAG;AACjD,aAAO,OAAO;AAAA,IAChB;AAGA,UAAM,SAAS,IAAI,IAAI,GAAG;AAU1B,UAAM,oBAAoB,eAAe,QAAkB,IAAI,mBAAmB,QAAQ,KAAM;AAGhG,WAAO,OAAO;AAGd,WAAO,aAAa,OAAO,cAAc,GAAG;AAG5C,UAAM,SAAS,eAAe,QAAkB,IAC5C,OAAO,SAAS,EAAE,QAAQ,uBAAuB,EAAE,IACnD,OAAO,SAAS;AAEpB,WAAO;AAAA,EACT,QAAQ;AACN,WAAO,OAAO;AAAA,EAChB;AACF;AAKO,MAAM,iBAAiB,CAAC,QAAgB;AAE7C,MAAI,MAAM,SAAS,KAAK,GAAG,GAAG;AAC5B,WAAO;AAAA,EACT;AAIA,MAAI,uCAAuC,KAAK,GAAG,GAAG;AACpD,WAAO;AAAA,EACT;AAEA,SAAO;AACT;AAKO,SAAS,eAAe,UAAmB,KAA6B;AAC7E,MAAI;AAEF,QAAI,CAAC,YAAY,CAAC,KAAK;AACrB,aAAO;AAAA,IACT;AAGA,QAAI,eAAe,GAAG,GAAG;AACvB,aAAO;AAAA,IACT;AAIA,QAAI,eAAe,QAAQ,GAAG;AAC5B,aAAO;AAAA,IACT;AAGA,QAAI,WAAW,QAAQ,GAAG;AACxB,aAAO;AAAA,IACT;AAIA,QAAI,WAAW,GAAG,GAAG;AACnB,aAAO;AAAA,IACT;AAGA,WAAO;AAAA,EACT,QAAQ;AACN,WAAO;AAAA,EACT;AACF;",
6
+ "names": []
7
+ }
@@ -0,0 +1,54 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { isRelativePath, redirectToProxy } from "./redirect-to-proxy.js";
3
+ describe("redirectToProxy", () => {
4
+ it("rewrites URLs", () => {
5
+ expect(redirectToProxy("https://proxy.scalar.com", "https://example.com")).toBe(
6
+ "https://proxy.scalar.com/?scalar_url=https%3A%2F%2Fexample.com"
7
+ );
8
+ });
9
+ it("keeps query parameters", () => {
10
+ expect(redirectToProxy("https://proxy.scalar.com?foo=bar", "https://example.com")).toBe(
11
+ "https://proxy.scalar.com/?foo=bar&scalar_url=https%3A%2F%2Fexample.com"
12
+ );
13
+ });
14
+ it("uses the proxy when the proxyUrl is relative", () => {
15
+ expect(redirectToProxy("/proxy", "http://localhost:3000/api")).toBe(
16
+ "/proxy?scalar_url=http%3A%2F%2Flocalhost%3A3000%2Fapi"
17
+ );
18
+ });
19
+ it("skips the proxy if no proxy url is passed", () => {
20
+ expect(redirectToProxy("", "https://example.com")).toBe("https://example.com");
21
+ });
22
+ it("skips the proxy for relative URLs starting with a slash", () => {
23
+ expect(redirectToProxy("https://proxy.scalar.com", "/api")).toBe("/api");
24
+ });
25
+ it("skips the proxy for relative URLs not starting with a slash", () => {
26
+ expect(redirectToProxy("https://proxy.scalar.com", "api")).toBe("api");
27
+ });
28
+ it("skips the proxy for relative URLs not starting with a slash, but containing a dot", () => {
29
+ expect(redirectToProxy("https://proxy.scalar.com", "openapi.json")).toBe("openapi.json");
30
+ });
31
+ it("uses the proxy when it\u2019s local", () => {
32
+ expect(redirectToProxy("http://localhost:3000/proxy", "http://localhost:3000/api")).toBe(
33
+ "http://localhost:3000/proxy?scalar_url=http%3A%2F%2Flocalhost%3A3000%2Fapi"
34
+ );
35
+ });
36
+ });
37
+ describe("isRelativePath", () => {
38
+ it("returns true for relative paths starting with a slash", () => {
39
+ expect(isRelativePath("/api")).toBe(true);
40
+ });
41
+ it("returns true for relative paths without a slash", () => {
42
+ expect(isRelativePath("api")).toBe(true);
43
+ });
44
+ it("returns false for absolute URLs with http", () => {
45
+ expect(isRelativePath("http://example.com")).toBe(false);
46
+ });
47
+ it("returns false for absolute URLs with https", () => {
48
+ expect(isRelativePath("https://example.com")).toBe(false);
49
+ });
50
+ it("returns false for domain-like URLs without protocol", () => {
51
+ expect(isRelativePath("example.com/api")).toBe(false);
52
+ });
53
+ });
54
+ //# sourceMappingURL=redirect-to-proxy.test.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/helpers/redirect-to-proxy.test.ts"],
4
+ "sourcesContent": ["import { describe, expect, it } from 'vitest'\n\nimport { isRelativePath, redirectToProxy } from './redirect-to-proxy'\n\ndescribe('redirectToProxy', () => {\n it('rewrites URLs', () => {\n expect(redirectToProxy('https://proxy.scalar.com', 'https://example.com')).toBe(\n 'https://proxy.scalar.com/?scalar_url=https%3A%2F%2Fexample.com',\n )\n })\n\n it('keeps query parameters', () => {\n expect(redirectToProxy('https://proxy.scalar.com?foo=bar', 'https://example.com')).toBe(\n 'https://proxy.scalar.com/?foo=bar&scalar_url=https%3A%2F%2Fexample.com',\n )\n })\n\n it('uses the proxy when the proxyUrl is relative', () => {\n expect(redirectToProxy('/proxy', 'http://localhost:3000/api')).toBe(\n '/proxy?scalar_url=http%3A%2F%2Flocalhost%3A3000%2Fapi',\n )\n })\n\n it('skips the proxy if no proxy url is passed', () => {\n expect(redirectToProxy('', 'https://example.com')).toBe('https://example.com')\n })\n\n it('skips the proxy for relative URLs starting with a slash', () => {\n expect(redirectToProxy('https://proxy.scalar.com', '/api')).toBe('/api')\n })\n\n it('skips the proxy for relative URLs not starting with a slash', () => {\n expect(redirectToProxy('https://proxy.scalar.com', 'api')).toBe('api')\n })\n\n it('skips the proxy for relative URLs not starting with a slash, but containing a dot', () => {\n expect(redirectToProxy('https://proxy.scalar.com', 'openapi.json')).toBe('openapi.json')\n })\n\n it('uses the proxy when it\u2019s local', () => {\n expect(redirectToProxy('http://localhost:3000/proxy', 'http://localhost:3000/api')).toBe(\n 'http://localhost:3000/proxy?scalar_url=http%3A%2F%2Flocalhost%3A3000%2Fapi',\n )\n })\n})\n\ndescribe('isRelativePath', () => {\n it('returns true for relative paths starting with a slash', () => {\n expect(isRelativePath('/api')).toBe(true)\n })\n\n it('returns true for relative paths without a slash', () => {\n expect(isRelativePath('api')).toBe(true)\n })\n\n it('returns false for absolute URLs with http', () => {\n expect(isRelativePath('http://example.com')).toBe(false)\n })\n\n it('returns false for absolute URLs with https', () => {\n expect(isRelativePath('https://example.com')).toBe(false)\n })\n\n it('returns false for domain-like URLs without protocol', () => {\n expect(isRelativePath('example.com/api')).toBe(false)\n })\n})\n"],
5
+ "mappings": "AAAA,SAAS,UAAU,QAAQ,UAAU;AAErC,SAAS,gBAAgB,uBAAuB;AAEhD,SAAS,mBAAmB,MAAM;AAChC,KAAG,iBAAiB,MAAM;AACxB,WAAO,gBAAgB,4BAA4B,qBAAqB,CAAC,EAAE;AAAA,MACzE;AAAA,IACF;AAAA,EACF,CAAC;AAED,KAAG,0BAA0B,MAAM;AACjC,WAAO,gBAAgB,oCAAoC,qBAAqB,CAAC,EAAE;AAAA,MACjF;AAAA,IACF;AAAA,EACF,CAAC;AAED,KAAG,gDAAgD,MAAM;AACvD,WAAO,gBAAgB,UAAU,2BAA2B,CAAC,EAAE;AAAA,MAC7D;AAAA,IACF;AAAA,EACF,CAAC;AAED,KAAG,6CAA6C,MAAM;AACpD,WAAO,gBAAgB,IAAI,qBAAqB,CAAC,EAAE,KAAK,qBAAqB;AAAA,EAC/E,CAAC;AAED,KAAG,2DAA2D,MAAM;AAClE,WAAO,gBAAgB,4BAA4B,MAAM,CAAC,EAAE,KAAK,MAAM;AAAA,EACzE,CAAC;AAED,KAAG,+DAA+D,MAAM;AACtE,WAAO,gBAAgB,4BAA4B,KAAK,CAAC,EAAE,KAAK,KAAK;AAAA,EACvE,CAAC;AAED,KAAG,qFAAqF,MAAM;AAC5F,WAAO,gBAAgB,4BAA4B,cAAc,CAAC,EAAE,KAAK,cAAc;AAAA,EACzF,CAAC;AAED,KAAG,uCAAkC,MAAM;AACzC,WAAO,gBAAgB,+BAA+B,2BAA2B,CAAC,EAAE;AAAA,MAClF;AAAA,IACF;AAAA,EACF,CAAC;AACH,CAAC;AAED,SAAS,kBAAkB,MAAM;AAC/B,KAAG,yDAAyD,MAAM;AAChE,WAAO,eAAe,MAAM,CAAC,EAAE,KAAK,IAAI;AAAA,EAC1C,CAAC;AAED,KAAG,mDAAmD,MAAM;AAC1D,WAAO,eAAe,KAAK,CAAC,EAAE,KAAK,IAAI;AAAA,EACzC,CAAC;AAED,KAAG,6CAA6C,MAAM;AACpD,WAAO,eAAe,oBAAoB,CAAC,EAAE,KAAK,KAAK;AAAA,EACzD,CAAC;AAED,KAAG,8CAA8C,MAAM;AACrD,WAAO,eAAe,qBAAqB,CAAC,EAAE,KAAK,KAAK;AAAA,EAC1D,CAAC;AAED,KAAG,uDAAuD,MAAM;AAC9D,WAAO,eAAe,iBAAiB,CAAC,EAAE,KAAK,KAAK;AAAA,EACtD,CAAC;AACH,CAAC;",
6
+ "names": []
7
+ }
@@ -1,11 +1,13 @@
1
1
  const REGEX = {
2
- /** Checks for a valid scheme */
3
- PROTOCOL: /^(?:https?|ftp|file|mailto|tel|data|wss?)*:\/\//,
4
- /** Finds multiple slashes after the scheme to replace with a single slash */
5
- MULTIPLE_SLASHES: /(?<!:)\/{2,}/g,
6
- VARIABLES: /{{((?:[^{}]|{[^{}]*})*)}}/g,
7
- PATH: /(?:{)([^{}]+)}(?!})/g,
8
- TEMPLATE_VARIABLE: /{{\s*([^}\s]+?)\s*}}|{\s*([^}\s]+?)\s*}|:\b[\w.]+\b/g,
2
+ /** Checks for a valid scheme */
3
+ PROTOCOL: /^(?:https?|ftp|file|mailto|tel|data|wss?)*:\/\//,
4
+ /** Finds multiple slashes after the scheme to replace with a single slash */
5
+ MULTIPLE_SLASHES: /(?<!:)\/{2,}/g,
6
+ VARIABLES: /{{((?:[^{}]|{[^{}]*})*)}}/g,
7
+ PATH: /(?:{)([^{}]+)}(?!})/g,
8
+ TEMPLATE_VARIABLE: /{{\s*([^}\s]+?)\s*}}|{\s*([^}\s]+?)\s*}|:\b[\w.]+\b/g
9
9
  };
10
-
11
- export { REGEX };
10
+ export {
11
+ REGEX
12
+ };
13
+ //# sourceMappingURL=regex-helpers.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/helpers/regex-helpers.ts"],
4
+ "sourcesContent": ["export const REGEX = {\n /** Checks for a valid scheme */\n PROTOCOL: /^(?:https?|ftp|file|mailto|tel|data|wss?)*:\\/\\//,\n /** Finds multiple slashes after the scheme to replace with a single slash */\n MULTIPLE_SLASHES: /(?<!:)\\/{2,}/g,\n VARIABLES: /{{((?:[^{}]|{[^{}]*})*)}}/g,\n PATH: /(?:{)([^{}]+)}(?!})/g,\n TEMPLATE_VARIABLE: /{{\\s*([^}\\s]+?)\\s*}}|{\\s*([^}\\s]+?)\\s*}|:\\b[\\w.]+\\b/g,\n} as const\n"],
5
+ "mappings": "AAAO,MAAM,QAAQ;AAAA;AAAA,EAEnB,UAAU;AAAA;AAAA,EAEV,kBAAkB;AAAA,EAClB,WAAW;AAAA,EACX,MAAM;AAAA,EACN,mBAAmB;AACrB;",
6
+ "names": []
7
+ }
@@ -0,0 +1,154 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { REGEX } from "./regex-helpers.js";
3
+ describe("protocolRegex", () => {
4
+ it("allows http://", () => {
5
+ const text = "http://example.com";
6
+ expect(text.match(REGEX.PROTOCOL)).toBeTruthy();
7
+ });
8
+ it("allows https://", () => {
9
+ const text = "https://example.com";
10
+ expect(text.match(REGEX.PROTOCOL)).toBeTruthy();
11
+ });
12
+ it("allows file://", () => {
13
+ const text = "file://example.com";
14
+ expect(text.match(REGEX.PROTOCOL)).toBeTruthy();
15
+ });
16
+ it("allows ftp://", () => {
17
+ const text = "ftp://example.com";
18
+ expect(text.match(REGEX.PROTOCOL)).toBeTruthy();
19
+ });
20
+ it("allows mailto://", () => {
21
+ const text = "mailto://example.com";
22
+ expect(text.match(REGEX.PROTOCOL)).toBeTruthy();
23
+ });
24
+ it("does not allow variables before ://", () => {
25
+ const text = "{protocol}://example.com";
26
+ expect(text.match(REGEX.PROTOCOL)).toBeNull();
27
+ });
28
+ it("does not allow no protocol with a variable", () => {
29
+ const text = "{base}/api";
30
+ expect(text.match(REGEX.PROTOCOL)).toBeNull();
31
+ });
32
+ it("does not allow no protocol with no variables", () => {
33
+ const text = "example.com";
34
+ expect(text.match(REGEX.PROTOCOL)).toBeNull();
35
+ });
36
+ });
37
+ describe("multipleSlashesRegex", () => {
38
+ it("matches multiple slashes", () => {
39
+ const text = "http://example.com//api".replace(REGEX.MULTIPLE_SLASHES, "/");
40
+ expect(text).toBe("http://example.com/api");
41
+ });
42
+ it("matches multiple slashes in the path", () => {
43
+ const text = "http://example.com/api//users////{id}".replace(REGEX.MULTIPLE_SLASHES, "/");
44
+ expect(text).toBe("http://example.com/api/users/{id}");
45
+ });
46
+ it("does not match single slash or the scheme", () => {
47
+ const text = "http://example.com/api/users/{id}";
48
+ expect(text.match(REGEX.MULTIPLE_SLASHES)).toBeNull();
49
+ });
50
+ it("does not do anything to the query params", () => {
51
+ const text = "http://example.com/api/users/{id}?query=param";
52
+ expect(text.replace(REGEX.MULTIPLE_SLASHES, "/")).toBe("http://example.com/api/users/{id}?query=param");
53
+ });
54
+ });
55
+ describe("variableRegex", () => {
56
+ it("matches variables with double curly braces", () => {
57
+ const text = "{{example.com}}";
58
+ const matches = [...text.matchAll(REGEX.VARIABLES)];
59
+ expect(matches.length).toBe(1);
60
+ expect(matches[0][1]).toBe("example.com");
61
+ });
62
+ it("matches variables with nested curly braces", () => {
63
+ const text = "{{example.com:{port}}}";
64
+ const matches = [...text.matchAll(REGEX.VARIABLES)];
65
+ expect(matches.length).toBe(1);
66
+ expect(matches[0][1]).toBe("example.com:{port}");
67
+ });
68
+ it("matches multiple variables", () => {
69
+ const text = "{{{host}.example.com:{port}}}";
70
+ const matches = [...text.matchAll(REGEX.VARIABLES)];
71
+ expect(matches.length).toBe(1);
72
+ expect(matches[0][1]).toBe("{host}.example.com:{port}");
73
+ });
74
+ it("does not match single curly braces", () => {
75
+ const text = "{example.com}";
76
+ const matches = [...text.matchAll(REGEX.VARIABLES)];
77
+ expect(matches.length).toBe(0);
78
+ });
79
+ it("does not match unbalanced curly braces", () => {
80
+ const text = "{{example.com}";
81
+ const matches = [...text.matchAll(REGEX.VARIABLES)];
82
+ expect(matches.length).toBe(0);
83
+ });
84
+ it("matches variables with whitespace", () => {
85
+ const text = "{{ example.com }}";
86
+ const matches = [...text.matchAll(REGEX.VARIABLES)];
87
+ expect(matches.length).toBe(1);
88
+ expect(matches[0][1]).toBe(" example.com ");
89
+ });
90
+ it("matches variables in longer text", () => {
91
+ const text = "prefix {{variable}} suffix";
92
+ const matches = [...text.matchAll(REGEX.VARIABLES)];
93
+ expect(matches.length).toBe(1);
94
+ expect(matches[0][1]).toBe("variable");
95
+ });
96
+ it("matches multiple separate variables", () => {
97
+ const text = "{{first}} middle {{second}}";
98
+ const matches = [...text.matchAll(REGEX.VARIABLES)];
99
+ expect(matches.length).toBe(2);
100
+ expect(matches[0][1]).toBe("first");
101
+ expect(matches[1][1]).toBe("second");
102
+ });
103
+ });
104
+ describe("pathRegex", () => {
105
+ it("matches path parameters", () => {
106
+ const text = "/users/{id}";
107
+ const matches = [...text.matchAll(REGEX.PATH)];
108
+ expect(matches.length).toBe(1);
109
+ expect(matches[0][1]).toBe("id");
110
+ });
111
+ it("matches multiple path parameters", () => {
112
+ const text = "/users/{userId}/posts/{postId}";
113
+ const matches = [...text.matchAll(REGEX.PATH)];
114
+ expect(matches.length).toBe(2);
115
+ expect(matches[0][1]).toBe("userId");
116
+ expect(matches[1][1]).toBe("postId");
117
+ });
118
+ it("does not match double curly braces", () => {
119
+ const text = "/users/{{id}}";
120
+ const matches = [...text.matchAll(REGEX.PATH)];
121
+ expect(matches.length).toBe(0);
122
+ });
123
+ it("matches path parameters with dots", () => {
124
+ const text = "/api/{version}/users";
125
+ const matches = [...text.matchAll(REGEX.PATH)];
126
+ expect(matches.length).toBe(1);
127
+ expect(matches[0][1]).toBe("version");
128
+ });
129
+ it("matches path parameters with hyphens and underscores", () => {
130
+ const text = "/users/{user-id}/posts/{post_id}";
131
+ const matches = [...text.matchAll(REGEX.PATH)];
132
+ expect(matches.length).toBe(2);
133
+ expect(matches[0][1]).toBe("user-id");
134
+ expect(matches[1][1]).toBe("post_id");
135
+ });
136
+ it("does not match nested curly braces", () => {
137
+ const text = "/users/{outer{inner}}";
138
+ const matches = [...text.matchAll(REGEX.PATH)];
139
+ expect(matches.length).toBe(0);
140
+ });
141
+ it("matches path parameters at start of string", () => {
142
+ const text = "{version}/api";
143
+ const matches = [...text.matchAll(REGEX.PATH)];
144
+ expect(matches.length).toBe(1);
145
+ expect(matches[0][1]).toBe("version");
146
+ });
147
+ it("matches path parameters at end of string", () => {
148
+ const text = "/api/{version}";
149
+ const matches = [...text.matchAll(REGEX.PATH)];
150
+ expect(matches.length).toBe(1);
151
+ expect(matches[0][1]).toBe("version");
152
+ });
153
+ });
154
+ //# sourceMappingURL=regex-helpers.test.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/helpers/regex-helpers.test.ts"],
4
+ "sourcesContent": ["import { describe, expect, it } from 'vitest'\n\nimport { REGEX } from './regex-helpers'\n\ndescribe('protocolRegex', () => {\n it('allows http://', () => {\n const text = 'http://example.com'\n expect(text.match(REGEX.PROTOCOL)).toBeTruthy()\n })\n\n it('allows https://', () => {\n const text = 'https://example.com'\n expect(text.match(REGEX.PROTOCOL)).toBeTruthy()\n })\n\n it('allows file://', () => {\n const text = 'file://example.com'\n expect(text.match(REGEX.PROTOCOL)).toBeTruthy()\n })\n\n it('allows ftp://', () => {\n const text = 'ftp://example.com'\n expect(text.match(REGEX.PROTOCOL)).toBeTruthy()\n })\n\n it('allows mailto://', () => {\n const text = 'mailto://example.com'\n expect(text.match(REGEX.PROTOCOL)).toBeTruthy()\n })\n\n it('does not allow variables before ://', () => {\n const text = '{protocol}://example.com'\n expect(text.match(REGEX.PROTOCOL)).toBeNull()\n })\n\n it('does not allow no protocol with a variable', () => {\n const text = '{base}/api'\n expect(text.match(REGEX.PROTOCOL)).toBeNull()\n })\n\n it('does not allow no protocol with no variables', () => {\n const text = 'example.com'\n expect(text.match(REGEX.PROTOCOL)).toBeNull()\n })\n})\n\ndescribe('multipleSlashesRegex', () => {\n it('matches multiple slashes', () => {\n const text = 'http://example.com//api'.replace(REGEX.MULTIPLE_SLASHES, '/')\n expect(text).toBe('http://example.com/api')\n })\n\n it('matches multiple slashes in the path', () => {\n const text = 'http://example.com/api//users////{id}'.replace(REGEX.MULTIPLE_SLASHES, '/')\n expect(text).toBe('http://example.com/api/users/{id}')\n })\n\n it('does not match single slash or the scheme', () => {\n const text = 'http://example.com/api/users/{id}'\n expect(text.match(REGEX.MULTIPLE_SLASHES)).toBeNull()\n })\n\n it('does not do anything to the query params', () => {\n const text = 'http://example.com/api/users/{id}?query=param'\n expect(text.replace(REGEX.MULTIPLE_SLASHES, '/')).toBe('http://example.com/api/users/{id}?query=param')\n })\n})\n\ndescribe('variableRegex', () => {\n it('matches variables with double curly braces', () => {\n const text = '{{example.com}}'\n const matches = [...text.matchAll(REGEX.VARIABLES)]\n expect(matches.length).toBe(1)\n expect(matches[0]![1]).toBe('example.com')\n })\n\n it('matches variables with nested curly braces', () => {\n const text = '{{example.com:{port}}}'\n const matches = [...text.matchAll(REGEX.VARIABLES)]\n expect(matches.length).toBe(1)\n expect(matches[0]![1]).toBe('example.com:{port}')\n })\n\n it('matches multiple variables', () => {\n const text = '{{{host}.example.com:{port}}}'\n const matches = [...text.matchAll(REGEX.VARIABLES)]\n expect(matches.length).toBe(1)\n expect(matches[0]![1]).toBe('{host}.example.com:{port}')\n })\n\n it('does not match single curly braces', () => {\n const text = '{example.com}'\n const matches = [...text.matchAll(REGEX.VARIABLES)]\n expect(matches.length).toBe(0)\n })\n\n it('does not match unbalanced curly braces', () => {\n const text = '{{example.com}'\n const matches = [...text.matchAll(REGEX.VARIABLES)]\n expect(matches.length).toBe(0)\n })\n\n it('matches variables with whitespace', () => {\n const text = '{{ example.com }}'\n const matches = [...text.matchAll(REGEX.VARIABLES)]\n expect(matches.length).toBe(1)\n expect(matches[0]![1]).toBe(' example.com ')\n })\n\n it('matches variables in longer text', () => {\n const text = 'prefix {{variable}} suffix'\n const matches = [...text.matchAll(REGEX.VARIABLES)]\n expect(matches.length).toBe(1)\n expect(matches[0]![1]).toBe('variable')\n })\n\n it('matches multiple separate variables', () => {\n const text = '{{first}} middle {{second}}'\n const matches = [...text.matchAll(REGEX.VARIABLES)]\n expect(matches.length).toBe(2)\n expect(matches[0]![1]).toBe('first')\n expect(matches[1]![1]).toBe('second')\n })\n})\n\ndescribe('pathRegex', () => {\n it('matches path parameters', () => {\n const text = '/users/{id}'\n const matches = [...text.matchAll(REGEX.PATH)]\n expect(matches.length).toBe(1)\n expect(matches[0]![1]).toBe('id')\n })\n\n it('matches multiple path parameters', () => {\n const text = '/users/{userId}/posts/{postId}'\n const matches = [...text.matchAll(REGEX.PATH)]\n expect(matches.length).toBe(2)\n expect(matches[0]![1]).toBe('userId')\n expect(matches[1]![1]).toBe('postId')\n })\n\n it('does not match double curly braces', () => {\n const text = '/users/{{id}}'\n const matches = [...text.matchAll(REGEX.PATH)]\n expect(matches.length).toBe(0)\n })\n\n it('matches path parameters with dots', () => {\n const text = '/api/{version}/users'\n const matches = [...text.matchAll(REGEX.PATH)]\n expect(matches.length).toBe(1)\n expect(matches[0]![1]).toBe('version')\n })\n\n it('matches path parameters with hyphens and underscores', () => {\n const text = '/users/{user-id}/posts/{post_id}'\n const matches = [...text.matchAll(REGEX.PATH)]\n expect(matches.length).toBe(2)\n expect(matches[0]![1]).toBe('user-id')\n expect(matches[1]![1]).toBe('post_id')\n })\n\n it('does not match nested curly braces', () => {\n const text = '/users/{outer{inner}}'\n const matches = [...text.matchAll(REGEX.PATH)]\n expect(matches.length).toBe(0)\n })\n\n it('matches path parameters at start of string', () => {\n const text = '{version}/api'\n const matches = [...text.matchAll(REGEX.PATH)]\n expect(matches.length).toBe(1)\n expect(matches[0]![1]).toBe('version')\n })\n\n it('matches path parameters at end of string', () => {\n const text = '/api/{version}'\n const matches = [...text.matchAll(REGEX.PATH)]\n expect(matches.length).toBe(1)\n expect(matches[0]![1]).toBe('version')\n })\n})\n"],
5
+ "mappings": "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",
6
+ "names": []
7
+ }
@@ -1,18 +1,15 @@
1
- /**
2
- * This function takes a string and replace {variables} with given values.
3
- */
4
1
  function replaceVariables(value, variablesOrCallback) {
5
- // Replace all variables (example: {{ baseurl }} with an HTML tag)
6
- const doubleCurlyBrackets = /{{\s*([\w.-]+)\s*}}/g;
7
- const singleCurlyBrackets = /{\s*([\w.-]+)\s*}/g;
8
- const callback = (_, match) => {
9
- if (typeof variablesOrCallback === 'function') {
10
- return variablesOrCallback(match);
11
- }
12
- return variablesOrCallback[match]?.toString() || `{${match}}`;
13
- };
14
- // Loop through all matches and replace the match with the variable value
15
- return value.replace(doubleCurlyBrackets, callback).replace(singleCurlyBrackets, callback);
2
+ const doubleCurlyBrackets = /{{\s*([\w.-]+)\s*}}/g;
3
+ const singleCurlyBrackets = /{\s*([\w.-]+)\s*}/g;
4
+ const callback = (_, match) => {
5
+ if (typeof variablesOrCallback === "function") {
6
+ return variablesOrCallback(match);
7
+ }
8
+ return variablesOrCallback[match]?.toString() || `{${match}}`;
9
+ };
10
+ return value.replace(doubleCurlyBrackets, callback).replace(singleCurlyBrackets, callback);
16
11
  }
17
-
18
- export { replaceVariables };
12
+ export {
13
+ replaceVariables
14
+ };
15
+ //# sourceMappingURL=replace-variables.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/helpers/replace-variables.ts"],
4
+ "sourcesContent": ["/**\n * This function takes a string and replace {variables} with given values.\n */\nexport function replaceVariables(\n value: string,\n variablesOrCallback: Record<string, string | number> | ((match: string) => string),\n) {\n // Replace all variables (example: {{ baseurl }} with an HTML tag)\n const doubleCurlyBrackets = /{{\\s*([\\w.-]+)\\s*}}/g\n const singleCurlyBrackets = /{\\s*([\\w.-]+)\\s*}/g\n\n const callback = (_: string, match: string): string => {\n if (typeof variablesOrCallback === 'function') {\n return variablesOrCallback(match)\n }\n return variablesOrCallback[match]?.toString() || `{${match}}`\n }\n\n // Loop through all matches and replace the match with the variable value\n return value.replace(doubleCurlyBrackets, callback).replace(singleCurlyBrackets, callback)\n}\n"],
5
+ "mappings": "AAGO,SAAS,iBACd,OACA,qBACA;AAEA,QAAM,sBAAsB;AAC5B,QAAM,sBAAsB;AAE5B,QAAM,WAAW,CAAC,GAAW,UAA0B;AACrD,QAAI,OAAO,wBAAwB,YAAY;AAC7C,aAAO,oBAAoB,KAAK;AAAA,IAClC;AACA,WAAO,oBAAoB,KAAK,GAAG,SAAS,KAAK,IAAI,KAAK;AAAA,EAC5D;AAGA,SAAO,MAAM,QAAQ,qBAAqB,QAAQ,EAAE,QAAQ,qBAAqB,QAAQ;AAC3F;",
6
+ "names": []
7
+ }
@@ -0,0 +1,30 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { replaceVariables } from "./replace-variables.js";
3
+ describe("replaceVariables", () => {
4
+ it("replaces variables with a single bracket", () => {
5
+ expect(
6
+ replaceVariables("foo{bar}foo", {
7
+ bar: "foo"
8
+ })
9
+ ).toMatchObject("foofoofoo");
10
+ });
11
+ it("replaces variables with a double bracket", () => {
12
+ expect(
13
+ replaceVariables("foo{{bar}}foo", {
14
+ bar: "foo"
15
+ })
16
+ ).toMatchObject("foofoofoo");
17
+ });
18
+ it("replaces variables with a custom callback", () => {
19
+ expect(replaceVariables("foo{{bar}}foo", (match) => match === "bar" ? "<span>foo</span>" : "")).toMatchObject(
20
+ "foo<span>foo</span>foo"
21
+ );
22
+ });
23
+ it("returns variable in curly braces when value is empty", () => {
24
+ const template = "http://{host}:{port}/api";
25
+ const variables = { host: "localhost", port: "" };
26
+ const result = replaceVariables(template, variables);
27
+ expect(result).toBe("http://localhost:{port}/api");
28
+ });
29
+ });
30
+ //# sourceMappingURL=replace-variables.test.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/helpers/replace-variables.test.ts"],
4
+ "sourcesContent": ["import { describe, expect, it } from 'vitest'\n\nimport { replaceVariables } from './replace-variables'\n\ndescribe('replaceVariables', () => {\n it('replaces variables with a single bracket', () => {\n expect(\n replaceVariables('foo{bar}foo', {\n bar: 'foo',\n }),\n ).toMatchObject('foofoofoo')\n })\n\n it('replaces variables with a double bracket', () => {\n expect(\n replaceVariables('foo{{bar}}foo', {\n bar: 'foo',\n }),\n ).toMatchObject('foofoofoo')\n })\n\n it('replaces variables with a custom callback', () => {\n expect(replaceVariables('foo{{bar}}foo', (match) => (match === 'bar' ? '<span>foo</span>' : ''))).toMatchObject(\n 'foo<span>foo</span>foo',\n )\n })\n\n it('returns variable in curly braces when value is empty', () => {\n const template = 'http://{host}:{port}/api'\n const variables = { host: 'localhost', port: '' }\n\n const result = replaceVariables(template, variables)\n\n expect(result).toBe('http://localhost:{port}/api')\n })\n})\n"],
5
+ "mappings": "AAAA,SAAS,UAAU,QAAQ,UAAU;AAErC,SAAS,wBAAwB;AAEjC,SAAS,oBAAoB,MAAM;AACjC,KAAG,4CAA4C,MAAM;AACnD;AAAA,MACE,iBAAiB,eAAe;AAAA,QAC9B,KAAK;AAAA,MACP,CAAC;AAAA,IACH,EAAE,cAAc,WAAW;AAAA,EAC7B,CAAC;AAED,KAAG,4CAA4C,MAAM;AACnD;AAAA,MACE,iBAAiB,iBAAiB;AAAA,QAChC,KAAK;AAAA,MACP,CAAC;AAAA,IACH,EAAE,cAAc,WAAW;AAAA,EAC7B,CAAC;AAED,KAAG,6CAA6C,MAAM;AACpD,WAAO,iBAAiB,iBAAiB,CAAC,UAAW,UAAU,QAAQ,qBAAqB,EAAG,CAAC,EAAE;AAAA,MAChG;AAAA,IACF;AAAA,EACF,CAAC;AAED,KAAG,wDAAwD,MAAM;AAC/D,UAAM,WAAW;AACjB,UAAM,YAAY,EAAE,MAAM,aAAa,MAAM,GAAG;AAEhD,UAAM,SAAS,iBAAiB,UAAU,SAAS;AAEnD,WAAO,MAAM,EAAE,KAAK,6BAA6B;AAAA,EACnD,CAAC;AACH,CAAC;",
6
+ "names": []
7
+ }
@@ -1,16 +1,17 @@
1
- /** Parse a Zod */
2
1
  function schemaModel(data, schema, throwError = true, errorLocation) {
3
- const result = schema.safeParse(data);
4
- if (!result.success) {
5
- console.group('Schema Error' + (errorLocation ? ` - ${errorLocation}` : ''));
6
- console.warn(JSON.stringify(result.error.format(), null, 2));
7
- console.log('Received: ', data);
8
- console.groupEnd();
9
- }
10
- if (throwError && !result.success) {
11
- throw new Error('Zod validation failure');
12
- }
13
- return result.data;
2
+ const result = schema.safeParse(data);
3
+ if (!result.success) {
4
+ console.group("Schema Error" + (errorLocation ? ` - ${errorLocation}` : ""));
5
+ console.warn(JSON.stringify(result.error.format(), null, 2));
6
+ console.log("Received: ", data);
7
+ console.groupEnd();
8
+ }
9
+ if (throwError && !result.success) {
10
+ throw new Error("Zod validation failure");
11
+ }
12
+ return result.data;
14
13
  }
15
-
16
- export { schemaModel };
14
+ export {
15
+ schemaModel
16
+ };
17
+ //# sourceMappingURL=schema-model.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/helpers/schema-model.ts"],
4
+ "sourcesContent": ["import type { ZodSchema, ZodTypeDef } from 'zod'\n\n/** Parse an value from a given schema with optional error or null return */\nexport function schemaModel<T, I = any>(\n data: I,\n schema: ZodSchema<T, ZodTypeDef, any>,\n throwError?: true,\n errorLocation?: string,\n): T\nexport function schemaModel<T, I = any>(\n data: I,\n schema: ZodSchema<T, ZodTypeDef, any>,\n throwError?: false,\n errorLocation?: string,\n): T | null\n/** Parse a Zod */\nexport function schemaModel<T, I = any>(\n data: I,\n schema: ZodSchema<T, ZodTypeDef, any>,\n throwError = true,\n errorLocation?: string,\n) {\n const result = schema.safeParse(data)\n\n if (!result.success) {\n console.group('Schema Error' + (errorLocation ? ` - ${errorLocation}` : ''))\n console.warn(JSON.stringify(result.error.format(), null, 2))\n console.log('Received: ', data)\n console.groupEnd()\n }\n\n if (throwError && !result.success) {\n throw new Error('Zod validation failure')\n }\n\n return result.data\n}\n"],
5
+ "mappings": "AAgBO,SAAS,YACd,MACA,QACA,aAAa,MACb,eACA;AACA,QAAM,SAAS,OAAO,UAAU,IAAI;AAEpC,MAAI,CAAC,OAAO,SAAS;AACnB,YAAQ,MAAM,kBAAkB,gBAAgB,MAAM,aAAa,KAAK,GAAG;AAC3E,YAAQ,KAAK,KAAK,UAAU,OAAO,MAAM,OAAO,GAAG,MAAM,CAAC,CAAC;AAC3D,YAAQ,IAAI,cAAc,IAAI;AAC9B,YAAQ,SAAS;AAAA,EACnB;AAEA,MAAI,cAAc,CAAC,OAAO,SAAS;AACjC,UAAM,IAAI,MAAM,wBAAwB;AAAA,EAC1C;AAEA,SAAO,OAAO;AAChB;",
6
+ "names": []
7
+ }
@@ -1,5 +1,5 @@
1
1
  import type { SecurityScheme } from '@scalar/types/entities';
2
- import type { Operation } from '../../entities/spec/operation.ts';
2
+ import type { Operation } from '../../entities/spec/operation.js';
3
3
  /** Parses a list of selected security scheme uids which may be an array or single uid and returns a flattened array of security schemes */
4
4
  export declare const getSchemes: (selectedSecuritySchemes: Operation["selectedSecuritySchemeUids"], securitySchemes: Record<SecurityScheme["uid"], SecurityScheme>) => NonNullable<{
5
5
  name: string;
@@ -1 +1 @@
1
- {"version":3,"file":"get-schemes.d.ts","sourceRoot":"","sources":["../../../src/helpers/security/get-schemes.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAA;AAE5D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAA;AAG7D,2IAA2I;AAC3I,eAAO,MAAM,UAAU,4BACI,SAAS,CAAC,4BAA4B,CAAC,mBAC/C,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,EAAE,cAAc,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;gBAOyr3D,CAAC;;;;;;;;;;;;gBAA4X,CAAC;;;;;;;;;;yBAA4V,CAAC;;;;;;;;;;yBAAkV,CAAC;;;;;;;;;;;;;;;;IADty5D,CAAA"}
1
+ {"version":3,"file":"get-schemes.d.ts","sourceRoot":"","sources":["../../../src/helpers/security/get-schemes.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAA;AAE5D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAA;AAG1D,2IAA2I;AAC3I,eAAO,MAAM,UAAU,4BACI,SAAS,CAAC,4BAA4B,CAAC,mBAC/C,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,EAAE,cAAc,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;gBAO+r3D,CAAC;;;;;;;;;;;;gBAA4X,CAAC;;;;;;;;;;yBAA4V,CAAC;;;;;;;;;;yBAAkV,CAAC;;;;;;;;;;;;;;;;IAD5y5D,CAAA"}
@@ -1,11 +1,9 @@
1
- import { isDefined } from '../is-defined.js';
2
-
3
- /** Parses a list of selected security scheme uids which may be an array or single uid and returns a flattened array of security schemes */
1
+ import { isDefined } from "../../helpers/is-defined.js";
4
2
  const getSchemes = (selectedSecuritySchemes, securitySchemes) => {
5
- const uids = new Set(selectedSecuritySchemes.flat());
6
- return Array.from(uids)
7
- .map((uid) => securitySchemes[uid])
8
- .filter(isDefined);
3
+ const uids = new Set(selectedSecuritySchemes.flat());
4
+ return Array.from(uids).map((uid) => securitySchemes[uid]).filter(isDefined);
9
5
  };
10
-
11
- export { getSchemes };
6
+ export {
7
+ getSchemes
8
+ };
9
+ //# sourceMappingURL=get-schemes.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/helpers/security/get-schemes.ts"],
4
+ "sourcesContent": ["import type { SecurityScheme } from '@scalar/types/entities'\n\nimport type { Operation } from '@/entities/spec/operation'\nimport { isDefined } from '@/helpers/is-defined'\n\n/** Parses a list of selected security scheme uids which may be an array or single uid and returns a flattened array of security schemes */\nexport const getSchemes = (\n selectedSecuritySchemes: Operation['selectedSecuritySchemeUids'],\n securitySchemes: Record<SecurityScheme['uid'], SecurityScheme>,\n) => {\n const uids = new Set(selectedSecuritySchemes.flat())\n return Array.from(uids)\n .map((uid) => securitySchemes[uid])\n .filter(isDefined)\n}\n"],
5
+ "mappings": "AAGA,SAAS,iBAAiB;AAGnB,MAAM,aAAa,CACxB,yBACA,oBACG;AACH,QAAM,OAAO,IAAI,IAAI,wBAAwB,KAAK,CAAC;AACnD,SAAO,MAAM,KAAK,IAAI,EACnB,IAAI,CAAC,QAAQ,gBAAgB,GAAG,CAAC,EACjC,OAAO,SAAS;AACrB;",
6
+ "names": []
7
+ }