@putkoff/abstract-utilities 0.1.178 → 0.1.180
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 +121 -109
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.js +120 -110
- package/dist/esm/index.js.map +1 -1
- package/dist/functions/fetch_utils/src/fetchIt_utils.d.ts +3 -10
- package/dist/functions/fetch_utils/src/fetch_utils.d.ts +28 -0
- package/dist/functions/fetch_utils/src/index.d.ts +1 -1
- package/dist/functions/fetch_utils/src/utils.d.ts +0 -19
- package/dist/functions/string_utils/src/string_utils.d.ts +2 -0
- package/package.json +1 -1
package/dist/esm/index.js
CHANGED
|
@@ -927,6 +927,28 @@ function eatEnd(obj, endings = ['/']) {
|
|
|
927
927
|
}
|
|
928
928
|
return result;
|
|
929
929
|
}
|
|
930
|
+
function tryParse(obj) {
|
|
931
|
+
try {
|
|
932
|
+
obj = JSON.stringify(obj);
|
|
933
|
+
}
|
|
934
|
+
catch (err) {
|
|
935
|
+
try {
|
|
936
|
+
obj = JSON.parse(obj);
|
|
937
|
+
}
|
|
938
|
+
catch (err) {
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
return obj;
|
|
942
|
+
}
|
|
943
|
+
function create_list_string(array_obj) {
|
|
944
|
+
let string = '';
|
|
945
|
+
for (const obj in array_obj) {
|
|
946
|
+
const array_value = array_obj[obj];
|
|
947
|
+
const parsed_value = tryParse(array_value);
|
|
948
|
+
string += `${obj} == ${parsed_value}\n`;
|
|
949
|
+
}
|
|
950
|
+
return string;
|
|
951
|
+
}
|
|
930
952
|
|
|
931
953
|
function ensure_list(obj) {
|
|
932
954
|
const objArray = Array.isArray(obj) ? obj : [obj];
|
|
@@ -1175,6 +1197,18 @@ function getConfig(key) {
|
|
|
1175
1197
|
});
|
|
1176
1198
|
}
|
|
1177
1199
|
|
|
1200
|
+
// Constructs API URL from endpoint
|
|
1201
|
+
function api(endpoint) {
|
|
1202
|
+
return ensureAbstractUrl(endpoint);
|
|
1203
|
+
}
|
|
1204
|
+
/**
|
|
1205
|
+
* Strip a leading host (http://host) from any URL-like string.
|
|
1206
|
+
*/
|
|
1207
|
+
function stripHost(str) {
|
|
1208
|
+
// now also removes "abstractendeavors.com" even without protocol
|
|
1209
|
+
const hostPattern = `(?:https?:\\/\\/)?${getWindowHost()}`;
|
|
1210
|
+
return str.replace(new RegExp(`^${hostPattern}`), '');
|
|
1211
|
+
}
|
|
1178
1212
|
function ensureAbstractUrl(endpoint, slices = []) {
|
|
1179
1213
|
slices = slices || ['https//abstractendeavors.com', 'api'];
|
|
1180
1214
|
// 1) build a prefix string like "api/v1/"
|
|
@@ -1192,6 +1226,28 @@ function ensureAbstractUrl(endpoint, slices = []) {
|
|
|
1192
1226
|
console.log('STRIPPED ENDPT:', stripped);
|
|
1193
1227
|
return make_path(prefix, stripped);
|
|
1194
1228
|
}
|
|
1229
|
+
/**
|
|
1230
|
+
* Given an “endpoint” slug like "api/list", build the full URL
|
|
1231
|
+
* from the BASE_API_URL entry in your JSON config.
|
|
1232
|
+
*/
|
|
1233
|
+
function get_app_config_url(endpoint) {
|
|
1234
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1235
|
+
// 1) normalize + strip prefixes
|
|
1236
|
+
const clean = stripHost(endpoint);
|
|
1237
|
+
// 2) fetch your BASE_API_URL
|
|
1238
|
+
const vars = getFetchVars(null, 'POST', {
|
|
1239
|
+
key: 'BASE_API_URL',
|
|
1240
|
+
path: '/var/www/.../config.json'
|
|
1241
|
+
});
|
|
1242
|
+
const resp = yield fetch(ensureAbstractUrl('/secure_env'), vars);
|
|
1243
|
+
checkResponse(resp);
|
|
1244
|
+
// 3) parse, then unwrap { result }
|
|
1245
|
+
const json = yield resp.json();
|
|
1246
|
+
const baseUrl = getResult(json);
|
|
1247
|
+
// 4) now build your final URL
|
|
1248
|
+
return make_path(baseUrl, clean);
|
|
1249
|
+
});
|
|
1250
|
+
}
|
|
1195
1251
|
/**
|
|
1196
1252
|
* Unwraps nested { result } fields until you hit a non-object or no more "result" keys.
|
|
1197
1253
|
*/
|
|
@@ -1204,42 +1260,6 @@ function getResult(obj) {
|
|
|
1204
1260
|
}
|
|
1205
1261
|
return current;
|
|
1206
1262
|
}
|
|
1207
|
-
// Constructs API URL from endpoint
|
|
1208
|
-
function api(endpoint) {
|
|
1209
|
-
return ensureAbstractUrl(endpoint);
|
|
1210
|
-
}
|
|
1211
|
-
/**
|
|
1212
|
-
* Intercept 401/403 and force a clean redirect to login
|
|
1213
|
-
* without ever showing an alert.
|
|
1214
|
-
*/
|
|
1215
|
-
function checkResponse(res) {
|
|
1216
|
-
if (res.status === 401 || res.status === 403) {
|
|
1217
|
-
// 1) clear out the stale token
|
|
1218
|
-
localStorage.removeItem("token");
|
|
1219
|
-
// 2) replace history so "back" doesn’t re-trigger the protected route
|
|
1220
|
-
window.history.replaceState({}, "", "/secure-files");
|
|
1221
|
-
// 3) short-circuit all further fetch logic
|
|
1222
|
-
throw new Error("SessionExpired");
|
|
1223
|
-
}
|
|
1224
|
-
return res;
|
|
1225
|
-
}
|
|
1226
|
-
/**
|
|
1227
|
-
* parseResult no longer needs to worry about JSON vs HTML redirect errors;
|
|
1228
|
-
* all 401/403 have already been handled above.
|
|
1229
|
-
*/
|
|
1230
|
-
function parseResult(res) {
|
|
1231
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1232
|
-
// runs checkResponse first, will throw if session is expired
|
|
1233
|
-
res = checkResponse(res);
|
|
1234
|
-
if (!res.ok) {
|
|
1235
|
-
// for any other non-401 errors, you can still surface them
|
|
1236
|
-
const errorText = yield res.text();
|
|
1237
|
-
throw new Error(errorText || res.statusText);
|
|
1238
|
-
}
|
|
1239
|
-
// now safely parse JSON
|
|
1240
|
-
return res.json();
|
|
1241
|
-
});
|
|
1242
|
-
}
|
|
1243
1263
|
// Determines HTTP method, defaults to GET or POST based on body
|
|
1244
1264
|
function getMethod(method = null, body = null) {
|
|
1245
1265
|
const validMethods = ['GET', 'POST', 'PUT', 'PATCH', 'PULL'];
|
|
@@ -1294,88 +1314,46 @@ function getFetchVars(headers = null, method = null, body = null) {
|
|
|
1294
1314
|
}
|
|
1295
1315
|
return { method, headers, body };
|
|
1296
1316
|
}
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
}
|
|
1301
|
-
// Fetches HTML content
|
|
1302
|
-
function fetchIndexHtml(filename_1) {
|
|
1303
|
-
return __awaiter(this, arguments, void 0, function* (filename, directory = 'sf_index', base = 'html') {
|
|
1304
|
-
const url = `/${base}/${directory}/${filename}.html`;
|
|
1305
|
-
const response = yield fetch(api(url));
|
|
1306
|
-
return yield response.text();
|
|
1307
|
-
});
|
|
1308
|
-
}
|
|
1309
|
-
// Fetches and injects HTML content into container
|
|
1310
|
-
function fetchIndexHtmlContainer(filename_1) {
|
|
1311
|
-
return __awaiter(this, arguments, void 0, function* (filename, doc = document, directory = 'html') {
|
|
1312
|
-
const container = `${filename}-container`;
|
|
1313
|
-
const html = yield fetchIndexHtml(filename, directory);
|
|
1314
|
-
const el = doc.getElementById(container);
|
|
1315
|
-
if (el) {
|
|
1316
|
-
el.innerHTML = html;
|
|
1317
|
-
}
|
|
1318
|
-
else {
|
|
1319
|
-
console.warn(`⚠️ No container found for: #${container}`);
|
|
1320
|
-
}
|
|
1321
|
-
});
|
|
1322
|
-
}
|
|
1323
|
-
|
|
1324
|
-
/**
|
|
1325
|
-
* Strip a leading host (http://host) from any URL-like string.
|
|
1326
|
-
*/
|
|
1327
|
-
function stripHost(str) {
|
|
1328
|
-
// now also removes "abstractendeavors.com" even without protocol
|
|
1329
|
-
const hostPattern = `(?:https?:\\/\\/)?${getWindowHost()}`;
|
|
1330
|
-
return str.replace(new RegExp(`^${hostPattern}`), '');
|
|
1331
|
-
}
|
|
1332
|
-
/**
|
|
1333
|
-
* Given an “endpoint” slug like "api/list", build the full URL
|
|
1334
|
-
* from the BASE_API_URL entry in your JSON config.
|
|
1317
|
+
/*
|
|
1318
|
+
* parseResult no longer needs to worry about JSON vs HTML redirect errors;
|
|
1319
|
+
* all 401/403 have already been handled above.
|
|
1335
1320
|
*/
|
|
1336
|
-
function
|
|
1321
|
+
function parseResult(res) {
|
|
1337
1322
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1338
|
-
//
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
const
|
|
1343
|
-
|
|
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
|
-
if (base && endpoint) {
|
|
1348
|
-
return make_path(base, endpoint);
|
|
1349
|
-
}
|
|
1350
|
-
if (base) {
|
|
1351
|
-
return base;
|
|
1352
|
-
}
|
|
1353
|
-
if (endpoint) {
|
|
1354
|
-
return endpoint;
|
|
1355
|
-
}
|
|
1356
|
-
}
|
|
1357
|
-
catch (_a) {
|
|
1358
|
-
return endpoint;
|
|
1323
|
+
// runs checkResponse first, will throw if session is expired
|
|
1324
|
+
res = checkResponse(res);
|
|
1325
|
+
if (!res.ok) {
|
|
1326
|
+
// for any other non-401 errors, you can still surface them
|
|
1327
|
+
const errorText = yield res.text();
|
|
1328
|
+
throw new Error(errorText || res.statusText);
|
|
1359
1329
|
}
|
|
1330
|
+
// now safely parse JSON
|
|
1331
|
+
return res.json();
|
|
1360
1332
|
});
|
|
1361
1333
|
}
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1334
|
+
/**
|
|
1335
|
+
* Intercept 401/403 and force a clean redirect to login
|
|
1336
|
+
* without ever showing an alert.
|
|
1337
|
+
*/
|
|
1338
|
+
function checkResponse(res) {
|
|
1339
|
+
if (res.status === 401 || res.status === 403) {
|
|
1340
|
+
// 1) clear out the stale token
|
|
1341
|
+
localStorage.removeItem("token");
|
|
1342
|
+
// 2) replace history so "back" doesn’t re-trigger the protected route
|
|
1343
|
+
window.history.replaceState({}, "", "/secure-files");
|
|
1344
|
+
// 3) short-circuit all further fetch logic
|
|
1345
|
+
throw new Error("SessionExpired");
|
|
1346
|
+
}
|
|
1347
|
+
return res;
|
|
1368
1348
|
}
|
|
1369
|
-
function
|
|
1370
|
-
return __awaiter(this, arguments, void 0, function* (
|
|
1371
|
-
// strip leading slashes off the endpoint
|
|
1349
|
+
function fetchIt(url_1) {
|
|
1350
|
+
return __awaiter(this, arguments, void 0, function* (url, body = null, method = null, headers = null, blob = false, configUrl = false, withCredentials = true, returnJson = true, returnReult = true) {
|
|
1372
1351
|
method = method || "GET";
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
url = yield get_app_config_url(endpoint);
|
|
1352
|
+
if (!configUrl) {
|
|
1353
|
+
url = yield get_app_config_url(url);
|
|
1376
1354
|
}
|
|
1377
1355
|
// headers: JSON by default, plus any auth + overrides
|
|
1378
|
-
|
|
1356
|
+
headers = Object.assign(Object.assign(Object.assign({}, (body instanceof FormData ? {} : { "Content-Type": "application/json" })), getAuthorizationHeader()), headers);
|
|
1379
1357
|
const opts = {
|
|
1380
1358
|
method: method.toUpperCase(),
|
|
1381
1359
|
credentials: withCredentials ? "include" : "same-origin",
|
|
@@ -1401,6 +1379,12 @@ function secureFetchIt(endpoint_1) {
|
|
|
1401
1379
|
return res;
|
|
1402
1380
|
});
|
|
1403
1381
|
}
|
|
1382
|
+
|
|
1383
|
+
function secureFetchIt(endpoint_1) {
|
|
1384
|
+
return __awaiter(this, arguments, void 0, function* (endpoint, body = null, method = null, customHeaders = null, blob = false, configUrl = false, withCredentials = true, returnJson = true, returnReult = true) {
|
|
1385
|
+
return yield fetchIt(endpoint, body, method, customHeaders, blob, configUrl, withCredentials, returnJson, returnReult);
|
|
1386
|
+
});
|
|
1387
|
+
}
|
|
1404
1388
|
// Performs PATCH request
|
|
1405
1389
|
function requestPatch(url_1) {
|
|
1406
1390
|
return __awaiter(this, arguments, void 0, function* (url, body = null) {
|
|
@@ -1435,6 +1419,32 @@ function fetchSharePatch(file_1) {
|
|
|
1435
1419
|
}
|
|
1436
1420
|
});
|
|
1437
1421
|
}
|
|
1422
|
+
// Constructs HTML directory path
|
|
1423
|
+
function getHtmlDirectory(directory, filename) {
|
|
1424
|
+
return `${directory}/${filename}.html`;
|
|
1425
|
+
}
|
|
1426
|
+
// Fetches HTML content
|
|
1427
|
+
function fetchIndexHtml(filename_1) {
|
|
1428
|
+
return __awaiter(this, arguments, void 0, function* (filename, directory = 'sf_index', base = 'html') {
|
|
1429
|
+
const url = `/${base}/${directory}/${filename}.html`;
|
|
1430
|
+
const response = yield fetch(api(url));
|
|
1431
|
+
return yield response.text();
|
|
1432
|
+
});
|
|
1433
|
+
}
|
|
1434
|
+
// Fetches and injects HTML content into container
|
|
1435
|
+
function fetchIndexHtmlContainer(filename_1) {
|
|
1436
|
+
return __awaiter(this, arguments, void 0, function* (filename, doc = document, directory = 'html') {
|
|
1437
|
+
const container = `${filename}-container`;
|
|
1438
|
+
const html = yield fetchIndexHtml(filename, directory);
|
|
1439
|
+
const el = doc.getElementById(container);
|
|
1440
|
+
if (el) {
|
|
1441
|
+
el.innerHTML = html;
|
|
1442
|
+
}
|
|
1443
|
+
else {
|
|
1444
|
+
console.warn(`⚠️ No container found for: #${container}`);
|
|
1445
|
+
}
|
|
1446
|
+
});
|
|
1447
|
+
}
|
|
1438
1448
|
|
|
1439
1449
|
function Button(_a) {
|
|
1440
1450
|
var { children, color = 'gray', variant = 'default', className = '' } = _a, rest = __rest(_a, ["children", "color", "variant", "className"]);
|
|
@@ -1518,5 +1528,5 @@ function readJsonFile(relativeOrAbsolutePath) {
|
|
|
1518
1528
|
});
|
|
1519
1529
|
}
|
|
1520
1530
|
|
|
1521
|
-
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 };
|
|
1531
|
+
export { API_PREFIX, BASE_URL, Button, Checkbox, DEV_PREFIX, DOMAIN_NAME, Input, PROD_PREFIX, PROTOCOL, SUB_DIR, Spinner, alertit, api, callStorage, callWindowMethod, checkResponse, create_list_string, 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, tryParse };
|
|
1522
1532
|
//# sourceMappingURL=index.js.map
|