@putkoff/abstract-utilities 0.1.178 → 0.1.179

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,22 +1200,43 @@ function getConfig(key) {
1178
1200
  });
1179
1201
  }
1180
1202
 
1181
- function ensureAbstractUrl(endpoint, slices = []) {
1182
- slices = slices || ['https//abstractendeavors.com', 'api'];
1183
- // 1) build a prefix string like "api/v1/"
1184
- const prefix = slices.map((s) => `${s}/`).join("");
1185
- const windowHost = getWindowHost();
1186
- const normalized = [
1187
- '/',
1188
- ...slices,
1189
- windowHost, // so "abstractendeavors.com" will be stripped
1190
- `${windowHost}/api` // etc, if you need it
1191
- ];
1192
- const stripped = stripPrefixes(endpoint, normalized);
1193
- console.log('BUILD PREFIX:', prefix);
1194
- console.log('RAW ENDPOINT:', endpoint);
1195
- console.log('STRIPPED ENDPT:', stripped);
1196
- return make_path(prefix, stripped);
1203
+ /**
1204
+ * Strip a leading host (http://host) from any URL-like string.
1205
+ */
1206
+ function stripHost(str) {
1207
+ // now also removes "abstractendeavors.com" even without protocol
1208
+ const hostPattern = `(?:https?:\\/\\/)?${getWindowHost()}`;
1209
+ return str.replace(new RegExp(`^${hostPattern}`), '');
1210
+ }
1211
+ /**
1212
+ * Given an “endpoint” slug like "api/list", build the full URL
1213
+ * from the BASE_API_URL entry in your JSON config.
1214
+ */
1215
+ function get_app_config_url(endpoint) {
1216
+ return __awaiter(this, void 0, void 0, function* () {
1217
+ // 1) normalize your input
1218
+ try {
1219
+ endpoint = endpoint || '';
1220
+ const clean = stripHost(endpoint.trim().toLowerCase());
1221
+ const cfg = yield loadConfig();
1222
+ // 2) pick the key you expect in your JSON
1223
+ const appKey = (`BASE_API_URL`);
1224
+ 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);
1225
+ endpoint = stripPrefixes(endpoint, ['/', 'https://', 'abstractendeavors.com', 'api']);
1226
+ if (base && endpoint) {
1227
+ return make_path(base, endpoint);
1228
+ }
1229
+ if (base) {
1230
+ return base;
1231
+ }
1232
+ if (endpoint) {
1233
+ return endpoint;
1234
+ }
1235
+ }
1236
+ catch (_a) {
1237
+ return endpoint;
1238
+ }
1239
+ });
1197
1240
  }
1198
1241
  /**
1199
1242
  * Unwraps nested { result } fields until you hit a non-object or no more "result" keys.
@@ -1207,42 +1250,6 @@ function getResult(obj) {
1207
1250
  }
1208
1251
  return current;
1209
1252
  }
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
1253
  // Determines HTTP method, defaults to GET or POST based on body
1247
1254
  function getMethod(method = null, body = null) {
1248
1255
  const validMethods = ['GET', 'POST', 'PUT', 'PATCH', 'PULL'];
@@ -1297,88 +1304,46 @@ function getFetchVars(headers = null, method = null, body = null) {
1297
1304
  }
1298
1305
  return { method, headers, body };
1299
1306
  }
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}`);
1307
+ /*
1308
+ * parseResult no longer needs to worry about JSON vs HTML redirect errors;
1309
+ * all 401/403 have already been handled above.
1310
+ */
1311
+ function parseResult(res) {
1312
+ return __awaiter(this, void 0, void 0, function* () {
1313
+ // runs checkResponse first, will throw if session is expired
1314
+ res = checkResponse(res);
1315
+ if (!res.ok) {
1316
+ // for any other non-401 errors, you can still surface them
1317
+ const errorText = yield res.text();
1318
+ throw new Error(errorText || res.statusText);
1323
1319
  }
1320
+ // now safely parse JSON
1321
+ return res.json();
1324
1322
  });
1325
1323
  }
1326
-
1327
1324
  /**
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.
1325
+ * Intercept 401/403 and force a clean redirect to login
1326
+ * without ever showing an alert.
1338
1327
  */
1339
- function get_app_config_url(endpoint) {
1340
- 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;
1362
- }
1363
- });
1328
+ function checkResponse(res) {
1329
+ if (res.status === 401 || res.status === 403) {
1330
+ // 1) clear out the stale token
1331
+ localStorage.removeItem("token");
1332
+ // 2) replace history so "back" doesn’t re-trigger the protected route
1333
+ window.history.replaceState({}, "", "/secure-files");
1334
+ // 3) short-circuit all further fetch logic
1335
+ throw new Error("SessionExpired");
1336
+ }
1337
+ return res;
1364
1338
  }
1365
1339
  function fetchIt(url, body, method, headers, blob, noApi, withCredentials, returnJson, returnReult) {
1366
1340
  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
- });
1371
- }
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
1375
1341
  method = method || "GET";
1376
- let url = endpoint;
1377
1342
  if (!noApi) {
1378
- url = yield get_app_config_url(endpoint);
1343
+ url = yield get_app_config_url(url);
1379
1344
  }
1380
1345
  // 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);
1346
+ headers = Object.assign(Object.assign(Object.assign({}, (body instanceof FormData ? {} : { "Content-Type": "application/json" })), getAuthorizationHeader()), headers);
1382
1347
  const opts = {
1383
1348
  method: method.toUpperCase(),
1384
1349
  credentials: withCredentials ? "include" : "same-origin",
@@ -1404,6 +1369,12 @@ function secureFetchIt(endpoint_1) {
1404
1369
  return res;
1405
1370
  });
1406
1371
  }
1372
+
1373
+ function secureFetchIt(endpoint_1) {
1374
+ return __awaiter(this, arguments, void 0, function* (endpoint, body = null, method = null, customHeaders = null, blob = false, configUrl = false, withCredentials = true, returnJson = true, returnReult = true) {
1375
+ return yield fetchIt(endpoint, body, method, customHeaders, blob, configUrl, withCredentials, returnJson, returnReult);
1376
+ });
1377
+ }
1407
1378
  // Performs PATCH request
1408
1379
  function requestPatch(url_1) {
1409
1380
  return __awaiter(this, arguments, void 0, function* (url, body = null) {
@@ -1439,6 +1410,54 @@ function fetchSharePatch(file_1) {
1439
1410
  });
1440
1411
  }
1441
1412
 
1413
+ function ensureAbstractUrl(endpoint, slices = []) {
1414
+ slices = slices || ['https//abstractendeavors.com', 'api'];
1415
+ // 1) build a prefix string like "api/v1/"
1416
+ const prefix = slices.map((s) => `${s}/`).join("");
1417
+ const windowHost = getWindowHost();
1418
+ const normalized = [
1419
+ '/',
1420
+ ...slices,
1421
+ windowHost, // so "abstractendeavors.com" will be stripped
1422
+ `${windowHost}/api` // etc, if you need it
1423
+ ];
1424
+ const stripped = stripPrefixes(endpoint, normalized);
1425
+ console.log('BUILD PREFIX:', prefix);
1426
+ console.log('RAW ENDPOINT:', endpoint);
1427
+ console.log('STRIPPED ENDPT:', stripped);
1428
+ return make_path(prefix, stripped);
1429
+ }
1430
+ // Constructs API URL from endpoint
1431
+ function api(endpoint) {
1432
+ return ensureAbstractUrl(endpoint);
1433
+ }
1434
+ // Constructs HTML directory path
1435
+ function getHtmlDirectory(directory, filename) {
1436
+ return `${directory}/${filename}.html`;
1437
+ }
1438
+ // Fetches HTML content
1439
+ function fetchIndexHtml(filename_1) {
1440
+ return __awaiter(this, arguments, void 0, function* (filename, directory = 'sf_index', base = 'html') {
1441
+ const url = `/${base}/${directory}/${filename}.html`;
1442
+ const response = yield fetch(api(url));
1443
+ return yield response.text();
1444
+ });
1445
+ }
1446
+ // Fetches and injects HTML content into container
1447
+ function fetchIndexHtmlContainer(filename_1) {
1448
+ return __awaiter(this, arguments, void 0, function* (filename, doc = document, directory = 'html') {
1449
+ const container = `${filename}-container`;
1450
+ const html = yield fetchIndexHtml(filename, directory);
1451
+ const el = doc.getElementById(container);
1452
+ if (el) {
1453
+ el.innerHTML = html;
1454
+ }
1455
+ else {
1456
+ console.warn(`⚠️ No container found for: #${container}`);
1457
+ }
1458
+ });
1459
+ }
1460
+
1442
1461
  function Button(_a) {
1443
1462
  var { children, color = 'gray', variant = 'default', className = '' } = _a, rest = __rest(_a, ["children", "color", "variant", "className"]);
1444
1463
  const base = 'rounded px-3 py-1 text-sm font-medium focus:outline-none focus:ring-2 focus:ring-offset-2 transition-colors duration-150';
@@ -1553,6 +1572,7 @@ exports.api = api;
1553
1572
  exports.callStorage = callStorage;
1554
1573
  exports.callWindowMethod = callWindowMethod;
1555
1574
  exports.checkResponse = checkResponse;
1575
+ exports.create_list_string = create_list_string;
1556
1576
  exports.currentUsername = currentUsername;
1557
1577
  exports.currentUsernames = currentUsernames;
1558
1578
  exports.decodeJwt = decodeJwt;
@@ -1636,4 +1656,5 @@ exports.secureFetchIt = secureFetchIt;
1636
1656
  exports.stripHost = stripHost;
1637
1657
  exports.stripPrefixes = stripPrefixes;
1638
1658
  exports.truncateString = truncateString;
1659
+ exports.tryParse = tryParse;
1639
1660
  //# sourceMappingURL=index.js.map