@putkoff/abstract-utilities 0.1.171 → 0.1.173

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
@@ -4,6 +4,19 @@ var react = require('react');
4
4
  var jsxRuntime = require('react/jsx-runtime');
5
5
 
6
6
  var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
7
+ function getSafeDocument() {
8
+ return typeof document !== 'undefined' ? document : undefined;
9
+ }
10
+ function getDocumentProp(...keys) {
11
+ let obj = getSafeDocument();
12
+ for (const k of keys) {
13
+ if (obj == null || typeof obj !== 'object')
14
+ return undefined;
15
+ obj = obj[k];
16
+ }
17
+ return obj;
18
+ }
19
+
7
20
  /**
8
21
  * Returns `window` if running in a browser, otherwise `undefined`.
9
22
  */
@@ -39,6 +52,45 @@ function callStorage(method, ...args) {
39
52
  return undefined;
40
53
  }
41
54
  }
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
62
+ */
63
+ function safeStorage(storageName, method, ...args) {
64
+ try {
65
+ const storage = globalThis[storageName];
66
+ if (!storage)
67
+ return undefined;
68
+ const fn = storage[method];
69
+ if (typeof fn !== "function")
70
+ return undefined;
71
+ // @ts-ignore
72
+ return fn.apply(storage, args);
73
+ }
74
+ catch (_a) {
75
+ return undefined;
76
+ }
77
+ }
78
+
79
+ /**
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"]
84
+ */
85
+ function safeGlobalProp(rootName, ...path) {
86
+ let obj = globalThis[rootName];
87
+ for (const key of path) {
88
+ if (obj == null)
89
+ return undefined;
90
+ obj = obj[key];
91
+ }
92
+ return obj;
93
+ }
42
94
 
43
95
  /**
44
96
  * Returns the global window object if it exists, otherwise undefined.
@@ -83,19 +135,6 @@ function getWindowHost() {
83
135
  return getWindowProp('location', 'host');
84
136
  }
85
137
 
86
- function getSafeDocument() {
87
- return typeof document !== 'undefined' ? document : undefined;
88
- }
89
- function getDocumentProp(...keys) {
90
- let obj = getSafeDocument();
91
- for (const k of keys) {
92
- if (obj == null || typeof obj !== 'object')
93
- return undefined;
94
- obj = obj[k];
95
- }
96
- return obj;
97
- }
98
-
99
138
  /**
100
139
  ***Changes**:
101
140
  *- Updated import path for `InputProps` to `../../types/interfaces`.
@@ -1105,28 +1144,30 @@ let _cachedConfig = null;
1105
1144
  function loadConfig() {
1106
1145
  return __awaiter(this, arguments, void 0, function* (filePath = null) {
1107
1146
  var _a;
1108
- // 1) decide which path
1109
- const relativePath = filePath || 'config.json';
1110
- // 2) pick a base URL: document.baseURI → window.location.href → '/'
1111
- const base = (_a = getDocumentProp('baseURI')) !== null && _a !== void 0 ? _a : (typeof window !== 'undefined' ? window.location.href : '/');
1112
- // 3) build the URL, safely
1147
+ // 1) If already cached, return it.
1148
+ if (_cachedConfig)
1149
+ 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";
1113
1157
  let configUrl;
1114
1158
  try {
1115
- configUrl = new URL(relativePath, base).href;
1159
+ configUrl = new URL(relative, base).href;
1116
1160
  }
1117
1161
  catch (err) {
1118
- console.warn(`[loadConfig] failed to resolve URL for "${relativePath}" against "${base}":`, err);
1119
- configUrl = relativePath; // try a bare-relative fetch
1120
- }
1121
- // 4) return cached if we have it
1122
- if (_cachedConfig) {
1123
- return _cachedConfig;
1162
+ console.warn(`[loadConfig] invalid URL with base=${base} and file=${relative}:`, err);
1163
+ // abort early in Node / bad base
1164
+ return {};
1124
1165
  }
1125
- // 5) actually fetch + parse, but never throw
1166
+ // 4) Attempt fetch & parse, but swallow all errors
1126
1167
  try {
1127
1168
  const res = yield fetch(configUrl);
1128
1169
  if (!res.ok) {
1129
- console.warn(`[loadConfig] server returned ${res.status} when fetching ${configUrl}`);
1170
+ console.warn(`[loadConfig] fetch failed ${res.status} ${res.statusText} @`, configUrl);
1130
1171
  return {};
1131
1172
  }
1132
1173
  const json = (yield res.json());
@@ -1134,7 +1175,7 @@ function loadConfig() {
1134
1175
  return json;
1135
1176
  }
1136
1177
  catch (err) {
1137
- console.warn(`[loadConfig] error fetching/parsing ${configUrl}:`, err);
1178
+ console.warn(`[loadConfig] network/json error for ${configUrl}:`, err);
1138
1179
  return {};
1139
1180
  }
1140
1181
  });
@@ -1583,6 +1624,8 @@ exports.readFileContents = readFileContents;
1583
1624
  exports.readJsonFile = readJsonFile;
1584
1625
  exports.requestPatch = requestPatch;
1585
1626
  exports.requireToken = requireToken;
1627
+ exports.safeGlobalProp = safeGlobalProp;
1628
+ exports.safeStorage = safeStorage;
1586
1629
  exports.sanitizeFilename = sanitizeFilename;
1587
1630
  exports.secureFetchIt = secureFetchIt;
1588
1631
  exports.stripPrefixes = stripPrefixes;