@calvear/env 2.10.5 โ 3.0.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 +26 -0
- package/README.md +97 -113
- package/arguments.d.ts +15 -15
- package/arguments.d.ts.map +1 -1
- package/arguments.js +142 -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 +65 -80
- package/commands/env.command.js.map +1 -1
- package/commands/export.command.d.ts +9 -2
- package/commands/export.command.d.ts.map +1 -1
- package/commands/export.command.js +54 -56
- 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 +21 -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 +21 -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 +17 -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 +88 -147
- 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 +53 -33
- package/providers/app-settings.provider.d.ts +5 -3
- package/providers/app-settings.provider.d.ts.map +1 -1
- package/providers/app-settings.provider.js +36 -51
- package/providers/app-settings.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/providers/secrets.provider.d.ts +5 -2
- package/providers/secrets.provider.d.ts.map +1 -1
- package/providers/secrets.provider.js +24 -33
- package/providers/secrets.provider.js.map +1 -1
- package/schemas/env.schema.json +1 -0
- 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 +71 -4
- package/utils/command.util.d.ts.map +1 -1
- package/utils/command.util.js +60 -135
- package/utils/command.util.js.map +1 -1
- package/utils/index.d.ts +8 -6
- package/utils/index.d.ts.map +1 -1
- package/utils/index.js +9 -23
- package/utils/interpolate.util.d.ts +29 -1
- package/utils/interpolate.util.d.ts.map +1 -1
- package/utils/interpolate.util.js +12 -29
- 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 +29 -41
- 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 -57
- package/utils/normalize.util.js.map +1 -1
- package/utils/schema.util.d.ts +62 -3
- package/utils/schema.util.d.ts.map +1 -1
- package/utils/schema.util.js +64 -92
- package/utils/schema.util.js.map +1 -1
- package/utils/ui.d.ts +14 -0
- package/utils/ui.d.ts.map +1 -0
- package/utils/ui.js +75 -0
- package/utils/ui.js.map +1 -0
- 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/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,61 +1,39 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
flattened[key] = value;
|
|
16
|
-
continue;
|
|
17
|
-
}
|
|
18
|
-
Object.assign(flattened, flatten(value, nestingDelimiter, `${key}${nestingDelimiter}`));
|
|
19
|
-
}
|
|
20
|
-
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;
|
|
21
15
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
if (Array.isArray(value)) {
|
|
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
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
if (typeof value[i] === 'object') {
|
|
50
|
-
Object.assign(flattened, normalize(value[i], nestingDelimiter, arrayDescomposition, `${key}${i}${nestingDelimiter}`));
|
|
51
|
-
}
|
|
52
|
-
else {
|
|
53
|
-
flattened[`${key}${i}`] = value[i];
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
else {
|
|
58
|
-
flattened[key] = value.filter((v) => typeof v !== 'object').join(',');
|
|
59
|
-
}
|
|
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(",");
|
|
60
35
|
}
|
|
36
|
+
//#endregion
|
|
37
|
+
export { e as flatten, t as normalize };
|
|
38
|
+
|
|
61
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 {*} {Promise<Record<string, unknown>>}
|
|
11
|
+
*/
|
|
3
12
|
export declare function schemaFrom(json: Record<string, unknown>, options?: Options & {
|
|
4
13
|
nullable?: boolean;
|
|
5
|
-
}): Record<string, unknown
|
|
14
|
+
}): Promise<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>;
|
|
9
|
-
|
|
10
|
-
|
|
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 {Promise<ValidateFunction>} validators
|
|
56
|
+
*/
|
|
57
|
+
export declare function createValidator(schema: Record<string, unknown>, enableFormats?: boolean): Promise<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 {Promise<Record<string, ValidateFunction>>} validators lookup
|
|
68
|
+
*/
|
|
69
|
+
export declare function createValidators(schemaLookup: Record<string, object>, enableFormats?: boolean): Promise<Record<string, ValidateFunction>>;
|
|
11
70
|
//# sourceMappingURL=schema.util.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.util.d.ts","sourceRoot":"","sources":["../../src/utils/schema.util.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"schema.util.d.ts","sourceRoot":"","sources":["../../src/utils/schema.util.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAU,cAAc,EAAE,gBAAgB,EAAE,MAAM,KAAK,CAAC;AACpE,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAiC9C;;;;;;;;GAQG;AACH,wBAAsB,UAAU,CAC/B,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,OAAO,CAAC,EAAE,OAAO,GAAG;IAAE,QAAQ,CAAC,EAAE,OAAO,CAAA;CAAE,GACxC,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAyBlC;AAED;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CACjC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC7B,MAAM,IAAI,cAAc,CAAC,MAAM,CAAC,CAOlC;AAED;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAC3B,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC/B,SAAS,GAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAM,GACjC,OAAO,CAST;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,UAAU,CACzB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC/B,SAAS,SAAK,EACd,gBAAgB,SAAO,EACvB,SAAS,GAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAM,GACjC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAmBzB;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,eAAe,CACpC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC/B,aAAa,UAAO,GAClB,OAAO,CAAC,gBAAgB,CAAC,CAI3B;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,gBAAgB,CACrC,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EACpC,aAAa,UAAO,GAClB,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,gBAAgB,CAAC,CAAC,CAS3C"}
|
package/utils/schema.util.js
CHANGED
|
@@ -1,100 +1,72 @@
|
|
|
1
|
-
|
|
2
|
-
var
|
|
3
|
-
|
|
1
|
+
//#region src/utils/schema.util.ts
|
|
2
|
+
var e, t, n, r, i, a = {
|
|
3
|
+
"ip-address": /^(?:(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d{1,2})$/,
|
|
4
|
+
color: /^(#?([\dA-Fa-f]{3}){1,2}\b|aqua|black|blue|fuchsia|gray|green|lime|maroon|navy|olive|orange|purple|red|silver|teal|white|yellow|(rgb\(\s*\b(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\b\s*,\s*\b(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\b\s*,\s*\b(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\b\s*\))|(rgb\(\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*,\s*(\d?\d%|100%)+\s*\)))$/,
|
|
5
|
+
hostname: /^(?=.{1,255}$)[\da-z](?:[\da-z-]{0,61}[\da-z])?(?:\.[\da-z](?:[\da-z-]{0,61}[\da-z])?)*\.?$/i,
|
|
6
|
+
alphanumeric: /^[\da-z]+$/i,
|
|
7
|
+
"utc-millisec": (e) => !Number.isNaN(+e),
|
|
8
|
+
alpha: /^[a-z]+$/i,
|
|
9
|
+
style: /\s*(.+?):\s*([^;]+);?/g,
|
|
10
|
+
phone: /^\+(?:\d ?){6,14}\d$/
|
|
4
11
|
};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
alphanumeric: /^[\dA-Za-z]+$/,
|
|
15
|
-
'utc-millisec': (input) => !Number.isNaN(+input),
|
|
16
|
-
alpha: /^[A-Za-z]+$/,
|
|
17
|
-
style: /\s*(.+?):\s*([^;]+);?/g,
|
|
18
|
-
phone: /^\+(?:\d ?){6,14}\d$/
|
|
19
|
-
};
|
|
20
|
-
function schemaFrom(json, options) {
|
|
21
|
-
return (0, to_json_schema_1.default)(json, {
|
|
22
|
-
required: false,
|
|
23
|
-
...options,
|
|
24
|
-
postProcessFnc: (type, schema, value, defaultFunc) => {
|
|
25
|
-
if (value !== json) {
|
|
26
|
-
schema.type = [type];
|
|
27
|
-
schema.nullable = options?.nullable ?? false;
|
|
28
|
-
}
|
|
29
|
-
if (value === null || value === undefined)
|
|
30
|
-
schema.nullable = true;
|
|
31
|
-
return defaultFunc(type, schema, value);
|
|
32
|
-
}
|
|
33
|
-
});
|
|
12
|
+
async function o(e, t) {
|
|
13
|
+
return i ||= (await import("to-json-schema")).default, i(e, {
|
|
14
|
+
required: !1,
|
|
15
|
+
...t,
|
|
16
|
+
postProcessFnc: (n, r, i, a) => (i !== e && (r.type = [n], r.nullable = t?.nullable ?? !1), i ?? (r.nullable = !0), a(n, r, i))
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
function s(e) {
|
|
20
|
+
return e.type === "object" ? !0 : Array.isArray(e.type) && e.type.includes("object");
|
|
34
21
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
22
|
+
function c(e, t = {}) {
|
|
23
|
+
if (s(e)) {
|
|
24
|
+
for (let n in e.properties) t[n] = c(e.properties[n]);
|
|
25
|
+
return t;
|
|
26
|
+
} else return e.default ?? (e.nullable ? null : void 0);
|
|
40
27
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
28
|
+
function l(e, t = "", n = "__", r = {}) {
|
|
29
|
+
if (s(e)) {
|
|
30
|
+
for (let i in e.properties) {
|
|
31
|
+
if (i[0] === "#") continue;
|
|
32
|
+
let a = t + (t ? n : "") + i;
|
|
33
|
+
r = {
|
|
34
|
+
...r,
|
|
35
|
+
...l(e.properties[i], a, n)
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
return r;
|
|
39
|
+
} else return { [t]: e };
|
|
51
40
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
if (isJsonSchemaObject(schema)) {
|
|
55
|
-
for (const key in schema.properties) {
|
|
56
|
-
if (key[0] === '#')
|
|
57
|
-
continue;
|
|
58
|
-
const subKey = parentKey + (parentKey ? nestingDelimiter : '') + key;
|
|
59
|
-
container = {
|
|
60
|
-
...container,
|
|
61
|
-
...flatSchema(schema.properties[key], subKey, nestingDelimiter)
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
return container;
|
|
65
|
-
}
|
|
66
|
-
else {
|
|
67
|
-
return { [parentKey]: schema };
|
|
68
|
-
}
|
|
41
|
+
async function u(e, t = !0) {
|
|
42
|
+
return (await f(t)).compile(e);
|
|
69
43
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
allowUnionTypes: true
|
|
75
|
-
});
|
|
76
|
-
if (enableFormats) {
|
|
77
|
-
(0, ajv_formats_1.default)(ajv, { mode: 'fast' });
|
|
78
|
-
for (const key in FORMAT_REGEXPS)
|
|
79
|
-
ajv.addFormat(key, FORMAT_REGEXPS[key]);
|
|
80
|
-
}
|
|
81
|
-
return ajv.compile(schema);
|
|
44
|
+
async function d(e, t = !0) {
|
|
45
|
+
let n = await f(t), r = {};
|
|
46
|
+
for (let t in e) r[t] = n.compile(e[t]);
|
|
47
|
+
return r;
|
|
82
48
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
49
|
+
async function f(i) {
|
|
50
|
+
if (!n) {
|
|
51
|
+
let [e, t] = await Promise.all([import("ajv"), import("ajv-formats")]);
|
|
52
|
+
n = e.Ajv, r = t.default;
|
|
53
|
+
}
|
|
54
|
+
if (i) {
|
|
55
|
+
if (!t) {
|
|
56
|
+
t = new n({
|
|
57
|
+
allErrors: !0,
|
|
58
|
+
allowUnionTypes: !0
|
|
59
|
+
}), r(t, { mode: "fast" });
|
|
60
|
+
for (let e in a) t.addFormat(e, a[e]);
|
|
61
|
+
}
|
|
62
|
+
return t;
|
|
63
|
+
}
|
|
64
|
+
return e ||= new n({
|
|
65
|
+
allErrors: !0,
|
|
66
|
+
allowUnionTypes: !0
|
|
67
|
+
}), e;
|
|
98
68
|
}
|
|
99
|
-
|
|
69
|
+
//#endregion
|
|
70
|
+
export { u as createValidator, d as createValidators, l as flatSchema, s as isJsonSchemaObject, o as schemaFrom, c as schemaToJson };
|
|
71
|
+
|
|
100
72
|
//# sourceMappingURL=schema.util.js.map
|
package/utils/schema.util.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.util.js","
|
|
1
|
+
{"version":3,"file":"schema.util.js","names":[],"sources":["../../src/utils/schema.util.ts"],"sourcesContent":["import type { Format, JSONSchemaType, ValidateFunction } from 'ajv';\r\nimport type { Options } from 'to-json-schema';\r\n\r\n// cached AJV instances (with and without formats)\r\nlet _ajv: any;\r\nlet _ajvWithFormats: any;\r\n\r\n// cached module references\r\nlet _AjvClass: any;\r\nlet _addFormats: any;\r\nlet _toJsonSchema: any;\r\n\r\n/* eslint-disable regexp/no-useless-assertions, regexp/no-super-linear-backtracking -- domain validation regexes from JSON Schema spec; modifying these patterns would change format validation behavior */\r\nconst FORMAT_REGEXPS: Record<string, Format> = {\r\n\t'ip-address':\r\n\t\t/^(?:(?:25[0-5]|2[0-4]\\d|[01]?\\d{1,2})\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d{1,2})$/,\r\n\r\n\tcolor: /^(#?([\\dA-Fa-f]{3}){1,2}\\b|aqua|black|blue|fuchsia|gray|green|lime|maroon|navy|olive|orange|purple|red|silver|teal|white|yellow|(rgb\\(\\s*\\b(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])\\b\\s*,\\s*\\b(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])\\b\\s*,\\s*\\b(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])\\b\\s*\\))|(rgb\\(\\s*(\\d?\\d%|100%)+\\s*,\\s*(\\d?\\d%|100%)+\\s*,\\s*(\\d?\\d%|100%)+\\s*\\)))$/,\r\n\r\n\thostname:\r\n\t\t/^(?=.{1,255}$)[\\da-z](?:[\\da-z-]{0,61}[\\da-z])?(?:\\.[\\da-z](?:[\\da-z-]{0,61}[\\da-z])?)*\\.?$/i,\r\n\r\n\talphanumeric: /^[\\da-z]+$/i,\r\n\r\n\t'utc-millisec': (input: string) => !Number.isNaN(+input),\r\n\r\n\talpha: /^[a-z]+$/i,\r\n\r\n\tstyle: /\\s*(.+?):\\s*([^;]+);?/g,\r\n\r\n\tphone: /^\\+(?:\\d ?){6,14}\\d$/,\r\n};\r\n/* eslint-enable regexp/no-useless-assertions, regexp/no-super-linear-backtracking */\r\n\r\n/**\r\n * Generates JSON schema from JSON template/object.\r\n *\r\n * @export\r\n * @param {Record<string, unknown>} json json object\r\n * @param {Options} [options]\r\n *\r\n * @returns {*} {Promise<Record<string, unknown>>}\r\n */\r\nexport async function schemaFrom(\r\n\tjson: Record<string, unknown>,\r\n\toptions?: Options & { nullable?: boolean },\r\n): Promise<Record<string, unknown>> {\r\n\tif (!_toJsonSchema) {\r\n\t\tconst mod = await import('to-json-schema');\r\n\t\t_toJsonSchema = mod.default;\r\n\t}\r\n\r\n\treturn _toJsonSchema(json, {\r\n\t\trequired: false,\r\n\t\t...options,\r\n\t\tpostProcessFnc: (\r\n\t\t\ttype: string,\r\n\t\t\tschema: any,\r\n\t\t\tvalue: unknown,\r\n\t\t\tdefaultFunc: any,\r\n\t\t) => {\r\n\t\t\tif (value !== json) {\r\n\t\t\t\tschema.type = [type];\r\n\t\t\t\tschema.nullable = options?.nullable ?? false;\r\n\t\t\t}\r\n\r\n\t\t\tif (value === null || value === undefined) schema.nullable = true;\r\n\r\n\t\t\treturn defaultFunc(type, schema, value);\r\n\t\t},\r\n\t});\r\n}\r\n\r\n/**\r\n * Validates if a object is a JSON schema.\r\n *\r\n * @export\r\n * @param {Record<string, unknown>} schema\r\n *\r\n * @returns {boolean} if is a JSON schema\r\n */\r\nexport function isJsonSchemaObject(\r\n\tschema: Record<string, unknown>,\r\n): schema is JSONSchemaType<object> {\r\n\tif (schema.type === 'object') return true;\r\n\r\n\treturn (\r\n\t\tArray.isArray(schema.type) &&\r\n\t\t(schema.type as string[]).includes('object')\r\n\t);\r\n}\r\n\r\n/**\r\n * Converts a JSON schema to JSON template.\r\n *\r\n * @export\r\n * @param {Record<string, unknown>} schema JSON schema\r\n * @param {Record<string, any>} [container] template container\r\n *\r\n * @returns {unknown} object or default value\r\n */\r\nexport function schemaToJson(\r\n\tschema: Record<string, unknown>,\r\n\tcontainer: Record<string, any> = {},\r\n): unknown {\r\n\tif (isJsonSchemaObject(schema)) {\r\n\t\tfor (const key in schema.properties)\r\n\t\t\tcontainer[key] = schemaToJson(schema.properties[key]);\r\n\r\n\t\treturn container;\r\n\t} else {\r\n\t\treturn schema.default ?? (schema.nullable ? null : undefined);\r\n\t}\r\n}\r\n\r\n/**\r\n * Flatten a JSON schema.\r\n *\r\n * @export\r\n * @param {Record<string, unknown>} schema JSON schema\r\n * @param {string} [parentKey] previous level key\r\n * @param {string} [nestingDelimiter] char for delimit nesting levels\r\n * @param {Record<string, any>} [container] result container\r\n *\r\n * @returns {Record<string, unknown>} flattened schema\r\n */\r\nexport function flatSchema(\r\n\tschema: Record<string, unknown>,\r\n\tparentKey = '',\r\n\tnestingDelimiter = '__',\r\n\tcontainer: Record<string, any> = {},\r\n): Record<string, unknown> {\r\n\tif (isJsonSchemaObject(schema)) {\r\n\t\tfor (const key in schema.properties) {\r\n\t\t\tif (key[0] === '#') continue;\r\n\r\n\t\t\t// global property, but prefix removed for injection\r\n\t\t\tconst subKey =\r\n\t\t\t\tparentKey + (parentKey ? nestingDelimiter : '') + key;\r\n\r\n\t\t\tcontainer = {\r\n\t\t\t\t...container,\r\n\t\t\t\t...flatSchema(schema.properties[key], subKey, nestingDelimiter),\r\n\t\t\t};\r\n\t\t}\r\n\r\n\t\treturn container;\r\n\t} else {\r\n\t\treturn { [parentKey]: schema };\r\n\t}\r\n}\r\n\r\n/**\r\n * Creates a JSON schema validator using AJV.\r\n *\r\n * @see https://ajv.js.org/\r\n *\r\n * @export\r\n * @param {Record<string, object>} schema json schema by provider\r\n * @param {boolean} enableFormats whether formats are enabled\r\n *\r\n * @returns {Promise<ValidateFunction>} validators\r\n */\r\nexport async function createValidator(\r\n\tschema: Record<string, unknown>,\r\n\tenableFormats = true,\r\n): Promise<ValidateFunction> {\r\n\tconst ajv = await getAjv(enableFormats);\r\n\r\n\treturn ajv.compile(schema);\r\n}\r\n\r\n/**\r\n * Creates a JSON schema validator lookup using AJV.\r\n *\r\n * @see https://ajv.js.org/\r\n *\r\n * @export\r\n * @param {Record<string, object>} schemaLookup json schema by provider\r\n * @param {boolean} enableFormats whether formats are enabled\r\n *\r\n * @returns {Promise<Record<string, ValidateFunction>>} validators lookup\r\n */\r\nexport async function createValidators(\r\n\tschemaLookup: Record<string, object>,\r\n\tenableFormats = true,\r\n): Promise<Record<string, ValidateFunction>> {\r\n\tconst ajv = await getAjv(enableFormats);\r\n\r\n\tconst validators: Record<string, ValidateFunction> = {};\r\n\r\n\tfor (const key in schemaLookup)\r\n\t\tvalidators[key] = ajv.compile(schemaLookup[key]);\r\n\r\n\treturn validators;\r\n}\r\n\r\n/**\r\n * Returns a cached AJV instance (lazy-loads ajv and ajv-formats on first call).\r\n *\r\n * @param {boolean} withFormats whether to include format validators\r\n * @returns {Promise<any>} cached AJV instance\r\n */\r\nasync function getAjv(withFormats: boolean): Promise<any> {\r\n\tif (!_AjvClass) {\r\n\t\tconst [ajvMod, formatsMod] = await Promise.all([\r\n\t\t\timport('ajv'),\r\n\t\t\timport('ajv-formats'),\r\n\t\t]);\r\n\t\t_AjvClass = ajvMod.Ajv;\r\n\t\t_addFormats = formatsMod.default;\r\n\t}\r\n\r\n\tif (withFormats) {\r\n\t\tif (!_ajvWithFormats) {\r\n\t\t\t_ajvWithFormats = new _AjvClass({\r\n\t\t\t\tallErrors: true,\r\n\t\t\t\tallowUnionTypes: true,\r\n\t\t\t});\r\n\r\n\t\t\t(\r\n\t\t\t\t_addFormats as unknown as (\r\n\t\t\t\t\tajv: any,\r\n\t\t\t\t\toptions?: { mode?: string },\r\n\t\t\t\t) => void\r\n\t\t\t)(_ajvWithFormats, { mode: 'fast' });\r\n\r\n\t\t\tfor (const key in FORMAT_REGEXPS)\r\n\t\t\t\t_ajvWithFormats.addFormat(key, FORMAT_REGEXPS[key]);\r\n\t\t}\r\n\r\n\t\treturn _ajvWithFormats;\r\n\t}\r\n\r\n\tif (!_ajv) {\r\n\t\t_ajv = new _AjvClass({\r\n\t\t\tallErrors: true,\r\n\t\t\tallowUnionTypes: true,\r\n\t\t});\r\n\t}\r\n\r\n\treturn _ajv;\r\n}\r\n"],"mappings":";AAIA,IAAI,GACA,GAGA,GACA,GACA,GAGE,IAAyC;CAC9C,cACC;CAED,OAAO;CAEP,UACC;CAED,cAAc;CAEd,iBAAiB,MAAkB,CAAC,OAAO,MAAM,CAAC,CAAK;CAEvD,OAAO;CAEP,OAAO;CAEP,OAAO;AACR;AAYA,eAAsB,EACrB,GACA,GACmC;CAMnC,OALA,AAEC,OAAgB,MADE,OAAO,mBACL,SAGd,EAAc,GAAM;EAC1B,UAAU;EACV,GAAG;EACH,iBACC,GACA,GACA,GACA,OAEI,MAAU,MACb,EAAO,OAAO,CAAC,CAAI,GACnB,EAAO,WAAW,GAAS,YAAY,KAGpC,MAAuC,EAAO,WAAW,KAEtD,EAAY,GAAM,GAAQ,CAAK;CAExC,CAAC;AACF;AAUA,SAAgB,EACf,GACmC;CAGnC,OAFI,EAAO,SAAS,WAAiB,KAGpC,MAAM,QAAQ,EAAO,IAAI,KACxB,EAAO,KAAkB,SAAS,QAAQ;AAE7C;AAWA,SAAgB,EACf,GACA,IAAiC,CAAC,GACxB;CACV,IAAI,EAAmB,CAAM,GAAG;EAC/B,KAAK,IAAM,KAAO,EAAO,YACxB,EAAU,KAAO,EAAa,EAAO,WAAW,EAAI;EAErD,OAAO;CACR,OACC,OAAO,EAAO,YAAY,EAAO,WAAW,OAAO,KAAA;AAErD;AAaA,SAAgB,EACf,GACA,IAAY,IACZ,IAAmB,MACnB,IAAiC,CAAC,GACR;CAC1B,IAAI,EAAmB,CAAM,GAAG;EAC/B,KAAK,IAAM,KAAO,EAAO,YAAY;GACpC,IAAI,EAAI,OAAO,KAAK;GAGpB,IAAM,IACL,KAAa,IAAY,IAAmB,MAAM;GAEnD,IAAY;IACX,GAAG;IACH,GAAG,EAAW,EAAO,WAAW,IAAM,GAAQ,CAAgB;GAC/D;EACD;EAEA,OAAO;CACR,OACC,OAAO,GAAG,IAAY,EAAO;AAE/B;AAaA,eAAsB,EACrB,GACA,IAAgB,IACY;CAG5B,QAAO,MAFW,EAAO,CAAa,GAE3B,QAAQ,CAAM;AAC1B;AAaA,eAAsB,EACrB,GACA,IAAgB,IAC4B;CAC5C,IAAM,IAAM,MAAM,EAAO,CAAa,GAEhC,IAA+C,CAAC;CAEtD,KAAK,IAAM,KAAO,GACjB,EAAW,KAAO,EAAI,QAAQ,EAAa,EAAI;CAEhD,OAAO;AACR;AAQA,eAAe,EAAO,GAAoC;CACzD,IAAI,CAAC,GAAW;EACf,IAAM,CAAC,GAAQ,KAAc,MAAM,QAAQ,IAAI,CAC9C,OAAO,QACP,OAAO,cACR,CAAC;EAED,AADA,IAAY,EAAO,KACnB,IAAc,EAAW;CAC1B;CAEA,IAAI,GAAa;EAChB,IAAI,CAAC,GAAiB;GAMrB,AALA,IAAkB,IAAI,EAAU;IAC/B,WAAW;IACX,iBAAiB;GAClB,CAAC,GAED,EAKE,GAAiB,EAAE,MAAM,OAAO,CAAC;GAEnC,KAAK,IAAM,KAAO,GACjB,EAAgB,UAAU,GAAK,EAAe,EAAI;EACpD;EAEA,OAAO;CACR;CASA,OAPA,AACC,MAAO,IAAI,EAAU;EACpB,WAAW;EACX,iBAAiB;CAClB,CAAC,GAGK;AACR"}
|
package/utils/ui.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** Formats a duration in ms as "142ms" or "1.4s". */
|
|
2
|
+
export declare function formatDuration(ms: number): string;
|
|
3
|
+
export declare const ui: {
|
|
4
|
+
header(version: string, env?: string, modes?: string[]): void;
|
|
5
|
+
provider(key: string, count: number): void;
|
|
6
|
+
/** Pretty, sorted, masked dump of the resolved environment (debug level). */
|
|
7
|
+
variables(env: Record<string, unknown>): void;
|
|
8
|
+
summary(total: number, ms: number): void;
|
|
9
|
+
running(command: string): void;
|
|
10
|
+
finished(ms: number): void;
|
|
11
|
+
failed(code: number): void;
|
|
12
|
+
action(emoji: string, message: string): void;
|
|
13
|
+
};
|
|
14
|
+
//# sourceMappingURL=ui.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ui.d.ts","sourceRoot":"","sources":["../../src/utils/ui.ts"],"names":[],"mappings":"AAqDA,qDAAqD;AACrD,wBAAgB,cAAc,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,CAEjD;AAED,eAAO,MAAM,EAAE;oBACE,MAAM,QAAQ,MAAM,UAAU,MAAM,EAAE,GAAG,IAAI;kBAY/C,MAAM,SAAS,MAAM,GAAG,IAAI;IAS1C,6EAA6E;mBAC9D,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI;mBA2B9B,MAAM,MAAM,MAAM,GAAG,IAAI;qBAQvB,MAAM,GAAG,IAAI;iBAMjB,MAAM,GAAG,IAAI;iBAKb,MAAM,GAAG,IAAI;kBAKZ,MAAM,WAAW,MAAM,GAAG,IAAI;CAG5C,CAAC"}
|
package/utils/ui.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { LOG_LEVELS as e, logger as t } from "./logger.js";
|
|
2
|
+
import n from "picocolors";
|
|
3
|
+
//#region src/utils/ui.ts
|
|
4
|
+
var r = {
|
|
5
|
+
"app-settings": "๐๏ธ",
|
|
6
|
+
local: "๐",
|
|
7
|
+
"package-json": "๐ฆ",
|
|
8
|
+
secrets: "๐"
|
|
9
|
+
};
|
|
10
|
+
function i(n) {
|
|
11
|
+
return (t.settings.minLevel ?? e.info) <= n;
|
|
12
|
+
}
|
|
13
|
+
function a(t = "", n = e.info) {
|
|
14
|
+
i(n) && process.stdout.write(`${t}\n`);
|
|
15
|
+
}
|
|
16
|
+
function o(e, n) {
|
|
17
|
+
let { maskPlaceholder: r = "***", maskValuesOfKeys: i = [], maskValuesRegEx: a = [] } = t.settings;
|
|
18
|
+
if (i.some((t) => t.toLowerCase() === e.toLowerCase())) return {
|
|
19
|
+
masked: !0,
|
|
20
|
+
value: r
|
|
21
|
+
};
|
|
22
|
+
let o = !1, s = n;
|
|
23
|
+
for (let e of a) {
|
|
24
|
+
let t = s.replaceAll(new RegExp(e.source, "g"), r);
|
|
25
|
+
t !== s && (o = !0), s = t;
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
masked: o,
|
|
29
|
+
value: s
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function s(e) {
|
|
33
|
+
return e < 1e3 ? `${Math.round(e)}ms` : `${(e / 1e3).toFixed(1)}s`;
|
|
34
|
+
}
|
|
35
|
+
var c = {
|
|
36
|
+
header(e, t, r) {
|
|
37
|
+
let i = [n.bold("โก env") + n.dim(` v${e}`)];
|
|
38
|
+
t && i.push(`๐ ${n.bold(n.green(t))}`), r && r.length > 0 && i.push(`๐งฉ ${n.magenta(r.join("+"))}`), a(), a(i.join(n.dim(" ยท "))), a();
|
|
39
|
+
},
|
|
40
|
+
provider(e, t) {
|
|
41
|
+
let i = r[e] ?? "๐งฉ", o = e === "secrets" ? "secrets" : "vars";
|
|
42
|
+
a(` ${i} ${n.cyan(e.padEnd(16))} ${n.bold(String(t))} ${n.dim(o)}`);
|
|
43
|
+
},
|
|
44
|
+
variables(t) {
|
|
45
|
+
if (!i(e.debug)) return;
|
|
46
|
+
let r = Object.entries(t).sort(([e], [t]) => e.localeCompare(t)), s = r.reduce((e, [t]) => Math.max(e, t.length), 0);
|
|
47
|
+
a(` ${n.dim(`environment (${r.length} variables)`)}`, e.debug);
|
|
48
|
+
for (let [t, i] of r) {
|
|
49
|
+
let { masked: r, value: c } = o(t, String(i)), l = r ? n.yellow(c) : n.green(c);
|
|
50
|
+
a(` ${n.cyan(t.padEnd(s))} ${n.dim("=")} ${l}`, e.debug);
|
|
51
|
+
}
|
|
52
|
+
a("", e.debug);
|
|
53
|
+
},
|
|
54
|
+
summary(e, t) {
|
|
55
|
+
let r = n.dim(`in ${s(t)}`);
|
|
56
|
+
a(), a(` ${n.green("โ")} ${n.bold(String(e))} variables loaded ${r}`);
|
|
57
|
+
},
|
|
58
|
+
running(e) {
|
|
59
|
+
a(), a(` ${n.yellow("โถ")} ${n.bold(e)}`), a();
|
|
60
|
+
},
|
|
61
|
+
finished(e) {
|
|
62
|
+
let t = n.dim(`finished in ${s(e)}`);
|
|
63
|
+
a(` ${n.green("โ")} ${t}`);
|
|
64
|
+
},
|
|
65
|
+
failed(e) {
|
|
66
|
+
a(` ${n.red(`โ exited with code ${e}`)}`);
|
|
67
|
+
},
|
|
68
|
+
action(e, t) {
|
|
69
|
+
a(` ${e} ${t}`);
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
//#endregion
|
|
73
|
+
export { s as formatDuration, c as ui };
|
|
74
|
+
|
|
75
|
+
//# sourceMappingURL=ui.js.map
|