@putkoff/abstract-utilities 0.1.113 → 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.
- package/dist/cjs/functions/config_utils/imports.d.ts +1 -0
- package/dist/cjs/functions/config_utils/index.d.ts +1 -0
- package/dist/cjs/functions/config_utils/src/config_utils.d.ts +7 -0
- package/dist/cjs/functions/config_utils/src/index.d.ts +1 -0
- package/dist/cjs/functions/index.d.ts +2 -0
- package/dist/cjs/functions/read_utils/imports.d.ts +3 -0
- package/dist/cjs/functions/read_utils/index.d.ts +1 -0
- package/dist/cjs/functions/read_utils/src/index.d.ts +1 -0
- package/dist/cjs/functions/read_utils/src/read_utils.d.ts +17 -0
- package/dist/cjs/index.js +88 -2
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types/src/utils.d.ts +6 -0
- package/dist/esm/functions/config_utils/imports.d.ts +1 -0
- package/dist/esm/functions/config_utils/index.d.ts +1 -0
- package/dist/esm/functions/config_utils/src/config_utils.d.ts +7 -0
- package/dist/esm/functions/config_utils/src/index.d.ts +1 -0
- package/dist/esm/functions/index.d.ts +2 -0
- package/dist/esm/functions/read_utils/imports.d.ts +3 -0
- package/dist/esm/functions/read_utils/index.d.ts +1 -0
- package/dist/esm/functions/read_utils/src/index.d.ts +1 -0
- package/dist/esm/functions/read_utils/src/read_utils.d.ts +17 -0
- package/dist/esm/index.js +85 -3
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types/src/utils.d.ts +6 -0
- package/dist/functions/config_utils/imports.d.ts +1 -0
- package/dist/functions/config_utils/index.d.ts +1 -0
- package/dist/functions/config_utils/src/config_utils.d.ts +7 -0
- package/dist/functions/config_utils/src/index.d.ts +1 -0
- package/dist/functions/index.d.ts +2 -0
- package/dist/functions/read_utils/imports.d.ts +3 -0
- package/dist/functions/read_utils/index.d.ts +1 -0
- package/dist/functions/read_utils/src/index.d.ts +1 -0
- package/dist/functions/read_utils/src/read_utils.d.ts +17 -0
- package/dist/index.d.ts +32 -1
- package/dist/types/src/utils.d.ts +6 -0
- package/package.json +1 -1
|
@@ -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/esm/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export { useCallback, useEffect, useRef, useState } from 'react';
|
|
2
2
|
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
3
|
+
import { promises } from 'fs';
|
|
4
|
+
import path from 'path';
|
|
3
5
|
|
|
4
6
|
/**
|
|
5
7
|
***Changes**:
|
|
@@ -1020,14 +1022,14 @@ function getAppConfig(endpoint) {
|
|
|
1020
1022
|
return base ? `${base}/${clean}` : `/api/${clean}`;
|
|
1021
1023
|
}
|
|
1022
1024
|
/** Accessor for the loaded config. Throws if you forgot to loadConfig(). */
|
|
1023
|
-
function getConfig() {
|
|
1025
|
+
function getConfig$1() {
|
|
1024
1026
|
{
|
|
1025
1027
|
throw new Error('Config not loaded! Call loadConfig() first.');
|
|
1026
1028
|
}
|
|
1027
1029
|
}
|
|
1028
1030
|
/** Convenience to grab the base URL once loaded. */
|
|
1029
1031
|
function apiBase() {
|
|
1030
|
-
return getConfig().API_BASE_URL.replace(/\/+$/, '');
|
|
1032
|
+
return getConfig$1().API_BASE_URL.replace(/\/+$/, '');
|
|
1031
1033
|
}
|
|
1032
1034
|
|
|
1033
1035
|
function ensureAbstractUrl(endpoint, slices = []) {
|
|
@@ -1277,5 +1279,85 @@ function Spinner() {
|
|
|
1277
1279
|
return (jsx("p", { className: 'animate-pulse', children: "Loading\u2026" }));
|
|
1278
1280
|
}
|
|
1279
1281
|
|
|
1280
|
-
|
|
1282
|
+
let _cachedConfig = null;
|
|
1283
|
+
function loadConfig() {
|
|
1284
|
+
return __awaiter(this, arguments, void 0, function* (filePath = null) {
|
|
1285
|
+
// 1. If nobody passed a custom path, we default to "config.json" (relative)
|
|
1286
|
+
const relativePath = filePath || 'config.json';
|
|
1287
|
+
// 2. Resolve it against the running page’s URL (document.baseURI)
|
|
1288
|
+
const configUrl = new URL(relativePath, document.baseURI).href;
|
|
1289
|
+
// 3. Fetch + cache
|
|
1290
|
+
if (_cachedConfig)
|
|
1291
|
+
return _cachedConfig;
|
|
1292
|
+
const res = yield fetch(configUrl);
|
|
1293
|
+
if (!res.ok) {
|
|
1294
|
+
throw new Error(`Could not fetch ${configUrl}: ${res.status}`);
|
|
1295
|
+
}
|
|
1296
|
+
_cachedConfig = (yield res.json());
|
|
1297
|
+
return _cachedConfig;
|
|
1298
|
+
});
|
|
1299
|
+
}
|
|
1300
|
+
function getConfig(key) {
|
|
1301
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1302
|
+
const cfg = yield loadConfig();
|
|
1303
|
+
return key ? cfg[key] : cfg;
|
|
1304
|
+
});
|
|
1305
|
+
}
|
|
1306
|
+
|
|
1307
|
+
/**
|
|
1308
|
+
* Reads a file at `relativeOrAbsolutePath` and returns its contents as a string.
|
|
1309
|
+
*/
|
|
1310
|
+
function readFileContents(relativeOrAbsolutePath) {
|
|
1311
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1312
|
+
// Resolve to an absolute path (so you’re always sure where it’s looking)
|
|
1313
|
+
const filePath = path.isAbsolute(relativeOrAbsolutePath)
|
|
1314
|
+
? relativeOrAbsolutePath
|
|
1315
|
+
: path.resolve(__dirname, relativeOrAbsolutePath);
|
|
1316
|
+
// Read and return as UTF-8 text
|
|
1317
|
+
return promises.readFile(filePath, 'utf8');
|
|
1318
|
+
});
|
|
1319
|
+
}
|
|
1320
|
+
/**
|
|
1321
|
+
* Reads a JSON file and returns the parsed object.
|
|
1322
|
+
*/
|
|
1323
|
+
function readJsonFile(relativeOrAbsolutePath) {
|
|
1324
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1325
|
+
const filePath = path.isAbsolute(relativeOrAbsolutePath)
|
|
1326
|
+
? relativeOrAbsolutePath
|
|
1327
|
+
: path.resolve(__dirname, relativeOrAbsolutePath);
|
|
1328
|
+
const text = yield promises.readFile(filePath, 'utf8');
|
|
1329
|
+
return JSON.parse(text);
|
|
1330
|
+
});
|
|
1331
|
+
}
|
|
1332
|
+
/**
|
|
1333
|
+
* Read a file and optionally parse it as JSON (and even pluck a field).
|
|
1334
|
+
*
|
|
1335
|
+
* @param filePath Path to your file (relative to cwd or absolute)
|
|
1336
|
+
* @param asJson If true, JSON.parse the file.
|
|
1337
|
+
* If a string, JSON.parse then return parsed[string].
|
|
1338
|
+
* @returns The raw string, whole parsed object, or a specific field.
|
|
1339
|
+
*/
|
|
1340
|
+
function reader(filePath_1) {
|
|
1341
|
+
return __awaiter(this, arguments, void 0, function* (filePath, asJson = false) {
|
|
1342
|
+
// resolve to absolute so you know exactly where you’re reading from
|
|
1343
|
+
const abs = path.isAbsolute(filePath)
|
|
1344
|
+
? filePath
|
|
1345
|
+
: path.resolve(process.cwd(), filePath);
|
|
1346
|
+
// 1) read file as text
|
|
1347
|
+
const text = yield promises.readFile(abs, 'utf8');
|
|
1348
|
+
// 2) if they want JSON, parse it
|
|
1349
|
+
if (asJson) {
|
|
1350
|
+
const obj = JSON.parse(text);
|
|
1351
|
+
// 3) if they passed a string, return that property
|
|
1352
|
+
if (typeof asJson === 'string') {
|
|
1353
|
+
return obj[asJson];
|
|
1354
|
+
}
|
|
1355
|
+
return obj;
|
|
1356
|
+
}
|
|
1357
|
+
// 4) otherwise, just return the raw text
|
|
1358
|
+
return text;
|
|
1359
|
+
});
|
|
1360
|
+
}
|
|
1361
|
+
|
|
1362
|
+
export { API_PREFIX, BASE_URL, Button, Checkbox, DEV_PREFIX, DOMAIN_NAME, Input, PROD_PREFIX, PROTOCOL, SUB_DIR, Spinner, alertit, currentUsername, currentUsernames, decodeJwt, eatAll, eatEnd, eatInner, eatOuter, ensure_list, fetchIt, fetchSharePatch, geAuthsUtilsDirectory, geBackupsUtilsDirectory, geConstantsUtilsDirectory, geEnvUtilsDirectory, geFetchUtilsDirectory, geFileUtilsDirectory, gePathUtilsDirectory, geStaticDirectory, geStringUtilsDirectory, geTypeUtilsDirectory, getAbsDir, getAbsPath, getAuthorizationHeader, getBaseDir, getComponentsUtilsDirectory, getConfig, getDbConfigsPath, getDistDir, getEnvDir, getEnvPath, getFunctionsDir, getFunctionsUtilsDirectory, getHooksUtilsDirectory, getLibUtilsDirectory, getPublicDir, getSchemasDirPath, getSchemasPath, getSrcDir, getSubstring, getToken, 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, make_path, make_sanitized_path, normalizeUrl, readFileContents, readJsonFile, reader, requestPatch, requireToken, sanitizeFilename, secureFetchIt, stripPrefixes, truncateString };
|
|
1281
1363
|
//# sourceMappingURL=index.js.map
|