@putkoff/abstract-utilities 0.1.172 → 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/cjs/index.js +35 -42
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.js +35 -42
- package/dist/esm/index.js.map +1 -1
- package/dist/functions/config_utils/imports.d.ts +1 -1
- package/dist/functions/config_utils/src/config_utils.d.ts +3 -3
- package/dist/functions/safe_utils/src/safe_globals.d.ts +3 -5
- package/dist/functions/safe_utils/src/safe_storage.d.ts +3 -7
- package/package.json +1 -1
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,37 +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
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
console.warn(`[loadConfig] failed to resolve URL for "${relativePath}" against "${base}":`, err);
|
|
1155
|
-
configUrl = relativePath; // try a bare-relative fetch
|
|
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
|
+
}
|
|
1156
1146
|
}
|
|
1157
|
-
//
|
|
1147
|
+
// 2) Return cache if we’ve already fetched once
|
|
1158
1148
|
if (_cachedConfig) {
|
|
1159
1149
|
return _cachedConfig;
|
|
1160
1150
|
}
|
|
1161
|
-
//
|
|
1151
|
+
// 3) Bail out in non-browser if fetch isn’t present
|
|
1152
|
+
const fetchFn = safeGlobalProp("fetch");
|
|
1153
|
+
if (typeof fetchFn !== "function") {
|
|
1154
|
+
return {};
|
|
1155
|
+
}
|
|
1156
|
+
// 4) Try to fetch + parse, swallowing any errors
|
|
1162
1157
|
try {
|
|
1163
|
-
const res = yield
|
|
1164
|
-
if (!res.ok) {
|
|
1165
|
-
console.warn(`[loadConfig] server returned ${res.status} when fetching ${configUrl}`);
|
|
1158
|
+
const res = yield fetchFn(configUrl).catch(() => null);
|
|
1159
|
+
if (!res || !res.ok) {
|
|
1166
1160
|
return {};
|
|
1167
1161
|
}
|
|
1168
|
-
|
|
1169
|
-
_cachedConfig = json;
|
|
1170
|
-
return
|
|
1162
|
+
// parse JSON
|
|
1163
|
+
_cachedConfig = (yield res.json());
|
|
1164
|
+
return _cachedConfig;
|
|
1171
1165
|
}
|
|
1172
|
-
catch (
|
|
1173
|
-
console.warn(`[loadConfig] error fetching/parsing ${configUrl}:`, err);
|
|
1166
|
+
catch (_b) {
|
|
1174
1167
|
return {};
|
|
1175
1168
|
}
|
|
1176
1169
|
});
|