@putkoff/abstract-utilities 0.1.187 → 0.1.189
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,56 +1303,6 @@ function getConfigVar() {
|
|
|
1303
1303
|
}
|
|
1304
1304
|
|
|
1305
1305
|
// Constructs API URL from endpoint
|
|
1306
|
-
function api(endpoint) {
|
|
1307
|
-
return ensureAbstractUrl(endpoint);
|
|
1308
|
-
}
|
|
1309
|
-
/**
|
|
1310
|
-
* Strip a leading host (http://host) from any URL-like string.
|
|
1311
|
-
*/
|
|
1312
|
-
function stripHost(str) {
|
|
1313
|
-
// now also removes "abstractendeavors.com" even without protocol
|
|
1314
|
-
const hostPattern = `(?:https?:\\/\\/)?${getWindowHost()}`;
|
|
1315
|
-
return str.replace(new RegExp(`^${hostPattern}`), '');
|
|
1316
|
-
}
|
|
1317
|
-
function ensureAbstractUrl(endpoint, slices = []) {
|
|
1318
|
-
slices = slices || ['https//abstractendeavors.com', 'api'];
|
|
1319
|
-
// 1) build a prefix string like "api/v1/"
|
|
1320
|
-
const prefix = slices.map((s) => `${s}/`).join("");
|
|
1321
|
-
const windowHost = getWindowHost();
|
|
1322
|
-
const normalized = [
|
|
1323
|
-
'/',
|
|
1324
|
-
...slices,
|
|
1325
|
-
windowHost, // so "abstractendeavors.com" will be stripped
|
|
1326
|
-
`${windowHost}/api` // etc, if you need it
|
|
1327
|
-
];
|
|
1328
|
-
const stripped = stripPrefixes(endpoint, normalized);
|
|
1329
|
-
return make_path(prefix, stripped);
|
|
1330
|
-
}
|
|
1331
|
-
/**
|
|
1332
|
-
* Given an “endpoint” slug like "api/list", build the full URL
|
|
1333
|
-
* from the BASE_API_URL entry in your JSON config.
|
|
1334
|
-
* If anything goes wrong, just returns the raw endpoint.
|
|
1335
|
-
*/
|
|
1336
|
-
function get_app_config_url(endpoint) {
|
|
1337
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1338
|
-
const clean = stripHost(endpoint);
|
|
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
|
-
}
|
|
1354
|
-
});
|
|
1355
|
-
}
|
|
1356
1306
|
/**
|
|
1357
1307
|
* Unwraps nested { result } fields until you hit a non-object or no more "result" keys.
|
|
1358
1308
|
*/
|
|
@@ -1452,50 +1402,28 @@ function checkResponse(res) {
|
|
|
1452
1402
|
return res;
|
|
1453
1403
|
}
|
|
1454
1404
|
function fetchIt(endpoint_1) {
|
|
1455
|
-
return __awaiter(this, arguments, void 0, function* (endpoint, body = null, method = null, headers = null, blob = false,
|
|
1456
|
-
|
|
1457
|
-
//
|
|
1458
|
-
let url;
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
if (isAbsolute || noApi) {
|
|
1462
|
-
url = endpoint;
|
|
1463
|
-
}
|
|
1464
|
-
else {
|
|
1465
|
-
url = yield get_app_config_url(endpoint);
|
|
1466
|
-
}
|
|
1467
|
-
}
|
|
1468
|
-
catch (_a) {
|
|
1469
|
-
url = endpoint;
|
|
1470
|
-
}
|
|
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);
|
|
1405
|
+
return __awaiter(this, arguments, void 0, function* (endpoint, body = null, method = null, headers = null, blob = false, configUrl = false, withCredentials = true, returnJson = true, returnReult = true) {
|
|
1406
|
+
method = (method || "GET").toUpperCase();
|
|
1407
|
+
// 2) choose the URL
|
|
1408
|
+
let url = endpoint;
|
|
1409
|
+
// 3) prepare headers & body
|
|
1410
|
+
headers = Object.assign(Object.assign(Object.assign({}, (body instanceof FormData ? {} : { "Content-Type": "application/json" })), getAuthorizationHeader()), headers);
|
|
1474
1411
|
const opts = {
|
|
1475
|
-
method
|
|
1476
|
-
credentials: withCredentials ?
|
|
1412
|
+
method,
|
|
1413
|
+
credentials: withCredentials ? "include" : "same-origin",
|
|
1477
1414
|
headers,
|
|
1478
1415
|
body: body instanceof FormData
|
|
1479
1416
|
? body
|
|
1480
|
-
: body != null &&
|
|
1417
|
+
: body != null && method !== "GET"
|
|
1481
1418
|
? JSON.stringify(body)
|
|
1482
1419
|
: undefined,
|
|
1483
1420
|
};
|
|
1484
|
-
console.debug(
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
if (!res.ok) {
|
|
1490
|
-
const err = yield res.text();
|
|
1491
|
-
throw new Error(`HTTP ${res.status}: ${err}`);
|
|
1492
|
-
}
|
|
1493
|
-
}
|
|
1494
|
-
catch (err) {
|
|
1495
|
-
console.warn(`[fetchIt] failed for ${url}, returning raw endpoint:`, err);
|
|
1496
|
-
return url;
|
|
1421
|
+
console.debug("➡️ secureFetchIt →", url, opts);
|
|
1422
|
+
const res = yield fetch(url, opts);
|
|
1423
|
+
if (!res.ok) {
|
|
1424
|
+
const err = yield res.text();
|
|
1425
|
+
throw new Error(`HTTP ${res.status}: ${err}`);
|
|
1497
1426
|
}
|
|
1498
|
-
// 4) produce the expected result
|
|
1499
1427
|
if (blob)
|
|
1500
1428
|
return res.blob();
|
|
1501
1429
|
if (returnReult)
|
|
@@ -1505,46 +1433,6 @@ function fetchIt(endpoint_1) {
|
|
|
1505
1433
|
return res;
|
|
1506
1434
|
});
|
|
1507
1435
|
}
|
|
1508
|
-
|
|
1509
|
-
function secureFetchIt(endpoint_1) {
|
|
1510
|
-
return __awaiter(this, arguments, void 0, function* (endpoint, body = null, method = null, customHeaders = null, blob = false, configUrl = false, withCredentials = true, returnJson = true, returnReult = true) {
|
|
1511
|
-
return yield fetchIt(yield getEndpoint(endpoint), body, method, customHeaders, blob, configUrl, withCredentials, returnJson, returnReult);
|
|
1512
|
-
});
|
|
1513
|
-
}
|
|
1514
|
-
// Performs PATCH request
|
|
1515
|
-
function requestPatch(url_1) {
|
|
1516
|
-
return __awaiter(this, arguments, void 0, function* (url, body = null) {
|
|
1517
|
-
const variables = getFetchVars(null, 'PATCH', body);
|
|
1518
|
-
return yield fetch(yield get_app_config_url(url), variables);
|
|
1519
|
-
});
|
|
1520
|
-
}
|
|
1521
|
-
// Performs PATCH request for file sharing
|
|
1522
|
-
function fetchSharePatch(file_1) {
|
|
1523
|
-
return __awaiter(this, arguments, void 0, function* (file, appKey = null) {
|
|
1524
|
-
const cleanEndpoint = '/files/share';
|
|
1525
|
-
// build final URL
|
|
1526
|
-
const url = yield get_app_config_url(cleanEndpoint);
|
|
1527
|
-
const token = callStorage('getItem', 'token');
|
|
1528
|
-
const body = JSON.stringify(file);
|
|
1529
|
-
const method = 'PATCH';
|
|
1530
|
-
const headers = {
|
|
1531
|
-
'Content-Type': 'application/json',
|
|
1532
|
-
'Authorization': `Bearer ${token}`,
|
|
1533
|
-
};
|
|
1534
|
-
const resp = yield fetch(url, {
|
|
1535
|
-
method,
|
|
1536
|
-
headers,
|
|
1537
|
-
body
|
|
1538
|
-
});
|
|
1539
|
-
if (!resp.ok) {
|
|
1540
|
-
console.error('Error from server', yield resp.text());
|
|
1541
|
-
}
|
|
1542
|
-
else {
|
|
1543
|
-
const data = yield resp.json();
|
|
1544
|
-
console.log('Success!', data);
|
|
1545
|
-
}
|
|
1546
|
-
});
|
|
1547
|
-
}
|
|
1548
1436
|
// Constructs HTML directory path
|
|
1549
1437
|
function getHtmlDirectory(directory, filename) {
|
|
1550
1438
|
return `${directory}/${filename}.html`;
|
|
@@ -1553,7 +1441,7 @@ function getHtmlDirectory(directory, filename) {
|
|
|
1553
1441
|
function fetchIndexHtml(filename_1) {
|
|
1554
1442
|
return __awaiter(this, arguments, void 0, function* (filename, directory = 'sf_index', base = 'html') {
|
|
1555
1443
|
const url = `/${base}/${directory}/${filename}.html`;
|
|
1556
|
-
const response = yield fetch(
|
|
1444
|
+
const response = yield fetch(url);
|
|
1557
1445
|
return yield response.text();
|
|
1558
1446
|
});
|
|
1559
1447
|
}
|
|
@@ -1571,51 +1459,6 @@ function fetchIndexHtmlContainer(filename_1) {
|
|
|
1571
1459
|
}
|
|
1572
1460
|
});
|
|
1573
1461
|
}
|
|
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
|
-
}
|
|
1619
1462
|
|
|
1620
1463
|
function Button(_a) {
|
|
1621
1464
|
var { children, color = 'gray', variant = 'default', className = '' } = _a, rest = __rest(_a, ["children", "color", "variant", "className"]);
|
|
@@ -1680,7 +1523,6 @@ exports.PROTOCOL = PROTOCOL;
|
|
|
1680
1523
|
exports.SUB_DIR = SUB_DIR;
|
|
1681
1524
|
exports.Spinner = Spinner;
|
|
1682
1525
|
exports.alertit = alertit;
|
|
1683
|
-
exports.api = api;
|
|
1684
1526
|
exports.callStorage = callStorage;
|
|
1685
1527
|
exports.callWindowMethod = callWindowMethod;
|
|
1686
1528
|
exports.checkResponse = checkResponse;
|
|
@@ -1692,12 +1534,10 @@ exports.eatAll = eatAll;
|
|
|
1692
1534
|
exports.eatEnd = eatEnd;
|
|
1693
1535
|
exports.eatInner = eatInner;
|
|
1694
1536
|
exports.eatOuter = eatOuter;
|
|
1695
|
-
exports.ensureAbstractUrl = ensureAbstractUrl;
|
|
1696
1537
|
exports.ensure_list = ensure_list;
|
|
1697
1538
|
exports.fetchIndexHtml = fetchIndexHtml;
|
|
1698
1539
|
exports.fetchIndexHtmlContainer = fetchIndexHtmlContainer;
|
|
1699
1540
|
exports.fetchIt = fetchIt;
|
|
1700
|
-
exports.fetchSharePatch = fetchSharePatch;
|
|
1701
1541
|
exports.geAuthsUtilsDirectory = geAuthsUtilsDirectory;
|
|
1702
1542
|
exports.geBackupsUtilsDirectory = geBackupsUtilsDirectory;
|
|
1703
1543
|
exports.geConstantsUtilsDirectory = geConstantsUtilsDirectory;
|
|
@@ -1712,7 +1552,6 @@ exports.getAbsDir = getAbsDir;
|
|
|
1712
1552
|
exports.getAbsPath = getAbsPath;
|
|
1713
1553
|
exports.getAuthorizationHeader = getAuthorizationHeader;
|
|
1714
1554
|
exports.getBaseDir = getBaseDir;
|
|
1715
|
-
exports.getBaseUrl = getBaseUrl;
|
|
1716
1555
|
exports.getBody = getBody;
|
|
1717
1556
|
exports.getComponentsUtilsDirectory = getComponentsUtilsDirectory;
|
|
1718
1557
|
exports.getConfig = getConfig;
|
|
@@ -1721,8 +1560,6 @@ exports.getConfigVar = getConfigVar;
|
|
|
1721
1560
|
exports.getDbConfigsPath = getDbConfigsPath;
|
|
1722
1561
|
exports.getDistDir = getDistDir;
|
|
1723
1562
|
exports.getDocumentProp = getDocumentProp;
|
|
1724
|
-
exports.getEndpoint = getEndpoint;
|
|
1725
|
-
exports.getEndpoints = getEndpoints;
|
|
1726
1563
|
exports.getEnvDir = getEnvDir;
|
|
1727
1564
|
exports.getEnvPath = getEnvPath;
|
|
1728
1565
|
exports.getFetchVars = getFetchVars;
|
|
@@ -1743,10 +1580,8 @@ exports.getSchemasPath = getSchemasPath;
|
|
|
1743
1580
|
exports.getSrcDir = getSrcDir;
|
|
1744
1581
|
exports.getSubstring = getSubstring;
|
|
1745
1582
|
exports.getToken = getToken;
|
|
1746
|
-
exports.getUrl = getUrl;
|
|
1747
1583
|
exports.getWindowHost = getWindowHost;
|
|
1748
1584
|
exports.getWindowProp = getWindowProp;
|
|
1749
|
-
exports.get_app_config_url = get_app_config_url;
|
|
1750
1585
|
exports.get_basename = get_basename;
|
|
1751
1586
|
exports.get_dirname = get_dirname;
|
|
1752
1587
|
exports.get_extname = get_extname;
|
|
@@ -1764,13 +1599,10 @@ exports.make_sanitized_path = make_sanitized_path;
|
|
|
1764
1599
|
exports.normalizeUrl = normalizeUrl;
|
|
1765
1600
|
exports.parseResult = parseResult;
|
|
1766
1601
|
exports.readJsonFile = readJsonFile;
|
|
1767
|
-
exports.requestPatch = requestPatch;
|
|
1768
1602
|
exports.requireToken = requireToken;
|
|
1769
1603
|
exports.safeGlobalProp = safeGlobalProp;
|
|
1770
1604
|
exports.safeStorage = safeStorage;
|
|
1771
1605
|
exports.sanitizeFilename = sanitizeFilename;
|
|
1772
|
-
exports.secureFetchIt = secureFetchIt;
|
|
1773
|
-
exports.stripHost = stripHost;
|
|
1774
1606
|
exports.stripPrefixes = stripPrefixes;
|
|
1775
1607
|
exports.truncateString = truncateString;
|
|
1776
1608
|
exports.tryParse = tryParse;
|