@putkoff/abstract-utilities 0.1.114 → 0.1.116

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.
Files changed (36) hide show
  1. package/dist/cjs/functions/config_utils/imports.d.ts +1 -0
  2. package/dist/cjs/functions/config_utils/index.d.ts +1 -0
  3. package/dist/cjs/functions/config_utils/src/config_utils.d.ts +7 -0
  4. package/dist/cjs/functions/config_utils/src/index.d.ts +1 -0
  5. package/dist/cjs/functions/index.d.ts +2 -0
  6. package/dist/cjs/functions/read_utils/imports.d.ts +3 -0
  7. package/dist/cjs/functions/read_utils/index.d.ts +1 -0
  8. package/dist/cjs/functions/read_utils/src/index.d.ts +1 -0
  9. package/dist/cjs/functions/read_utils/src/read_utils.d.ts +9 -0
  10. package/dist/cjs/index.js +77 -2
  11. package/dist/cjs/index.js.map +1 -1
  12. package/dist/cjs/types/src/utils.d.ts +6 -0
  13. package/dist/esm/functions/config_utils/imports.d.ts +1 -0
  14. package/dist/esm/functions/config_utils/index.d.ts +1 -0
  15. package/dist/esm/functions/config_utils/src/config_utils.d.ts +7 -0
  16. package/dist/esm/functions/config_utils/src/index.d.ts +1 -0
  17. package/dist/esm/functions/index.d.ts +2 -0
  18. package/dist/esm/functions/read_utils/imports.d.ts +3 -0
  19. package/dist/esm/functions/read_utils/index.d.ts +1 -0
  20. package/dist/esm/functions/read_utils/src/index.d.ts +1 -0
  21. package/dist/esm/functions/read_utils/src/read_utils.d.ts +9 -0
  22. package/dist/esm/index.js +75 -3
  23. package/dist/esm/index.js.map +1 -1
  24. package/dist/esm/types/src/utils.d.ts +6 -0
  25. package/dist/functions/config_utils/imports.d.ts +1 -0
  26. package/dist/functions/config_utils/index.d.ts +1 -0
  27. package/dist/functions/config_utils/src/config_utils.d.ts +7 -0
  28. package/dist/functions/config_utils/src/index.d.ts +1 -0
  29. package/dist/functions/index.d.ts +2 -0
  30. package/dist/functions/read_utils/imports.d.ts +0 -0
  31. package/dist/functions/read_utils/index.d.ts +1 -0
  32. package/dist/functions/read_utils/src/index.d.ts +1 -0
  33. package/dist/functions/read_utils/src/read_utils.d.ts +9 -0
  34. package/dist/index.d.ts +24 -1
  35. package/dist/types/src/utils.d.ts +6 -0
  36. package/package.json +1 -1
@@ -0,0 +1 @@
1
+ export type { AppConfig } from './../../types';
@@ -0,0 +1 @@
1
+ export * from './src';
@@ -0,0 +1,7 @@
1
+ import type { AppConfig } from './../imports';
2
+ /**
3
+ * getConfig(): load the full AppConfig
4
+ * getConfig(key): load just that key
5
+ */
6
+ export declare function getConfig(): Promise<AppConfig>;
7
+ export declare function getConfig<K extends keyof AppConfig>(key: K): Promise<AppConfig[K]>;
@@ -0,0 +1 @@
1
+ export * from './config_utils';
@@ -8,3 +8,5 @@ export * from './rndm_utils';
8
8
  export * from './string_utils';
9
9
  export * from './type_utils';
10
10
  export * from './ui_utils';
11
+ export * from './config_utils';
12
+ export * from './read_utils';
@@ -0,0 +1,3 @@
1
+ export { promises as fs } from 'fs';
2
+ import path from 'path';
3
+ export { path };
@@ -0,0 +1 @@
1
+ export * from './src';
@@ -0,0 +1 @@
1
+ export * from './read_utils';
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Reads a file at `relativeOrAbsolutePath` and returns its contents as a string.
3
+ * In a browser this will reject immediately.
4
+ */
5
+ export declare function readFileContents(relativeOrAbsolutePath: string): Promise<string>;
6
+ /**
7
+ * Reads a JSON file and returns the parsed object.
8
+ */
9
+ export declare function readJsonFile<T = any>(relativeOrAbsolutePath: string): Promise<T>;
package/dist/cjs/index.js CHANGED
@@ -1023,14 +1023,14 @@ function getAppConfig(endpoint) {
1023
1023
  return base ? `${base}/${clean}` : `/api/${clean}`;
1024
1024
  }
1025
1025
  /** Accessor for the loaded config. Throws if you forgot to loadConfig(). */
1026
- function getConfig() {
1026
+ function getConfig$1() {
1027
1027
  {
1028
1028
  throw new Error('Config not loaded! Call loadConfig() first.');
1029
1029
  }
1030
1030
  }
1031
1031
  /** Convenience to grab the base URL once loaded. */
1032
1032
  function apiBase() {
1033
- return getConfig().API_BASE_URL.replace(/\/+$/, '');
1033
+ return getConfig$1().API_BASE_URL.replace(/\/+$/, '');
1034
1034
  }
1035
1035
 
1036
1036
  function ensureAbstractUrl(endpoint, slices = []) {
@@ -1280,6 +1280,78 @@ function Spinner() {
1280
1280
  return (jsxRuntime.jsx("p", { className: 'animate-pulse', children: "Loading\u2026" }));
1281
1281
  }
1282
1282
 
1283
+ let _cachedConfig = null;
1284
+ function loadConfig() {
1285
+ return __awaiter(this, arguments, void 0, function* (filePath = null) {
1286
+ // 1. If nobody passed a custom path, we default to "config.json" (relative)
1287
+ const relativePath = filePath || 'config.json';
1288
+ // 2. Resolve it against the running page’s URL (document.baseURI)
1289
+ const configUrl = new URL(relativePath, document.baseURI).href;
1290
+ // 3. Fetch + cache
1291
+ if (_cachedConfig)
1292
+ return _cachedConfig;
1293
+ const res = yield fetch(configUrl);
1294
+ if (!res.ok) {
1295
+ throw new Error(`Could not fetch ${configUrl}: ${res.status}`);
1296
+ }
1297
+ _cachedConfig = (yield res.json());
1298
+ return _cachedConfig;
1299
+ });
1300
+ }
1301
+ function getConfig(key) {
1302
+ return __awaiter(this, void 0, void 0, function* () {
1303
+ const cfg = yield loadConfig();
1304
+ return key ? cfg[key] : cfg;
1305
+ });
1306
+ }
1307
+
1308
+ // src/read_utils.ts
1309
+ /**
1310
+ * Attempt to load the Node-only modules.
1311
+ * Returns { fs: null, path: null } in the browser.
1312
+ */
1313
+ function tryNodeModules() {
1314
+ return __awaiter(this, void 0, void 0, function* () {
1315
+ try {
1316
+ const fsMod = yield import('fs');
1317
+ const pathMod = yield import('path');
1318
+ return {
1319
+ fs: fsMod.promises, // keep the `promises` API
1320
+ path: pathMod
1321
+ };
1322
+ }
1323
+ catch (_a) {
1324
+ return { fs: null, path: null };
1325
+ }
1326
+ });
1327
+ }
1328
+ /**
1329
+ * Reads a file at `relativeOrAbsolutePath` and returns its contents as a string.
1330
+ * In a browser this will reject immediately.
1331
+ */
1332
+ function readFileContents(relativeOrAbsolutePath) {
1333
+ return __awaiter(this, void 0, void 0, function* () {
1334
+ const { fs, path } = yield tryNodeModules();
1335
+ if (!fs || !path) {
1336
+ throw new Error('readFileContents can only be used in Node.js');
1337
+ }
1338
+ // resolve absolute
1339
+ const filePath = path.isAbsolute(relativeOrAbsolutePath)
1340
+ ? relativeOrAbsolutePath
1341
+ : path.resolve(__dirname, relativeOrAbsolutePath);
1342
+ return fs.readFile(filePath, 'utf8');
1343
+ });
1344
+ }
1345
+ /**
1346
+ * Reads a JSON file and returns the parsed object.
1347
+ */
1348
+ function readJsonFile(relativeOrAbsolutePath) {
1349
+ return __awaiter(this, void 0, void 0, function* () {
1350
+ const text = yield readFileContents(relativeOrAbsolutePath);
1351
+ return JSON.parse(text);
1352
+ });
1353
+ }
1354
+
1283
1355
  Object.defineProperty(exports, "useCallback", {
1284
1356
  enumerable: true,
1285
1357
  get: function () { return react.useCallback; }
@@ -1333,6 +1405,7 @@ exports.getAbsPath = getAbsPath;
1333
1405
  exports.getAuthorizationHeader = getAuthorizationHeader;
1334
1406
  exports.getBaseDir = getBaseDir;
1335
1407
  exports.getComponentsUtilsDirectory = getComponentsUtilsDirectory;
1408
+ exports.getConfig = getConfig;
1336
1409
  exports.getDbConfigsPath = getDbConfigsPath;
1337
1410
  exports.getDistDir = getDistDir;
1338
1411
  exports.getEnvDir = getEnvDir;
@@ -1362,6 +1435,8 @@ exports.isTokenExpired = isTokenExpired;
1362
1435
  exports.make_path = make_path;
1363
1436
  exports.make_sanitized_path = make_sanitized_path;
1364
1437
  exports.normalizeUrl = normalizeUrl;
1438
+ exports.readFileContents = readFileContents;
1439
+ exports.readJsonFile = readJsonFile;
1365
1440
  exports.requestPatch = requestPatch;
1366
1441
  exports.requireToken = requireToken;
1367
1442
  exports.sanitizeFilename = sanitizeFilename;