@putkoff/abstract-utilities 0.1.181 → 0.1.183
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 +121 -80
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.js +122 -80
- package/dist/esm/index.js.map +1 -1
- package/dist/functions/config_utils/imports.d.ts +1 -0
- package/dist/functions/fetch_utils/imports.d.ts +1 -0
- package/dist/functions/index.d.ts +1 -0
- package/dist/functions/read_utils/imports.d.ts +1 -0
- package/dist/functions/read_utils/src/read_utils.d.ts +4 -7
- package/package.json +1 -1
package/dist/cjs/index.js
CHANGED
|
@@ -1154,43 +1154,122 @@ function alertit(obj = null) {
|
|
|
1154
1154
|
alert(msg);
|
|
1155
1155
|
}
|
|
1156
1156
|
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1157
|
+
// src/functions/read_utils/src/read_utils.ts
|
|
1158
|
+
/**
|
|
1159
|
+
* Reads a JSON file, either via Node’s fs (if available)
|
|
1160
|
+
* or via window.fetch in the browser. Never throws — returns
|
|
1161
|
+
* the parsed object or null on any error.
|
|
1162
|
+
*/
|
|
1163
|
+
function readJsonFile(relativeOrAbsolutePath) {
|
|
1164
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1165
|
+
// 1) Try Node.js fs
|
|
1166
|
+
if (typeof process !== 'undefined' &&
|
|
1167
|
+
process.versions != null &&
|
|
1168
|
+
process.versions.node) {
|
|
1165
1169
|
try {
|
|
1166
|
-
|
|
1170
|
+
const fs = yield import('fs');
|
|
1171
|
+
const path = yield import('path');
|
|
1172
|
+
const filePath = path.isAbsolute(relativeOrAbsolutePath)
|
|
1173
|
+
? relativeOrAbsolutePath
|
|
1174
|
+
: path.resolve(process.cwd(), relativeOrAbsolutePath);
|
|
1175
|
+
const text = yield fs.promises.readFile(filePath, 'utf8');
|
|
1176
|
+
return JSON.parse(text);
|
|
1167
1177
|
}
|
|
1168
1178
|
catch (_a) {
|
|
1169
|
-
//
|
|
1179
|
+
// swallow and fall back to browser
|
|
1170
1180
|
}
|
|
1171
1181
|
}
|
|
1172
|
-
// 2)
|
|
1182
|
+
// 2) Try browser fetch
|
|
1183
|
+
const fetchFn = safeGlobalProp('fetch');
|
|
1184
|
+
if (typeof fetchFn !== 'function') {
|
|
1185
|
+
return null;
|
|
1186
|
+
}
|
|
1187
|
+
// Resolve URL against document.baseURI if possible
|
|
1188
|
+
let url = relativeOrAbsolutePath;
|
|
1189
|
+
const baseURI = safeGlobalProp('document', 'baseURI');
|
|
1190
|
+
if (baseURI) {
|
|
1191
|
+
try {
|
|
1192
|
+
url = new URL(relativeOrAbsolutePath, baseURI).href;
|
|
1193
|
+
}
|
|
1194
|
+
catch (_b) {
|
|
1195
|
+
// keep url as-is
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
try {
|
|
1199
|
+
const res = yield fetchFn(url);
|
|
1200
|
+
if (!res.ok)
|
|
1201
|
+
return null;
|
|
1202
|
+
return (yield res.json());
|
|
1203
|
+
}
|
|
1204
|
+
catch (_c) {
|
|
1205
|
+
return null;
|
|
1206
|
+
}
|
|
1207
|
+
});
|
|
1208
|
+
}
|
|
1209
|
+
|
|
1210
|
+
// src/functions/config_utils/src/config_utils.ts
|
|
1211
|
+
let _cachedConfig = null;
|
|
1212
|
+
function loadConfig() {
|
|
1213
|
+
return __awaiter(this, arguments, void 0, function* (filePath = null) {
|
|
1214
|
+
var _a;
|
|
1173
1215
|
if (_cachedConfig) {
|
|
1174
1216
|
return _cachedConfig;
|
|
1175
1217
|
}
|
|
1176
|
-
//
|
|
1177
|
-
|
|
1178
|
-
if (
|
|
1179
|
-
|
|
1218
|
+
// 1) figure out where config.json lives
|
|
1219
|
+
let configUrl;
|
|
1220
|
+
if (filePath) {
|
|
1221
|
+
configUrl = filePath;
|
|
1180
1222
|
}
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1223
|
+
else if (typeof ({ url: (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.js', document.baseURI).href)) }) !== 'undefined' && typeof (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.js', document.baseURI).href)) === 'string') {
|
|
1224
|
+
// ES module: resolve relative to this file
|
|
1225
|
+
try {
|
|
1226
|
+
configUrl = new URL('./config.json', (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.js', document.baseURI).href))).href;
|
|
1227
|
+
}
|
|
1228
|
+
catch (_b) {
|
|
1229
|
+
configUrl = 'config.json';
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
1232
|
+
else {
|
|
1233
|
+
// browser fallback
|
|
1234
|
+
const baseURI = safeGlobalProp('document', 'baseURI');
|
|
1235
|
+
try {
|
|
1236
|
+
configUrl =
|
|
1237
|
+
typeof baseURI === 'string'
|
|
1238
|
+
? new URL('config.json', baseURI).href
|
|
1239
|
+
: 'config.json';
|
|
1186
1240
|
}
|
|
1187
|
-
|
|
1188
|
-
|
|
1241
|
+
catch (_c) {
|
|
1242
|
+
configUrl = 'config.json';
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
// 2) if we have a fetch, try HTTP(S)
|
|
1246
|
+
const fetchFn = safeGlobalProp('fetch');
|
|
1247
|
+
if (typeof fetchFn === 'function') {
|
|
1248
|
+
try {
|
|
1249
|
+
const res = yield fetchFn(configUrl);
|
|
1250
|
+
if (res.ok) {
|
|
1251
|
+
const json = yield res.json();
|
|
1252
|
+
// cache & return
|
|
1253
|
+
_cachedConfig = (_a = json) !== null && _a !== void 0 ? _a : {};
|
|
1254
|
+
return _cachedConfig;
|
|
1255
|
+
}
|
|
1256
|
+
}
|
|
1257
|
+
catch (_d) {
|
|
1258
|
+
/* swallow */
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1261
|
+
// 3) Node fallback: try reading from disk (requires your readJsonFile util)
|
|
1262
|
+
try {
|
|
1263
|
+
const disk = yield readJsonFile(configUrl);
|
|
1264
|
+
_cachedConfig = disk !== null && disk !== void 0 ? disk : {};
|
|
1189
1265
|
return _cachedConfig;
|
|
1190
1266
|
}
|
|
1191
|
-
catch (
|
|
1192
|
-
|
|
1267
|
+
catch (_e) {
|
|
1268
|
+
/* swallow */
|
|
1193
1269
|
}
|
|
1270
|
+
// 4) if all else fails, return an empty config
|
|
1271
|
+
_cachedConfig = {};
|
|
1272
|
+
return _cachedConfig;
|
|
1194
1273
|
});
|
|
1195
1274
|
}
|
|
1196
1275
|
function getConfig(key) {
|
|
@@ -1349,21 +1428,31 @@ function checkResponse(res) {
|
|
|
1349
1428
|
}
|
|
1350
1429
|
return res;
|
|
1351
1430
|
}
|
|
1352
|
-
function fetchIt(
|
|
1353
|
-
return __awaiter(this, arguments, void 0, function* (
|
|
1354
|
-
method = method || "GET";
|
|
1355
|
-
|
|
1356
|
-
|
|
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
|
+
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
|
|
1437
|
+
let url;
|
|
1438
|
+
if (isAbsolute) {
|
|
1439
|
+
url = endpoint;
|
|
1440
|
+
}
|
|
1441
|
+
else if (noApi) {
|
|
1442
|
+
url = endpoint;
|
|
1357
1443
|
}
|
|
1358
|
-
|
|
1444
|
+
else {
|
|
1445
|
+
url = yield get_app_config_url(endpoint);
|
|
1446
|
+
}
|
|
1447
|
+
// 3) prepare headers & body
|
|
1359
1448
|
headers = Object.assign(Object.assign(Object.assign({}, (body instanceof FormData ? {} : { "Content-Type": "application/json" })), getAuthorizationHeader()), headers);
|
|
1360
1449
|
const opts = {
|
|
1361
|
-
method
|
|
1450
|
+
method,
|
|
1362
1451
|
credentials: withCredentials ? "include" : "same-origin",
|
|
1363
1452
|
headers,
|
|
1364
1453
|
body: body instanceof FormData
|
|
1365
1454
|
? body
|
|
1366
|
-
: body != null && method
|
|
1455
|
+
: body != null && method !== "GET"
|
|
1367
1456
|
? JSON.stringify(body)
|
|
1368
1457
|
: undefined,
|
|
1369
1458
|
};
|
|
@@ -1484,53 +1573,6 @@ function Spinner() {
|
|
|
1484
1573
|
return (jsxRuntime.jsx("p", { className: 'animate-pulse', children: "Loading\u2026" }));
|
|
1485
1574
|
}
|
|
1486
1575
|
|
|
1487
|
-
// src/read_utils.ts
|
|
1488
|
-
/**
|
|
1489
|
-
* Attempt to load the Node-only modules.
|
|
1490
|
-
* Returns { fs: null, path: null } in the browser.
|
|
1491
|
-
*/
|
|
1492
|
-
function tryNodeModules() {
|
|
1493
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1494
|
-
try {
|
|
1495
|
-
const fsMod = yield import('fs');
|
|
1496
|
-
const pathMod = yield import('path');
|
|
1497
|
-
return {
|
|
1498
|
-
fs: fsMod.promises, // keep the `promises` API
|
|
1499
|
-
path: pathMod
|
|
1500
|
-
};
|
|
1501
|
-
}
|
|
1502
|
-
catch (_a) {
|
|
1503
|
-
return { fs: null, path: null };
|
|
1504
|
-
}
|
|
1505
|
-
});
|
|
1506
|
-
}
|
|
1507
|
-
/**
|
|
1508
|
-
* Reads a file at `relativeOrAbsolutePath` and returns its contents as a string.
|
|
1509
|
-
* In a browser this will reject immediately.
|
|
1510
|
-
*/
|
|
1511
|
-
function readFileContents(relativeOrAbsolutePath) {
|
|
1512
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1513
|
-
const { fs, path } = yield tryNodeModules();
|
|
1514
|
-
if (!fs || !path) {
|
|
1515
|
-
throw new Error('readFileContents can only be used in Node.js');
|
|
1516
|
-
}
|
|
1517
|
-
// resolve absolute
|
|
1518
|
-
const filePath = path.isAbsolute(relativeOrAbsolutePath)
|
|
1519
|
-
? relativeOrAbsolutePath
|
|
1520
|
-
: path.resolve(__dirname, relativeOrAbsolutePath);
|
|
1521
|
-
return fs.readFile(filePath, 'utf8');
|
|
1522
|
-
});
|
|
1523
|
-
}
|
|
1524
|
-
/**
|
|
1525
|
-
* Reads a JSON file and returns the parsed object.
|
|
1526
|
-
*/
|
|
1527
|
-
function readJsonFile(relativeOrAbsolutePath) {
|
|
1528
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1529
|
-
const text = yield readFileContents(relativeOrAbsolutePath);
|
|
1530
|
-
return JSON.parse(text);
|
|
1531
|
-
});
|
|
1532
|
-
}
|
|
1533
|
-
|
|
1534
1576
|
Object.defineProperty(exports, "useCallback", {
|
|
1535
1577
|
enumerable: true,
|
|
1536
1578
|
get: function () { return react.useCallback; }
|
|
@@ -1636,7 +1678,6 @@ exports.make_path = make_path;
|
|
|
1636
1678
|
exports.make_sanitized_path = make_sanitized_path;
|
|
1637
1679
|
exports.normalizeUrl = normalizeUrl;
|
|
1638
1680
|
exports.parseResult = parseResult;
|
|
1639
|
-
exports.readFileContents = readFileContents;
|
|
1640
1681
|
exports.readJsonFile = readJsonFile;
|
|
1641
1682
|
exports.requestPatch = requestPatch;
|
|
1642
1683
|
exports.requireToken = requireToken;
|