@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 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
@@ -213,4 +213,6 @@ npm run preview
213
213
 
214
214
  ## License
215
215
 
216
- ISC
216
+ MIT © [Zoltan Radics](https://github.com/zoltanradics)
217
+
218
+ Made with ❤️ by [Zoltan Radics](https://github.com/zoltanradics)
package/dist/index.d.ts CHANGED
@@ -1,5 +1,3 @@
1
- export declare function asyncScriptLoader(baseUrl: string, queryParamObject: {
2
- [key: string]: string;
3
- }): Promise<void>;
1
+ export declare function asyncScriptLoader(baseUrl: string, queryParamObject: Record<string, string>): Promise<void>;
4
2
 
5
3
  export { }
package/dist/index.es.js CHANGED
@@ -1,58 +1,63 @@
1
- function y(t, e) {
2
- if (typeof t != "string")
3
- return Promise.reject(new TypeError("baseUrl must be a string"));
4
- if (t.trim() === "")
5
- return Promise.reject(new Error("baseUrl cannot be empty"));
6
- if (t.startsWith("http") || t.startsWith("https"))
7
- try {
8
- new URL(t);
9
- } catch {
10
- return Promise.reject(new Error("baseUrl must be a valid URL"));
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 Promise.reject(new TypeError("queryParamObject must be an object"));
14
+ return { valid: !1, error: new TypeError("queryParamsObject must be an object") };
14
15
  if (typeof e != "object" || Array.isArray(e))
15
- return Promise.reject(new TypeError("queryParamObject must be a plain object"));
16
- for (const n of Object.keys(e))
17
- if (typeof e[n] != "string")
18
- return Promise.reject(new TypeError(`queryParamObject value for key "${n}" must be a string`));
19
- const c = 2e3, i = p(t, e), r = document.createElement("script");
20
- r.src = i, r.async = !0;
21
- const o = document.getElementsByTagName("head")[0];
22
- return o ? (o.insertAdjacentElement("beforeend", r), new Promise(function(n, u) {
23
- const a = setTimeout(() => {
24
- s(), n(), console.warn(`${i} was loading for too long time.`);
25
- }, c), s = () => {
26
- clearTimeout(a), r.removeEventListener("load", f), r.removeEventListener("error", m);
27
- }, f = function(d) {
28
- s(), n();
29
- }, m = function(d) {
30
- s(), u(new Error(`Failed to load ${i}.`));
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
- r.addEventListener("load", f), r.addEventListener("error", m);
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 p(t, e) {
36
- if (typeof t != "string")
37
- throw new TypeError("baseUrl must be a string");
38
- if (t.trim() === "")
39
- throw new Error("baseUrl cannot be empty");
40
- if (e == null)
41
- throw new TypeError("queryParamsObject must be an object");
42
- if (typeof e != "object" || Array.isArray(e))
43
- throw new TypeError("queryParamsObject must be a plain object");
44
- for (const o of Object.keys(e))
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 `${t}${r}`;
59
+ return `${n}${t}`;
55
60
  }
56
61
  export {
57
- y as asyncScriptLoader
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(t,e){if(typeof t!="string")return Promise.reject(new TypeError("baseUrl must be a string"));if(t.trim()==="")return Promise.reject(new Error("baseUrl cannot be empty"));if(t.startsWith("http")||t.startsWith("https"))try{new URL(t)}catch{return Promise.reject(new Error("baseUrl must be a valid URL"))}if(e==null)return Promise.reject(new TypeError("queryParamObject must be an object"));if(typeof e!="object"||Array.isArray(e))return Promise.reject(new TypeError("queryParamObject must be a plain object"));for(const n of Object.keys(e))if(typeof e[n]!="string")return Promise.reject(new TypeError(`queryParamObject value for key "${n}" must be a string`));const f=2e3,c=y(t,e),r=document.createElement("script");r.src=c,r.async=!0;const o=document.getElementsByTagName("head")[0];return o?(o.insertAdjacentElement("beforeend",r),new Promise(function(n,d){const a=setTimeout(()=>{u(),n(),console.warn(`${c} was loading for too long time.`)},f),u=()=>{clearTimeout(a),r.removeEventListener("load",p),r.removeEventListener("error",m)},p=function(l){u(),n()},m=function(l){u(),d(new Error(`Failed to load ${c}.`))};r.addEventListener("load",p),r.addEventListener("error",m)})):Promise.reject(new Error("No <head> element found in document"))}function y(t,e){if(typeof t!="string")throw new TypeError("baseUrl must be a string");if(t.trim()==="")throw new Error("baseUrl cannot be empty");if(e==null)throw new TypeError("queryParamsObject must be an object");if(typeof e!="object"||Array.isArray(e))throw new TypeError("queryParamsObject must be a plain object");for(const o of Object.keys(e))if(typeof e[o]!="string")throw new TypeError(`queryParamsObject value for key "${o}" must be a string`);const f=Object.keys(e);if(f.length===0)return t;const c=t.includes("?")?"&":"?",r=f.reduce((o,n,d)=>{const a=encodeURIComponent(n),u=encodeURIComponent(e[n]);return`${o}${d===0?c:"&"}${a}=${u}`},"");return`${t}${r}`}i.asyncScriptLoader=s,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})}));
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.1",
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": "ISC",
41
+ "license": "MIT",
42
42
  "repository": {
43
43
  "type": "git",
44
44
  "url": "git+https://github.com/zoltanradics/async-script-loader.git"