@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 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,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
- var _a;
1147
- // 1) decide which path
1148
- const relativePath = filePath || 'config.json';
1149
- // 2) pick a base URL: document.baseURI → window.location.href → '/'
1150
- const base = (_a = getDocumentProp('baseURI')) !== null && _a !== void 0 ? _a : (typeof window !== 'undefined' ? window.location.href : '/');
1151
- // 3) build the URL, safely
1152
- let configUrl;
1153
- try {
1154
- configUrl = new URL(relativePath, base).href;
1155
- }
1156
- catch (err) {
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
- // 4) return cached if we have it
1150
+ // 2) Return cache if we’ve already fetched once
1161
1151
  if (_cachedConfig) {
1162
1152
  return _cachedConfig;
1163
1153
  }
1164
- // 5) actually fetch + parse, but never throw
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 fetch(configUrl);
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
- const json = (yield res.json());
1172
- _cachedConfig = json;
1173
- return json;
1165
+ // parse JSON
1166
+ _cachedConfig = (yield res.json());
1167
+ return _cachedConfig;
1174
1168
  }
1175
- catch (err) {
1176
- console.warn(`[loadConfig] error fetching/parsing ${configUrl}:`, err);
1169
+ catch (_b) {
1177
1170
  return {};
1178
1171
  }
1179
1172
  });