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