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