@scalar/helpers 0.2.8 → 0.2.10

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,17 @@
1
1
  # @scalar/helpers
2
2
 
3
+ ## 0.2.10
4
+
5
+ ### Patch Changes
6
+
7
+ - [#7963](https://github.com/scalar/scalar/pull/7963): feat: unify is-object helpers
8
+
9
+ ## 0.2.9
10
+
11
+ ### Patch Changes
12
+
13
+ - [#7894](https://github.com/scalar/scalar/pull/7894): fix: the import and export of redirect to proxy
14
+
3
15
  ## 0.2.8
4
16
 
5
17
  ### Patch Changes
@@ -1,17 +1,19 @@
1
1
  /**
2
- * Returns true if the provided value is a plain object
3
- * (i.e. not null, not an array, and typeof value is "object").
2
+ * Returns true if the provided value is a record object
3
+ * (i.e. not null, not an array, and has an actual object as the prototype).
4
4
  *
5
- * This is a type guard useful for narrowing types in TypeScript.
5
+ * Differs from the previous isObject in that it returns false for Date,
6
+ * RegExp, Error, Map, Set, WeakMap, WeakSet, Promise, and other non-plain objects.
6
7
  *
7
8
  * Examples:
8
- * isObject({}) // true
9
- * isObject({ a: 1 }) // true
10
- * isObject([]) // false (Array)
11
- * isObject(null) // false
12
- * isObject(123) // false
13
- * isObject('string') // false
14
- * isObject(new Date()) // true (note: Date is technically an object)
9
+ * isObject({}) // true
10
+ * isObject({ a: 1 }) // true
11
+ * isObject([]) // false (Array)
12
+ * isObject(null) // false
13
+ * isObject(123) // false
14
+ * isObject('string') // false
15
+ * isObject(new Error('test')) // false
16
+ * isObject(new Date()) // false
15
17
  * isObject(Object.create(null)) // true
16
18
  */
17
19
  export declare const isObject: (value: unknown) => value is Record<string, unknown>;
@@ -1 +1 @@
1
- {"version":3,"file":"is-object.d.ts","sourceRoot":"","sources":["../../src/object/is-object.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,QAAQ,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAExE,CAAA"}
1
+ {"version":3,"file":"is-object.d.ts","sourceRoot":"","sources":["../../src/object/is-object.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,QAAQ,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAOxE,CAAA"}
@@ -1,5 +1,9 @@
1
1
  const isObject = (value) => {
2
- return typeof value === "object" && value !== null && !Array.isArray(value);
2
+ if (value === null || typeof value !== "object") {
3
+ return false;
4
+ }
5
+ const proto = Object.getPrototypeOf(value);
6
+ return proto === Object.prototype || proto === null;
3
7
  };
4
8
  export {
5
9
  isObject
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/object/is-object.ts"],
4
- "sourcesContent": ["/**\n * Returns true if the provided value is a plain object\n * (i.e. not null, not an array, and typeof value is \"object\").\n *\n * This is a type guard useful for narrowing types in TypeScript.\n *\n * Examples:\n * isObject({}) // true\n * isObject({ a: 1 }) // true\n * isObject([]) // false (Array)\n * isObject(null) // false\n * isObject(123) // false\n * isObject('string') // false\n * isObject(new Date()) // true (note: Date is technically an object)\n * isObject(Object.create(null)) // true\n */\nexport const isObject = (value: unknown): value is Record<string, unknown> => {\n return typeof value === 'object' && value !== null && !Array.isArray(value)\n}\n"],
5
- "mappings": "AAgBO,MAAM,WAAW,CAAC,UAAqD;AAC5E,SAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,KAAK;AAC5E;",
4
+ "sourcesContent": ["/**\n * Returns true if the provided value is a record object\n * (i.e. not null, not an array, and has an actual object as the prototype).\n *\n * Differs from the previous isObject in that it returns false for Date,\n * RegExp, Error, Map, Set, WeakMap, WeakSet, Promise, and other non-plain objects.\n *\n * Examples:\n * isObject({}) // true\n * isObject({ a: 1 }) // true\n * isObject([]) // false (Array)\n * isObject(null) // false\n * isObject(123) // false\n * isObject('string') // false\n * isObject(new Error('test')) // false\n * isObject(new Date()) // false\n * isObject(Object.create(null)) // true\n */\nexport const isObject = (value: unknown): value is Record<string, unknown> => {\n if (value === null || typeof value !== 'object') {\n return false\n }\n\n const proto = Object.getPrototypeOf(value)\n return proto === Object.prototype || proto === null\n}\n"],
5
+ "mappings": "AAkBO,MAAM,WAAW,CAAC,UAAqD;AAC5E,MAAI,UAAU,QAAQ,OAAO,UAAU,UAAU;AAC/C,WAAO;AAAA,EACT;AAEA,QAAM,QAAQ,OAAO,eAAe,KAAK;AACzC,SAAO,UAAU,OAAO,aAAa,UAAU;AACjD;",
6
6
  "names": []
7
7
  }
@@ -0,0 +1,13 @@
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 const redirectToProxy: (proxyUrl?: string, url?: string) => string;
9
+ /**
10
+ * Returns false for requests to localhost, relative URLs, if no proxy is defined …
11
+ **/
12
+ export declare const shouldUseProxy: (proxyUrl?: string, url?: string) => url is string;
13
+ //# sourceMappingURL=redirect-to-proxy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redirect-to-proxy.d.ts","sourceRoot":"","sources":["../../src/url/redirect-to-proxy.ts"],"names":[],"mappings":"AAGA;;;;;;IAMI;AACJ,eAAO,MAAM,eAAe,GAAI,WAAW,MAAM,EAAE,MAAM,MAAM,KAAG,MAkCjE,CAAA;AAED;;IAEI;AACJ,eAAO,MAAM,cAAc,GAAI,WAAW,MAAM,EAAE,MAAM,MAAM,KAAG,GAAG,IAAI,MAkCvE,CAAA"}
@@ -0,0 +1,44 @@
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 ?? "";
7
+ }
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;
28
+ }
29
+ if (isLocalUrl(proxyUrl)) {
30
+ return true;
31
+ }
32
+ if (isLocalUrl(url)) {
33
+ return false;
34
+ }
35
+ return true;
36
+ } catch {
37
+ return false;
38
+ }
39
+ };
40
+ export {
41
+ redirectToProxy,
42
+ shouldUseProxy
43
+ };
44
+ //# sourceMappingURL=redirect-to-proxy.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/url/redirect-to-proxy.ts"],
4
+ "sourcesContent": ["import { isLocalUrl } from './is-local-url'\nimport { isRelativePath } from './is-relative-path'\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't look like a domain\n **/\nexport const redirectToProxy = (proxyUrl?: string, url?: string): string => {\n try {\n if (!shouldUseProxy(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't 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 * Returns false for requests to localhost, relative URLs, if no proxy is defined \u2026\n **/\nexport const shouldUseProxy = (proxyUrl?: string, url?: string): url is string => {\n try {\n // \u274C We don't have a proxy URL or the URL\n if (!proxyUrl || !url) {\n return false\n }\n\n // \u274C Request to relative URLs (won't 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's 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't 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,sBAAsB;AASxB,MAAM,kBAAkB,CAAC,UAAmB,QAAyB;AAC1E,MAAI;AACF,QAAI,CAAC,eAAe,UAAU,GAAG,GAAG;AAClC,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,UAAmB,QAAgC;AAChF,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
+ }
package/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "helpers",
15
15
  "js"
16
16
  ],
17
- "version": "0.2.8",
17
+ "version": "0.2.10",
18
18
  "engines": {
19
19
  "node": ">=20"
20
20
  },
@@ -88,7 +88,7 @@
88
88
  "CHANGELOG.md"
89
89
  ],
90
90
  "devDependencies": {
91
- "jsdom": "26.1.0",
91
+ "jsdom": "27.4.0",
92
92
  "vite": "^7.3.1",
93
93
  "vitest": "4.0.16",
94
94
  "@scalar/build-tooling": "0.4.1"