@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 +71 -28
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.js +70 -29
- package/dist/esm/index.js.map +1 -1
- package/dist/functions/config_utils/imports.d.ts +1 -1
- package/dist/functions/config_utils/src/config_utils.d.ts +3 -3
- package/dist/functions/safe_utils/src/index.d.ts +3 -2
- package/dist/functions/safe_utils/src/safe_globals.d.ts +7 -0
- package/dist/functions/safe_utils/src/safe_storage.d.ts +21 -0
- package/package.json +1 -1
package/dist/esm/index.js
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
export { useCallback, useEffect, useRef, useState } from 'react';
|
|
2
2
|
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
3
3
|
|
|
4
|
+
function getSafeDocument() {
|
|
5
|
+
return typeof document !== 'undefined' ? document : undefined;
|
|
6
|
+
}
|
|
7
|
+
function getDocumentProp(...keys) {
|
|
8
|
+
let obj = getSafeDocument();
|
|
9
|
+
for (const k of keys) {
|
|
10
|
+
if (obj == null || typeof obj !== 'object')
|
|
11
|
+
return undefined;
|
|
12
|
+
obj = obj[k];
|
|
13
|
+
}
|
|
14
|
+
return obj;
|
|
15
|
+
}
|
|
16
|
+
|
|
4
17
|
/**
|
|
5
18
|
* Returns `window` if running in a browser, otherwise `undefined`.
|
|
6
19
|
*/
|
|
@@ -36,6 +49,45 @@ function callStorage(method, ...args) {
|
|
|
36
49
|
return undefined;
|
|
37
50
|
}
|
|
38
51
|
}
|
|
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
|
|
59
|
+
*/
|
|
60
|
+
function safeStorage(storageName, method, ...args) {
|
|
61
|
+
try {
|
|
62
|
+
const storage = globalThis[storageName];
|
|
63
|
+
if (!storage)
|
|
64
|
+
return undefined;
|
|
65
|
+
const fn = storage[method];
|
|
66
|
+
if (typeof fn !== "function")
|
|
67
|
+
return undefined;
|
|
68
|
+
// @ts-ignore
|
|
69
|
+
return fn.apply(storage, args);
|
|
70
|
+
}
|
|
71
|
+
catch (_a) {
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
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"]
|
|
81
|
+
*/
|
|
82
|
+
function safeGlobalProp(rootName, ...path) {
|
|
83
|
+
let obj = globalThis[rootName];
|
|
84
|
+
for (const key of path) {
|
|
85
|
+
if (obj == null)
|
|
86
|
+
return undefined;
|
|
87
|
+
obj = obj[key];
|
|
88
|
+
}
|
|
89
|
+
return obj;
|
|
90
|
+
}
|
|
39
91
|
|
|
40
92
|
/**
|
|
41
93
|
* Returns the global window object if it exists, otherwise undefined.
|
|
@@ -80,19 +132,6 @@ function getWindowHost() {
|
|
|
80
132
|
return getWindowProp('location', 'host');
|
|
81
133
|
}
|
|
82
134
|
|
|
83
|
-
function getSafeDocument() {
|
|
84
|
-
return typeof document !== 'undefined' ? document : undefined;
|
|
85
|
-
}
|
|
86
|
-
function getDocumentProp(...keys) {
|
|
87
|
-
let obj = getSafeDocument();
|
|
88
|
-
for (const k of keys) {
|
|
89
|
-
if (obj == null || typeof obj !== 'object')
|
|
90
|
-
return undefined;
|
|
91
|
-
obj = obj[k];
|
|
92
|
-
}
|
|
93
|
-
return obj;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
135
|
/**
|
|
97
136
|
***Changes**:
|
|
98
137
|
*- Updated import path for `InputProps` to `../../types/interfaces`.
|
|
@@ -1102,28 +1141,30 @@ let _cachedConfig = null;
|
|
|
1102
1141
|
function loadConfig() {
|
|
1103
1142
|
return __awaiter(this, arguments, void 0, function* (filePath = null) {
|
|
1104
1143
|
var _a;
|
|
1105
|
-
// 1)
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
//
|
|
1144
|
+
// 1) If already cached, return it.
|
|
1145
|
+
if (_cachedConfig)
|
|
1146
|
+
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";
|
|
1110
1154
|
let configUrl;
|
|
1111
1155
|
try {
|
|
1112
|
-
configUrl = new URL(
|
|
1156
|
+
configUrl = new URL(relative, base).href;
|
|
1113
1157
|
}
|
|
1114
1158
|
catch (err) {
|
|
1115
|
-
console.warn(`[loadConfig]
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
// 4) return cached if we have it
|
|
1119
|
-
if (_cachedConfig) {
|
|
1120
|
-
return _cachedConfig;
|
|
1159
|
+
console.warn(`[loadConfig] invalid URL with base=${base} and file=${relative}:`, err);
|
|
1160
|
+
// abort early in Node / bad base
|
|
1161
|
+
return {};
|
|
1121
1162
|
}
|
|
1122
|
-
//
|
|
1163
|
+
// 4) Attempt fetch & parse, but swallow all errors
|
|
1123
1164
|
try {
|
|
1124
1165
|
const res = yield fetch(configUrl);
|
|
1125
1166
|
if (!res.ok) {
|
|
1126
|
-
console.warn(`[loadConfig]
|
|
1167
|
+
console.warn(`[loadConfig] fetch failed ${res.status} ${res.statusText} @`, configUrl);
|
|
1127
1168
|
return {};
|
|
1128
1169
|
}
|
|
1129
1170
|
const json = (yield res.json());
|
|
@@ -1131,7 +1172,7 @@ function loadConfig() {
|
|
|
1131
1172
|
return json;
|
|
1132
1173
|
}
|
|
1133
1174
|
catch (err) {
|
|
1134
|
-
console.warn(`[loadConfig] error
|
|
1175
|
+
console.warn(`[loadConfig] network/json error for ${configUrl}:`, err);
|
|
1135
1176
|
return {};
|
|
1136
1177
|
}
|
|
1137
1178
|
});
|
|
@@ -1472,5 +1513,5 @@ function readJsonFile(relativeOrAbsolutePath) {
|
|
|
1472
1513
|
});
|
|
1473
1514
|
}
|
|
1474
1515
|
|
|
1475
|
-
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, sanitizeFilename, secureFetchIt, stripPrefixes, truncateString };
|
|
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 };
|
|
1476
1517
|
//# sourceMappingURL=index.js.map
|