@putkoff/abstract-utilities 0.1.173 → 0.1.175

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,39 +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) If already cached, return it.
1145
- if (_cachedConfig)
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
+ }
1146
+ }
1147
+ // 2) Return cache if we’ve already fetched once
1148
+ if (_cachedConfig) {
1146
1149
  return _cachedConfig;
1147
- // 2) Build a base for URL.resolve:
1148
- // try document.baseURI → window.location.href → "/"
1149
- const docBase = safeGlobalProp("document", "baseURI");
1150
- const winHref = typeof window !== "undefined" ? window.location.href : undefined;
1151
- const base = (_a = docBase !== null && docBase !== void 0 ? docBase : winHref) !== null && _a !== void 0 ? _a : "/";
1152
- // 3) Compute configUrl
1153
- const relative = filePath || "config.json";
1154
- let configUrl;
1155
- try {
1156
- configUrl = new URL(relative, base).href;
1157
1150
  }
1158
- catch (err) {
1159
- console.warn(`[loadConfig] invalid URL with base=${base} and file=${relative}:`, err);
1160
- // abort early in Node / bad base
1151
+ // 3) Bail out in non-browser if fetch isn’t present
1152
+ const fetchFn = safeGlobalProp("fetch");
1153
+ if (typeof fetchFn !== "function") {
1161
1154
  return {};
1162
1155
  }
1163
- // 4) Attempt fetch & parse, but swallow all errors
1156
+ // 4) Try to fetch + parse, swallowing any errors
1164
1157
  try {
1165
- const res = yield fetch(configUrl);
1166
- if (!res.ok) {
1167
- console.warn(`[loadConfig] fetch failed ${res.status} ${res.statusText} @`, configUrl);
1158
+ const res = yield fetchFn(configUrl).catch(() => null);
1159
+ if (!res || !res.ok) {
1168
1160
  return {};
1169
1161
  }
1170
- const json = (yield res.json());
1171
- _cachedConfig = json;
1172
- return json;
1162
+ // parse JSON
1163
+ _cachedConfig = (yield res.json());
1164
+ return _cachedConfig;
1173
1165
  }
1174
- catch (err) {
1175
- console.warn(`[loadConfig] network/json error for ${configUrl}:`, err);
1166
+ catch (_b) {
1176
1167
  return {};
1177
1168
  }
1178
1169
  });
@@ -1345,12 +1336,19 @@ function stripHost(str) {
1345
1336
  function get_app_config_url(endpoint) {
1346
1337
  return __awaiter(this, void 0, void 0, function* () {
1347
1338
  // 1) normalize your input
1348
- endpoint = endpoint || '';
1349
- stripHost(endpoint.trim().toLowerCase());
1350
- yield loadConfig();
1351
- const base = yield fetchIt('https://abstractendeavors.com/api/secure_env', { "key": 'BASE_API_URL', 'path': '/var/www/abstractendeavors/secure-files/public/config.json' }, 'POST', null, false, true);
1352
- endpoint = stripPrefixes(endpoint, ['/', 'https://', 'abstractendeavors.com', 'api']);
1353
- return make_path(base, endpoint);
1339
+ try {
1340
+ endpoint = endpoint || '';
1341
+ const clean = stripHost(endpoint.trim().toLowerCase());
1342
+ const cfg = yield loadConfig();
1343
+ // 2) pick the key you expect in your JSON
1344
+ const appKey = (`BASE_API_URL`);
1345
+ const base = yield fetchIt('https://abstractendeavors.com/api/secure_env', { "key": 'BASE_API_URL', 'path': '/var/www/abstractendeavors/secure-files/public/config.json' }, 'POST', null, false, true);
1346
+ endpoint = stripPrefixes(endpoint, ['/', 'https://', 'abstractendeavors.com', 'api']);
1347
+ return make_path(base, endpoint);
1348
+ }
1349
+ catch (_a) {
1350
+ return endpoint;
1351
+ }
1354
1352
  });
1355
1353
  }
1356
1354
  function fetchIt(endpoint, body, method, headers, blob, noApi, withCredentials, returnJson, returnReult) {
@@ -1513,5 +1511,5 @@ function readJsonFile(relativeOrAbsolutePath) {
1513
1511
  });
1514
1512
  }
1515
1513
 
1516
- export { API_PREFIX, BASE_URL, Button, Checkbox, DEV_PREFIX, DOMAIN_NAME, Input, PROD_PREFIX, PROTOCOL, SUB_DIR, Spinner, alertit, api, callStorage, callWindowMethod, checkResponse, currentUsername, currentUsernames, decodeJwt, eatAll, eatEnd, eatInner, eatOuter, ensureAbstractUrl, ensure_list, fetchIndexHtml, fetchIndexHtmlContainer, fetchIt, fetchSharePatch, geAuthsUtilsDirectory, geBackupsUtilsDirectory, geConstantsUtilsDirectory, geEnvUtilsDirectory, geFetchUtilsDirectory, geFileUtilsDirectory, gePathUtilsDirectory, geStaticDirectory, geStringUtilsDirectory, geTypeUtilsDirectory, getAbsDir, getAbsPath, getAuthorizationHeader, getBaseDir, getBody, getComponentsUtilsDirectory, getConfig, getDbConfigsPath, getDistDir, getDocumentProp, getEnvDir, getEnvPath, getFetchVars, getFunctionsDir, getFunctionsUtilsDirectory, getHeaders, getHooksUtilsDirectory, getHtmlDirectory, getLibUtilsDirectory, getMethod, getPublicDir, getResult, getSafeDocument, getSafeLocalStorage, getSafeWindow, getSchemasDirPath, getSchemasPath, getSrcDir, getSubstring, getToken, getWindowHost, getWindowProp, get_app_config_url, get_basename, get_dirname, get_extname, get_filename, get_splitext, get_window, get_window_location, get_window_parts, get_window_pathname, isLoggedIn, isTokenExpired, loadConfig, make_path, make_sanitized_path, normalizeUrl, parseResult, readFileContents, readJsonFile, requestPatch, requireToken, safeGlobalProp, safeStorage, sanitizeFilename, secureFetchIt, stripPrefixes, truncateString };
1514
+ export { API_PREFIX, BASE_URL, Button, Checkbox, DEV_PREFIX, DOMAIN_NAME, Input, PROD_PREFIX, PROTOCOL, SUB_DIR, Spinner, alertit, api, callStorage, callWindowMethod, checkResponse, currentUsername, currentUsernames, decodeJwt, eatAll, eatEnd, eatInner, eatOuter, ensureAbstractUrl, ensure_list, fetchIndexHtml, fetchIndexHtmlContainer, fetchIt, fetchSharePatch, geAuthsUtilsDirectory, geBackupsUtilsDirectory, geConstantsUtilsDirectory, geEnvUtilsDirectory, geFetchUtilsDirectory, geFileUtilsDirectory, gePathUtilsDirectory, geStaticDirectory, geStringUtilsDirectory, geTypeUtilsDirectory, getAbsDir, getAbsPath, getAuthorizationHeader, getBaseDir, getBody, getComponentsUtilsDirectory, getConfig, getDbConfigsPath, getDistDir, getDocumentProp, getEnvDir, getEnvPath, getFetchVars, getFunctionsDir, getFunctionsUtilsDirectory, getHeaders, getHooksUtilsDirectory, getHtmlDirectory, getLibUtilsDirectory, getMethod, getPublicDir, getResult, getSafeDocument, getSafeLocalStorage, getSafeWindow, getSchemasDirPath, getSchemasPath, getSrcDir, getSubstring, getToken, getWindowHost, getWindowProp, get_app_config_url, get_basename, get_dirname, get_extname, get_filename, get_splitext, get_window, get_window_location, get_window_parts, get_window_pathname, isLoggedIn, isTokenExpired, loadConfig, make_path, make_sanitized_path, normalizeUrl, parseResult, readFileContents, readJsonFile, requestPatch, requireToken, safeGlobalProp, safeStorage, sanitizeFilename, secureFetchIt, stripHost, stripPrefixes, truncateString };
1517
1515
  //# sourceMappingURL=index.js.map