@achs/env 4.11.0 → 5.0.0-alpha.0
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/CHANGELOG.md +31 -0
- package/README.md +98 -110
- package/arguments.d.ts +14 -14
- package/arguments.d.ts.map +1 -1
- package/arguments.js +135 -131
- package/arguments.js.map +1 -1
- package/commands/env.command.d.ts +8 -2
- package/commands/env.command.d.ts.map +1 -1
- package/commands/env.command.js +55 -82
- package/commands/env.command.js.map +1 -1
- package/commands/export.command.d.ts +8 -2
- package/commands/export.command.d.ts.map +1 -1
- package/commands/export.command.js +53 -62
- package/commands/export.command.js.map +1 -1
- package/commands/index.d.ts +5 -5
- package/commands/index.d.ts.map +1 -1
- package/commands/index.js +5 -14
- package/commands/pull.command.d.ts +6 -1
- package/commands/pull.command.d.ts.map +1 -1
- package/commands/pull.command.js +20 -37
- package/commands/pull.command.js.map +1 -1
- package/commands/push.command.d.ts +6 -1
- package/commands/push.command.d.ts.map +1 -1
- package/commands/push.command.js +20 -36
- package/commands/push.command.js.map +1 -1
- package/commands/schema.command.d.ts +6 -1
- package/commands/schema.command.d.ts.map +1 -1
- package/commands/schema.command.js +16 -17
- package/commands/schema.command.js.map +1 -1
- package/exec.d.ts +7 -1
- package/exec.d.ts.map +1 -1
- package/exec.js +84 -158
- package/exec.js.map +1 -1
- package/index.d.ts +3 -3
- package/index.d.ts.map +1 -1
- package/index.js +3 -20
- package/interfaces/index.d.ts +1 -1
- package/interfaces/index.d.ts.map +1 -1
- package/interfaces/loader.interface.d.ts +29 -3
- package/interfaces/loader.interface.d.ts.map +1 -1
- package/main.d.ts +0 -1
- package/main.js +7 -26
- package/main.js.map +1 -1
- package/package.json +56 -35
- package/providers/app-settings.provider.d.ts +5 -2
- package/providers/app-settings.provider.d.ts.map +1 -1
- package/providers/app-settings.provider.js +36 -49
- package/providers/app-settings.provider.js.map +1 -1
- package/providers/azure-key-vault.provider.d.ts +12 -7
- package/providers/azure-key-vault.provider.d.ts.map +1 -1
- package/providers/azure-key-vault.provider.js +138 -158
- package/providers/azure-key-vault.provider.js.map +1 -1
- package/providers/index.d.ts +2 -2
- package/providers/index.d.ts.map +1 -1
- package/providers/index.js +18 -28
- package/providers/index.js.map +1 -1
- package/providers/local.provider.d.ts +5 -2
- package/providers/local.provider.d.ts.map +1 -1
- package/providers/local.provider.js +27 -40
- package/providers/local.provider.js.map +1 -1
- package/providers/package-json.provider.d.ts +5 -2
- package/providers/package-json.provider.d.ts.map +1 -1
- package/providers/package-json.provider.js +26 -28
- package/providers/package-json.provider.js.map +1 -1
- package/utils/argv.util.d.ts +10 -0
- package/utils/argv.util.d.ts.map +1 -0
- package/utils/argv.util.js +24 -0
- package/utils/argv.util.js.map +1 -0
- package/utils/command.util.d.ts +70 -3
- package/utils/command.util.d.ts.map +1 -1
- package/utils/command.util.js +60 -140
- package/utils/command.util.js.map +1 -1
- package/utils/index.d.ts +7 -6
- package/utils/index.d.ts.map +1 -1
- package/utils/index.js +8 -23
- package/utils/interpolate.util.d.ts +29 -1
- package/utils/interpolate.util.d.ts.map +1 -1
- package/utils/interpolate.util.js +12 -30
- package/utils/interpolate.util.js.map +1 -1
- package/utils/json.util.d.ts +41 -3
- package/utils/json.util.d.ts.map +1 -1
- package/utils/json.util.js +24 -42
- package/utils/json.util.js.map +1 -1
- package/utils/logger.d.ts +21 -2
- package/utils/logger.d.ts.map +1 -1
- package/utils/logger.js +31 -17
- package/utils/logger.js.map +1 -1
- package/utils/normalize.util.d.ts +24 -2
- package/utils/normalize.util.d.ts.map +1 -1
- package/utils/normalize.util.js +35 -56
- package/utils/normalize.util.js.map +1 -1
- package/utils/schema.util.d.ts +59 -0
- package/utils/schema.util.d.ts.map +1 -1
- package/utils/schema.util.js +62 -97
- package/utils/schema.util.js.map +1 -1
- package/commands/index.js.map +0 -1
- package/index.js.map +0 -1
- package/interfaces/index.js +0 -18
- package/interfaces/index.js.map +0 -1
- package/interfaces/loader.interface.js +0 -3
- package/interfaces/loader.interface.js.map +0 -1
- package/tsconfig.build.tsbuildinfo +0 -1
- package/utils/index.js.map +0 -1
package/utils/index.js
CHANGED
|
@@ -1,23 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./command.util"), exports);
|
|
18
|
-
__exportStar(require("./interpolate.util"), exports);
|
|
19
|
-
__exportStar(require("./json.util"), exports);
|
|
20
|
-
__exportStar(require("./logger"), exports);
|
|
21
|
-
__exportStar(require("./normalize.util"), exports);
|
|
22
|
-
__exportStar(require("./schema.util"), exports);
|
|
23
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
import { normalizeRawArgv as e } from "./argv.util.js";
|
|
2
|
+
import { flatAndValidateResults as t, flatResults as n, generateSchemaFrom as r, getSubcommand as i, loadConfigFile as a, loadProjectInfo as o, loadSchemaFile as s, loadVariablesFromProviders as c } from "./command.util.js";
|
|
3
|
+
import { interpolate as l, interpolateJson as u, isRecord as d } from "./interpolate.util.js";
|
|
4
|
+
import { readJson as f, resolvePath as p, writeEnvFromJson as m, writeJson as h } from "./json.util.js";
|
|
5
|
+
import { LOG_LEVELS as g, configureLogger as _, createLogger as v, logger as y } from "./logger.js";
|
|
6
|
+
import { flatten as b, normalize as x } from "./normalize.util.js";
|
|
7
|
+
import { createValidator as S, createValidators as C, flatSchema as w, isJsonSchemaObject as T, schemaFrom as E, schemaToJson as D } from "./schema.util.js";
|
|
8
|
+
export { g as LOG_LEVELS, _ as configureLogger, v as createLogger, S as createValidator, C as createValidators, t as flatAndValidateResults, n as flatResults, w as flatSchema, b as flatten, r as generateSchemaFrom, i as getSubcommand, l as interpolate, u as interpolateJson, T as isJsonSchemaObject, d as isRecord, a as loadConfigFile, o as loadProjectInfo, s as loadSchemaFile, c as loadVariablesFromProviders, y as logger, x as normalize, e as normalizeRawArgv, f as readJson, p as resolvePath, E as schemaFrom, D as schemaToJson, m as writeEnvFromJson, h as writeJson };
|
|
@@ -1,4 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates value must be a record.
|
|
3
|
+
*
|
|
4
|
+
* @export
|
|
5
|
+
* @param {unknown} obj
|
|
6
|
+
* @returns {*} {obj is Record<string, unknown>}
|
|
7
|
+
*/
|
|
1
8
|
export declare function isRecord(obj: unknown): obj is Record<string, unknown>;
|
|
2
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Replaces string arguments with regex interpolation.
|
|
11
|
+
*
|
|
12
|
+
* @export
|
|
13
|
+
* @template T
|
|
14
|
+
* @param {T} value
|
|
15
|
+
* @param {Record<string, unknown>} args
|
|
16
|
+
* @param {[string, string]} [delimiters=['[[', ']]']]
|
|
17
|
+
*
|
|
18
|
+
* @returns {T} mutated value
|
|
19
|
+
*/
|
|
20
|
+
export declare function interpolate<T>(value: T, args: Record<string, unknown>, delimiters?: [string, string]): T;
|
|
21
|
+
/**
|
|
22
|
+
* Replaces JSON string arguments with regex interpolation.
|
|
23
|
+
*
|
|
24
|
+
* @export
|
|
25
|
+
* @param {Record<string, unknown>} args
|
|
26
|
+
* @param {Record<string, unknown>} values
|
|
27
|
+
* @param {[string, string]} [delimiters=['[[', ']]']]
|
|
28
|
+
*
|
|
29
|
+
* @returns {Record<string, unknown>} mutated args
|
|
30
|
+
*/
|
|
3
31
|
export declare function interpolateJson(values: Record<string, unknown>, args: Record<string, unknown>, delimiters?: [string, string]): Record<string, unknown>;
|
|
4
32
|
//# sourceMappingURL=interpolate.util.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interpolate.util.d.ts","sourceRoot":"","sources":["../../src/utils/interpolate.util.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"interpolate.util.d.ts","sourceRoot":"","sources":["../../src/utils/interpolate.util.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAC,GAAG,EAAE,OAAO,GAAG,GAAG,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAIrE;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAC5B,KAAK,EAAE,CAAC,EACR,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,UAAU,GAAE,CAAC,MAAM,EAAE,MAAM,CAAgB,GACzC,CAAC,CAeH;AAED;;;;;;;;;GASG;AACH,wBAAgB,eAAe,CAC9B,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC/B,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,UAAU,GAAE,CAAC,MAAM,EAAE,MAAM,CAAgB,GACzC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAKzB"}
|
|
@@ -1,34 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.isRecord = isRecord;
|
|
7
|
-
exports.interpolate = interpolate;
|
|
8
|
-
exports.interpolateJson = interpolateJson;
|
|
9
|
-
const subslate_1 = __importDefault(require("subslate"));
|
|
10
|
-
function isRecord(obj) {
|
|
11
|
-
if (!obj || typeof obj !== 'object')
|
|
12
|
-
return false;
|
|
13
|
-
return Object.keys(obj).length > 0;
|
|
1
|
+
import e from "subslate";
|
|
2
|
+
//#region src/utils/interpolate.util.ts
|
|
3
|
+
function t(e) {
|
|
4
|
+
return !e || typeof e != "object" ? !1 : Object.keys(e).length > 0;
|
|
14
5
|
}
|
|
15
|
-
function
|
|
16
|
-
|
|
17
|
-
if (!value.includes(delimiters[0]))
|
|
18
|
-
return value;
|
|
19
|
-
return (0, subslate_1.default)(value, args, {
|
|
20
|
-
startStopPairs: delimiters
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
if (Array.isArray(value))
|
|
24
|
-
return value.map((a) => interpolate(a, args, delimiters));
|
|
25
|
-
if (isRecord(value))
|
|
26
|
-
return interpolateJson(value, args, delimiters);
|
|
27
|
-
return value;
|
|
6
|
+
function n(i, a, o = ["[[", "]]"]) {
|
|
7
|
+
return typeof i == "string" ? i.includes(o[0]) ? e(i, a, { startStopPairs: o }) : i : Array.isArray(i) ? i.map((e) => n(e, a, o)) : t(i) ? r(i, a, o) : i;
|
|
28
8
|
}
|
|
29
|
-
function
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
return values;
|
|
9
|
+
function r(e, t, r = ["[[", "]]"]) {
|
|
10
|
+
for (let i in e) e[i] = n(e[i], t, r);
|
|
11
|
+
return e;
|
|
33
12
|
}
|
|
13
|
+
//#endregion
|
|
14
|
+
export { n as interpolate, r as interpolateJson, t as isRecord };
|
|
15
|
+
|
|
34
16
|
//# sourceMappingURL=interpolate.util.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interpolate.util.js","
|
|
1
|
+
{"version":3,"file":"interpolate.util.js","names":[],"sources":["../../src/utils/interpolate.util.ts"],"sourcesContent":["import subslate from 'subslate';\r\n\r\n/**\r\n * Validates value must be a record.\r\n *\r\n * @export\r\n * @param {unknown} obj\r\n * @returns {*} {obj is Record<string, unknown>}\r\n */\r\nexport function isRecord(obj: unknown): obj is Record<string, unknown> {\r\n\tif (!obj || typeof obj !== 'object') return false;\r\n\r\n\treturn Object.keys(obj).length > 0;\r\n}\r\n\r\n/**\r\n * Replaces string arguments with regex interpolation.\r\n *\r\n * @export\r\n * @template T\r\n * @param {T} value\r\n * @param {Record<string, unknown>} args\r\n * @param {[string, string]} [delimiters=['[[', ']]']]\r\n *\r\n * @returns {T} mutated value\r\n */\r\nexport function interpolate<T>(\r\n\tvalue: T,\r\n\targs: Record<string, unknown>,\r\n\tdelimiters: [string, string] = ['[[', ']]'],\r\n): T {\r\n\tif (typeof value === 'string') {\r\n\t\tif (!value.includes(delimiters[0])) return value as T;\r\n\r\n\t\treturn subslate(value, args, {\r\n\t\t\tstartStopPairs: delimiters,\r\n\t\t}) as T;\r\n\t}\r\n\r\n\tif (Array.isArray(value))\r\n\t\treturn value.map((a) => interpolate(a, args, delimiters)) as T;\r\n\r\n\tif (isRecord(value)) return interpolateJson(value, args, delimiters) as T;\r\n\r\n\treturn value;\r\n}\r\n\r\n/**\r\n * Replaces JSON string arguments with regex interpolation.\r\n *\r\n * @export\r\n * @param {Record<string, unknown>} args\r\n * @param {Record<string, unknown>} values\r\n * @param {[string, string]} [delimiters=['[[', ']]']]\r\n *\r\n * @returns {Record<string, unknown>} mutated args\r\n */\r\nexport function interpolateJson(\r\n\tvalues: Record<string, unknown>,\r\n\targs: Record<string, unknown>,\r\n\tdelimiters: [string, string] = ['[[', ']]'],\r\n): Record<string, unknown> {\r\n\tfor (const key in values)\r\n\t\tvalues[key] = interpolate(values[key], args, delimiters);\r\n\r\n\treturn values;\r\n}\r\n"],"mappings":";;AASA,SAAgB,EAAS,GAA8C;CAGtE,OAFI,CAAC,KAAO,OAAO,KAAQ,WAAiB,KAErC,OAAO,KAAK,CAAG,EAAE,SAAS;AAClC;AAaA,SAAgB,EACf,GACA,GACA,IAA+B,CAAC,MAAM,IAAI,GACtC;CAcJ,OAbI,OAAO,KAAU,WACf,EAAM,SAAS,EAAW,EAAE,IAE1B,EAAS,GAAO,GAAM,EAC5B,gBAAgB,EACjB,CAAC,IAJ0C,IAOxC,MAAM,QAAQ,CAAK,IACf,EAAM,KAAK,MAAM,EAAY,GAAG,GAAM,CAAU,CAAC,IAErD,EAAS,CAAK,IAAU,EAAgB,GAAO,GAAM,CAAU,IAE5D;AACR;AAYA,SAAgB,EACf,GACA,GACA,IAA+B,CAAC,MAAM,IAAI,GAChB;CAC1B,KAAK,IAAM,KAAO,GACjB,EAAO,KAAO,EAAY,EAAO,IAAM,GAAM,CAAU;CAExD,OAAO;AACR"}
|
package/utils/json.util.d.ts
CHANGED
|
@@ -1,5 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolve a relative path for os.
|
|
3
|
+
*
|
|
4
|
+
* @export
|
|
5
|
+
* @param {string} filePath relative path from project root
|
|
6
|
+
*
|
|
7
|
+
* @returns {string} path
|
|
8
|
+
*/
|
|
1
9
|
export declare function resolvePath(filePath: string): string;
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Reads and parses a JSON file.
|
|
12
|
+
*
|
|
13
|
+
* @export
|
|
14
|
+
* @template T
|
|
15
|
+
* @param {string} path
|
|
16
|
+
*
|
|
17
|
+
* @returns {Promise<[Record<string, any>, boolean]>}
|
|
18
|
+
*/
|
|
19
|
+
export declare function readJson<T = Record<string, any>>(path: string): Promise<[Record<string, any> | T, boolean]>;
|
|
20
|
+
/**
|
|
21
|
+
* Saves a JSON into a file.
|
|
22
|
+
*
|
|
23
|
+
* @export
|
|
24
|
+
* @param {string} path
|
|
25
|
+
* @param {unknown} content
|
|
26
|
+
* @param {false} overwrite
|
|
27
|
+
* @param {false} undefinedAsNull replaces undefined by null
|
|
28
|
+
*
|
|
29
|
+
* @returns {Promise<boolean>}
|
|
30
|
+
*/
|
|
31
|
+
export declare function writeJson(path: string, content: Record<string, unknown>, overwrite?: boolean, undefinedAsNull?: boolean): Promise<boolean>;
|
|
32
|
+
/**
|
|
33
|
+
* Saves a JSON into a file as dotenv.
|
|
34
|
+
*
|
|
35
|
+
* @export
|
|
36
|
+
* @param {string} path
|
|
37
|
+
* @param {unknown} content
|
|
38
|
+
* @param {false} overwrite
|
|
39
|
+
*
|
|
40
|
+
* @returns {Promise<boolean>}
|
|
41
|
+
*/
|
|
42
|
+
export declare function writeEnvFromJson(path: string, content: Record<string, unknown>, overwrite?: boolean, quotes?: boolean): Promise<boolean>;
|
|
5
43
|
//# sourceMappingURL=json.util.d.ts.map
|
package/utils/json.util.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"json.util.d.ts","sourceRoot":"","sources":["../../src/utils/json.util.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"json.util.d.ts","sourceRoot":"","sources":["../../src/utils/json.util.ts"],"names":[],"mappings":"AAgBA;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAMpD;AAED;;;;;;;;GAQG;AACH,wBAAsB,QAAQ,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EACrD,IAAI,EAAE,MAAM,GACV,OAAO,CAAC,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,CAAC,EAAE,OAAO,CAAC,CAAC,CAI7C;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,SAAS,CAC9B,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAChC,SAAS,UAAQ,EACjB,eAAe,UAAQ,GACrB,OAAO,CAAC,OAAO,CAAC,CAgBlB;AAED;;;;;;;;;GASG;AACH,wBAAsB,gBAAgB,CACrC,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAChC,SAAS,UAAQ,EACjB,MAAM,UAAQ,GACZ,OAAO,CAAC,OAAO,CAAC,CAiBlB"}
|
package/utils/json.util.js
CHANGED
|
@@ -1,47 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const os_1 = __importDefault(require("os"));
|
|
11
|
-
const path_1 = __importDefault(require("path"));
|
|
12
|
-
const fs_1 = require("fs");
|
|
13
|
-
const promises_1 = require("fs/promises");
|
|
14
|
-
const replacer = (_, value) => value === undefined ? null : value;
|
|
15
|
-
function resolvePath(filePath) {
|
|
16
|
-
const home = os_1.default.homedir();
|
|
17
|
-
if (home !== undefined)
|
|
18
|
-
filePath = filePath.replace(/^~($|\/|\\)/, `${home}$1`);
|
|
19
|
-
return path_1.default.resolve(process.cwd(), filePath);
|
|
1
|
+
import { existsSync as e } from "node:fs";
|
|
2
|
+
import t from "node:path";
|
|
3
|
+
import { readFile as n, writeFile as r } from "node:fs/promises";
|
|
4
|
+
import i from "node:os";
|
|
5
|
+
//#region src/utils/json.util.ts
|
|
6
|
+
var a = (e, t) => t === void 0 ? null : t;
|
|
7
|
+
function o(e) {
|
|
8
|
+
let n = i.homedir();
|
|
9
|
+
return n && (e = e.replace(/^~($|\/|\\)/, `${n}$1`)), t.resolve(process.cwd(), e);
|
|
20
10
|
}
|
|
21
|
-
async function
|
|
22
|
-
|
|
23
|
-
return [{}, false];
|
|
24
|
-
return [JSON.parse(await (0, promises_1.readFile)(path, 'utf8')), true];
|
|
11
|
+
async function s(t) {
|
|
12
|
+
return e(t) ? [JSON.parse(await n(t, "utf8")), !0] : [{}, !1];
|
|
25
13
|
}
|
|
26
|
-
async function
|
|
27
|
-
|
|
28
|
-
if (exists && !overwrite)
|
|
29
|
-
return false;
|
|
30
|
-
await (0, promises_1.writeFile)(path, `${JSON.stringify(content, undefinedAsNull ? replacer : undefined, 4)}\n`, 'utf8');
|
|
31
|
-
return true;
|
|
14
|
+
async function c(t, n, i = !1, o = !1) {
|
|
15
|
+
return e(t) && !i ? !1 : (await r(t, `${JSON.stringify(n, o ? a : void 0, 4)}\n`, "utf8"), !0);
|
|
32
16
|
}
|
|
33
|
-
async function
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
value = `"${value}"`;
|
|
42
|
-
data += `${key}=${value}\n`;
|
|
43
|
-
}
|
|
44
|
-
await (0, promises_1.writeFile)(path, data, 'utf8');
|
|
45
|
-
return true;
|
|
17
|
+
async function l(t, n, i = !1, a = !1) {
|
|
18
|
+
if (e(t) && !i) return !1;
|
|
19
|
+
let o = "";
|
|
20
|
+
for (let e in n) {
|
|
21
|
+
let t = n[e];
|
|
22
|
+
a && (t = `"${t}"`), o += `${e}=${t}\n`;
|
|
23
|
+
}
|
|
24
|
+
return await r(t, o, "utf8"), !0;
|
|
46
25
|
}
|
|
26
|
+
//#endregion
|
|
27
|
+
export { s as readJson, o as resolvePath, l as writeEnvFromJson, c as writeJson };
|
|
28
|
+
|
|
47
29
|
//# sourceMappingURL=json.util.js.map
|
package/utils/json.util.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"json.util.js","
|
|
1
|
+
{"version":3,"file":"json.util.js","names":[],"sources":["../../src/utils/json.util.ts"],"sourcesContent":["import { existsSync } from 'node:fs';\r\nimport { readFile, writeFile } from 'node:fs/promises';\r\nimport os from 'node:os';\r\nimport path from 'node:path';\r\n\r\n/**\r\n * Replaces undefined by null in JSON.stringify()\r\n *\r\n * @param {string} _ property key\r\n * @param {any} value property value\r\n *\r\n * @returns {any} value\r\n */\r\nconst replacer = (_: string, value: any): any =>\r\n\tvalue === undefined ? null : value;\r\n\r\n/**\r\n * Resolve a relative path for os.\r\n *\r\n * @export\r\n * @param {string} filePath relative path from project root\r\n *\r\n * @returns {string} path\r\n */\r\nexport function resolvePath(filePath: string): string {\r\n\tconst home = os.homedir();\r\n\r\n\tif (home) filePath = filePath.replace(/^~($|\\/|\\\\)/, `${home}$1`);\r\n\r\n\treturn path.resolve(process.cwd(), filePath);\r\n}\r\n\r\n/**\r\n * Reads and parses a JSON file.\r\n *\r\n * @export\r\n * @template T\r\n * @param {string} path\r\n *\r\n * @returns {Promise<[Record<string, any>, boolean]>}\r\n */\r\nexport async function readJson<T = Record<string, any>>(\r\n\tpath: string,\r\n): Promise<[Record<string, any> | T, boolean]> {\r\n\tif (!existsSync(path)) return [{}, false];\r\n\r\n\treturn [JSON.parse(await readFile(path, 'utf8')), true];\r\n}\r\n\r\n/**\r\n * Saves a JSON into a file.\r\n *\r\n * @export\r\n * @param {string} path\r\n * @param {unknown} content\r\n * @param {false} overwrite\r\n * @param {false} undefinedAsNull replaces undefined by null\r\n *\r\n * @returns {Promise<boolean>}\r\n */\r\nexport async function writeJson(\r\n\tpath: string,\r\n\tcontent: Record<string, unknown>,\r\n\toverwrite = false,\r\n\tundefinedAsNull = false,\r\n): Promise<boolean> {\r\n\tconst exists = existsSync(path);\r\n\r\n\tif (exists && !overwrite) return false;\r\n\r\n\tawait writeFile(\r\n\t\tpath,\r\n\t\t`${JSON.stringify(\r\n\t\t\tcontent,\r\n\t\t\tundefinedAsNull ? replacer : undefined,\r\n\t\t\t4,\r\n\t\t)}\\n`,\r\n\t\t'utf8',\r\n\t);\r\n\r\n\treturn true;\r\n}\r\n\r\n/**\r\n * Saves a JSON into a file as dotenv.\r\n *\r\n * @export\r\n * @param {string} path\r\n * @param {unknown} content\r\n * @param {false} overwrite\r\n *\r\n * @returns {Promise<boolean>}\r\n */\r\nexport async function writeEnvFromJson(\r\n\tpath: string,\r\n\tcontent: Record<string, unknown>,\r\n\toverwrite = false,\r\n\tquotes = false,\r\n): Promise<boolean> {\r\n\tconst exists = existsSync(path);\r\n\r\n\tif (exists && !overwrite) return false;\r\n\r\n\tlet data = '';\r\n\r\n\tfor (const key in content) {\r\n\t\tlet value = content[key];\r\n\t\tif (quotes) value = `\"${value}\"`;\r\n\r\n\t\tdata += `${key}=${value}\\n`;\r\n\t}\r\n\r\n\tawait writeFile(path, data, 'utf8');\r\n\r\n\treturn true;\r\n}\r\n"],"mappings":";;;;;AAaA,IAAM,KAAY,GAAW,MAC5B,MAAU,KAAA,IAAY,OAAO;AAU9B,SAAgB,EAAY,GAA0B;CACrD,IAAM,IAAO,EAAG,QAAQ;CAIxB,OAFI,MAAM,IAAW,EAAS,QAAQ,eAAe,GAAG,EAAK,GAAG,IAEzD,EAAK,QAAQ,QAAQ,IAAI,GAAG,CAAQ;AAC5C;AAWA,eAAsB,EACrB,GAC8C;CAG9C,OAFK,EAAW,CAAI,IAEb,CAAC,KAAK,MAAM,MAAM,EAAS,GAAM,MAAM,CAAC,GAAG,EAAI,IAFxB,CAAC,CAAC,GAAG,EAAK;AAGzC;AAaA,eAAsB,EACrB,GACA,GACA,IAAY,IACZ,IAAkB,IACC;CAenB,OAde,EAAW,CAEtB,KAAU,CAAC,IAAkB,MAEjC,MAAM,EACL,GACA,GAAG,KAAK,UACP,GACA,IAAkB,IAAW,KAAA,GAC7B,CACD,EAAE,KACF,MACD,GAEO;AACR;AAYA,eAAsB,EACrB,GACA,GACA,IAAY,IACZ,IAAS,IACU;CAGnB,IAFe,EAAW,CAEtB,KAAU,CAAC,GAAW,OAAO;CAEjC,IAAI,IAAO;CAEX,KAAK,IAAM,KAAO,GAAS;EAC1B,IAAI,IAAQ,EAAQ;EAGpB,AAFI,MAAQ,IAAQ,IAAI,EAAM,KAE9B,KAAQ,GAAG,EAAI,GAAG,EAAM;CACzB;CAIA,OAFA,MAAM,EAAU,GAAM,GAAM,MAAM,GAE3B;AACR"}
|
package/utils/logger.d.ts
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export
|
|
1
|
+
import { Logger } from 'tslog';
|
|
2
|
+
export type LogLevelName = 'debug' | 'error' | 'info' | 'silly' | 'trace' | 'warn';
|
|
3
|
+
/** tslog v4 numeric minLevel by level name. */
|
|
4
|
+
export declare const LOG_LEVELS: Record<LogLevelName, number>;
|
|
5
|
+
export interface LoggerSettings {
|
|
6
|
+
maskAnyRegEx?: string[];
|
|
7
|
+
maskValuesOfKeys?: string[];
|
|
8
|
+
minLevel?: LogLevelName;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Builds a configured tslog logger. Extracted as a factory so settings logic
|
|
12
|
+
* is unit-testable and reused by sub-loggers.
|
|
13
|
+
*/
|
|
14
|
+
export declare function createLogger(settings?: LoggerSettings): Logger<unknown>;
|
|
15
|
+
/**
|
|
16
|
+
* Applies runtime settings to an existing logger by mutating `logger.settings`
|
|
17
|
+
* (tslog v4 has no `setSettings`).
|
|
18
|
+
*/
|
|
19
|
+
export declare function configureLogger(logger: Logger<unknown>, settings: LoggerSettings): void;
|
|
20
|
+
/** Global stdout logger. */
|
|
21
|
+
export declare const logger: Logger<unknown>;
|
|
3
22
|
//# sourceMappingURL=logger.d.ts.map
|
package/utils/logger.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../src/utils/logger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../src/utils/logger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAG/B,MAAM,MAAM,YAAY,GACrB,OAAO,GACP,OAAO,GACP,MAAM,GACN,OAAO,GACP,OAAO,GACP,MAAM,CAAC;AAEV,+CAA+C;AAC/C,eAAO,MAAM,UAAU,EAAE,MAAM,CAAC,YAAY,EAAE,MAAM,CAOnD,CAAC;AAEF,MAAM,WAAW,cAAc;IAC9B,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IACxB,gBAAgB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC5B,QAAQ,CAAC,EAAE,YAAY,CAAC;CACxB;AAUD;;;GAGG;AACH,wBAAgB,YAAY,CAAC,QAAQ,GAAE,cAAmB,GAAG,MAAM,CAAC,OAAO,CAAC,CAS3E;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAC9B,MAAM,EAAE,MAAM,CAAC,OAAO,CAAC,EACvB,QAAQ,EAAE,cAAc,GACtB,IAAI,CAWN;AAED,4BAA4B;AAC5B,eAAO,MAAM,MAAM,iBAAiB,CAAC"}
|
package/utils/logger.js
CHANGED
|
@@ -1,18 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
})
|
|
1
|
+
import { Logger as e } from "tslog";
|
|
2
|
+
//#region src/utils/logger.ts
|
|
3
|
+
var t = {
|
|
4
|
+
debug: 2,
|
|
5
|
+
error: 5,
|
|
6
|
+
info: 3,
|
|
7
|
+
silly: 0,
|
|
8
|
+
trace: 1,
|
|
9
|
+
warn: 4
|
|
10
|
+
}, n = {
|
|
11
|
+
hideLogPositionForProduction: !0,
|
|
12
|
+
maskPlaceholder: "***",
|
|
13
|
+
prettyLogTemplate: "{{hh}}:{{MM}}:{{ss}}.{{ms}} {{logLevelName}} ",
|
|
14
|
+
prettyLogTimeZone: "local",
|
|
15
|
+
type: "pretty"
|
|
16
|
+
};
|
|
17
|
+
function r(r = {}) {
|
|
18
|
+
return new e({
|
|
19
|
+
...n,
|
|
20
|
+
maskValuesOfKeys: r.maskValuesOfKeys ?? [],
|
|
21
|
+
minLevel: t[r.minLevel ?? "info"],
|
|
22
|
+
maskValuesRegEx: (r.maskAnyRegEx ?? []).map((e) => new RegExp(e))
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
function i(e, n) {
|
|
26
|
+
n.minLevel && (e.settings.minLevel = t[n.minLevel]), n.maskValuesOfKeys && (e.settings.maskValuesOfKeys = n.maskValuesOfKeys), n.maskAnyRegEx && (e.settings.maskValuesRegEx = n.maskAnyRegEx.map((e) => new RegExp(e)));
|
|
27
|
+
}
|
|
28
|
+
var a = r();
|
|
29
|
+
//#endregion
|
|
30
|
+
export { t as LOG_LEVELS, i as configureLogger, r as createLogger, a as logger };
|
|
31
|
+
|
|
18
32
|
//# sourceMappingURL=logger.js.map
|
package/utils/logger.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logger.js","
|
|
1
|
+
{"version":3,"file":"logger.js","names":[],"sources":["../../src/utils/logger.ts"],"sourcesContent":["import { Logger } from 'tslog';\r\nimport type { ISettingsParam } from 'tslog';\r\n\r\nexport type LogLevelName =\r\n\t| 'debug'\r\n\t| 'error'\r\n\t| 'info'\r\n\t| 'silly'\r\n\t| 'trace'\r\n\t| 'warn';\r\n\r\n/** tslog v4 numeric minLevel by level name. */\r\nexport const LOG_LEVELS: Record<LogLevelName, number> = {\r\n\tdebug: 2,\r\n\terror: 5,\r\n\tinfo: 3,\r\n\tsilly: 0,\r\n\ttrace: 1,\r\n\twarn: 4,\r\n};\r\n\r\nexport interface LoggerSettings {\r\n\tmaskAnyRegEx?: string[];\r\n\tmaskValuesOfKeys?: string[];\r\n\tminLevel?: LogLevelName;\r\n}\r\n\r\nconst BASE_SETTINGS: ISettingsParam<unknown> = {\r\n\thideLogPositionForProduction: true,\r\n\tmaskPlaceholder: '***',\r\n\tprettyLogTemplate: '{{hh}}:{{MM}}:{{ss}}.{{ms}}\\t{{logLevelName}}\\t',\r\n\tprettyLogTimeZone: 'local',\r\n\ttype: 'pretty',\r\n};\r\n\r\n/**\r\n * Builds a configured tslog logger. Extracted as a factory so settings logic\r\n * is unit-testable and reused by sub-loggers.\r\n */\r\nexport function createLogger(settings: LoggerSettings = {}): Logger<unknown> {\r\n\treturn new Logger({\r\n\t\t...BASE_SETTINGS,\r\n\t\tmaskValuesOfKeys: settings.maskValuesOfKeys ?? [],\r\n\t\tminLevel: LOG_LEVELS[settings.minLevel ?? 'info'],\r\n\t\tmaskValuesRegEx: (settings.maskAnyRegEx ?? []).map(\r\n\t\t\t(pattern) => new RegExp(pattern),\r\n\t\t),\r\n\t});\r\n}\r\n\r\n/**\r\n * Applies runtime settings to an existing logger by mutating `logger.settings`\r\n * (tslog v4 has no `setSettings`).\r\n */\r\nexport function configureLogger(\r\n\tlogger: Logger<unknown>,\r\n\tsettings: LoggerSettings,\r\n): void {\r\n\tif (settings.minLevel)\r\n\t\tlogger.settings.minLevel = LOG_LEVELS[settings.minLevel];\r\n\r\n\tif (settings.maskValuesOfKeys)\r\n\t\tlogger.settings.maskValuesOfKeys = settings.maskValuesOfKeys;\r\n\r\n\tif (settings.maskAnyRegEx)\r\n\t\tlogger.settings.maskValuesRegEx = settings.maskAnyRegEx.map(\r\n\t\t\t(pattern) => new RegExp(pattern),\r\n\t\t);\r\n}\r\n\r\n/** Global stdout logger. */\r\nexport const logger = createLogger();\r\n"],"mappings":";;AAYA,IAAa,IAA2C;CACvD,OAAO;CACP,OAAO;CACP,MAAM;CACN,OAAO;CACP,OAAO;CACP,MAAM;AACP,GAQM,IAAyC;CAC9C,8BAA8B;CAC9B,iBAAiB;CACjB,mBAAmB;CACnB,mBAAmB;CACnB,MAAM;AACP;AAMA,SAAgB,EAAa,IAA2B,CAAC,GAAoB;CAC5E,OAAO,IAAI,EAAO;EACjB,GAAG;EACH,kBAAkB,EAAS,oBAAoB,CAAC;EAChD,UAAU,EAAW,EAAS,YAAY;EAC1C,kBAAkB,EAAS,gBAAgB,CAAC,GAAG,KAC7C,MAAY,IAAI,OAAO,CAAO,CAChC;CACD,CAAC;AACF;AAMA,SAAgB,EACf,GACA,GACO;CAOP,AANI,EAAS,aACZ,EAAO,SAAS,WAAW,EAAW,EAAS,YAE5C,EAAS,qBACZ,EAAO,SAAS,mBAAmB,EAAS,mBAEzC,EAAS,iBACZ,EAAO,SAAS,kBAAkB,EAAS,aAAa,KACtD,MAAY,IAAI,OAAO,CAAO,CAChC;AACF;AAGA,IAAa,IAAS,EAAa"}
|
|
@@ -1,3 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Flatten a object keeping depth path
|
|
3
|
+
* in key using __ as level separator.
|
|
4
|
+
*
|
|
5
|
+
* @param {Record<string, any>} obj
|
|
6
|
+
* @param {string} nestingDelimiter char for delimit nesting levels
|
|
7
|
+
* @param {string} pkey first level key
|
|
8
|
+
*
|
|
9
|
+
* @returns {Record<string, unknown>} flattened object
|
|
10
|
+
*/
|
|
11
|
+
export declare function flatten(obj: Record<string, any>, nestingDelimiter?: string, pkey?: string): Record<string, unknown>;
|
|
12
|
+
/**
|
|
13
|
+
* Normalizes env object, converts arrays in list strings,
|
|
14
|
+
* only primitives types array,
|
|
15
|
+
* and removes $ global character from keys.
|
|
16
|
+
*
|
|
17
|
+
* @param {Record<string, any>} obj
|
|
18
|
+
* @param {string} nestingDelimiter char for delimit nesting levels
|
|
19
|
+
* @param {boolean} arrayDescomposition serialize or break down arrays
|
|
20
|
+
* @param {string} pkey first level key
|
|
21
|
+
*
|
|
22
|
+
* @returns {Record<string, unknown>} normalized object
|
|
23
|
+
*/
|
|
24
|
+
export declare function normalize(obj: Record<string, any>, nestingDelimiter?: string, arrayDescomposition?: boolean, pkey?: string): Record<string, unknown>;
|
|
3
25
|
//# sourceMappingURL=normalize.util.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"normalize.util.d.ts","sourceRoot":"","sources":["../../src/utils/normalize.util.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"normalize.util.d.ts","sourceRoot":"","sources":["../../src/utils/normalize.util.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,wBAAgB,OAAO,CACtB,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EACxB,gBAAgB,SAAO,EACvB,IAAI,SAAK,GACP,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CA0BzB;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,SAAS,CACxB,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EACxB,gBAAgB,SAAO,EACvB,mBAAmB,UAAQ,EAC3B,IAAI,SAAK,GACP,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAyCzB"}
|
package/utils/normalize.util.js
CHANGED
|
@@ -1,60 +1,39 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
if (value === null || type !== 'object' || Array.isArray(value)) {
|
|
16
|
-
flattened[key] = value;
|
|
17
|
-
continue;
|
|
18
|
-
}
|
|
19
|
-
Object.assign(flattened, flatten(value, nestingDelimiter, `${key}${nestingDelimiter}`));
|
|
20
|
-
}
|
|
21
|
-
return flattened;
|
|
1
|
+
//#region src/utils/normalize.util.ts
|
|
2
|
+
function e(t, n = "__", r = "") {
|
|
3
|
+
let i = {};
|
|
4
|
+
for (let a in t) {
|
|
5
|
+
let o = t[a], s = typeof o;
|
|
6
|
+
if (!(o === void 0 || s === "function") && a[0] !== "#") {
|
|
7
|
+
if (a = r + a, o === null || s !== "object" || Array.isArray(o)) {
|
|
8
|
+
i[a] = o;
|
|
9
|
+
continue;
|
|
10
|
+
}
|
|
11
|
+
Object.assign(i, e(o, n, `${a}${n}`));
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return i;
|
|
22
15
|
}
|
|
23
|
-
function
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
normalizeArray(flattened, key, value, nestingDelimiter, arrayDescomposition);
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
Object.assign(flattened, normalize(value, nestingDelimiter, arrayDescomposition, `${key}${nestingDelimiter}`));
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
return flattened;
|
|
16
|
+
function t(e, r = "__", i = !1, a = "") {
|
|
17
|
+
let o = {};
|
|
18
|
+
for (let s in e) {
|
|
19
|
+
let c = e[s], l = typeof c;
|
|
20
|
+
if (!(c == null || l === "function")) {
|
|
21
|
+
if (s = a + (s[0] === "$" ? s.slice(1) : s), l !== "object") {
|
|
22
|
+
o[s] = c;
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
Array.isArray(c) ? n(o, s, c, r, i) : Object.assign(o, t(c, r, i, `${s}${r}`));
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return o;
|
|
43
29
|
}
|
|
44
|
-
function
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
Object.assign(flattened, normalize(value[i], nestingDelimiter, arrayDescomposition, `${key}${i}${nestingDelimiter}`));
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
52
|
-
flattened[`${key}${i}`] = value[i];
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
flattened[key] = value.filter((v) => typeof v !== 'object').join(',');
|
|
58
|
-
}
|
|
30
|
+
function n(e, n, r, i = "__", a = !1) {
|
|
31
|
+
if (a) {
|
|
32
|
+
n = `${n}${i}`;
|
|
33
|
+
for (let [o, s] of r.entries()) typeof s == "object" ? Object.assign(e, t(s, i, a, `${n}${o}${i}`)) : e[`${n}${o}`] = s;
|
|
34
|
+
} else e[n] = r.filter((e) => typeof e != "object").join(",");
|
|
59
35
|
}
|
|
36
|
+
//#endregion
|
|
37
|
+
export { e as flatten, t as normalize };
|
|
38
|
+
|
|
60
39
|
//# sourceMappingURL=normalize.util.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"normalize.util.js","
|
|
1
|
+
{"version":3,"file":"normalize.util.js","names":[],"sources":["../../src/utils/normalize.util.ts"],"sourcesContent":["/**\r\n * Flatten a object keeping depth path\r\n * in key using __ as level separator.\r\n *\r\n * @param {Record<string, any>} obj\r\n * @param {string} nestingDelimiter char for delimit nesting levels\r\n * @param {string} pkey first level key\r\n *\r\n * @returns {Record<string, unknown>} flattened object\r\n */\r\nexport function flatten(\r\n\tobj: Record<string, any>,\r\n\tnestingDelimiter = '__',\r\n\tpkey = '',\r\n): Record<string, unknown> {\r\n\tconst flattened: Record<string, unknown> = {};\r\n\r\n\tfor (let key in obj) {\r\n\t\tconst value = obj[key];\r\n\t\tconst type = typeof value;\r\n\r\n\t\tif (value === undefined || type === 'function') continue;\r\n\r\n\t\t// skipped property\r\n\t\tif (key[0] === '#') continue;\r\n\t\tkey = pkey + key;\r\n\r\n\t\tif (value === null || type !== 'object' || Array.isArray(value)) {\r\n\t\t\tflattened[key] = value;\r\n\r\n\t\t\tcontinue;\r\n\t\t}\r\n\r\n\t\tObject.assign(\r\n\t\t\tflattened,\r\n\t\t\tflatten(value, nestingDelimiter, `${key}${nestingDelimiter}`),\r\n\t\t);\r\n\t}\r\n\r\n\treturn flattened;\r\n}\r\n\r\n/**\r\n * Normalizes env object, converts arrays in list strings,\r\n * only primitives types array,\r\n * and removes $ global character from keys.\r\n *\r\n * @param {Record<string, any>} obj\r\n * @param {string} nestingDelimiter char for delimit nesting levels\r\n * @param {boolean} arrayDescomposition serialize or break down arrays\r\n * @param {string} pkey first level key\r\n *\r\n * @returns {Record<string, unknown>} normalized object\r\n */\r\nexport function normalize(\r\n\tobj: Record<string, any>,\r\n\tnestingDelimiter = '__',\r\n\tarrayDescomposition = false,\r\n\tpkey = '',\r\n): Record<string, unknown> {\r\n\tconst flattened: Record<string, unknown> = {};\r\n\r\n\tfor (let key in obj) {\r\n\t\tconst value = obj[key];\r\n\t\tconst type = typeof value;\r\n\r\n\t\tif (value === null || value === undefined || type === 'function')\r\n\t\t\tcontinue;\r\n\r\n\t\t// shared/global keys are prefixed with `$`; strip it for injection\r\n\t\tkey = pkey + (key[0] === '$' ? key.slice(1) : key);\r\n\r\n\t\tif (type !== 'object') {\r\n\t\t\tflattened[key] = value;\r\n\r\n\t\t\tcontinue;\r\n\t\t}\r\n\r\n\t\tif (Array.isArray(value)) {\r\n\t\t\tnormalizeArray(\r\n\t\t\t\tflattened,\r\n\t\t\t\tkey,\r\n\t\t\t\tvalue,\r\n\t\t\t\tnestingDelimiter,\r\n\t\t\t\tarrayDescomposition,\r\n\t\t\t);\r\n\t\t} else {\r\n\t\t\tObject.assign(\r\n\t\t\t\tflattened,\r\n\t\t\t\tnormalize(\r\n\t\t\t\t\tvalue,\r\n\t\t\t\t\tnestingDelimiter,\r\n\t\t\t\t\tarrayDescomposition,\r\n\t\t\t\t\t`${key}${nestingDelimiter}`,\r\n\t\t\t\t),\r\n\t\t\t);\r\n\t\t}\r\n\t}\r\n\r\n\treturn flattened;\r\n}\r\n\r\n/**\r\n * Flatten and normalizes an array.\r\n *\r\n * @param {Record<string, unknown>} flattened\r\n * @param {string} key\r\n * @param {any[]} value\r\n * @param {string} [nestingDelimiter='__']\r\n * @param {boolean} [arrayDescomposition=false]\r\n */\r\nfunction normalizeArray(\r\n\tflattened: Record<string, unknown>,\r\n\tkey: string,\r\n\tvalue: any[],\r\n\tnestingDelimiter = '__',\r\n\tarrayDescomposition = false,\r\n): void {\r\n\tif (arrayDescomposition) {\r\n\t\tkey = `${key}${nestingDelimiter}`;\r\n\r\n\t\tfor (const [i, item] of value.entries()) {\r\n\t\t\tif (typeof item === 'object') {\r\n\t\t\t\tObject.assign(\r\n\t\t\t\t\tflattened,\r\n\t\t\t\t\tnormalize(\r\n\t\t\t\t\t\titem,\r\n\t\t\t\t\t\tnestingDelimiter,\r\n\t\t\t\t\t\tarrayDescomposition,\r\n\t\t\t\t\t\t`${key}${i}${nestingDelimiter}`,\r\n\t\t\t\t\t),\r\n\t\t\t\t);\r\n\t\t\t} else {\r\n\t\t\t\tflattened[`${key}${i}`] = item;\r\n\t\t\t}\r\n\t\t}\r\n\t} else {\r\n\t\tflattened[key] = value.filter((v) => typeof v !== 'object').join(',');\r\n\t}\r\n}\r\n"],"mappings":";AAUA,SAAgB,EACf,GACA,IAAmB,MACnB,IAAO,IACmB;CAC1B,IAAM,IAAqC,CAAC;CAE5C,KAAK,IAAI,KAAO,GAAK;EACpB,IAAM,IAAQ,EAAI,IACZ,IAAO,OAAO;EAEhB,YAAU,KAAA,KAAa,MAAS,eAGhC,EAAI,OAAO,KAGf;OAFA,IAAM,IAAO,GAET,MAAU,QAAQ,MAAS,YAAY,MAAM,QAAQ,CAAK,GAAG;IAChE,EAAU,KAAO;IAEjB;GACD;GAEA,OAAO,OACN,GACA,EAAQ,GAAO,GAAkB,GAAG,IAAM,GAAkB,CAC7D;EALA;CAMD;CAEA,OAAO;AACR;AAcA,SAAgB,EACf,GACA,IAAmB,MACnB,IAAsB,IACtB,IAAO,IACmB;CAC1B,IAAM,IAAqC,CAAC;CAE5C,KAAK,IAAI,KAAO,GAAK;EACpB,IAAM,IAAQ,EAAI,IACZ,IAAO,OAAO;EAEhB,WAAU,QAA+B,MAAS,aAMtD;OAFA,IAAM,KAAQ,EAAI,OAAO,MAAM,EAAI,MAAM,CAAC,IAAI,IAE1C,MAAS,UAAU;IACtB,EAAU,KAAO;IAEjB;GACD;GAEA,AAAI,MAAM,QAAQ,CAAK,IACtB,EACC,GACA,GACA,GACA,GACA,CACD,IAEA,OAAO,OACN,GACA,EACC,GACA,GACA,GACA,GAAG,IAAM,GACV,CACD;EAnBD;CAqBD;CAEA,OAAO;AACR;AAWA,SAAS,EACR,GACA,GACA,GACA,IAAmB,MACnB,IAAsB,IACf;CACP,IAAI,GAAqB;EACxB,IAAM,GAAG,IAAM;EAEf,KAAK,IAAM,CAAC,GAAG,MAAS,EAAM,QAAQ,GACrC,AAAI,OAAO,KAAS,WACnB,OAAO,OACN,GACA,EACC,GACA,GACA,GACA,GAAG,IAAM,IAAI,GACd,CACD,IAEA,EAAU,GAAG,IAAM,OAAO;CAG7B,OACC,EAAU,KAAO,EAAM,QAAQ,MAAM,OAAO,KAAM,QAAQ,EAAE,KAAK,GAAG;AAEtE"}
|
package/utils/schema.util.d.ts
CHANGED
|
@@ -1,11 +1,70 @@
|
|
|
1
1
|
import { JSONSchemaType, ValidateFunction } from 'ajv';
|
|
2
2
|
import { Options } from 'to-json-schema';
|
|
3
|
+
/**
|
|
4
|
+
* Generates JSON schema from JSON template/object.
|
|
5
|
+
*
|
|
6
|
+
* @export
|
|
7
|
+
* @param {Record<string, unknown>} json json object
|
|
8
|
+
* @param {Options} [options]
|
|
9
|
+
*
|
|
10
|
+
* @returns {*} {Record<string, unknown>}
|
|
11
|
+
*/
|
|
3
12
|
export declare function schemaFrom(json: Record<string, unknown>, options?: Options & {
|
|
4
13
|
nullable?: boolean;
|
|
5
14
|
}): Record<string, unknown>;
|
|
15
|
+
/**
|
|
16
|
+
* Validates if a object is a JSON schema.
|
|
17
|
+
*
|
|
18
|
+
* @export
|
|
19
|
+
* @param {Record<string, unknown>} schema
|
|
20
|
+
*
|
|
21
|
+
* @returns {boolean} if is a JSON schema
|
|
22
|
+
*/
|
|
6
23
|
export declare function isJsonSchemaObject(schema: Record<string, unknown>): schema is JSONSchemaType<object>;
|
|
24
|
+
/**
|
|
25
|
+
* Converts a JSON schema to JSON template.
|
|
26
|
+
*
|
|
27
|
+
* @export
|
|
28
|
+
* @param {Record<string, unknown>} schema JSON schema
|
|
29
|
+
* @param {Record<string, any>} [container] template container
|
|
30
|
+
*
|
|
31
|
+
* @returns {unknown} object or default value
|
|
32
|
+
*/
|
|
7
33
|
export declare function schemaToJson(schema: Record<string, unknown>, container?: Record<string, any>): unknown;
|
|
34
|
+
/**
|
|
35
|
+
* Flatten a JSON schema.
|
|
36
|
+
*
|
|
37
|
+
* @export
|
|
38
|
+
* @param {Record<string, unknown>} schema JSON schema
|
|
39
|
+
* @param {string} [parentKey] previous level key
|
|
40
|
+
* @param {string} [nestingDelimiter] char for delimit nesting levels
|
|
41
|
+
* @param {Record<string, any>} [container] result container
|
|
42
|
+
*
|
|
43
|
+
* @returns {Record<string, unknown>} flattened schema
|
|
44
|
+
*/
|
|
8
45
|
export declare function flatSchema(schema: Record<string, unknown>, parentKey?: string, nestingDelimiter?: string, container?: Record<string, any>): Record<string, unknown>;
|
|
46
|
+
/**
|
|
47
|
+
* Creates a JSON schema validator using AJV.
|
|
48
|
+
*
|
|
49
|
+
* @see https://ajv.js.org/
|
|
50
|
+
*
|
|
51
|
+
* @export
|
|
52
|
+
* @param {Record<string, object>} schema json schema by provider
|
|
53
|
+
* @param {boolean} enableFormats whether formats are enabled
|
|
54
|
+
*
|
|
55
|
+
* @returns {ValidateFunction} validators
|
|
56
|
+
*/
|
|
9
57
|
export declare function createValidator(schema: Record<string, unknown>, enableFormats?: boolean): ValidateFunction;
|
|
58
|
+
/**
|
|
59
|
+
* Creates a JSON schema validator lookup using AJV.
|
|
60
|
+
*
|
|
61
|
+
* @see https://ajv.js.org/
|
|
62
|
+
*
|
|
63
|
+
* @export
|
|
64
|
+
* @param {Record<string, object>} schemaLookup json schema by provider
|
|
65
|
+
* @param {boolean} enableFormats whether formats are enabled
|
|
66
|
+
*
|
|
67
|
+
* @returns {Record<string, ValidateFunction>} validators lookup
|
|
68
|
+
*/
|
|
10
69
|
export declare function createValidators(schemaLookup: Record<string, object>, enableFormats?: boolean): Record<string, ValidateFunction>;
|
|
11
70
|
//# sourceMappingURL=schema.util.d.ts.map
|