@putkoff/abstract-utilities 0.1.173 → 0.1.174
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/dist/esm/index.js
CHANGED
|
@@ -50,23 +50,16 @@ function callStorage(method, ...args) {
|
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
52
|
/**
|
|
53
|
-
* Safely call localStorage
|
|
54
|
-
*
|
|
55
|
-
* @param storageName "localStorage" or "sessionStorage"
|
|
56
|
-
* @param method one of the Storage methods, e.g. "getItem", "setItem", etc.
|
|
57
|
-
* @param args arguments to pass along (e.g. key, value)
|
|
58
|
-
* @returns whatever the underlying method returns, or undefined if unavailable/fails
|
|
53
|
+
* Safely call storage methods (`localStorage` or `sessionStorage`) without blowing up.
|
|
54
|
+
* Returns `undefined` on any error.
|
|
59
55
|
*/
|
|
60
56
|
function safeStorage(storageName, method, ...args) {
|
|
61
57
|
try {
|
|
62
|
-
const
|
|
63
|
-
if (!
|
|
64
|
-
return undefined;
|
|
65
|
-
const fn = storage[method];
|
|
66
|
-
if (typeof fn !== "function")
|
|
58
|
+
const store = safeGlobalProp(storageName);
|
|
59
|
+
if (!store || typeof store[method] !== "function")
|
|
67
60
|
return undefined;
|
|
68
61
|
// @ts-ignore
|
|
69
|
-
return
|
|
62
|
+
return store[method](...args);
|
|
70
63
|
}
|
|
71
64
|
catch (_a) {
|
|
72
65
|
return undefined;
|
|
@@ -74,16 +67,15 @@ function safeStorage(storageName, method, ...args) {
|
|
|
74
67
|
}
|
|
75
68
|
|
|
76
69
|
/**
|
|
77
|
-
* Safely walk `
|
|
78
|
-
*
|
|
79
|
-
* @param rootName "window" or "document"
|
|
80
|
-
* @param path sequence of property names, e.g. ["location","host"] or ["baseURI"]
|
|
70
|
+
* Safely walk `globalThis` (or window/document) by a chain of property names.
|
|
71
|
+
* Returns `undefined` if any step is missing.
|
|
81
72
|
*/
|
|
82
|
-
function safeGlobalProp(
|
|
83
|
-
let obj = globalThis
|
|
73
|
+
function safeGlobalProp(...path) {
|
|
74
|
+
let obj = globalThis;
|
|
84
75
|
for (const key of path) {
|
|
85
|
-
if (obj == null)
|
|
76
|
+
if (obj == null || typeof obj !== "object" || !(key in obj)) {
|
|
86
77
|
return undefined;
|
|
78
|
+
}
|
|
87
79
|
obj = obj[key];
|
|
88
80
|
}
|
|
89
81
|
return obj;
|
|
@@ -1140,39 +1132,38 @@ function alertit(obj = null) {
|
|
|
1140
1132
|
let _cachedConfig = null;
|
|
1141
1133
|
function loadConfig() {
|
|
1142
1134
|
return __awaiter(this, arguments, void 0, function* (filePath = null) {
|
|
1143
|
-
|
|
1144
|
-
// 1)
|
|
1145
|
-
|
|
1135
|
+
const relativePath = filePath || "config.json";
|
|
1136
|
+
// 1) Resolve URL against document.baseURI if available
|
|
1137
|
+
let configUrl = relativePath;
|
|
1138
|
+
const baseURI = safeGlobalProp("document", "baseURI");
|
|
1139
|
+
if (baseURI) {
|
|
1140
|
+
try {
|
|
1141
|
+
configUrl = new URL(relativePath, baseURI).href;
|
|
1142
|
+
}
|
|
1143
|
+
catch (_a) {
|
|
1144
|
+
// ignore—keep configUrl = relativePath
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
1147
|
+
// 2) Return cache if we’ve already fetched once
|
|
1148
|
+
if (_cachedConfig) {
|
|
1146
1149
|
return _cachedConfig;
|
|
1147
|
-
// 2) Build a base for URL.resolve:
|
|
1148
|
-
// try document.baseURI → window.location.href → "/"
|
|
1149
|
-
const docBase = safeGlobalProp("document", "baseURI");
|
|
1150
|
-
const winHref = typeof window !== "undefined" ? window.location.href : undefined;
|
|
1151
|
-
const base = (_a = docBase !== null && docBase !== void 0 ? docBase : winHref) !== null && _a !== void 0 ? _a : "/";
|
|
1152
|
-
// 3) Compute configUrl
|
|
1153
|
-
const relative = filePath || "config.json";
|
|
1154
|
-
let configUrl;
|
|
1155
|
-
try {
|
|
1156
|
-
configUrl = new URL(relative, base).href;
|
|
1157
1150
|
}
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1151
|
+
// 3) Bail out in non-browser if fetch isn’t present
|
|
1152
|
+
const fetchFn = safeGlobalProp("fetch");
|
|
1153
|
+
if (typeof fetchFn !== "function") {
|
|
1161
1154
|
return {};
|
|
1162
1155
|
}
|
|
1163
|
-
// 4)
|
|
1156
|
+
// 4) Try to fetch + parse, swallowing any errors
|
|
1164
1157
|
try {
|
|
1165
|
-
const res = yield
|
|
1166
|
-
if (!res.ok) {
|
|
1167
|
-
console.warn(`[loadConfig] fetch failed ${res.status} ${res.statusText} @`, configUrl);
|
|
1158
|
+
const res = yield fetchFn(configUrl).catch(() => null);
|
|
1159
|
+
if (!res || !res.ok) {
|
|
1168
1160
|
return {};
|
|
1169
1161
|
}
|
|
1170
|
-
|
|
1171
|
-
_cachedConfig = json;
|
|
1172
|
-
return
|
|
1162
|
+
// parse JSON
|
|
1163
|
+
_cachedConfig = (yield res.json());
|
|
1164
|
+
return _cachedConfig;
|
|
1173
1165
|
}
|
|
1174
|
-
catch (
|
|
1175
|
-
console.warn(`[loadConfig] network/json error for ${configUrl}:`, err);
|
|
1166
|
+
catch (_b) {
|
|
1176
1167
|
return {};
|
|
1177
1168
|
}
|
|
1178
1169
|
});
|