@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.
- package/CHANGELOG.md +13 -0
- package/dist/array/add-to-map-array.js +7 -8
- package/dist/array/is-defined.js +12 -5
- package/dist/array/sort-by-order.js +60 -18
- package/dist/consts/content-types.js +13 -14
- package/dist/dom/freeze-element.js +56 -42
- package/dist/dom/get-selector.d.ts +7 -0
- package/dist/dom/get-selector.d.ts.map +1 -0
- package/dist/dom/get-selector.js +31 -0
- package/dist/dom/scroll-to-id.js +33 -27
- package/dist/file/json2xml.js +79 -61
- package/dist/formatters/format-bytes.d.ts +7 -0
- package/dist/formatters/format-bytes.d.ts.map +1 -0
- package/dist/formatters/format-bytes.js +18 -0
- package/dist/formatters/format-milliseconds.d.ts +6 -0
- package/dist/formatters/format-milliseconds.d.ts.map +1 -0
- package/dist/formatters/format-milliseconds.js +10 -0
- package/dist/general/create-limiter.js +47 -31
- package/dist/general/debounce.js +86 -66
- package/dist/general/extract-config-secrets.js +29 -27
- package/dist/general/has-modifier.js +9 -8
- package/dist/general/is-mac-os.js +23 -21
- package/dist/http/can-method-have-body.js +4 -6
- package/dist/http/http-info.js +63 -61
- package/dist/http/http-methods.js +4 -7
- package/dist/http/http-status-codes.js +316 -320
- package/dist/http/is-http-method.js +3 -6
- package/dist/http/normalize-http-method.js +19 -19
- package/dist/json/escape-json-pointer.js +6 -5
- package/dist/json/parse-json-pointer-segments.js +11 -6
- package/dist/json/pretty-print-json.d.ts +9 -0
- package/dist/json/pretty-print-json.d.ts.map +1 -0
- package/dist/json/pretty-print-json.js +42 -0
- package/dist/json/unescape-json-pointer.js +7 -5
- package/dist/markdown/get-markdown-headings.d.ts +11 -0
- package/dist/markdown/get-markdown-headings.d.ts.map +1 -0
- package/dist/markdown/get-markdown-headings.js +99 -0
- package/dist/node/path.js +168 -138
- package/dist/object/get-value-at-path.js +17 -11
- package/dist/object/is-object.js +24 -10
- package/dist/object/local-storage.js +50 -42
- package/dist/object/object-entries.js +2 -5
- package/dist/object/object-keys.js +5 -5
- package/dist/object/object-replace.js +13 -12
- package/dist/object/omit-undefined-values.js +18 -18
- package/dist/object/prevent-pollution.js +27 -10
- package/dist/object/to-json-compatible.js +71 -62
- package/dist/queue/queue.js +103 -84
- package/dist/regex/find-variables.js +13 -8
- package/dist/regex/regex-helpers.js +17 -17
- package/dist/regex/replace-variables.js +21 -19
- package/dist/string/camel-to-title.js +10 -5
- package/dist/string/capitalize.js +5 -5
- package/dist/string/create-hash.js +19 -16
- package/dist/string/generate-hash.js +153 -127
- package/dist/string/iterate-title.js +13 -11
- package/dist/string/truncate.js +16 -9
- package/dist/testing/console-spies.js +19 -24
- package/dist/testing/measure.js +42 -19
- package/dist/testing/measure.test-d.js +12 -9
- package/dist/testing/sleep.js +5 -5
- package/dist/url/ensure-protocol.js +8 -10
- package/dist/url/extract-server-from-path.js +53 -28
- package/dist/url/is-local-url.js +24 -18
- package/dist/url/is-relative-path.js +15 -13
- package/dist/url/is-valid-url.js +17 -10
- package/dist/url/make-url-absolute.js +39 -28
- package/dist/url/merge-urls.js +74 -52
- package/dist/url/redirect-to-proxy.js +68 -39
- package/package.json +15 -9
- package/dist/array/add-to-map-array.js.map +0 -7
- package/dist/array/is-defined.js.map +0 -7
- package/dist/array/sort-by-order.js.map +0 -7
- package/dist/consts/content-types.js.map +0 -7
- package/dist/dom/freeze-element.js.map +0 -7
- package/dist/dom/scroll-to-id.js.map +0 -7
- package/dist/file/json2xml.js.map +0 -7
- package/dist/general/create-limiter.js.map +0 -7
- package/dist/general/debounce.js.map +0 -7
- package/dist/general/extract-config-secrets.js.map +0 -7
- package/dist/general/has-modifier.js.map +0 -7
- package/dist/general/is-mac-os.js.map +0 -7
- package/dist/http/can-method-have-body.js.map +0 -7
- package/dist/http/http-info.js.map +0 -7
- package/dist/http/http-methods.js.map +0 -7
- package/dist/http/http-status-codes.js.map +0 -7
- package/dist/http/is-http-method.js.map +0 -7
- package/dist/http/normalize-http-method.js.map +0 -7
- package/dist/json/escape-json-pointer.js.map +0 -7
- package/dist/json/parse-json-pointer-segments.js.map +0 -7
- package/dist/json/unescape-json-pointer.js.map +0 -7
- package/dist/node/path.js.map +0 -7
- package/dist/object/get-value-at-path.js.map +0 -7
- package/dist/object/is-object.js.map +0 -7
- package/dist/object/local-storage.js.map +0 -7
- package/dist/object/object-entries.js.map +0 -7
- package/dist/object/object-keys.js.map +0 -7
- package/dist/object/object-replace.js.map +0 -7
- package/dist/object/omit-undefined-values.js.map +0 -7
- package/dist/object/prevent-pollution.js.map +0 -7
- package/dist/object/to-json-compatible.js.map +0 -7
- package/dist/queue/queue.js.map +0 -7
- package/dist/regex/find-variables.js.map +0 -7
- package/dist/regex/regex-helpers.js.map +0 -7
- package/dist/regex/replace-variables.js.map +0 -7
- package/dist/string/camel-to-title.js.map +0 -7
- package/dist/string/capitalize.js.map +0 -7
- package/dist/string/create-hash.js.map +0 -7
- package/dist/string/generate-hash.js.map +0 -7
- package/dist/string/iterate-title.js.map +0 -7
- package/dist/string/truncate.js.map +0 -7
- package/dist/testing/console-spies.js.map +0 -7
- package/dist/testing/measure.js.map +0 -7
- package/dist/testing/measure.test-d.js.map +0 -7
- package/dist/testing/sleep.js.map +0 -7
- package/dist/url/ensure-protocol.js.map +0 -7
- package/dist/url/extract-server-from-path.js.map +0 -7
- package/dist/url/is-local-url.js.map +0 -7
- package/dist/url/is-relative-path.js.map +0 -7
- package/dist/url/is-valid-url.js.map +0 -7
- package/dist/url/make-url-absolute.js.map +0 -7
- package/dist/url/merge-urls.js.map +0 -7
- package/dist/url/redirect-to-proxy.js.map +0 -7
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
import { REGEX } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
package/dist/url/is-local-url.js
CHANGED
|
@@ -1,21 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
const
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
package/dist/url/is-valid-url.js
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
package/dist/url/merge-urls.js
CHANGED
|
@@ -1,56 +1,78 @@
|
|
|
1
|
-
import { REGEX } from
|
|
2
|
-
import { isRelativePath } from
|
|
3
|
-
import { ensureProtocol } from
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
|
2
|
-
import { isRelativePath } from
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
|
|
9
|
-
|
|
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
|
-
|
|
30
|
-
|
|
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
|
-
|
|
33
|
-
|
|
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.
|
|
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
|
-
"
|
|
103
|
-
"vitest": "4.0.16",
|
|
104
|
-
"@scalar/build-tooling": "0.5.0"
|
|
113
|
+
"vitest": "4.1.0"
|
|
105
114
|
},
|
|
106
115
|
"scripts": {
|
|
107
|
-
"build": "
|
|
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:
|
|
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
|
-
}
|