@putkoff/abstract-utilities 0.1.115 → 0.1.117

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.
@@ -5,4 +5,4 @@ export { alertit } from './../rndm_utils';
5
5
  export { ensure_list } from './../type_utils';
6
6
  export { eatInner } from './../string_utils';
7
7
  export type { LogoutButtonProps, FetchVariables } from './../../types';
8
- export { getAppConfig } from '@putkoff/abstract-configs';
8
+ export { getConfig } from './../config_utils';
@@ -7,9 +7,9 @@
7
7
  *
8
8
  */
9
9
  /** Pulls base-URL from AppConfig.API_BASE_URL, strips trailing slashes */
10
- export declare function get_app_config_url(endpoint: any): string;
10
+ export declare function get_app_config_url(endpoint: any, appKey?: any): string;
11
11
  export declare function fetchIt(endpoint: string, body?: unknown, method?: string | null, headers?: Record<string, string> | null, blob?: boolean | null, no_api?: boolean, requireAuth?: boolean): Promise<unknown>;
12
12
  export declare function secureFetchIt<T>(endpoint: any, body?: any, method?: any, headers?: Record<string, string> | null, blob?: boolean, noApi?: boolean, withCredentials?: boolean, returnJson?: boolean, returnReult?: boolean): Promise<T>;
13
13
  export declare function secureFetchIt(endpoint: any, body?: any, method?: any, headers?: Record<string, string> | null, blob?: boolean, noApi?: boolean, withCredentials?: boolean, returnJson?: boolean, returnReult?: boolean): Promise<Blob>;
14
14
  export declare function requestPatch(url: string, body?: unknown): Promise<Response>;
15
- export declare function fetchSharePatch(file: unknown): Promise<void>;
15
+ export declare function fetchSharePatch(file: unknown, appKey?: any): Promise<void>;
@@ -1,17 +1,9 @@
1
1
  /**
2
2
  * Reads a file at `relativeOrAbsolutePath` and returns its contents as a string.
3
+ * In a browser this will reject immediately.
3
4
  */
4
5
  export declare function readFileContents(relativeOrAbsolutePath: string): Promise<string>;
5
6
  /**
6
7
  * Reads a JSON file and returns the parsed object.
7
8
  */
8
9
  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>;
@@ -1,2 +1,3 @@
1
1
  export * from './functions';
2
2
  export * from './types';
3
+ export { loadConfig } from './utils';
package/dist/cjs/index.js CHANGED
@@ -2,8 +2,6 @@
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');
7
5
 
8
6
  var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
9
7
  /**
@@ -1018,21 +1016,29 @@ function alertit(obj = null) {
1018
1016
  alert(msg);
1019
1017
  }
1020
1018
 
1021
- /** Will be filled in by loadConfig() */
1022
- function getAppConfig(endpoint) {
1023
- const base = apiBase(); // '' until load() finishes
1024
- const clean = endpoint.replace(/^\/+/, '');
1025
- return base ? `${base}/${clean}` : `/api/${clean}`;
1026
- }
1027
- /** Accessor for the loaded config. Throws if you forgot to loadConfig(). */
1028
- function getConfig$1() {
1029
- {
1030
- throw new Error('Config not loaded! Call loadConfig() first.');
1031
- }
1019
+ let _cachedConfig = null;
1020
+ function loadConfig$1() {
1021
+ return __awaiter(this, arguments, void 0, function* (filePath = null) {
1022
+ // 1. If nobody passed a custom path, we default to "config.json" (relative)
1023
+ const relativePath = filePath || 'config.json';
1024
+ // 2. Resolve it against the running page’s URL (document.baseURI)
1025
+ const configUrl = new URL(relativePath, document.baseURI).href;
1026
+ // 3. Fetch + cache
1027
+ if (_cachedConfig)
1028
+ return _cachedConfig;
1029
+ const res = yield fetch(configUrl);
1030
+ if (!res.ok) {
1031
+ throw new Error(`Could not fetch ${configUrl}: ${res.status}`);
1032
+ }
1033
+ _cachedConfig = (yield res.json());
1034
+ return _cachedConfig;
1035
+ });
1032
1036
  }
1033
- /** Convenience to grab the base URL once loaded. */
1034
- function apiBase() {
1035
- return getConfig$1().API_BASE_URL.replace(/\/+$/, '');
1037
+ function getConfig(key) {
1038
+ return __awaiter(this, void 0, void 0, function* () {
1039
+ const cfg = yield loadConfig$1();
1040
+ return key ? cfg[key] : cfg;
1041
+ });
1036
1042
  }
1037
1043
 
1038
1044
  function ensureAbstractUrl(endpoint, slices = []) {
@@ -1160,9 +1166,11 @@ function getFetchVars(headers = null, method = null, body = null) {
1160
1166
  /** Pulls base-URL from AppConfig.API_BASE_URL, strips trailing slashes */
1161
1167
  // src/functions/fetch/secureFetchIt.ts
1162
1168
  // --- fetch_utils/src/fetch_utils.ts ----------------------------
1163
- function get_app_config_url(endpoint) {
1169
+ function get_app_config_url(endpoint, appKey = null) {
1164
1170
  const cleanEndpoint = endpoint.replace(/^\/+/, "");
1165
- const baseUrl = getAppConfig(cleanEndpoint);
1171
+ const baseUrl = getConfig();
1172
+ appKey = appKey || 'BASE_API_URL';
1173
+ baseUrl[appKey];
1166
1174
  const baseSlices = baseUrl.split("/").filter(Boolean);
1167
1175
  const url = ensureAbstractUrl(cleanEndpoint, baseSlices);
1168
1176
  return url;
@@ -1181,7 +1189,7 @@ function secureFetchIt(endpoint_1) {
1181
1189
  method = method || "GET";
1182
1190
  let url = endpoint;
1183
1191
  if (!noApi) {
1184
- url = get_app_config_url(url);
1192
+ url = get_app_config_url(endpoint);
1185
1193
  }
1186
1194
  // headers: JSON by default, plus any auth + overrides
1187
1195
  const headers = Object.assign(Object.assign(Object.assign({}, (body instanceof FormData ? {} : { "Content-Type": "application/json" })), getAuthorizationHeader()), customHeaders);
@@ -1218,13 +1226,11 @@ function requestPatch(url_1) {
1218
1226
  });
1219
1227
  }
1220
1228
  // Performs PATCH request for file sharing
1221
- function fetchSharePatch(file) {
1222
- return __awaiter(this, void 0, void 0, function* () {
1229
+ function fetchSharePatch(file_1) {
1230
+ return __awaiter(this, arguments, void 0, function* (file, appKey = null) {
1223
1231
  const cleanEndpoint = '/files/share';
1224
- const baseUrl = getAppConfig(cleanEndpoint);
1225
- const baseSlices = baseUrl.split("/").filter(Boolean);
1226
- const url = ensureAbstractUrl(cleanEndpoint, baseSlices);
1227
1232
  // build final URL
1233
+ const url = get_app_config_url(cleanEndpoint);
1228
1234
  const token = localStorage.getItem('token');
1229
1235
  const body = JSON.stringify(file);
1230
1236
  const method = 'PATCH';
@@ -1282,42 +1288,41 @@ function Spinner() {
1282
1288
  return (jsxRuntime.jsx("p", { className: 'animate-pulse', children: "Loading\u2026" }));
1283
1289
  }
1284
1290
 
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) {
1291
+ // src/read_utils.ts
1292
+ /**
1293
+ * Attempt to load the Node-only modules.
1294
+ * Returns { fs: null, path: null } in the browser.
1295
+ */
1296
+ function tryNodeModules() {
1304
1297
  return __awaiter(this, void 0, void 0, function* () {
1305
- const cfg = yield loadConfig();
1306
- return key ? cfg[key] : cfg;
1298
+ try {
1299
+ const fsMod = yield import('fs');
1300
+ const pathMod = yield import('path');
1301
+ return {
1302
+ fs: fsMod.promises, // keep the `promises` API
1303
+ path: pathMod
1304
+ };
1305
+ }
1306
+ catch (_a) {
1307
+ return { fs: null, path: null };
1308
+ }
1307
1309
  });
1308
1310
  }
1309
-
1310
1311
  /**
1311
1312
  * Reads a file at `relativeOrAbsolutePath` and returns its contents as a string.
1313
+ * In a browser this will reject immediately.
1312
1314
  */
1313
1315
  function readFileContents(relativeOrAbsolutePath) {
1314
1316
  return __awaiter(this, void 0, void 0, function* () {
1315
- // Resolve to an absolute path (so you’re always sure where it’s looking)
1317
+ const { fs, path } = yield tryNodeModules();
1318
+ if (!fs || !path) {
1319
+ throw new Error('readFileContents can only be used in Node.js');
1320
+ }
1321
+ // resolve absolute
1316
1322
  const filePath = path.isAbsolute(relativeOrAbsolutePath)
1317
1323
  ? relativeOrAbsolutePath
1318
1324
  : path.resolve(__dirname, relativeOrAbsolutePath);
1319
- // Read and return as UTF-8 text
1320
- return fs.promises.readFile(filePath, 'utf8');
1325
+ return fs.readFile(filePath, 'utf8');
1321
1326
  });
1322
1327
  }
1323
1328
  /**
@@ -1325,40 +1330,19 @@ function readFileContents(relativeOrAbsolutePath) {
1325
1330
  */
1326
1331
  function readJsonFile(relativeOrAbsolutePath) {
1327
1332
  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');
1333
+ const text = yield readFileContents(relativeOrAbsolutePath);
1332
1334
  return JSON.parse(text);
1333
1335
  });
1334
1336
  }
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;
1337
+
1338
+ function loadConfig() {
1339
+ return __awaiter(this, void 0, void 0, function* () {
1340
+ const base = (process.env.BASE_API_URL || "").replace(/\/+$/, "");
1341
+ const url = `${base}/config.json`;
1342
+ const res = yield fetch(url);
1343
+ if (!res.ok)
1344
+ throw new Error(`Failed to load ${url}: ${res.status}`);
1345
+ yield res.json();
1362
1346
  });
1363
1347
  }
1364
1348
 
@@ -1442,12 +1426,12 @@ exports.get_window_parts = get_window_parts;
1442
1426
  exports.get_window_pathname = get_window_pathname;
1443
1427
  exports.isLoggedIn = isLoggedIn;
1444
1428
  exports.isTokenExpired = isTokenExpired;
1429
+ exports.loadConfig = loadConfig;
1445
1430
  exports.make_path = make_path;
1446
1431
  exports.make_sanitized_path = make_sanitized_path;
1447
1432
  exports.normalizeUrl = normalizeUrl;
1448
1433
  exports.readFileContents = readFileContents;
1449
1434
  exports.readJsonFile = readJsonFile;
1450
- exports.reader = reader;
1451
1435
  exports.requestPatch = requestPatch;
1452
1436
  exports.requireToken = requireToken;
1453
1437
  exports.sanitizeFilename = sanitizeFilename;