@zoltanradics/async-script-loader 1.0.1 → 1.0.2
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/LICENSE +21 -0
- package/README.md +3 -1
- package/dist/index.d.ts +1 -3
- package/dist/index.es.js +54 -49
- package/dist/index.umd.js +1 -1
- package/package.json +2 -2
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Zoltan Radics
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.es.js
CHANGED
|
@@ -1,58 +1,63 @@
|
|
|
1
|
-
function
|
|
2
|
-
if (typeof
|
|
3
|
-
return
|
|
4
|
-
if (
|
|
5
|
-
return
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
1
|
+
function p(n, e) {
|
|
2
|
+
if (typeof n != "string")
|
|
3
|
+
return { valid: !1, error: new TypeError("baseUrl must be a string") };
|
|
4
|
+
if (n.trim() === "")
|
|
5
|
+
return { valid: !1, error: new Error("baseUrl cannot be empty") };
|
|
6
|
+
try {
|
|
7
|
+
const r = new URL(n, window.location.origin);
|
|
8
|
+
if (r.protocol !== "http:" && r.protocol !== "https:")
|
|
9
|
+
return { valid: !1, error: new Error("baseUrl must use http: or https: protocol") };
|
|
10
|
+
} catch {
|
|
11
|
+
return { valid: !1, error: new Error("baseUrl must be a valid URL") };
|
|
12
|
+
}
|
|
12
13
|
if (e == null)
|
|
13
|
-
return
|
|
14
|
+
return { valid: !1, error: new TypeError("queryParamsObject must be an object") };
|
|
14
15
|
if (typeof e != "object" || Array.isArray(e))
|
|
15
|
-
return
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
16
|
+
return { valid: !1, error: new TypeError("queryParamsObject must be a plain object") };
|
|
17
|
+
const o = e;
|
|
18
|
+
for (const r of Object.keys(o)) {
|
|
19
|
+
if (r.trim() === "")
|
|
20
|
+
return { valid: !1, error: new Error("queryParamsObject keys cannot be empty") };
|
|
21
|
+
if (typeof o[r] != "string")
|
|
22
|
+
return { valid: !1, error: new TypeError(`queryParamsObject value for key "${r}" must be a string`) };
|
|
23
|
+
}
|
|
24
|
+
return { valid: !0 };
|
|
25
|
+
}
|
|
26
|
+
function E(n, e) {
|
|
27
|
+
if (typeof window > "u")
|
|
28
|
+
return Promise.reject(new Error("asyncScriptLoader requires a browser environment"));
|
|
29
|
+
const o = p(n, e);
|
|
30
|
+
if (!o.valid)
|
|
31
|
+
return Promise.reject(o.error);
|
|
32
|
+
const r = 2e3, i = v(n, e), t = document.createElement("script");
|
|
33
|
+
t.src = i, t.async = !0;
|
|
34
|
+
const s = document.getElementsByTagName("head")[0];
|
|
35
|
+
return s ? (s.insertAdjacentElement("beforeend", t), new Promise(function(c, d) {
|
|
36
|
+
const a = (f = !1) => {
|
|
37
|
+
clearTimeout(u), t.removeEventListener("load", l), t.removeEventListener("error", m), f && t.parentNode && t.parentNode.removeChild(t);
|
|
38
|
+
}, u = setTimeout(() => {
|
|
39
|
+
a(!0), d(new Error(`Script loading timed out after ${r}ms: ${i}`));
|
|
40
|
+
}, r), l = function(f) {
|
|
41
|
+
a(), c();
|
|
42
|
+
}, m = function(f) {
|
|
43
|
+
a(!0), d(new Error(`Failed to load ${i}.`));
|
|
31
44
|
};
|
|
32
|
-
|
|
45
|
+
t.addEventListener("load", l), t.addEventListener("error", m);
|
|
33
46
|
})) : Promise.reject(new Error("No <head> element found in document"));
|
|
34
47
|
}
|
|
35
|
-
function
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
if (
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
if (typeof e[o] != "string")
|
|
46
|
-
throw new TypeError(`queryParamsObject value for key "${o}" must be a string`);
|
|
47
|
-
const c = Object.keys(e);
|
|
48
|
-
if (c.length === 0)
|
|
49
|
-
return t;
|
|
50
|
-
const i = t.includes("?") ? "&" : "?", r = c.reduce((o, n, u) => {
|
|
51
|
-
const a = encodeURIComponent(n), s = encodeURIComponent(e[n]);
|
|
52
|
-
return `${o}${u === 0 ? i : "&"}${a}=${s}`;
|
|
48
|
+
function v(n, e) {
|
|
49
|
+
const o = p(n, e);
|
|
50
|
+
if (!o.valid)
|
|
51
|
+
throw o.error;
|
|
52
|
+
const r = Object.keys(e);
|
|
53
|
+
if (r.length === 0)
|
|
54
|
+
return n;
|
|
55
|
+
const i = n.includes("?") ? "&" : "?", t = r.reduce((s, c, d) => {
|
|
56
|
+
const a = encodeURIComponent(c), u = encodeURIComponent(e[c]);
|
|
57
|
+
return `${s}${d === 0 ? i : "&"}${a}=${u}`;
|
|
53
58
|
}, "");
|
|
54
|
-
return `${
|
|
59
|
+
return `${n}${t}`;
|
|
55
60
|
}
|
|
56
61
|
export {
|
|
57
|
-
|
|
62
|
+
E as asyncScriptLoader
|
|
58
63
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(i,s){typeof exports=="object"&&typeof module<"u"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(i=typeof globalThis<"u"?globalThis:i||self,s(i.AsyncScriptLoader={}))})(this,(function(i){"use strict";function s(
|
|
1
|
+
(function(i,s){typeof exports=="object"&&typeof module<"u"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(i=typeof globalThis<"u"?globalThis:i||self,s(i.AsyncScriptLoader={}))})(this,(function(i){"use strict";function s(n,e){if(typeof n!="string")return{valid:!1,error:new TypeError("baseUrl must be a string")};if(n.trim()==="")return{valid:!1,error:new Error("baseUrl cannot be empty")};try{const r=new URL(n,window.location.origin);if(r.protocol!=="http:"&&r.protocol!=="https:")return{valid:!1,error:new Error("baseUrl must use http: or https: protocol")}}catch{return{valid:!1,error:new Error("baseUrl must be a valid URL")}}if(e==null)return{valid:!1,error:new TypeError("queryParamsObject must be an object")};if(typeof e!="object"||Array.isArray(e))return{valid:!1,error:new TypeError("queryParamsObject must be a plain object")};const o=e;for(const r of Object.keys(o)){if(r.trim()==="")return{valid:!1,error:new Error("queryParamsObject keys cannot be empty")};if(typeof o[r]!="string")return{valid:!1,error:new TypeError(`queryParamsObject value for key "${r}" must be a string`)}}return{valid:!0}}function y(n,e){if(typeof window>"u")return Promise.reject(new Error("asyncScriptLoader requires a browser environment"));const o=s(n,e);if(!o.valid)return Promise.reject(o.error);const r=2e3,a=E(n,e),t=document.createElement("script");t.src=a,t.async=!0;const d=document.getElementsByTagName("head")[0];return d?(d.insertAdjacentElement("beforeend",t),new Promise(function(u,l){const c=(m=!1)=>{clearTimeout(f),t.removeEventListener("load",p),t.removeEventListener("error",v),m&&t.parentNode&&t.parentNode.removeChild(t)},f=setTimeout(()=>{c(!0),l(new Error(`Script loading timed out after ${r}ms: ${a}`))},r),p=function(m){c(),u()},v=function(m){c(!0),l(new Error(`Failed to load ${a}.`))};t.addEventListener("load",p),t.addEventListener("error",v)})):Promise.reject(new Error("No <head> element found in document"))}function E(n,e){const o=s(n,e);if(!o.valid)throw o.error;const r=Object.keys(e);if(r.length===0)return n;const a=n.includes("?")?"&":"?",t=r.reduce((d,u,l)=>{const c=encodeURIComponent(u),f=encodeURIComponent(e[u]);return`${d}${l===0?a:"&"}${c}=${f}`},"");return`${n}${t}`}i.asyncScriptLoader=y,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zoltanradics/async-script-loader",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Async javascript loader",
|
|
5
5
|
"main": "./dist/index.umd.js",
|
|
6
6
|
"module": "./dist/index.es.js",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"cdn"
|
|
39
39
|
],
|
|
40
40
|
"author": "Zoltan Radics",
|
|
41
|
-
"license": "
|
|
41
|
+
"license": "MIT",
|
|
42
42
|
"repository": {
|
|
43
43
|
"type": "git",
|
|
44
44
|
"url": "git+https://github.com/zoltanradics/async-script-loader.git"
|