@scalar/helpers 0.4.1 → 0.4.3

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 (123) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/array/add-to-map-array.js +7 -8
  3. package/dist/array/is-defined.js +12 -5
  4. package/dist/array/sort-by-order.js +60 -18
  5. package/dist/consts/content-types.js +13 -14
  6. package/dist/dom/freeze-element.js +56 -42
  7. package/dist/dom/get-selector.d.ts +7 -0
  8. package/dist/dom/get-selector.d.ts.map +1 -0
  9. package/dist/dom/get-selector.js +31 -0
  10. package/dist/dom/scroll-to-id.js +33 -27
  11. package/dist/file/json2xml.js +79 -61
  12. package/dist/formatters/format-bytes.d.ts +7 -0
  13. package/dist/formatters/format-bytes.d.ts.map +1 -0
  14. package/dist/formatters/format-bytes.js +18 -0
  15. package/dist/formatters/format-milliseconds.d.ts +6 -0
  16. package/dist/formatters/format-milliseconds.d.ts.map +1 -0
  17. package/dist/formatters/format-milliseconds.js +10 -0
  18. package/dist/general/create-limiter.js +47 -31
  19. package/dist/general/debounce.js +86 -66
  20. package/dist/general/extract-config-secrets.js +29 -27
  21. package/dist/general/has-modifier.js +9 -8
  22. package/dist/general/is-mac-os.js +23 -21
  23. package/dist/http/can-method-have-body.js +4 -6
  24. package/dist/http/http-info.js +63 -61
  25. package/dist/http/http-methods.js +4 -7
  26. package/dist/http/http-status-codes.js +316 -320
  27. package/dist/http/is-http-method.js +3 -6
  28. package/dist/http/normalize-http-method.js +19 -19
  29. package/dist/json/escape-json-pointer.js +6 -5
  30. package/dist/json/parse-json-pointer-segments.js +11 -6
  31. package/dist/json/pretty-print-json.d.ts +9 -0
  32. package/dist/json/pretty-print-json.d.ts.map +1 -0
  33. package/dist/json/pretty-print-json.js +42 -0
  34. package/dist/json/unescape-json-pointer.js +7 -5
  35. package/dist/markdown/get-markdown-headings.d.ts +11 -0
  36. package/dist/markdown/get-markdown-headings.d.ts.map +1 -0
  37. package/dist/markdown/get-markdown-headings.js +99 -0
  38. package/dist/node/path.js +168 -138
  39. package/dist/object/get-value-at-path.js +17 -11
  40. package/dist/object/is-object.js +24 -10
  41. package/dist/object/local-storage.js +50 -42
  42. package/dist/object/object-entries.js +2 -5
  43. package/dist/object/object-keys.js +5 -5
  44. package/dist/object/object-replace.js +13 -12
  45. package/dist/object/omit-undefined-values.js +18 -18
  46. package/dist/object/prevent-pollution.js +27 -10
  47. package/dist/object/to-json-compatible.js +71 -62
  48. package/dist/queue/queue.js +103 -84
  49. package/dist/regex/find-variables.js +13 -8
  50. package/dist/regex/regex-helpers.js +17 -17
  51. package/dist/regex/replace-variables.js +21 -19
  52. package/dist/string/camel-to-title.js +10 -5
  53. package/dist/string/capitalize.js +5 -5
  54. package/dist/string/create-hash.js +19 -16
  55. package/dist/string/generate-hash.js +153 -127
  56. package/dist/string/iterate-title.js +13 -11
  57. package/dist/string/truncate.js +16 -9
  58. package/dist/testing/console-spies.js +19 -24
  59. package/dist/testing/measure.js +42 -19
  60. package/dist/testing/measure.test-d.js +12 -9
  61. package/dist/testing/sleep.js +5 -5
  62. package/dist/url/ensure-protocol.js +8 -10
  63. package/dist/url/extract-server-from-path.js +53 -28
  64. package/dist/url/is-local-url.js +24 -18
  65. package/dist/url/is-relative-path.js +15 -13
  66. package/dist/url/is-valid-url.js +17 -10
  67. package/dist/url/make-url-absolute.js +39 -28
  68. package/dist/url/merge-urls.js +74 -52
  69. package/dist/url/redirect-to-proxy.js +68 -39
  70. package/package.json +15 -9
  71. package/dist/array/add-to-map-array.js.map +0 -7
  72. package/dist/array/is-defined.js.map +0 -7
  73. package/dist/array/sort-by-order.js.map +0 -7
  74. package/dist/consts/content-types.js.map +0 -7
  75. package/dist/dom/freeze-element.js.map +0 -7
  76. package/dist/dom/scroll-to-id.js.map +0 -7
  77. package/dist/file/json2xml.js.map +0 -7
  78. package/dist/general/create-limiter.js.map +0 -7
  79. package/dist/general/debounce.js.map +0 -7
  80. package/dist/general/extract-config-secrets.js.map +0 -7
  81. package/dist/general/has-modifier.js.map +0 -7
  82. package/dist/general/is-mac-os.js.map +0 -7
  83. package/dist/http/can-method-have-body.js.map +0 -7
  84. package/dist/http/http-info.js.map +0 -7
  85. package/dist/http/http-methods.js.map +0 -7
  86. package/dist/http/http-status-codes.js.map +0 -7
  87. package/dist/http/is-http-method.js.map +0 -7
  88. package/dist/http/normalize-http-method.js.map +0 -7
  89. package/dist/json/escape-json-pointer.js.map +0 -7
  90. package/dist/json/parse-json-pointer-segments.js.map +0 -7
  91. package/dist/json/unescape-json-pointer.js.map +0 -7
  92. package/dist/node/path.js.map +0 -7
  93. package/dist/object/get-value-at-path.js.map +0 -7
  94. package/dist/object/is-object.js.map +0 -7
  95. package/dist/object/local-storage.js.map +0 -7
  96. package/dist/object/object-entries.js.map +0 -7
  97. package/dist/object/object-keys.js.map +0 -7
  98. package/dist/object/object-replace.js.map +0 -7
  99. package/dist/object/omit-undefined-values.js.map +0 -7
  100. package/dist/object/prevent-pollution.js.map +0 -7
  101. package/dist/object/to-json-compatible.js.map +0 -7
  102. package/dist/queue/queue.js.map +0 -7
  103. package/dist/regex/find-variables.js.map +0 -7
  104. package/dist/regex/regex-helpers.js.map +0 -7
  105. package/dist/regex/replace-variables.js.map +0 -7
  106. package/dist/string/camel-to-title.js.map +0 -7
  107. package/dist/string/capitalize.js.map +0 -7
  108. package/dist/string/create-hash.js.map +0 -7
  109. package/dist/string/generate-hash.js.map +0 -7
  110. package/dist/string/iterate-title.js.map +0 -7
  111. package/dist/string/truncate.js.map +0 -7
  112. package/dist/testing/console-spies.js.map +0 -7
  113. package/dist/testing/measure.js.map +0 -7
  114. package/dist/testing/measure.test-d.js.map +0 -7
  115. package/dist/testing/sleep.js.map +0 -7
  116. package/dist/url/ensure-protocol.js.map +0 -7
  117. package/dist/url/extract-server-from-path.js.map +0 -7
  118. package/dist/url/is-local-url.js.map +0 -7
  119. package/dist/url/is-relative-path.js.map +0 -7
  120. package/dist/url/is-valid-url.js.map +0 -7
  121. package/dist/url/make-url-absolute.js.map +0 -7
  122. package/dist/url/merge-urls.js.map +0 -7
  123. package/dist/url/redirect-to-proxy.js.map +0 -7
@@ -1,11 +1,9 @@
1
- import { REGEX } from "../regex/regex-helpers.js";
2
- function ensureProtocol(url) {
3
- if (REGEX.PROTOCOL.test(url)) {
4
- return url;
5
- }
6
- return `http://${url.replace(/^\//, "")}`;
1
+ import { REGEX } from '../regex/regex-helpers.js';
2
+ /** Ensure URL has a protocol prefix */
3
+ export function ensureProtocol(url) {
4
+ if (REGEX.PROTOCOL.test(url)) {
5
+ return url;
6
+ }
7
+ // Default to http if no protocol is specified
8
+ return `http://${url.replace(/^\//, '')}`;
7
9
  }
8
- export {
9
- ensureProtocol
10
- };
11
- //# sourceMappingURL=ensure-protocol.js.map
@@ -1,32 +1,57 @@
1
- const extractServerFromPath = (path = "") => {
2
- if (!path.trim()) {
3
- return null;
4
- }
5
- if (path.startsWith("//")) {
6
- try {
7
- const url = new URL(`https:${path}`);
8
- if (url.origin === "null") {
1
+ /**
2
+ * Extracts the server from a string, used to check for servers in paths during migration
3
+ *
4
+ * @param path - The URL string to parse. If no protocol is provided, the URL API will throw an error.
5
+ * @returns A tuple of [origin, remainingPath] or null if the input is empty, whitespace-only, or invalid.
6
+ *
7
+ * @example
8
+ * extractServer('https://api.example.com/v1/users?id=123')
9
+ * // Returns: ['https://api.example.com', '/v1/users?id=123']
10
+ *
11
+ * @example
12
+ * extractServer('/users')
13
+ * // Returns: null
14
+ *
15
+ * @example
16
+ * extractServer('/users')
17
+ * // Returns: null
18
+ *
19
+ * @example
20
+ * extractServer('//api.example.com/v1/users')
21
+ * // Returns: ['//api.example.com', '/v1/users']
22
+ */
23
+ export const extractServerFromPath = (path = '') => {
24
+ if (!path.trim()) {
9
25
  return null;
10
- }
11
- const origin = url.origin.replace(/^https?:/, "");
12
- const remainingPath = decodeURIComponent(url.pathname) + url.search + url.hash;
13
- return [origin, remainingPath];
14
- } catch {
15
- return null;
16
26
  }
17
- }
18
- try {
19
- const url = new URL(path);
20
- if (url.origin === "null") {
21
- return null;
27
+ /** Handle protocol-relative URLs (e.g., "//api.example.com") */
28
+ if (path.startsWith('//')) {
29
+ try {
30
+ /** Use dummy protocol to parse, then strip it */
31
+ const url = new URL(`https:${path}`);
32
+ if (url.origin === 'null') {
33
+ return null;
34
+ }
35
+ const origin = url.origin.replace(/^https?:/, '');
36
+ /** Decode pathname to preserve OpenAPI template variables like {userId} */
37
+ const remainingPath = decodeURIComponent(url.pathname) + url.search + url.hash;
38
+ return [origin, remainingPath];
39
+ }
40
+ catch {
41
+ return null;
42
+ }
43
+ }
44
+ try {
45
+ const url = new URL(path);
46
+ /** URL API returns "null" for file:// and other invalid protocols */
47
+ if (url.origin === 'null') {
48
+ return null;
49
+ }
50
+ /** Decode pathname to preserve OpenAPI template variables like {userId} */
51
+ const remainingPath = decodeURIComponent(url.pathname) + url.search + url.hash;
52
+ return [url.origin, remainingPath];
53
+ }
54
+ catch {
55
+ return null;
22
56
  }
23
- const remainingPath = decodeURIComponent(url.pathname) + url.search + url.hash;
24
- return [url.origin, remainingPath];
25
- } catch {
26
- return null;
27
- }
28
- };
29
- export {
30
- extractServerFromPath
31
57
  };
32
- //# sourceMappingURL=extract-server-from-path.js.map
@@ -1,21 +1,27 @@
1
- const LOCAL_HOSTNAMES = ["localhost", "127.0.0.1", "[::1]", "0.0.0.0"];
2
- const RESERVED_TLDS = ["test", "example", "invalid", "localhost"];
3
- function isLocalUrl(url) {
4
- try {
5
- const { hostname } = new URL(url);
6
- if (LOCAL_HOSTNAMES.includes(hostname)) {
7
- return true;
1
+ /** Obviously local hostnames */
2
+ const LOCAL_HOSTNAMES = ['localhost', '127.0.0.1', '[::1]', '0.0.0.0'];
3
+ /** Reserved TLDs that are guaranteed to never be assigned */
4
+ const RESERVED_TLDS = ['test', 'example', 'invalid', 'localhost'];
5
+ /**
6
+ * Detect requests to localhost or reserved TLDs
7
+ */
8
+ export function isLocalUrl(url) {
9
+ try {
10
+ const { hostname } = new URL(url);
11
+ // Check if hostname is in the local hostnames list
12
+ if (LOCAL_HOSTNAMES.includes(hostname)) {
13
+ return true;
14
+ }
15
+ // Check if hostname ends with a reserved TLD
16
+ const tld = hostname.split('.').pop();
17
+ if (tld && RESERVED_TLDS.includes(tld)) {
18
+ return true;
19
+ }
20
+ return false;
8
21
  }
9
- const tld = hostname.split(".").pop();
10
- if (tld && RESERVED_TLDS.includes(tld)) {
11
- return true;
22
+ catch {
23
+ // If it's not a valid URL, we can't use the proxy anyway,
24
+ // but it also covers cases like relative URLs (e.g. `openapi.json`).
25
+ return true;
12
26
  }
13
- return false;
14
- } catch {
15
- return true;
16
- }
17
27
  }
18
- export {
19
- isLocalUrl
20
- };
21
- //# sourceMappingURL=is-local-url.js.map
@@ -1,14 +1,16 @@
1
- import { REGEX } from "../regex/regex-helpers.js";
2
- const isRelativePath = (url) => {
3
- if (REGEX.PROTOCOL.test(url)) {
4
- return false;
5
- }
6
- if (/^[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+(\/|$)/.test(url)) {
7
- return false;
8
- }
9
- return true;
1
+ import { REGEX } from '../regex/regex-helpers.js';
2
+ /**
3
+ * Check if the URL is relative or if it's a domain without protocol
4
+ **/
5
+ export const isRelativePath = (url) => {
6
+ // Allow http:// https:// and other protocols such as file://
7
+ if (REGEX.PROTOCOL.test(url)) {
8
+ return false;
9
+ }
10
+ // Check if it looks like a domain (contains dots and no spaces)
11
+ // This catches cases like "galaxy.scalar.com/planets"
12
+ if (/^[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+(\/|$)/.test(url)) {
13
+ return false;
14
+ }
15
+ return true;
10
16
  };
11
- export {
12
- isRelativePath
13
- };
14
- //# sourceMappingURL=is-relative-path.js.map
@@ -1,11 +1,18 @@
1
- function isValidUrl(url) {
2
- try {
3
- return Boolean(new URL(url));
4
- } catch {
5
- return false;
6
- }
1
+ /**
2
+ * Checks if a given string is a valid URL.
3
+ *
4
+ * @param {string} url - The string to be validated as a URL.
5
+ * @returns {boolean} Returns true if the string is a valid URL, false otherwise.
6
+ *
7
+ * @example
8
+ * isValidUrl('https://www.example.com'); // returns true
9
+ * isValidUrl('not a url'); // returns false
10
+ */
11
+ export function isValidUrl(url) {
12
+ try {
13
+ return Boolean(new URL(url));
14
+ }
15
+ catch {
16
+ return false;
17
+ }
7
18
  }
8
- export {
9
- isValidUrl
10
- };
11
- //# sourceMappingURL=is-valid-url.js.map
@@ -1,30 +1,41 @@
1
- import { combineUrlAndPath } from "../url/merge-urls.js";
2
- const makeUrlAbsolute = (url, {
3
- /** Optional base URL to resolve against (defaults to window.location.href) */
4
- baseUrl,
5
- /** If we have a basePath then we resolve against window.location.origin + basePath */
6
- basePath
7
- } = {}) => {
8
- if (typeof window === "undefined" && !baseUrl) {
9
- return url;
10
- }
11
- try {
12
- new URL(url);
13
- return url;
14
- } catch {
15
- }
16
- try {
17
- let base = baseUrl || window.location.href;
18
- if (basePath) {
19
- const origin = baseUrl ? new URL(baseUrl).origin : window.location.origin;
20
- base = combineUrlAndPath(origin, basePath + "/");
1
+ import { combineUrlAndPath } from '../url/merge-urls.js';
2
+ /**
3
+ * Converts a relative URL to an absolute URL using the provided base URL or current window location.
4
+ * @param url - The URL to make absolute
5
+ * @param options - Configuration options
6
+ * @param options.baseUrl - Optional base URL to resolve against (defaults to window.location.href)
7
+ * @param options.basePath - If provided, combines with baseUrl or window.location.origin before resolving
8
+ * @returns The absolute URL, or the original URL if it's already absolute or invalid
9
+ */
10
+ export const makeUrlAbsolute = (url, {
11
+ /** Optional base URL to resolve against (defaults to window.location.href) */
12
+ baseUrl,
13
+ /** If we have a basePath then we resolve against window.location.origin + basePath */
14
+ basePath, } = {}) => {
15
+ // If no base URL provided and we're not in a browser environment, return as-is
16
+ if (typeof window === 'undefined' && !baseUrl) {
17
+ return url;
18
+ }
19
+ try {
20
+ // If we can create a URL object without a base, it's already absolute
21
+ new URL(url);
22
+ return url;
23
+ }
24
+ catch {
25
+ // URL is relative, proceed with resolution
26
+ }
27
+ // Use URL constructor which handles path normalization automatically
28
+ try {
29
+ let base = baseUrl || window.location.href;
30
+ // If basePath is provided, combine it with the base URL
31
+ if (basePath) {
32
+ const origin = baseUrl ? new URL(baseUrl).origin : window.location.origin;
33
+ base = combineUrlAndPath(origin, basePath + '/');
34
+ }
35
+ return new URL(url, base).toString();
36
+ }
37
+ catch {
38
+ // If URL construction fails, return the original URL
39
+ return url;
21
40
  }
22
- return new URL(url, base).toString();
23
- } catch {
24
- return url;
25
- }
26
- };
27
- export {
28
- makeUrlAbsolute
29
41
  };
30
- //# sourceMappingURL=make-url-absolute.js.map
@@ -1,56 +1,78 @@
1
- import { REGEX } from "../regex/regex-helpers.js";
2
- import { isRelativePath } from "./is-relative-path.js";
3
- import { ensureProtocol } from "./ensure-protocol.js";
4
- const mergeSearchParams = (...params) => {
5
- const merged = {};
6
- params.forEach((p) => {
7
- const keys = Array.from(p.keys());
8
- const uniqueKeys = new Set(keys);
9
- uniqueKeys.forEach((key) => {
10
- const values = p.getAll(key);
11
- const value = values.length > 1 ? values : values[0] ?? "";
12
- merged[key] = value;
1
+ import { REGEX } from '../regex/regex-helpers.js';
2
+ import { isRelativePath } from './is-relative-path.js';
3
+ import { ensureProtocol } from './ensure-protocol.js';
4
+ /**
5
+ * Merges multiple URLSearchParams objects, preserving multiple values per param
6
+ * within each source, but later sources overwrite earlier ones completely
7
+ * This should de-dupe our query params while allowing multiple keys for "arrays"
8
+ */
9
+ export const mergeSearchParams = (...params) => {
10
+ // We keep a merged record to ensure the next group will overwrite the previous
11
+ const merged = {};
12
+ // Loops over each group and grabs unique keys
13
+ params.forEach((p) => {
14
+ const keys = Array.from(p.keys());
15
+ const uniqueKeys = new Set(keys);
16
+ uniqueKeys.forEach((key) => {
17
+ const values = p.getAll(key);
18
+ const value = values.length > 1 ? values : (values[0] ?? '');
19
+ merged[key] = value;
20
+ });
13
21
  });
14
- });
15
- const result = new URLSearchParams();
16
- Object.entries(merged).forEach(([key, value]) => {
17
- if (Array.isArray(value)) {
18
- value.forEach((v) => result.append(key, v));
19
- } else {
20
- result.append(key, value);
21
- }
22
- });
23
- return result;
24
- };
25
- const combineUrlAndPath = (url, path) => {
26
- if (!path || url === path) {
27
- return url.trim();
28
- }
29
- if (!url) {
30
- return path.trim();
31
- }
32
- return `${url.trim()}/${path.trim()}`.replace(REGEX.MULTIPLE_SLASHES, "/");
22
+ const result = new URLSearchParams();
23
+ // We maintain multiple values for each key if they existed
24
+ Object.entries(merged).forEach(([key, value]) => {
25
+ if (Array.isArray(value)) {
26
+ value.forEach((v) => result.append(key, v));
27
+ }
28
+ else {
29
+ result.append(key, value);
30
+ }
31
+ });
32
+ return result;
33
33
  };
34
- const mergeUrls = (url, path, urlParams = new URLSearchParams(), disableOriginPrefix = false) => {
35
- if (url && (!isRelativePath(url) || typeof window !== "undefined")) {
36
- const base = disableOriginPrefix ? url : isRelativePath(url) ? combineUrlAndPath(window.location.origin, url) : ensureProtocol(url);
37
- const [baseUrl = "", baseQuery] = base.split("?");
38
- const baseParams = new URLSearchParams(baseQuery || "");
39
- const [pathWithoutQuery = "", pathQuery] = path.split("?");
40
- const pathParams = new URLSearchParams(pathQuery || "");
41
- const mergedUrl = url === path ? baseUrl : combineUrlAndPath(baseUrl, pathWithoutQuery);
42
- const mergedSearchParams = mergeSearchParams(baseParams, pathParams, urlParams);
43
- const search = mergedSearchParams.toString();
44
- return search ? `${mergedUrl}?${search}` : mergedUrl;
45
- }
46
- if (path) {
47
- return combineUrlAndPath(url, path);
48
- }
49
- return "";
34
+ /** Combines a base URL and a path ensuring there's only one slash between them */
35
+ export const combineUrlAndPath = (url, path) => {
36
+ if (!path || url === path) {
37
+ return url.trim();
38
+ }
39
+ if (!url) {
40
+ return path.trim();
41
+ }
42
+ return `${url.trim()}/${path.trim()}`.replace(REGEX.MULTIPLE_SLASHES, '/');
50
43
  };
51
- export {
52
- combineUrlAndPath,
53
- mergeSearchParams,
54
- mergeUrls
44
+ /**
45
+ * Creates a URL from the path and server
46
+ * also optionally merges query params if you include urlSearchParams
47
+ * This was re-written without using URL to support variables in the scheme
48
+ */
49
+ export const mergeUrls = (url, path, urlParams = new URLSearchParams(),
50
+ /** To disable prefixing the url with the origin or a scheme*/
51
+ disableOriginPrefix = false) => {
52
+ // Extract and merge all query params
53
+ if (url && (!isRelativePath(url) || typeof window !== 'undefined')) {
54
+ /** Prefix the url with the origin if it is relative and we wish to */
55
+ const base = disableOriginPrefix
56
+ ? url
57
+ : isRelativePath(url)
58
+ ? combineUrlAndPath(window.location.origin, url)
59
+ : ensureProtocol(url);
60
+ // Extract search params from base URL if any
61
+ const [baseUrl = '', baseQuery] = base.split('?');
62
+ const baseParams = new URLSearchParams(baseQuery || '');
63
+ // Extract search params from path if any
64
+ const [pathWithoutQuery = '', pathQuery] = path.split('?');
65
+ const pathParams = new URLSearchParams(pathQuery || '');
66
+ // Merge the baseUrl and path
67
+ const mergedUrl = url === path ? baseUrl : combineUrlAndPath(baseUrl, pathWithoutQuery);
68
+ // Merge all search params
69
+ const mergedSearchParams = mergeSearchParams(baseParams, pathParams, urlParams);
70
+ // Build the final URL
71
+ const search = mergedSearchParams.toString();
72
+ return search ? `${mergedUrl}?${search}` : mergedUrl;
73
+ }
74
+ if (path) {
75
+ return combineUrlAndPath(url, path);
76
+ }
77
+ return '';
55
78
  };
56
- //# sourceMappingURL=merge-urls.js.map
@@ -1,44 +1,73 @@
1
- import { isLocalUrl } from "./is-local-url.js";
2
- import { isRelativePath } from "./is-relative-path.js";
3
- const redirectToProxy = (proxyUrl, url) => {
4
- try {
5
- if (!shouldUseProxy(proxyUrl, url)) {
6
- return url ?? "";
1
+ import { isLocalUrl } from './is-local-url.js';
2
+ import { isRelativePath } from './is-relative-path.js';
3
+ /**
4
+ * Redirects the request to a proxy server with a given URL. But not for:
5
+ *
6
+ * - Relative URLs
7
+ * - URLs that seem to point to a local IP (except the proxy is on the same domain)
8
+ * - URLs that don't look like a domain
9
+ **/
10
+ export const redirectToProxy = (proxyUrl, url) => {
11
+ try {
12
+ if (!shouldUseProxy(proxyUrl, url)) {
13
+ return url ?? '';
14
+ }
15
+ // Create new URL object from url
16
+ const newUrl = new URL(url);
17
+ // Add temporary domain for relative proxy URLs
18
+ //
19
+ // Q: Why isn't proxyUrl type guarded?
20
+ // A: Type guarding works for one parameter only (as of now).
21
+ //
22
+ // Q: Why do we need to add http://localhost to relative proxy URLs?
23
+ // A: Because the `new URL()` would otherwise fail.
24
+ //
25
+ const temporaryProxyUrl = isRelativePath(proxyUrl) ? `http://localhost${proxyUrl}` : proxyUrl;
26
+ // Rewrite the URL with the proxy
27
+ newUrl.href = temporaryProxyUrl;
28
+ // Add the original URL as a query parameter
29
+ newUrl.searchParams.append('scalar_url', url);
30
+ // Remove the temporary domain if we added it, but only from the start of the URL
31
+ const result = isRelativePath(proxyUrl)
32
+ ? newUrl.toString().replace(/^http:\/\/localhost/, '')
33
+ : newUrl.toString();
34
+ return result;
7
35
  }
8
- const newUrl = new URL(url);
9
- const temporaryProxyUrl = isRelativePath(proxyUrl) ? `http://localhost${proxyUrl}` : proxyUrl;
10
- newUrl.href = temporaryProxyUrl;
11
- newUrl.searchParams.append("scalar_url", url);
12
- const result = isRelativePath(proxyUrl) ? newUrl.toString().replace(/^http:\/\/localhost/, "") : newUrl.toString();
13
- return result;
14
- } catch {
15
- return url ?? "";
16
- }
17
- };
18
- const shouldUseProxy = (proxyUrl, url) => {
19
- try {
20
- if (!proxyUrl || !url) {
21
- return false;
22
- }
23
- if (isRelativePath(url)) {
24
- return false;
25
- }
26
- if (isRelativePath(proxyUrl)) {
27
- return true;
36
+ catch {
37
+ return url ?? '';
28
38
  }
29
- if (isLocalUrl(proxyUrl)) {
30
- return true;
39
+ };
40
+ /**
41
+ * Returns false for requests to localhost, relative URLs, if no proxy is defined …
42
+ **/
43
+ export const shouldUseProxy = (proxyUrl, url) => {
44
+ try {
45
+ // ❌ We don't have a proxy URL or the URL
46
+ if (!proxyUrl || !url) {
47
+ return false;
48
+ }
49
+ // ❌ Request to relative URLs (won't be blocked by CORS anyway)
50
+ if (isRelativePath(url)) {
51
+ return false;
52
+ }
53
+ // ✅ Proxy URL is on the same domain (e.g. /proxy)
54
+ // It's more likely (not guaranteed, though) that the proxy has access to local domains.
55
+ if (isRelativePath(proxyUrl)) {
56
+ return true;
57
+ }
58
+ // ✅ Proxy URL is local
59
+ if (isLocalUrl(proxyUrl)) {
60
+ return true;
61
+ }
62
+ // ❌ Requests to localhost
63
+ // We won't reach them from a (likely remote) proxy.
64
+ if (isLocalUrl(url)) {
65
+ return false;
66
+ }
67
+ // ✅ Seems fine (e.g. remote proxy + remote URL)
68
+ return true;
31
69
  }
32
- if (isLocalUrl(url)) {
33
- return false;
70
+ catch {
71
+ return false;
34
72
  }
35
- return true;
36
- } catch {
37
- return false;
38
- }
39
- };
40
- export {
41
- redirectToProxy,
42
- shouldUseProxy
43
73
  };
44
- //# sourceMappingURL=redirect-to-proxy.js.map
package/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "helpers",
15
15
  "js"
16
16
  ],
17
- "version": "0.4.1",
17
+ "version": "0.4.3",
18
18
  "engines": {
19
19
  "node": ">=22"
20
20
  },
@@ -47,6 +47,11 @@
47
47
  "types": "./dist/file/*.d.ts",
48
48
  "default": "./dist/file/*.js"
49
49
  },
50
+ "./formatters/*": {
51
+ "import": "./dist/formatters/*.js",
52
+ "types": "./dist/formatters/*.d.ts",
53
+ "default": "./dist/formatters/*.js"
54
+ },
50
55
  "./general/*": {
51
56
  "import": "./dist/general/*.js",
52
57
  "types": "./dist/general/*.d.ts",
@@ -62,6 +67,11 @@
62
67
  "types": "./dist/json/*.d.ts",
63
68
  "default": "./dist/json/*.js"
64
69
  },
70
+ "./markdown/*": {
71
+ "import": "./dist/markdown/*.js",
72
+ "types": "./dist/markdown/*.d.ts",
73
+ "default": "./dist/markdown/*.js"
74
+ },
65
75
  "./node/*": {
66
76
  "import": "./dist/node/*.js",
67
77
  "types": "./dist/node/*.d.ts",
@@ -97,18 +107,14 @@
97
107
  "dist",
98
108
  "CHANGELOG.md"
99
109
  ],
110
+ "sideEffects": false,
100
111
  "devDependencies": {
101
112
  "jsdom": "27.4.0",
102
- "vite": "^7.3.1",
103
- "vitest": "4.0.16",
104
- "@scalar/build-tooling": "0.5.0"
113
+ "vitest": "4.1.0"
105
114
  },
106
115
  "scripts": {
107
- "build": "scalar-build-esbuild",
108
- "lint:check": "biome lint --diagnostic-level=error",
109
- "lint:fix": "biome lint --write",
116
+ "build": "tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json",
110
117
  "test": "vitest",
111
- "types:build": "scalar-types-build",
112
- "types:check": "scalar-types-check"
118
+ "types:check": "tsc --noEmit"
113
119
  }
114
120
  }
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../src/array/add-to-map-array.ts"],
4
- "sourcesContent": ["/**\n * A little helper to add a value to a map array where the type is Map<string, any[]>\n */\nexport const addToMapArray = (map: Map<string, unknown[]>, key: string, value: unknown) => {\n const prev = map.get(key) ?? []\n prev.push(value)\n map.set(key, prev)\n}\n"],
5
- "mappings": "AAGO,MAAM,gBAAgB,CAAC,KAA6B,KAAa,UAAmB;AACzF,QAAM,OAAO,IAAI,IAAI,GAAG,KAAK,CAAC;AAC9B,OAAK,KAAK,KAAK;AACf,MAAI,IAAI,KAAK,IAAI;AACnB;",
6
- "names": []
7
- }
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../src/array/is-defined.ts"],
4
- "sourcesContent": ["/**\n * Type safe alternative to array.filter(Boolean)\n *\n * @example\n *\n * ```ts\n * const dataArray = [1, null, 2, undefined, 3].filter(isDefined)\n * ```\n *\n * @see https://jaketrent.com/post/typescript-type-safe-filter-boolean/\n */\nexport const isDefined = <T>(value: T | null | undefined): value is NonNullable<T> =>\n value !== null && value !== undefined\n"],
5
- "mappings": "AAWO,MAAM,YAAY,CAAI,UAC3B,UAAU,QAAQ,UAAU;",
6
- "names": []
7
- }
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../src/array/sort-by-order.ts"],
4
- "sourcesContent": ["/**\n * Immutably sorts an array based on a given custom order, as specified by a separate list of identifiers.\n *\n * Note: Make sure that the identifier is unique for each item in the array.\n * If the identifier is not unique, only the last occurrence of the identifier will be sorted.\n * Any other elements will be overridden by the last occurrence of the identifier.\n *\n * This function efficiently arranges elements of an input array so that items whose IDs (obtained via the `getId` callback)\n * match the order array will appear first, strictly in the order specified. Items not found in the order array will be\n * appended after, maintaining their original order.\n *\n * This is a flexible utility: the identifier can be extracted from any data structure via the `getId` callback,\n * and any primitive (string, number, symbol, etc.) can serve as the identifier type.\n * Sorting is O(n) with respect to the array size, making it performant even for large arrays.\n *\n * @template T - The type of array elements.\n * @template N - The type of values in the order array and the identifier returned by the getId callback.\n * @param arr - The array to be sorted.\n * @param order - Array specifying the desired sequence (contains identifiers returned by getId).\n * @param getId - A callback to extract the unique identifier from each array element.\n * @returns A new array sorted according to the order provided, with unmatched elements following.\n *\n * @example\n * // Sorting an array of objects:\n * const items = [\n * { id: 'a', name: 'Alpha' },\n * { id: 'b', name: 'Bravo' },\n * { id: 'c', name: 'Charlie' }\n * ];\n * const order = ['c', 'a'];\n * const sorted = sortByOrder(items, order, item => item.id);\n * // Result:\n * // [\n * // { id: 'c', name: 'Charlie' },\n * // { id: 'a', name: 'Alpha' },\n * // { id: 'b', name: 'Bravo' }\n * // ]\n *\n * @example\n * // Sorting an array of primitive values:\n * const input = ['a', 'b', 'c', 'd'];\n * const order = ['c', 'a'];\n * sortByOrder(input, order, item => item);\n * // Result: ['c', 'a', 'b', 'd']\n */\nexport function sortByOrder<T, N>(arr: T[], order: N[], getId: (item: T) => N): T[] {\n // Map the order to keep a single lookup table\n const orderMap = new Map<N, number>()\n order.forEach((e, idx) => orderMap.set(e, idx))\n\n const sorted: T[] = []\n const untagged: T[] = []\n\n arr.forEach((e) => {\n const sortedIdx = orderMap.get(getId(e))\n if (sortedIdx === undefined) {\n untagged.push(e)\n return\n }\n sorted[sortedIdx] = e\n })\n\n return [...sorted.filter((it) => it !== undefined), ...untagged]\n}\n"],
5
- "mappings": "AA6CO,SAAS,YAAkB,KAAU,OAAY,OAA4B;AAElF,QAAM,WAAW,oBAAI,IAAe;AACpC,QAAM,QAAQ,CAAC,GAAG,QAAQ,SAAS,IAAI,GAAG,GAAG,CAAC;AAE9C,QAAM,SAAc,CAAC;AACrB,QAAM,WAAgB,CAAC;AAEvB,MAAI,QAAQ,CAAC,MAAM;AACjB,UAAM,YAAY,SAAS,IAAI,MAAM,CAAC,CAAC;AACvC,QAAI,cAAc,QAAW;AAC3B,eAAS,KAAK,CAAC;AACf;AAAA,IACF;AACA,WAAO,SAAS,IAAI;AAAA,EACtB,CAAC;AAED,SAAO,CAAC,GAAG,OAAO,OAAO,CAAC,OAAO,OAAO,MAAS,GAAG,GAAG,QAAQ;AACjE;",
6
- "names": []
7
- }
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../src/consts/content-types.ts"],
4
- "sourcesContent": ["/**\n * Content types that we automatically add in the client\n */\nexport const CONTENT_TYPES = {\n 'multipart/form-data': 'Multipart Form',\n 'application/x-www-form-urlencoded': 'Form URL Encoded',\n 'application/octet-stream': 'Binary File',\n 'application/json': 'JSON',\n 'application/xml': 'XML',\n 'application/yaml': 'YAML',\n 'application/edn': 'EDN',\n 'other': 'Other',\n 'none': 'None',\n} as const\n"],
5
- "mappings": "AAGO,MAAM,gBAAgB;AAAA,EAC3B,uBAAuB;AAAA,EACvB,qCAAqC;AAAA,EACrC,4BAA4B;AAAA,EAC5B,oBAAoB;AAAA,EACpB,mBAAmB;AAAA,EACnB,oBAAoB;AAAA,EACpB,mBAAmB;AAAA,EACnB,SAAS;AAAA,EACT,QAAQ;AACV;",
6
- "names": []
7
- }
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../src/dom/freeze-element.ts"],
4
- "sourcesContent": ["/**\n * Scroll Freezing Utility\n * \"Freezes\" the scroll position of an element, so that it doesn't move when the rest of the content changes\n *\n * @example\n * const unfreeze = freezeElement(document.querySelector('#your-element'))\n * ... content changes ...\n * unfreeze()\n */\nexport const freezeElement = (element: HTMLElement) => {\n if (!element) {\n return () => null\n }\n\n // Get initial position relative to viewport\n const rect = element.getBoundingClientRect()\n const initialViewportTop = rect.top\n let rafId: number | null = null\n\n // Create mutation observer to watch for DOM changes\n const observer = new MutationObserver((mutations) => {\n // Only process if we have mutations that might affect layout\n const shouldProcess = mutations.some(\n (mutation) =>\n mutation.type === 'childList' ||\n (mutation.type === 'attributes' && (mutation.attributeName === 'style' || mutation.attributeName === 'class')),\n )\n\n if (!shouldProcess) {\n return\n }\n\n // Cancel any pending animation frame\n if (rafId !== null) {\n cancelAnimationFrame(rafId)\n }\n\n // Schedule the scroll adjustment for the next frame\n rafId = requestAnimationFrame(() => {\n const newRect = element.getBoundingClientRect()\n const currentViewportTop = newRect.top\n\n // If element has moved from its initial viewport position\n if (currentViewportTop !== initialViewportTop) {\n // Calculate how far it moved\n const diff = currentViewportTop - initialViewportTop\n // Adjust scroll to maintain position\n window.scrollBy(0, diff)\n }\n rafId = null\n })\n })\n\n // Start observing with more specific configuration\n observer.observe(document.body, {\n childList: true,\n subtree: true,\n attributes: true,\n attributeFilter: ['style', 'class'],\n characterData: false,\n })\n\n // Return function to stop maintaining position\n return () => {\n if (rafId !== null) {\n cancelAnimationFrame(rafId)\n }\n observer.disconnect()\n }\n}\n"],
5
- "mappings": "AASO,MAAM,gBAAgB,CAAC,YAAyB;AACrD,MAAI,CAAC,SAAS;AACZ,WAAO,MAAM;AAAA,EACf;AAGA,QAAM,OAAO,QAAQ,sBAAsB;AAC3C,QAAM,qBAAqB,KAAK;AAChC,MAAI,QAAuB;AAG3B,QAAM,WAAW,IAAI,iBAAiB,CAAC,cAAc;AAEnD,UAAM,gBAAgB,UAAU;AAAA,MAC9B,CAAC,aACC,SAAS,SAAS,eACjB,SAAS,SAAS,iBAAiB,SAAS,kBAAkB,WAAW,SAAS,kBAAkB;AAAA,IACzG;AAEA,QAAI,CAAC,eAAe;AAClB;AAAA,IACF;AAGA,QAAI,UAAU,MAAM;AAClB,2BAAqB,KAAK;AAAA,IAC5B;AAGA,YAAQ,sBAAsB,MAAM;AAClC,YAAM,UAAU,QAAQ,sBAAsB;AAC9C,YAAM,qBAAqB,QAAQ;AAGnC,UAAI,uBAAuB,oBAAoB;AAE7C,cAAM,OAAO,qBAAqB;AAElC,eAAO,SAAS,GAAG,IAAI;AAAA,MACzB;AACA,cAAQ;AAAA,IACV,CAAC;AAAA,EACH,CAAC;AAGD,WAAS,QAAQ,SAAS,MAAM;AAAA,IAC9B,WAAW;AAAA,IACX,SAAS;AAAA,IACT,YAAY;AAAA,IACZ,iBAAiB,CAAC,SAAS,OAAO;AAAA,IAClC,eAAe;AAAA,EACjB,CAAC;AAGD,SAAO,MAAM;AACX,QAAI,UAAU,MAAM;AAClB,2BAAqB,KAAK;AAAA,IAC5B;AACA,aAAS,WAAW;AAAA,EACtB;AACF;",
6
- "names": []
7
- }
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../src/dom/scroll-to-id.ts"],
4
- "sourcesContent": ["/**\n * Tiny wrapper around the scrollIntoView API\n *\n * Also focuses the element if the focus flag is true\n */\nexport const scrollToId = (id: string, focus?: boolean): void => {\n const scrollToElement = (element: HTMLElement) => {\n element.scrollIntoView()\n if (focus) {\n element.focus()\n }\n }\n\n // Try to find the element immediately\n const element = document.getElementById(id)\n if (element) {\n scrollToElement(element)\n return\n }\n\n /** Try to find the element for up to 1 second\n * allowing it to render for instance in markdown heading usage\n */\n const stopTime = Date.now() + 1000\n\n const tryScroll = (): void => {\n const element = document.getElementById(id)\n if (element) {\n scrollToElement(element)\n return\n }\n\n if (Date.now() < stopTime) {\n requestAnimationFrame(tryScroll)\n }\n }\n\n // Start the retry process if the element doesn't exist yet\n requestAnimationFrame(tryScroll)\n}\n"],
5
- "mappings": "AAKO,MAAM,aAAa,CAAC,IAAY,UAA0B;AAC/D,QAAM,kBAAkB,CAACA,aAAyB;AAChD,IAAAA,SAAQ,eAAe;AACvB,QAAI,OAAO;AACT,MAAAA,SAAQ,MAAM;AAAA,IAChB;AAAA,EACF;AAGA,QAAM,UAAU,SAAS,eAAe,EAAE;AAC1C,MAAI,SAAS;AACX,oBAAgB,OAAO;AACvB;AAAA,EACF;AAKA,QAAM,WAAW,KAAK,IAAI,IAAI;AAE9B,QAAM,YAAY,MAAY;AAC5B,UAAMA,WAAU,SAAS,eAAe,EAAE;AAC1C,QAAIA,UAAS;AACX,sBAAgBA,QAAO;AACvB;AAAA,IACF;AAEA,QAAI,KAAK,IAAI,IAAI,UAAU;AACzB,4BAAsB,SAAS;AAAA,IACjC;AAAA,EACF;AAGA,wBAAsB,SAAS;AACjC;",
6
- "names": ["element"]
7
- }