@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/esm/index.js CHANGED
@@ -50,23 +50,16 @@ function callStorage(method, ...args) {
50
50
  }
51
51
  }
52
52
  /**
53
- * Safely call localStorage / sessionStorage / any Storage API.
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 storage = globalThis[storageName];
63
- if (!storage)
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 fn.apply(storage, args);
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 `window` or `document` to grab nested props without blowing up in Node.
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(rootName, ...path) {
83
- let obj = globalThis[rootName];
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
- var _a;
1144
- // 1) decide which path
1145
- const relativePath = filePath || 'config.json';
1146
- // 2) pick a base URL: document.baseURI → window.location.href → '/'
1147
- const base = (_a = getDocumentProp('baseURI')) !== null && _a !== void 0 ? _a : (typeof window !== 'undefined' ? window.location.href : '/');
1148
- // 3) build the URL, safely
1149
- let configUrl;
1150
- try {
1151
- configUrl = new URL(relativePath, base).href;
1152
- }
1153
- catch (err) {
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
- // 4) return cached if we have it
1147
+ // 2) Return cache if we’ve already fetched once
1158
1148
  if (_cachedConfig) {
1159
1149
  return _cachedConfig;
1160
1150
  }
1161
- // 5) actually fetch + parse, but never throw
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 fetch(configUrl);
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
- const json = (yield res.json());
1169
- _cachedConfig = json;
1170
- return json;
1162
+ // parse JSON
1163
+ _cachedConfig = (yield res.json());
1164
+ return _cachedConfig;
1171
1165
  }
1172
- catch (err) {
1173
- console.warn(`[loadConfig] error fetching/parsing ${configUrl}:`, err);
1166
+ catch (_b) {
1174
1167
  return {};
1175
1168
  }
1176
1169
  });