@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/esm/index.js CHANGED
@@ -1151,43 +1151,122 @@ function alertit(obj = null) {
1151
1151
  alert(msg);
1152
1152
  }
1153
1153
 
1154
- let _cachedConfig = null;
1155
- function loadConfig() {
1156
- return __awaiter(this, arguments, void 0, function* (filePath = null) {
1157
- const relativePath = filePath || "config.json";
1158
- // 1) Resolve URL against document.baseURI if available
1159
- let configUrl = relativePath;
1160
- const baseURI = safeGlobalProp("document", "baseURI");
1161
- if (baseURI) {
1154
+ // src/functions/read_utils/src/read_utils.ts
1155
+ /**
1156
+ * Reads a JSON file, either via Node’s fs (if available)
1157
+ * or via window.fetch in the browser. Never throws — returns
1158
+ * the parsed object or null on any error.
1159
+ */
1160
+ function readJsonFile(relativeOrAbsolutePath) {
1161
+ return __awaiter(this, void 0, void 0, function* () {
1162
+ // 1) Try Node.js fs
1163
+ if (typeof process !== 'undefined' &&
1164
+ process.versions != null &&
1165
+ process.versions.node) {
1162
1166
  try {
1163
- configUrl = new URL(relativePath, baseURI).href;
1167
+ const fs = yield import('fs');
1168
+ const path = yield import('path');
1169
+ const filePath = path.isAbsolute(relativeOrAbsolutePath)
1170
+ ? relativeOrAbsolutePath
1171
+ : path.resolve(process.cwd(), relativeOrAbsolutePath);
1172
+ const text = yield fs.promises.readFile(filePath, 'utf8');
1173
+ return JSON.parse(text);
1164
1174
  }
1165
1175
  catch (_a) {
1166
- // ignore—keep configUrl = relativePath
1176
+ // swallow and fall back to browser
1167
1177
  }
1168
1178
  }
1169
- // 2) Return cache if we’ve already fetched once
1179
+ // 2) Try browser fetch
1180
+ const fetchFn = safeGlobalProp('fetch');
1181
+ if (typeof fetchFn !== 'function') {
1182
+ return null;
1183
+ }
1184
+ // Resolve URL against document.baseURI if possible
1185
+ let url = relativeOrAbsolutePath;
1186
+ const baseURI = safeGlobalProp('document', 'baseURI');
1187
+ if (baseURI) {
1188
+ try {
1189
+ url = new URL(relativeOrAbsolutePath, baseURI).href;
1190
+ }
1191
+ catch (_b) {
1192
+ // keep url as-is
1193
+ }
1194
+ }
1195
+ try {
1196
+ const res = yield fetchFn(url);
1197
+ if (!res.ok)
1198
+ return null;
1199
+ return (yield res.json());
1200
+ }
1201
+ catch (_c) {
1202
+ return null;
1203
+ }
1204
+ });
1205
+ }
1206
+
1207
+ // src/functions/config_utils/src/config_utils.ts
1208
+ let _cachedConfig = null;
1209
+ function loadConfig() {
1210
+ return __awaiter(this, arguments, void 0, function* (filePath = null) {
1211
+ var _a;
1170
1212
  if (_cachedConfig) {
1171
1213
  return _cachedConfig;
1172
1214
  }
1173
- // 3) Bail out in non-browser if fetch isn’t present
1174
- const fetchFn = safeGlobalProp("fetch");
1175
- if (typeof fetchFn !== "function") {
1176
- return {};
1215
+ // 1) figure out where config.json lives
1216
+ let configUrl;
1217
+ if (filePath) {
1218
+ configUrl = filePath;
1177
1219
  }
1178
- // 4) Try to fetch + parse, swallowing any errors
1179
- try {
1180
- const res = yield fetchFn(configUrl).catch(() => null);
1181
- if (!res || !res.ok) {
1182
- return {};
1220
+ else if (typeof import.meta !== 'undefined' && typeof import.meta.url === 'string') {
1221
+ // ES module: resolve relative to this file
1222
+ try {
1223
+ configUrl = new URL('./config.json', import.meta.url).href;
1224
+ }
1225
+ catch (_b) {
1226
+ configUrl = 'config.json';
1227
+ }
1228
+ }
1229
+ else {
1230
+ // browser fallback
1231
+ const baseURI = safeGlobalProp('document', 'baseURI');
1232
+ try {
1233
+ configUrl =
1234
+ typeof baseURI === 'string'
1235
+ ? new URL('config.json', baseURI).href
1236
+ : 'config.json';
1183
1237
  }
1184
- // parse JSON
1185
- _cachedConfig = (yield res.json());
1238
+ catch (_c) {
1239
+ configUrl = 'config.json';
1240
+ }
1241
+ }
1242
+ // 2) if we have a fetch, try HTTP(S)
1243
+ const fetchFn = safeGlobalProp('fetch');
1244
+ if (typeof fetchFn === 'function') {
1245
+ try {
1246
+ const res = yield fetchFn(configUrl);
1247
+ if (res.ok) {
1248
+ const json = yield res.json();
1249
+ // cache & return
1250
+ _cachedConfig = (_a = json) !== null && _a !== void 0 ? _a : {};
1251
+ return _cachedConfig;
1252
+ }
1253
+ }
1254
+ catch (_d) {
1255
+ /* swallow */
1256
+ }
1257
+ }
1258
+ // 3) Node fallback: try reading from disk (requires your readJsonFile util)
1259
+ try {
1260
+ const disk = yield readJsonFile(configUrl);
1261
+ _cachedConfig = disk !== null && disk !== void 0 ? disk : {};
1186
1262
  return _cachedConfig;
1187
1263
  }
1188
- catch (_b) {
1189
- return {};
1264
+ catch (_e) {
1265
+ /* swallow */
1190
1266
  }
1267
+ // 4) if all else fails, return an empty config
1268
+ _cachedConfig = {};
1269
+ return _cachedConfig;
1191
1270
  });
1192
1271
  }
1193
1272
  function getConfig(key) {
@@ -1346,21 +1425,31 @@ function checkResponse(res) {
1346
1425
  }
1347
1426
  return res;
1348
1427
  }
1349
- function fetchIt(url_1) {
1350
- return __awaiter(this, arguments, void 0, function* (url, body = null, method = null, headers = null, blob = false, configUrl = false, withCredentials = true, returnJson = true, returnReult = true) {
1351
- method = method || "GET";
1352
- if (!configUrl) {
1353
- 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
+ method = (method || "GET").toUpperCase();
1431
+ // 1) auto-detect absolute URLs
1432
+ const isAbsolute = typeof endpoint === "string" && /^https?:\/\//i.test(endpoint);
1433
+ // 2) choose the URL
1434
+ let url;
1435
+ if (isAbsolute) {
1436
+ url = endpoint;
1437
+ }
1438
+ else if (noApi) {
1439
+ url = endpoint;
1354
1440
  }
1355
- // headers: JSON by default, plus any auth + overrides
1441
+ else {
1442
+ url = yield get_app_config_url(endpoint);
1443
+ }
1444
+ // 3) prepare headers & body
1356
1445
  headers = Object.assign(Object.assign(Object.assign({}, (body instanceof FormData ? {} : { "Content-Type": "application/json" })), getAuthorizationHeader()), headers);
1357
1446
  const opts = {
1358
- method: method.toUpperCase(),
1447
+ method,
1359
1448
  credentials: withCredentials ? "include" : "same-origin",
1360
1449
  headers,
1361
1450
  body: body instanceof FormData
1362
1451
  ? body
1363
- : body != null && method.toUpperCase() !== "GET"
1452
+ : body != null && method !== "GET"
1364
1453
  ? JSON.stringify(body)
1365
1454
  : undefined,
1366
1455
  };
@@ -1481,52 +1570,5 @@ function Spinner() {
1481
1570
  return (jsx("p", { className: 'animate-pulse', children: "Loading\u2026" }));
1482
1571
  }
1483
1572
 
1484
- // src/read_utils.ts
1485
- /**
1486
- * Attempt to load the Node-only modules.
1487
- * Returns { fs: null, path: null } in the browser.
1488
- */
1489
- function tryNodeModules() {
1490
- return __awaiter(this, void 0, void 0, function* () {
1491
- try {
1492
- const fsMod = yield import('fs');
1493
- const pathMod = yield import('path');
1494
- return {
1495
- fs: fsMod.promises, // keep the `promises` API
1496
- path: pathMod
1497
- };
1498
- }
1499
- catch (_a) {
1500
- return { fs: null, path: null };
1501
- }
1502
- });
1503
- }
1504
- /**
1505
- * Reads a file at `relativeOrAbsolutePath` and returns its contents as a string.
1506
- * In a browser this will reject immediately.
1507
- */
1508
- function readFileContents(relativeOrAbsolutePath) {
1509
- return __awaiter(this, void 0, void 0, function* () {
1510
- const { fs, path } = yield tryNodeModules();
1511
- if (!fs || !path) {
1512
- throw new Error('readFileContents can only be used in Node.js');
1513
- }
1514
- // resolve absolute
1515
- const filePath = path.isAbsolute(relativeOrAbsolutePath)
1516
- ? relativeOrAbsolutePath
1517
- : path.resolve(__dirname, relativeOrAbsolutePath);
1518
- return fs.readFile(filePath, 'utf8');
1519
- });
1520
- }
1521
- /**
1522
- * Reads a JSON file and returns the parsed object.
1523
- */
1524
- function readJsonFile(relativeOrAbsolutePath) {
1525
- return __awaiter(this, void 0, void 0, function* () {
1526
- const text = yield readFileContents(relativeOrAbsolutePath);
1527
- return JSON.parse(text);
1528
- });
1529
- }
1530
-
1531
- export { API_PREFIX, BASE_URL, Button, Checkbox, DEV_PREFIX, DOMAIN_NAME, Input, PROD_PREFIX, PROTOCOL, SUB_DIR, Spinner, alertit, api, callStorage, callWindowMethod, checkResponse, create_list_string, currentUsername, currentUsernames, decodeJwt, eatAll, eatEnd, eatInner, eatOuter, ensureAbstractUrl, ensure_list, fetchIndexHtml, fetchIndexHtmlContainer, fetchIt, fetchSharePatch, geAuthsUtilsDirectory, geBackupsUtilsDirectory, geConstantsUtilsDirectory, geEnvUtilsDirectory, geFetchUtilsDirectory, geFileUtilsDirectory, gePathUtilsDirectory, geStaticDirectory, geStringUtilsDirectory, geTypeUtilsDirectory, getAbsDir, getAbsPath, getAuthorizationHeader, getBaseDir, getBody, getComponentsUtilsDirectory, getConfig, getDbConfigsPath, getDistDir, getDocumentProp, getEnvDir, getEnvPath, getFetchVars, getFunctionsDir, getFunctionsUtilsDirectory, getHeaders, getHooksUtilsDirectory, getHtmlDirectory, getLibUtilsDirectory, getMethod, getPublicDir, getResult, getSafeDocument, getSafeLocalStorage, getSafeWindow, getSchemasDirPath, getSchemasPath, getSrcDir, getSubstring, getToken, getWindowHost, getWindowProp, get_app_config_url, get_basename, get_dirname, get_extname, get_filename, get_splitext, get_window, get_window_location, get_window_parts, get_window_pathname, isLoggedIn, isTokenExpired, loadConfig, make_path, make_sanitized_path, normalizeUrl, parseResult, readFileContents, readJsonFile, requestPatch, requireToken, safeGlobalProp, safeStorage, sanitizeFilename, secureFetchIt, stripHost, stripPrefixes, truncateString, tryParse };
1573
+ export { API_PREFIX, BASE_URL, Button, Checkbox, DEV_PREFIX, DOMAIN_NAME, Input, PROD_PREFIX, PROTOCOL, SUB_DIR, Spinner, alertit, api, callStorage, callWindowMethod, checkResponse, create_list_string, currentUsername, currentUsernames, decodeJwt, eatAll, eatEnd, eatInner, eatOuter, ensureAbstractUrl, ensure_list, fetchIndexHtml, fetchIndexHtmlContainer, fetchIt, fetchSharePatch, geAuthsUtilsDirectory, geBackupsUtilsDirectory, geConstantsUtilsDirectory, geEnvUtilsDirectory, geFetchUtilsDirectory, geFileUtilsDirectory, gePathUtilsDirectory, geStaticDirectory, geStringUtilsDirectory, geTypeUtilsDirectory, getAbsDir, getAbsPath, getAuthorizationHeader, getBaseDir, getBody, getComponentsUtilsDirectory, getConfig, getDbConfigsPath, getDistDir, getDocumentProp, getEnvDir, getEnvPath, getFetchVars, getFunctionsDir, getFunctionsUtilsDirectory, getHeaders, getHooksUtilsDirectory, getHtmlDirectory, getLibUtilsDirectory, getMethod, getPublicDir, getResult, getSafeDocument, getSafeLocalStorage, getSafeWindow, getSchemasDirPath, getSchemasPath, getSrcDir, getSubstring, getToken, getWindowHost, getWindowProp, get_app_config_url, get_basename, get_dirname, get_extname, get_filename, get_splitext, get_window, get_window_location, get_window_parts, get_window_pathname, isLoggedIn, isTokenExpired, loadConfig, make_path, make_sanitized_path, normalizeUrl, parseResult, readJsonFile, requestPatch, requireToken, safeGlobalProp, safeStorage, sanitizeFilename, secureFetchIt, stripHost, stripPrefixes, truncateString, tryParse };
1532
1574
  //# sourceMappingURL=index.js.map