@putkoff/abstract-utilities 0.1.183 → 0.1.185

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
@@ -1278,6 +1278,29 @@ function getConfig(key) {
1278
1278
  return key != null ? cfg[key] : cfg;
1279
1279
  });
1280
1280
  }
1281
+ function getConfigContent() {
1282
+ return __awaiter(this, void 0, void 0, function* () {
1283
+ try {
1284
+ // `readJsonFile` should throw if the file isn’t there or isn’t valid JSON
1285
+ const cfg = yield readJsonFile('./config.json');
1286
+ return cfg;
1287
+ }
1288
+ catch (_a) {
1289
+ // swallow errors & return null so callers can detect “no config”
1290
+ return null;
1291
+ }
1292
+ });
1293
+ }
1294
+ // 2) Pull a single key out of that object
1295
+ function getConfigVar() {
1296
+ return __awaiter(this, arguments, void 0, function* (key = null) {
1297
+ const cfg = yield getConfigContent();
1298
+ if (cfg && typeof cfg === 'object' && key in cfg) {
1299
+ return cfg[key];
1300
+ }
1301
+ return undefined;
1302
+ });
1303
+ }
1281
1304
 
1282
1305
  // Constructs API URL from endpoint
1283
1306
  function api(endpoint) {
@@ -1303,31 +1326,31 @@ function ensureAbstractUrl(endpoint, slices = []) {
1303
1326
  `${windowHost}/api` // etc, if you need it
1304
1327
  ];
1305
1328
  const stripped = stripPrefixes(endpoint, normalized);
1306
- console.log('BUILD PREFIX:', prefix);
1307
- console.log('RAW ENDPOINT:', endpoint);
1308
- console.log('STRIPPED ENDPT:', stripped);
1309
1329
  return make_path(prefix, stripped);
1310
1330
  }
1311
1331
  /**
1312
1332
  * Given an “endpoint” slug like "api/list", build the full URL
1313
1333
  * from the BASE_API_URL entry in your JSON config.
1334
+ * If anything goes wrong, just returns the raw endpoint.
1314
1335
  */
1315
1336
  function get_app_config_url(endpoint) {
1316
1337
  return __awaiter(this, void 0, void 0, function* () {
1317
- // 1) normalize + strip prefixes
1318
1338
  const clean = stripHost(endpoint);
1319
- // 2) fetch your BASE_API_URL
1320
- const vars = getFetchVars(null, 'POST', {
1321
- key: 'BASE_API_URL',
1322
- path: '/var/www/abstractendeavors/secure-files/public/config.json'
1323
- });
1324
- const resp = yield fetch(ensureAbstractUrl('/secure_env'), vars);
1325
- checkResponse(resp);
1326
- // 3) parse, then unwrap { result }
1327
- const json = yield resp.json();
1328
- const baseUrl = getResult(json);
1329
- // 4) now build your final URL
1330
- return make_path(baseUrl, clean);
1339
+ try {
1340
+ const vars = getFetchVars(null, 'POST', {
1341
+ key: 'BASE_API_URL',
1342
+ path: '/var/www/abstractendeavors/secure-files/public/config.json'
1343
+ });
1344
+ const resp = yield fetch(ensureAbstractUrl('/secure_env'), vars);
1345
+ checkResponse(resp);
1346
+ const json = yield resp.json();
1347
+ const baseUrl = getResult(json);
1348
+ return make_path(baseUrl, clean);
1349
+ }
1350
+ catch (err) {
1351
+ console.warn(`[get_app_config_url] failed, falling back to raw endpoint:`, endpoint, err);
1352
+ return endpoint;
1353
+ }
1331
1354
  });
1332
1355
  }
1333
1356
  /**
@@ -1430,38 +1453,49 @@ function checkResponse(res) {
1430
1453
  }
1431
1454
  function fetchIt(endpoint_1) {
1432
1455
  return __awaiter(this, arguments, void 0, function* (endpoint, body = null, method = null, headers = null, blob = false, noApi = false, withCredentials = true, returnJson = true, returnReult = true) {
1433
- method = (method || "GET").toUpperCase();
1434
- // 1) auto-detect absolute URLs
1435
- const isAbsolute = typeof endpoint === "string" && /^https?:\/\//i.test(endpoint);
1436
- // 2) choose the URL
1456
+ const verb = (method || 'GET').toUpperCase();
1457
+ // 1) auto-detect absolute URLs, or use API lookup, but never throw
1437
1458
  let url;
1438
- if (isAbsolute) {
1439
- url = endpoint;
1459
+ try {
1460
+ const isAbsolute = typeof endpoint === 'string' && /^https?:\/\//i.test(endpoint);
1461
+ if (isAbsolute || noApi) {
1462
+ url = endpoint;
1463
+ }
1464
+ else {
1465
+ url = yield get_app_config_url(endpoint);
1466
+ }
1440
1467
  }
1441
- else if (noApi) {
1468
+ catch (_a) {
1442
1469
  url = endpoint;
1443
1470
  }
1444
- else {
1445
- url = yield get_app_config_url(endpoint);
1446
- }
1447
- // 3) prepare headers & body
1448
- headers = Object.assign(Object.assign(Object.assign({}, (body instanceof FormData ? {} : { "Content-Type": "application/json" })), getAuthorizationHeader()), headers);
1471
+ // 2) prepare headers & body
1472
+ const authHeaders = Object.assign({}, (body instanceof FormData ? {} : { 'Content-Type': 'application/json' }));
1473
+ headers = Object.assign(Object.assign(Object.assign({}, authHeaders), getFetchVars(null, verb, body).headers), headers);
1449
1474
  const opts = {
1450
- method,
1451
- credentials: withCredentials ? "include" : "same-origin",
1475
+ method: verb,
1476
+ credentials: withCredentials ? 'include' : 'same-origin',
1452
1477
  headers,
1453
1478
  body: body instanceof FormData
1454
1479
  ? body
1455
- : body != null && method !== "GET"
1480
+ : body != null && verb !== 'GET'
1456
1481
  ? JSON.stringify(body)
1457
1482
  : undefined,
1458
1483
  };
1459
- console.debug("➡️ secureFetchIt", url, opts);
1460
- const res = yield fetch(url, opts);
1461
- if (!res.ok) {
1462
- const err = yield res.text();
1463
- throw new Error(`HTTP ${res.status}: ${err}`);
1484
+ console.debug('➡️ fetchIt', url, opts);
1485
+ // 3) perform fetch, swallow any errors and return raw URL on failure
1486
+ let res;
1487
+ try {
1488
+ res = yield fetch(url, opts);
1489
+ if (!res.ok) {
1490
+ const err = yield res.text();
1491
+ throw new Error(`HTTP ${res.status}: ${err}`);
1492
+ }
1464
1493
  }
1494
+ catch (err) {
1495
+ console.warn(`[fetchIt] failed for ${url}, returning raw endpoint:`, err);
1496
+ return url;
1497
+ }
1498
+ // 4) produce the expected result
1465
1499
  if (blob)
1466
1500
  return res.blob();
1467
1501
  if (returnReult)
@@ -1537,6 +1571,51 @@ function fetchIndexHtmlContainer(filename_1) {
1537
1571
  }
1538
1572
  });
1539
1573
  }
1574
+ // 2) Pull a single key out of that object
1575
+ function getBaseUrl() {
1576
+ return __awaiter(this, arguments, void 0, function* (key = null) {
1577
+ key = key || 'BASE_API_URL';
1578
+ const value = yield getConfigVar(key);
1579
+ return value;
1580
+ });
1581
+ }
1582
+ function getEndpoints() {
1583
+ return __awaiter(this, arguments, void 0, function* (base_url = null) {
1584
+ base_url = yield getUrl(base_url);
1585
+ const endpoints_url = `${base_url}/api/endpoints`;
1586
+ return yield fetchIt(endpoints_url, {}, "GET");
1587
+ });
1588
+ }
1589
+ function getUrl() {
1590
+ return __awaiter(this, arguments, void 0, function* (base_url = null) {
1591
+ return base_url || (yield getBaseUrl()) || 'https://abstractendeavors.com';
1592
+ });
1593
+ }
1594
+ /**
1595
+ * Find the most specific endpoint matching the given keyword.
1596
+ * @param keyword A fragment to search for, e.g. 'list'
1597
+ * @returns The full path string, or null if nothing matches
1598
+ */
1599
+ function getEndpoint(keyword_1) {
1600
+ return __awaiter(this, arguments, void 0, function* (keyword, base_url = null) {
1601
+ base_url = yield getUrl(base_url);
1602
+ const endpoints = yield getEndpoints(base_url);
1603
+ const lower = keyword.toLowerCase();
1604
+ let bestMatch = null;
1605
+ let bestLen = -1;
1606
+ for (const [path] of endpoints) {
1607
+ const lastSegment = path.split("/").pop().toLowerCase();
1608
+ // does either string contain the other?
1609
+ if (lastSegment.includes(lower)) {
1610
+ if (lastSegment.length > bestLen) {
1611
+ bestLen = lastSegment.length;
1612
+ bestMatch = path;
1613
+ }
1614
+ }
1615
+ }
1616
+ return `${base_url}${bestMatch}`;
1617
+ });
1618
+ }
1540
1619
 
1541
1620
  function Button(_a) {
1542
1621
  var { children, color = 'gray', variant = 'default', className = '' } = _a, rest = __rest(_a, ["children", "color", "variant", "className"]);
@@ -1633,12 +1712,17 @@ exports.getAbsDir = getAbsDir;
1633
1712
  exports.getAbsPath = getAbsPath;
1634
1713
  exports.getAuthorizationHeader = getAuthorizationHeader;
1635
1714
  exports.getBaseDir = getBaseDir;
1715
+ exports.getBaseUrl = getBaseUrl;
1636
1716
  exports.getBody = getBody;
1637
1717
  exports.getComponentsUtilsDirectory = getComponentsUtilsDirectory;
1638
1718
  exports.getConfig = getConfig;
1719
+ exports.getConfigContent = getConfigContent;
1720
+ exports.getConfigVar = getConfigVar;
1639
1721
  exports.getDbConfigsPath = getDbConfigsPath;
1640
1722
  exports.getDistDir = getDistDir;
1641
1723
  exports.getDocumentProp = getDocumentProp;
1724
+ exports.getEndpoint = getEndpoint;
1725
+ exports.getEndpoints = getEndpoints;
1642
1726
  exports.getEnvDir = getEnvDir;
1643
1727
  exports.getEnvPath = getEnvPath;
1644
1728
  exports.getFetchVars = getFetchVars;
@@ -1659,6 +1743,7 @@ exports.getSchemasPath = getSchemasPath;
1659
1743
  exports.getSrcDir = getSrcDir;
1660
1744
  exports.getSubstring = getSubstring;
1661
1745
  exports.getToken = getToken;
1746
+ exports.getUrl = getUrl;
1662
1747
  exports.getWindowHost = getWindowHost;
1663
1748
  exports.getWindowProp = getWindowProp;
1664
1749
  exports.get_app_config_url = get_app_config_url;