@wevu/web-apis 1.1.0 → 1.1.1

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/README.md CHANGED
@@ -33,7 +33,7 @@ import { installRequestGlobals } from '@wevu/web-apis'
33
33
 
34
34
  installRequestGlobals()
35
35
 
36
- const response = await fetch('https://example.com/data')
36
+ const response = await fetch('https://request-globals.invalid/data')
37
37
  console.log(await response.json())
38
38
  ```
39
39
 
package/dist/index.mjs CHANGED
@@ -6,6 +6,15 @@ import { URLPolyfill, URLSearchParamsPolyfill } from "./url.mjs";
6
6
  import { BlobPolyfill, FormDataPolyfill } from "./web.mjs";
7
7
  import { XMLHttpRequestPolyfill } from "./xhr.mjs";
8
8
  //#region src/index.ts
9
+ function hasUsableConstructor(value, args = []) {
10
+ if (typeof value !== "function") return false;
11
+ try {
12
+ Reflect.construct(value, args);
13
+ return true;
14
+ } catch {
15
+ return false;
16
+ }
17
+ }
9
18
  function installSingleTarget(host, target) {
10
19
  if (target === "fetch") {
11
20
  if (typeof host.fetch !== "function") host.fetch = fetch;
@@ -34,10 +43,10 @@ function installSingleTarget(host, target) {
34
43
  if (target === "XMLHttpRequest" && typeof host.XMLHttpRequest !== "function") host.XMLHttpRequest = XMLHttpRequestPolyfill;
35
44
  }
36
45
  function installUrlGlobals(host) {
37
- if (typeof host.URL !== "function") host.URL = URLPolyfill;
38
- if (typeof host.URLSearchParams !== "function") host.URLSearchParams = URLSearchParamsPolyfill;
39
- if (typeof host.Blob !== "function") host.Blob = BlobPolyfill;
40
- if (typeof host.FormData !== "function") host.FormData = FormDataPolyfill;
46
+ if (!hasUsableConstructor(host.URL, ["https://request-globals.invalid"])) host.URL = URLPolyfill;
47
+ if (!hasUsableConstructor(host.URLSearchParams, ["client=graphql-request"])) host.URLSearchParams = URLSearchParamsPolyfill;
48
+ if (!hasUsableConstructor(host.Blob)) host.Blob = BlobPolyfill;
49
+ if (!hasUsableConstructor(host.FormData)) host.FormData = FormDataPolyfill;
41
50
  }
42
51
  function installGlobalBindingIfNeeded(host, target) {
43
52
  const value = host[target];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@wevu/web-apis",
3
3
  "type": "module",
4
- "version": "1.1.0",
4
+ "version": "1.1.1",
5
5
  "description": "Web API polyfills and global installers for mini-program runtimes",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",
@@ -66,7 +66,7 @@
66
66
  "node": "^20.19.0 || >=22.12.0"
67
67
  },
68
68
  "dependencies": {
69
- "@wevu/api": "0.2.2"
69
+ "@wevu/api": "0.2.3"
70
70
  },
71
71
  "publishConfig": {
72
72
  "access": "public",