@scalar/oas-utils 0.2.125 → 0.2.126

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # @scalar/oas-utils
2
2
 
3
+ ## 0.2.126
4
+
5
+ ### Patch Changes
6
+
7
+ - a571d36: refactor: use central helper fetchDocument to fetch OpenAPI documents
8
+ - Updated dependencies [3783345]
9
+ - @scalar/types@0.1.6
10
+ - @scalar/themes@0.9.84
11
+
3
12
  ## 0.2.125
4
13
 
5
14
  ### Patch Changes
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Fetches an OpenAPI/Swagger document from a given URL
3
+ *
4
+ * @throws an error if the fetch fails
5
+ */
6
+ export declare function fetchDocument(url: string, proxyUrl?: string, prettyPrint?: boolean): Promise<string>;
7
+ //# sourceMappingURL=fetch-document.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fetch-document.d.ts","sourceRoot":"","sources":["../../src/helpers/fetch-document.ts"],"names":[],"mappings":"AAQA;;;;GAIG;AACH,wBAAsB,aAAa,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,EAAE,WAAW,UAAO,GAAG,OAAO,CAAC,MAAM,CAAC,CA4BvG"}
@@ -0,0 +1,35 @@
1
+ import { formatJsonOrYamlString } from './parse.js';
2
+ import { redirectToProxy } from './redirect-to-proxy.js';
3
+
4
+ // Doesn’t work
5
+ const OLD_PROXY_URL = 'https://api.scalar.com/request-proxy';
6
+ // Does work
7
+ const NEW_PROXY_URL = 'https://proxy.scalar.com';
8
+ /**
9
+ * Fetches an OpenAPI/Swagger document from a given URL
10
+ *
11
+ * @throws an error if the fetch fails
12
+ */
13
+ async function fetchDocument(url, proxyUrl, prettyPrint = true) {
14
+ // This replaces the OLD_PROXY_URL with the NEW_PROXY_URL on the fly.
15
+ if (proxyUrl === OLD_PROXY_URL) {
16
+ // biome-ignore lint/style/noParameterAssign: It’s ok, let’s make an exception here.
17
+ proxyUrl = NEW_PROXY_URL;
18
+ }
19
+ const response = await fetch(redirectToProxy(proxyUrl, url));
20
+ // Looks like the request failed
21
+ if (response.status !== 200) {
22
+ console.error(`[fetchDocument] Failed to fetch the OpenAPI document from ${url} (Status: ${response.status})`);
23
+ if (!proxyUrl) {
24
+ console.warn(`[fetchDocument] Tried to fetch the OpenAPI document from ${url} without a proxy. Are the CORS headers configured to allow cross-domain requests? https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS`);
25
+ }
26
+ throw new Error(`Failed to fetch the OpenAPI document from ${url} (Status: ${response.status})`);
27
+ }
28
+ // If it’s JSON, make it pretty
29
+ if (prettyPrint) {
30
+ return formatJsonOrYamlString(await response.text());
31
+ }
32
+ return await response.text();
33
+ }
34
+
35
+ export { fetchDocument };
@@ -1,15 +1,15 @@
1
1
  export type FetchWithProxyFallbackOptions = {
2
- proxy: string | undefined;
2
+ proxyUrl: string | undefined;
3
3
  /**
4
4
  * @see https://developer.mozilla.org/en-US/docs/Web/API/Request/cache
5
5
  */
6
6
  cache?: RequestInit['cache'];
7
7
  };
8
8
  /**
9
- * Fetches an OpenAPI document with a proxy fallback mechanism.
9
+ * Fetches an OpenAPI document with a proxyUrl fallback mechanism.
10
10
  *
11
11
  * If a proxy is provided and the URL requires it, it will first attempt to fetch using the proxy.
12
12
  * If the proxy fetch fails or is not used, it will fall back to a direct fetch.
13
13
  */
14
- export declare function fetchWithProxyFallback(url: string, { proxy, cache }: FetchWithProxyFallbackOptions): Promise<Response>;
14
+ export declare function fetchWithProxyFallback(url: string, { proxyUrl, cache }: FetchWithProxyFallbackOptions): Promise<Response>;
15
15
  //# sourceMappingURL=fetch-with-proxy-fallback.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"fetch-with-proxy-fallback.d.ts","sourceRoot":"","sources":["../../src/helpers/fetch-with-proxy-fallback.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,6BAA6B,GAAG;IAC1C,KAAK,EAAE,MAAM,GAAG,SAAS,CAAA;IACzB;;OAEG;IACH,KAAK,CAAC,EAAE,WAAW,CAAC,OAAO,CAAC,CAAA;CAC7B,CAAA;AAED;;;;;GAKG;AACH,wBAAsB,sBAAsB,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,6BAA6B,qBAuBxG"}
1
+ {"version":3,"file":"fetch-with-proxy-fallback.d.ts","sourceRoot":"","sources":["../../src/helpers/fetch-with-proxy-fallback.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,6BAA6B,GAAG;IAC1C,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAA;IAC5B;;OAEG;IACH,KAAK,CAAC,EAAE,WAAW,CAAC,OAAO,CAAC,CAAA;CAC7B,CAAA;AAED;;;;;GAKG;AACH,wBAAsB,sBAAsB,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE,QAAQ,EAAE,KAAK,EAAE,EAAE,6BAA6B,qBAuB3G"}
@@ -1,28 +1,28 @@
1
1
  import { shouldUseProxy, redirectToProxy } from './redirect-to-proxy.js';
2
2
 
3
3
  /**
4
- * Fetches an OpenAPI document with a proxy fallback mechanism.
4
+ * Fetches an OpenAPI document with a proxyUrl fallback mechanism.
5
5
  *
6
6
  * If a proxy is provided and the URL requires it, it will first attempt to fetch using the proxy.
7
7
  * If the proxy fetch fails or is not used, it will fall back to a direct fetch.
8
8
  */
9
- async function fetchWithProxyFallback(url, { proxy, cache }) {
9
+ async function fetchWithProxyFallback(url, { proxyUrl, cache }) {
10
10
  const fetchOptions = {
11
11
  cache: cache || 'default',
12
12
  };
13
- const shouldTryProxy = shouldUseProxy(proxy, url);
14
- const initialUrl = shouldTryProxy ? redirectToProxy(proxy, url) : url;
13
+ const shouldTryProxy = shouldUseProxy(proxyUrl, url);
14
+ const initialUrl = shouldTryProxy ? redirectToProxy(proxyUrl, url) : url;
15
15
  try {
16
16
  const result = await fetch(initialUrl, fetchOptions);
17
17
  if (result.ok || !shouldTryProxy) {
18
18
  return result;
19
19
  }
20
- // Retry without proxy if the initial request failed
20
+ // Retry without proxyUrl if the initial request failed
21
21
  return await fetch(url, fetchOptions);
22
22
  }
23
23
  catch (error) {
24
24
  if (shouldTryProxy) {
25
- // If proxy failed, try without it
25
+ // If proxyUrl failed, try without it
26
26
  return await fetch(url, fetchOptions);
27
27
  }
28
28
  throw error;
@@ -1,6 +1,6 @@
1
1
  export { createHash } from './create-hash.ts';
2
2
  export { ensureProtocol } from './ensure-protocol.ts';
3
- export { fetchSpecFromUrl } from './fetch-spec-from-url.ts';
3
+ export { fetchDocument } from './fetch-document.ts';
4
4
  export { type FetchWithProxyFallbackOptions, fetchWithProxyFallback } from './fetch-with-proxy-fallback.ts';
5
5
  export { findVariables } from './find-variables.ts';
6
6
  export { canMethodHaveBody, getHttpMethodInfo, isHttpMethod, REQUEST_METHODS } from './http-methods.ts';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/helpers/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAC7C,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AACrD,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAA;AAC3D,OAAO,EAAE,KAAK,6BAA6B,EAAE,sBAAsB,EAAE,MAAM,gCAAgC,CAAA;AAC3G,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AACnD,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACvG,OAAO,EAAE,KAAK,cAAc,EAAE,KAAK,eAAe,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAA;AACnG,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAC3C,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AAC9C,OAAO,EAAE,U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OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAA;AAC1C,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA;AACzD,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC/C,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAA;AAC9D,OAAO,EAAE,mBAAmB,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA;AAC9D,OAAO,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA"}
@@ -1,6 +1,6 @@
1
1
  export { createHash } from './create-hash.js';
2
2
  export { ensureProtocol } from './ensure-protocol.js';
3
- export { fetchSpecFromUrl } from './fetch-spec-from-url.js';
3
+ export { fetchDocument } from './fetch-document.js';
4
4
  export { fetchWithProxyFallback } from './fetch-with-proxy-fallback.js';
5
5
  export { findVariables } from './find-variables.js';
6
6
  export { REQUEST_METHODS, canMethodHaveBody, getHttpMethodInfo, isHttpMethod } from './http-methods.js';
@@ -1,7 +1,17 @@
1
- /** Redirects the request to a proxy server with a given URL. */
2
- export declare function redirectToProxy(proxy?: string, url?: string): string;
3
- /** Check if the URL is relative or if it's a domain without protocol */
1
+ /**
2
+ * Redirects the request to a proxy server with a given URL. But not for:
3
+ *
4
+ * - Relative URLs
5
+ * - URLs that seem to point to a local IP (except the proxy is on the same domain)
6
+ * - URLs that don’t look like a domain
7
+ **/
8
+ export declare function redirectToProxy(proxyUrl?: string, url?: string): string;
9
+ /**
10
+ * Check if the URL is relative or if it's a domain without protocol
11
+ **/
4
12
  export declare const isRelativePath: (url: string) => boolean;
5
- /** Returns false for requests to localhost, relative URLs, if no proxy is defined … */
6
- export declare function shouldUseProxy(proxy?: string, url?: string): boolean;
13
+ /**
14
+ * Returns false for requests to localhost, relative URLs, if no proxy is defined …
15
+ **/
16
+ export declare function shouldUseProxy(proxyUrl?: string, url?: string): url is string;
7
17
  //# sourceMappingURL=redirect-to-proxy.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"redirect-to-proxy.d.ts","sourceRoot":"","sources":["../../src/helpers/redirect-to-proxy.ts"],"names":[],"mappings":"AAGA,gEAAgE;AAChE,wBAAgB,eAAe,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,MAAM,CAepE;AAED,wEAAwE;AACxE,eAAO,MAAM,cAAc,QAAS,MAAM,YAazC,CAAA;AAED,uFAAuF;AACvF,wBAAgB,cAAc,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,OAAO,CAiBpE"}
1
+ {"version":3,"file":"redirect-to-proxy.d.ts","sourceRoot":"","sources":["../../src/helpers/redirect-to-proxy.ts"],"names":[],"mappings":"AAGA;;;;;;IAMI;AACJ,wBAAgB,eAAe,CAAC,QAAQ,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,MAAM,CAmCvE;AAED;;IAEI;AACJ,eAAO,MAAM,cAAc,QAAS,MAAM,YAazC,CAAA;AAED;;IAEI;AACJ,wBAAgB,cAAc,CAAC,QAAQ,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,GAAG,IAAI,MAAM,CAkC7E"}
@@ -1,20 +1,47 @@
1
1
  import { isLocalUrl } from './is-local-url.js';
2
2
  import { REGEX } from './regex-helpers.js';
3
3
 
4
- /** Redirects the request to a proxy server with a given URL. */
5
- function redirectToProxy(proxy, url) {
6
- if (!shouldUseProxy(proxy, url)) {
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
+ **/
11
+ 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 {
7
39
  return url ?? '';
8
40
  }
9
- // Create new URL object from url
10
- const newUrl = new URL(url);
11
- // Rewrite the URL with the proxy
12
- newUrl.href = proxy;
13
- // Add the original URL as a query parameter
14
- newUrl.searchParams.append('scalar_url', url);
15
- return newUrl.toString();
16
41
  }
17
- /** Check if the URL is relative or if it's a domain without protocol */
42
+ /**
43
+ * Check if the URL is relative or if it's a domain without protocol
44
+ **/
18
45
  const isRelativePath = (url) => {
19
46
  // Allow http:// https:// and other protocols such as file://
20
47
  if (REGEX.PROTOCOL.test(url)) {
@@ -27,21 +54,39 @@ const isRelativePath = (url) => {
27
54
  }
28
55
  return true;
29
56
  };
30
- /** Returns false for requests to localhost, relative URLs, if no proxy is defined … */
31
- function shouldUseProxy(proxy, url) {
32
- // No proxy or url
33
- if (!proxy || !url) {
34
- return false;
57
+ /**
58
+ * Returns false for requests to localhost, relative URLs, if no proxy is defined …
59
+ **/
60
+ 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;
35
86
  }
36
- // Relative URLs
37
- if (isRelativePath(url)) {
87
+ catch {
38
88
  return false;
39
89
  }
40
- // Requests to localhost
41
- if (isLocalUrl(url)) {
42
- return false;
43
- }
44
- return true;
45
90
  }
46
91
 
47
92
  export { isRelativePath, redirectToProxy, shouldUseProxy };
package/package.json CHANGED
@@ -16,7 +16,7 @@
16
16
  "specification",
17
17
  "yaml"
18
18
  ],
19
- "version": "0.2.125",
19
+ "version": "0.2.126",
20
20
  "engines": {
21
21
  "node": ">=18"
22
22
  },
@@ -88,9 +88,9 @@
88
88
  "yaml": "^2.4.5",
89
89
  "zod": "^3.23.8",
90
90
  "@scalar/object-utils": "1.1.13",
91
+ "@scalar/themes": "0.9.84",
91
92
  "@scalar/openapi-types": "0.2.0",
92
- "@scalar/themes": "0.9.83",
93
- "@scalar/types": "0.1.5"
93
+ "@scalar/types": "0.1.6"
94
94
  },
95
95
  "devDependencies": {
96
96
  "@types/node": "^20.17.10",
@@ -99,8 +99,8 @@
99
99
  "vitest": "^1.6.0",
100
100
  "zod-to-ts": "github:amritk/zod-to-ts#build",
101
101
  "@scalar/build-tooling": "0.1.17",
102
- "@scalar/openapi-types": "0.2.0",
103
- "@scalar/openapi-parser": "0.10.13"
102
+ "@scalar/openapi-parser": "0.10.14",
103
+ "@scalar/openapi-types": "0.2.0"
104
104
  },
105
105
  "scripts": {
106
106
  "build": "scalar-build-rollup",
@@ -1,7 +0,0 @@
1
- /**
2
- * Fetches an OpenAPI/Swagger specification from a given URL.
3
- *
4
- * @throws an error if the fetch fails
5
- */
6
- export declare function fetchSpecFromUrl(url: string, proxy?: string, beautify?: boolean): Promise<string>;
7
- //# sourceMappingURL=fetch-spec-from-url.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"fetch-spec-from-url.d.ts","sourceRoot":"","sources":["../../src/helpers/fetch-spec-from-url.ts"],"names":[],"mappings":"AAQA;;;;GAIG;AACH,wBAAsB,gBAAgB,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,EAAE,QAAQ,UAAO,GAAG,OAAO,CAAC,MAAM,CAAC,CA4BpG"}
@@ -1,36 +0,0 @@
1
- import { formatJsonOrYamlString } from './parse.js';
2
- import { redirectToProxy } from './redirect-to-proxy.js';
3
-
4
- // Doesn’t work
5
- const OLD_PROXY_URL = 'https://api.scalar.com/request-proxy';
6
- // Does work
7
- const NEW_PROXY_URL = 'https://proxy.scalar.com';
8
- /**
9
- * Fetches an OpenAPI/Swagger specification from a given URL.
10
- *
11
- * @throws an error if the fetch fails
12
- */
13
- async function fetchSpecFromUrl(url, proxy, beautify = true) {
14
- // This replaces the OLD_PROXY_URL with the NEW_PROXY_URL on the fly.
15
- if (proxy === OLD_PROXY_URL) {
16
- // biome-ignore lint/style/noParameterAssign: It’s ok, let’s make an exception here.
17
- proxy = NEW_PROXY_URL;
18
- }
19
- // To use a proxy or not to use a proxy
20
- const response = await fetch(proxy ? redirectToProxy(proxy, url) : url);
21
- // Looks like the request failed
22
- if (response.status !== 200) {
23
- console.error(`[fetchSpecFromUrl] Failed to fetch the specification at ${url} (Status: ${response.status})`);
24
- if (!proxy) {
25
- console.warn(`[fetchSpecFromUrl] Tried to fetch the specification (url: ${url}) without a proxy. Are the CORS headers configured to allow cross-domain requests? https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS`);
26
- }
27
- throw new Error(`Failed to fetch the specification (Status: ${response.status})`);
28
- }
29
- // If it’s JSON, make it pretty
30
- if (beautify) {
31
- return formatJsonOrYamlString(await response.text());
32
- }
33
- return await response.text();
34
- }
35
-
36
- export { fetchSpecFromUrl };