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