@putkoff/abstract-utilities 0.1.182 → 0.1.184

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
@@ -1303,31 +1303,31 @@ function ensureAbstractUrl(endpoint, slices = []) {
1303
1303
  `${windowHost}/api` // etc, if you need it
1304
1304
  ];
1305
1305
  const stripped = stripPrefixes(endpoint, normalized);
1306
- console.log('BUILD PREFIX:', prefix);
1307
- console.log('RAW ENDPOINT:', endpoint);
1308
- console.log('STRIPPED ENDPT:', stripped);
1309
1306
  return make_path(prefix, stripped);
1310
1307
  }
1311
1308
  /**
1312
1309
  * Given an “endpoint” slug like "api/list", build the full URL
1313
1310
  * from the BASE_API_URL entry in your JSON config.
1311
+ * If anything goes wrong, just returns the raw endpoint.
1314
1312
  */
1315
1313
  function get_app_config_url(endpoint) {
1316
1314
  return __awaiter(this, void 0, void 0, function* () {
1317
- // 1) normalize + strip prefixes
1318
1315
  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);
1316
+ try {
1317
+ const vars = getFetchVars(null, 'POST', {
1318
+ key: 'BASE_API_URL',
1319
+ path: '/var/www/abstractendeavors/secure-files/public/config.json'
1320
+ });
1321
+ const resp = yield fetch(ensureAbstractUrl('/secure_env'), vars);
1322
+ checkResponse(resp);
1323
+ const json = yield resp.json();
1324
+ const baseUrl = getResult(json);
1325
+ return make_path(baseUrl, clean);
1326
+ }
1327
+ catch (err) {
1328
+ console.warn(`[get_app_config_url] failed, falling back to raw endpoint:`, endpoint, err);
1329
+ return endpoint;
1330
+ }
1331
1331
  });
1332
1332
  }
1333
1333
  /**
@@ -1428,30 +1428,51 @@ function checkResponse(res) {
1428
1428
  }
1429
1429
  return res;
1430
1430
  }
1431
- function fetchIt(url_1) {
1432
- return __awaiter(this, arguments, void 0, function* (url, body = null, method = null, headers = null, blob = false, configUrl = false, withCredentials = true, returnJson = true, returnReult = true) {
1433
- method = method || "GET";
1434
- if (!configUrl) {
1435
- url = yield get_app_config_url(url);
1431
+ function fetchIt(endpoint_1) {
1432
+ 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
+ const verb = (method || 'GET').toUpperCase();
1434
+ // 1) auto-detect absolute URLs, or use API lookup, but never throw
1435
+ let url;
1436
+ try {
1437
+ const isAbsolute = typeof endpoint === 'string' && /^https?:\/\//i.test(endpoint);
1438
+ if (isAbsolute || noApi) {
1439
+ url = endpoint;
1440
+ }
1441
+ else {
1442
+ url = yield get_app_config_url(endpoint);
1443
+ }
1444
+ }
1445
+ catch (_a) {
1446
+ url = endpoint;
1436
1447
  }
1437
- // headers: JSON by default, plus any auth + overrides
1438
- headers = Object.assign(Object.assign(Object.assign({}, (body instanceof FormData ? {} : { "Content-Type": "application/json" })), getAuthorizationHeader()), headers);
1448
+ // 2) prepare headers & body
1449
+ const authHeaders = Object.assign({}, (body instanceof FormData ? {} : { 'Content-Type': 'application/json' }));
1450
+ headers = Object.assign(Object.assign(Object.assign({}, authHeaders), getFetchVars(null, verb, body).headers), headers);
1439
1451
  const opts = {
1440
- method: method.toUpperCase(),
1441
- credentials: withCredentials ? "include" : "same-origin",
1452
+ method: verb,
1453
+ credentials: withCredentials ? 'include' : 'same-origin',
1442
1454
  headers,
1443
1455
  body: body instanceof FormData
1444
1456
  ? body
1445
- : body != null && method.toUpperCase() !== "GET"
1457
+ : body != null && verb !== 'GET'
1446
1458
  ? JSON.stringify(body)
1447
1459
  : undefined,
1448
1460
  };
1449
- console.debug("➡️ secureFetchIt", url, opts);
1450
- const res = yield fetch(url, opts);
1451
- if (!res.ok) {
1452
- const err = yield res.text();
1453
- throw new Error(`HTTP ${res.status}: ${err}`);
1461
+ console.debug('➡️ fetchIt', url, opts);
1462
+ // 3) perform fetch, swallow any errors and return raw URL on failure
1463
+ let res;
1464
+ try {
1465
+ res = yield fetch(url, opts);
1466
+ if (!res.ok) {
1467
+ const err = yield res.text();
1468
+ throw new Error(`HTTP ${res.status}: ${err}`);
1469
+ }
1470
+ }
1471
+ catch (err) {
1472
+ console.warn(`[fetchIt] failed for ${url}, returning raw endpoint:`, err);
1473
+ return url;
1454
1474
  }
1475
+ // 4) produce the expected result
1455
1476
  if (blob)
1456
1477
  return res.blob();
1457
1478
  if (returnReult)