@putkoff/abstract-utilities 0.1.114 → 0.1.115

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 +17 -0
  10. package/dist/cjs/index.js +88 -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 +17 -0
  22. package/dist/esm/index.js +85 -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 +3 -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 +17 -0
  34. package/dist/index.d.ts +32 -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,17 @@
1
+ /**
2
+ * Reads a file at `relativeOrAbsolutePath` and returns its contents as a string.
3
+ */
4
+ export declare function readFileContents(relativeOrAbsolutePath: string): Promise<string>;
5
+ /**
6
+ * Reads a JSON file and returns the parsed object.
7
+ */
8
+ export declare function readJsonFile<T = any>(relativeOrAbsolutePath: string): Promise<T>;
9
+ /**
10
+ * Read a file and optionally parse it as JSON (and even pluck a field).
11
+ *
12
+ * @param filePath Path to your file (relative to cwd or absolute)
13
+ * @param asJson If true, JSON.parse the file.
14
+ * If a string, JSON.parse then return parsed[string].
15
+ * @returns The raw string, whole parsed object, or a specific field.
16
+ */
17
+ export declare function reader(filePath: string, asJson?: boolean | string): Promise<any>;
package/dist/cjs/index.js CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  var react = require('react');
4
4
  var jsxRuntime = require('react/jsx-runtime');
5
+ var fs = require('fs');
6
+ var path = require('path');
5
7
 
6
8
  var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
7
9
  /**
@@ -1023,14 +1025,14 @@ function getAppConfig(endpoint) {
1023
1025
  return base ? `${base}/${clean}` : `/api/${clean}`;
1024
1026
  }
1025
1027
  /** Accessor for the loaded config. Throws if you forgot to loadConfig(). */
1026
- function getConfig() {
1028
+ function getConfig$1() {
1027
1029
  {
1028
1030
  throw new Error('Config not loaded! Call loadConfig() first.');
1029
1031
  }
1030
1032
  }
1031
1033
  /** Convenience to grab the base URL once loaded. */
1032
1034
  function apiBase() {
1033
- return getConfig().API_BASE_URL.replace(/\/+$/, '');
1035
+ return getConfig$1().API_BASE_URL.replace(/\/+$/, '');
1034
1036
  }
1035
1037
 
1036
1038
  function ensureAbstractUrl(endpoint, slices = []) {
@@ -1280,6 +1282,86 @@ function Spinner() {
1280
1282
  return (jsxRuntime.jsx("p", { className: 'animate-pulse', children: "Loading\u2026" }));
1281
1283
  }
1282
1284
 
1285
+ let _cachedConfig = null;
1286
+ function loadConfig() {
1287
+ return __awaiter(this, arguments, void 0, function* (filePath = null) {
1288
+ // 1. If nobody passed a custom path, we default to "config.json" (relative)
1289
+ const relativePath = filePath || 'config.json';
1290
+ // 2. Resolve it against the running page’s URL (document.baseURI)
1291
+ const configUrl = new URL(relativePath, document.baseURI).href;
1292
+ // 3. Fetch + cache
1293
+ if (_cachedConfig)
1294
+ return _cachedConfig;
1295
+ const res = yield fetch(configUrl);
1296
+ if (!res.ok) {
1297
+ throw new Error(`Could not fetch ${configUrl}: ${res.status}`);
1298
+ }
1299
+ _cachedConfig = (yield res.json());
1300
+ return _cachedConfig;
1301
+ });
1302
+ }
1303
+ function getConfig(key) {
1304
+ return __awaiter(this, void 0, void 0, function* () {
1305
+ const cfg = yield loadConfig();
1306
+ return key ? cfg[key] : cfg;
1307
+ });
1308
+ }
1309
+
1310
+ /**
1311
+ * Reads a file at `relativeOrAbsolutePath` and returns its contents as a string.
1312
+ */
1313
+ function readFileContents(relativeOrAbsolutePath) {
1314
+ return __awaiter(this, void 0, void 0, function* () {
1315
+ // Resolve to an absolute path (so you’re always sure where it’s looking)
1316
+ const filePath = path.isAbsolute(relativeOrAbsolutePath)
1317
+ ? relativeOrAbsolutePath
1318
+ : path.resolve(__dirname, relativeOrAbsolutePath);
1319
+ // Read and return as UTF-8 text
1320
+ return fs.promises.readFile(filePath, 'utf8');
1321
+ });
1322
+ }
1323
+ /**
1324
+ * Reads a JSON file and returns the parsed object.
1325
+ */
1326
+ function readJsonFile(relativeOrAbsolutePath) {
1327
+ return __awaiter(this, void 0, void 0, function* () {
1328
+ const filePath = path.isAbsolute(relativeOrAbsolutePath)
1329
+ ? relativeOrAbsolutePath
1330
+ : path.resolve(__dirname, relativeOrAbsolutePath);
1331
+ const text = yield fs.promises.readFile(filePath, 'utf8');
1332
+ return JSON.parse(text);
1333
+ });
1334
+ }
1335
+ /**
1336
+ * Read a file and optionally parse it as JSON (and even pluck a field).
1337
+ *
1338
+ * @param filePath Path to your file (relative to cwd or absolute)
1339
+ * @param asJson If true, JSON.parse the file.
1340
+ * If a string, JSON.parse then return parsed[string].
1341
+ * @returns The raw string, whole parsed object, or a specific field.
1342
+ */
1343
+ function reader(filePath_1) {
1344
+ return __awaiter(this, arguments, void 0, function* (filePath, asJson = false) {
1345
+ // resolve to absolute so you know exactly where you’re reading from
1346
+ const abs = path.isAbsolute(filePath)
1347
+ ? filePath
1348
+ : path.resolve(process.cwd(), filePath);
1349
+ // 1) read file as text
1350
+ const text = yield fs.promises.readFile(abs, 'utf8');
1351
+ // 2) if they want JSON, parse it
1352
+ if (asJson) {
1353
+ const obj = JSON.parse(text);
1354
+ // 3) if they passed a string, return that property
1355
+ if (typeof asJson === 'string') {
1356
+ return obj[asJson];
1357
+ }
1358
+ return obj;
1359
+ }
1360
+ // 4) otherwise, just return the raw text
1361
+ return text;
1362
+ });
1363
+ }
1364
+
1283
1365
  Object.defineProperty(exports, "useCallback", {
1284
1366
  enumerable: true,
1285
1367
  get: function () { return react.useCallback; }
@@ -1333,6 +1415,7 @@ exports.getAbsPath = getAbsPath;
1333
1415
  exports.getAuthorizationHeader = getAuthorizationHeader;
1334
1416
  exports.getBaseDir = getBaseDir;
1335
1417
  exports.getComponentsUtilsDirectory = getComponentsUtilsDirectory;
1418
+ exports.getConfig = getConfig;
1336
1419
  exports.getDbConfigsPath = getDbConfigsPath;
1337
1420
  exports.getDistDir = getDistDir;
1338
1421
  exports.getEnvDir = getEnvDir;
@@ -1362,6 +1445,9 @@ exports.isTokenExpired = isTokenExpired;
1362
1445
  exports.make_path = make_path;
1363
1446
  exports.make_sanitized_path = make_sanitized_path;
1364
1447
  exports.normalizeUrl = normalizeUrl;
1448
+ exports.readFileContents = readFileContents;
1449
+ exports.readJsonFile = readJsonFile;
1450
+ exports.reader = reader;
1365
1451
  exports.requestPatch = requestPatch;
1366
1452
  exports.requireToken = requireToken;
1367
1453
  exports.sanitizeFilename = sanitizeFilename;