@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/cjs/index.js CHANGED
@@ -53,23 +53,16 @@ function callStorage(method, ...args) {
53
53
  }
54
54
  }
55
55
  /**
56
- * Safely call localStorage / sessionStorage / any Storage API.
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 storage = globalThis[storageName];
66
- if (!storage)
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 fn.apply(storage, args);
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 `window` or `document` to grab nested props without blowing up in Node.
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(rootName, ...path) {
86
- let obj = globalThis[rootName];
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,39 +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
- var _a;
1147
- // 1) If already cached, return it.
1148
- if (_cachedConfig)
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
+ }
1149
+ }
1150
+ // 2) Return cache if we’ve already fetched once
1151
+ if (_cachedConfig) {
1149
1152
  return _cachedConfig;
1150
- // 2) Build a base for URL.resolve:
1151
- // try document.baseURI → window.location.href → "/"
1152
- const docBase = safeGlobalProp("document", "baseURI");
1153
- const winHref = typeof window !== "undefined" ? window.location.href : undefined;
1154
- const base = (_a = docBase !== null && docBase !== void 0 ? docBase : winHref) !== null && _a !== void 0 ? _a : "/";
1155
- // 3) Compute configUrl
1156
- const relative = filePath || "config.json";
1157
- let configUrl;
1158
- try {
1159
- configUrl = new URL(relative, base).href;
1160
1153
  }
1161
- catch (err) {
1162
- console.warn(`[loadConfig] invalid URL with base=${base} and file=${relative}:`, err);
1163
- // abort early in Node / bad base
1154
+ // 3) Bail out in non-browser if fetch isn’t present
1155
+ const fetchFn = safeGlobalProp("fetch");
1156
+ if (typeof fetchFn !== "function") {
1164
1157
  return {};
1165
1158
  }
1166
- // 4) Attempt fetch & parse, but swallow all errors
1159
+ // 4) Try to fetch + parse, swallowing any errors
1167
1160
  try {
1168
- const res = yield fetch(configUrl);
1169
- if (!res.ok) {
1170
- console.warn(`[loadConfig] fetch failed ${res.status} ${res.statusText} @`, configUrl);
1161
+ const res = yield fetchFn(configUrl).catch(() => null);
1162
+ if (!res || !res.ok) {
1171
1163
  return {};
1172
1164
  }
1173
- const json = (yield res.json());
1174
- _cachedConfig = json;
1175
- return json;
1165
+ // parse JSON
1166
+ _cachedConfig = (yield res.json());
1167
+ return _cachedConfig;
1176
1168
  }
1177
- catch (err) {
1178
- console.warn(`[loadConfig] network/json error for ${configUrl}:`, err);
1169
+ catch (_b) {
1179
1170
  return {};
1180
1171
  }
1181
1172
  });