@x-fiber-sys/be-config 0.1.0 → 0.1.1
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/config.js +88 -0
- package/dist/_cjs/index.js +17 -0
- package/dist/_types/config.d.ts +26 -0
- package/dist/_types/index.d.ts +1 -0
- package/package.json +18 -2
- package/src/config.ts +0 -149
- package/src/index.ts +0 -1
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Config = void 0;
|
|
4
|
+
require("dotenv/config");
|
|
5
|
+
const envalid_1 = require("envalid");
|
|
6
|
+
class Config {
|
|
7
|
+
constructor() {
|
|
8
|
+
this._config = {};
|
|
9
|
+
this._hotConfig = {};
|
|
10
|
+
}
|
|
11
|
+
async init(config) {
|
|
12
|
+
const specs = {};
|
|
13
|
+
for (const key in config) {
|
|
14
|
+
const env = config[key];
|
|
15
|
+
switch (env.type) {
|
|
16
|
+
case 'string':
|
|
17
|
+
specs[key] = env.specs ? (0, envalid_1.str)(env.specs) : (0, envalid_1.str)();
|
|
18
|
+
break;
|
|
19
|
+
case 'number':
|
|
20
|
+
specs[key] = env.specs ? (0, envalid_1.num)(env.specs) : (0, envalid_1.num)();
|
|
21
|
+
break;
|
|
22
|
+
case 'boolean':
|
|
23
|
+
specs[key] = env.specs ? (0, envalid_1.bool)(env.specs) : (0, envalid_1.bool)();
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
this._config = (0, envalid_1.cleanEnv)(process.env, specs);
|
|
28
|
+
}
|
|
29
|
+
get hotConfig() {
|
|
30
|
+
return this._hotConfig;
|
|
31
|
+
}
|
|
32
|
+
get serviceVersion() {
|
|
33
|
+
return process.env.npm_package_version;
|
|
34
|
+
}
|
|
35
|
+
get nodeVersion() {
|
|
36
|
+
return process.env.NODE_VERSION;
|
|
37
|
+
}
|
|
38
|
+
get isDevelopment() {
|
|
39
|
+
return this._config.isDevelopment;
|
|
40
|
+
}
|
|
41
|
+
get isTest() {
|
|
42
|
+
return process.env.NODE_ENV === 'test';
|
|
43
|
+
}
|
|
44
|
+
getString(key, isRequired = true, devVal) {
|
|
45
|
+
const val = this._config[key];
|
|
46
|
+
if (!val) {
|
|
47
|
+
if (isRequired) {
|
|
48
|
+
throw new Error(`Key '${String(key)}' is required, but was not found.`);
|
|
49
|
+
}
|
|
50
|
+
return devVal || null;
|
|
51
|
+
}
|
|
52
|
+
if (typeof val !== 'string') {
|
|
53
|
+
throw new Error(`Key '${String(key)}' is required, but was not found.`);
|
|
54
|
+
}
|
|
55
|
+
return val;
|
|
56
|
+
}
|
|
57
|
+
getBoolean(key, isRequired = true, devVal) {
|
|
58
|
+
const val = this._config[key];
|
|
59
|
+
if (val === undefined || val === null) {
|
|
60
|
+
if (isRequired) {
|
|
61
|
+
throw new Error(`Key '${String(key)}' is required, but was not found.`);
|
|
62
|
+
}
|
|
63
|
+
return devVal ?? null;
|
|
64
|
+
}
|
|
65
|
+
if (typeof val !== 'boolean') {
|
|
66
|
+
throw new Error(`Key '${String(key)}' must be a boolean, but got '${typeof val}'.`);
|
|
67
|
+
}
|
|
68
|
+
return val;
|
|
69
|
+
}
|
|
70
|
+
getNumber(key, isRequired = true, devVal) {
|
|
71
|
+
const val = this._config[key];
|
|
72
|
+
if (!val) {
|
|
73
|
+
if (isRequired) {
|
|
74
|
+
throw new Error(`Key '${String(key)}' is required, but was not found.`);
|
|
75
|
+
}
|
|
76
|
+
return devVal || null;
|
|
77
|
+
}
|
|
78
|
+
if (typeof val !== 'number') {
|
|
79
|
+
throw new Error(`Key '${String(key)}' is required, but was not found.`);
|
|
80
|
+
}
|
|
81
|
+
return val;
|
|
82
|
+
}
|
|
83
|
+
async destroy() {
|
|
84
|
+
new Config();
|
|
85
|
+
this._config = {};
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
exports.Config = Config;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
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("./config"), exports);
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import 'dotenv/config';
|
|
2
|
+
import type { Spec } from 'envalid';
|
|
3
|
+
export type ConfigType = 'string' | 'boolean' | 'number';
|
|
4
|
+
export type ConfigEnvs = Record<string, ConfigType>;
|
|
5
|
+
export type ConfigVal<T = ConfigType> = {
|
|
6
|
+
type: T;
|
|
7
|
+
specs?: Spec<unknown>;
|
|
8
|
+
};
|
|
9
|
+
export type ConfigObj<C extends ConfigEnvs> = {
|
|
10
|
+
[K in keyof C]: ConfigVal<C[K]>;
|
|
11
|
+
};
|
|
12
|
+
export declare class Config<C extends Record<string, ConfigType> = Record<string, ConfigType>> {
|
|
13
|
+
private _config;
|
|
14
|
+
private readonly _hotConfig;
|
|
15
|
+
constructor();
|
|
16
|
+
init(config: ConfigObj<C>): Promise<void>;
|
|
17
|
+
get hotConfig(): Record<string, string | number | boolean>;
|
|
18
|
+
get serviceVersion(): string;
|
|
19
|
+
get nodeVersion(): string;
|
|
20
|
+
get isDevelopment(): boolean;
|
|
21
|
+
get isTest(): boolean;
|
|
22
|
+
getString<V extends string, K extends keyof C = keyof C>(key: K, isRequired?: true, devVal?: V): V;
|
|
23
|
+
getBoolean<K extends keyof C = keyof C>(key: K, isRequired?: true, devVal?: boolean): boolean;
|
|
24
|
+
getNumber<K extends keyof C>(key: C[K] extends 'number' ? K : never, isRequired?: true, devVal?: number): number;
|
|
25
|
+
destroy(): Promise<void>;
|
|
26
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './config';
|
package/package.json
CHANGED
|
@@ -1,11 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@x-fiber-sys/be-config",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Service env configurations processing library",
|
|
5
5
|
"author": "pestsov-v <pestsov.js@gmail.com>",
|
|
6
6
|
"homepage": "https://github.com/pestsov-v/c-argo-back-mono#readme",
|
|
7
7
|
"license": "ISC",
|
|
8
|
-
"main": "
|
|
8
|
+
"main": "dist/_cjs/index.js",
|
|
9
|
+
"types": "dist/_types/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/_types/index.d.ts",
|
|
13
|
+
"default": "./dist/_cjs/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist/_cjs",
|
|
18
|
+
"dist/_types",
|
|
19
|
+
"README.md"
|
|
20
|
+
],
|
|
9
21
|
"publishConfig": {
|
|
10
22
|
"access": "public"
|
|
11
23
|
},
|
|
@@ -16,6 +28,10 @@
|
|
|
16
28
|
"bugs": {
|
|
17
29
|
"url": "https://github.com/pestsov-v/c-argo-back-mono/issues"
|
|
18
30
|
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"packages": "rm -rf dist && tsc -p tsconfig.cjs.json && tsc -p tsconfig.types.json",
|
|
33
|
+
"build": "npm run packages"
|
|
34
|
+
},
|
|
19
35
|
"dependencies": {
|
|
20
36
|
"dotenv": "^17.2.1",
|
|
21
37
|
"envalid": "^8.1.0"
|
package/src/config.ts
DELETED
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import 'dotenv/config'
|
|
2
|
-
import { num, str, bool, cleanEnv } from 'envalid'
|
|
3
|
-
|
|
4
|
-
import type { OptionalValidatorSpec, Spec } from 'envalid'
|
|
5
|
-
|
|
6
|
-
export type ConfigType = 'string' | 'boolean' | 'number'
|
|
7
|
-
|
|
8
|
-
export type ConfigEnvs = Record<string, ConfigType>
|
|
9
|
-
export type ConfigVal<T = ConfigType> = { type: T; specs?: Spec<unknown> }
|
|
10
|
-
export type ConfigObj<C extends ConfigEnvs> = { [K in keyof C]: ConfigVal<C[K]> }
|
|
11
|
-
|
|
12
|
-
export class Config<C extends Record<string, ConfigType> = Record<string, ConfigType>> {
|
|
13
|
-
private _config: Record<string, string | number | boolean>
|
|
14
|
-
private readonly _hotConfig: Record<string, string | number | boolean>
|
|
15
|
-
|
|
16
|
-
constructor() {
|
|
17
|
-
this._config = {}
|
|
18
|
-
this._hotConfig = {}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
public async init(config: ConfigObj<C>): Promise<void> {
|
|
22
|
-
const specs: Record<string, Spec<unknown>> = {}
|
|
23
|
-
|
|
24
|
-
for (const key in config) {
|
|
25
|
-
const env = config[key]
|
|
26
|
-
|
|
27
|
-
switch (env.type) {
|
|
28
|
-
case 'string':
|
|
29
|
-
specs[key] = env.specs ? str(env.specs) : str()
|
|
30
|
-
break
|
|
31
|
-
case 'number':
|
|
32
|
-
specs[key] = env.specs ? num(env.specs) : num()
|
|
33
|
-
break
|
|
34
|
-
case 'boolean':
|
|
35
|
-
specs[key] = env.specs ? bool(env.specs as OptionalValidatorSpec<boolean>) : bool()
|
|
36
|
-
break
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
this._config = cleanEnv(process.env, specs)
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
public get hotConfig() {
|
|
44
|
-
return this._hotConfig
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
public get serviceVersion() {
|
|
48
|
-
return process.env.npm_package_version as string
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
public get nodeVersion() {
|
|
52
|
-
return process.env.NODE_VERSION as string
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
public get isDevelopment(): boolean {
|
|
56
|
-
return this._config.isDevelopment as boolean
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
public get isTest(): boolean {
|
|
60
|
-
return process.env.NODE_ENV === 'test'
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
public getString<V extends string, K extends keyof C = keyof C>(
|
|
64
|
-
key: K,
|
|
65
|
-
isRequired?: true,
|
|
66
|
-
devVal?: V,
|
|
67
|
-
): V
|
|
68
|
-
public getString<V extends string, K extends keyof C = keyof C>(
|
|
69
|
-
key: K,
|
|
70
|
-
isRequired: boolean = true,
|
|
71
|
-
devVal?: string,
|
|
72
|
-
): V | null {
|
|
73
|
-
const val = this._config[key as string]
|
|
74
|
-
|
|
75
|
-
if (!val) {
|
|
76
|
-
if (isRequired) {
|
|
77
|
-
throw new Error(`Key '${String(key)}' is required, but was not found.`)
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return (devVal as V) || null
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (typeof val !== 'string') {
|
|
84
|
-
throw new Error(`Key '${String(key)}' is required, but was not found.`)
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
return val as V
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
public getBoolean<K extends keyof C = keyof C>(
|
|
91
|
-
key: K,
|
|
92
|
-
isRequired?: true,
|
|
93
|
-
devVal?: boolean,
|
|
94
|
-
): boolean
|
|
95
|
-
public getBoolean<K extends keyof C = keyof C>(
|
|
96
|
-
key: K,
|
|
97
|
-
isRequired: boolean = true,
|
|
98
|
-
devVal?: boolean,
|
|
99
|
-
): boolean | null {
|
|
100
|
-
const val = this._config[key as string]
|
|
101
|
-
|
|
102
|
-
if (val === undefined || val === null) {
|
|
103
|
-
if (isRequired) {
|
|
104
|
-
throw new Error(`Key '${String(key)}' is required, but was not found.`)
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
return devVal ?? null
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
if (typeof val !== 'boolean') {
|
|
111
|
-
throw new Error(`Key '${String(key)}' must be a boolean, but got '${typeof val}'.`)
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
return val
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
public getNumber<K extends keyof C>(
|
|
118
|
-
key: C[K] extends 'number' ? K : never,
|
|
119
|
-
isRequired?: true,
|
|
120
|
-
devVal?: number,
|
|
121
|
-
): number
|
|
122
|
-
public getNumber<K extends keyof C>(
|
|
123
|
-
key: C[K] extends 'number' ? K : never,
|
|
124
|
-
isRequired: boolean = true,
|
|
125
|
-
devVal?: number,
|
|
126
|
-
): number | null {
|
|
127
|
-
const val = this._config[key as string]
|
|
128
|
-
|
|
129
|
-
if (!val) {
|
|
130
|
-
if (isRequired) {
|
|
131
|
-
throw new Error(`Key '${String(key)}' is required, but was not found.`)
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
return devVal || null
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
if (typeof val !== 'number') {
|
|
138
|
-
throw new Error(`Key '${String(key)}' is required, but was not found.`)
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
return val
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
public async destroy() {
|
|
145
|
-
new Config()
|
|
146
|
-
|
|
147
|
-
this._config = {}
|
|
148
|
-
}
|
|
149
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './config'
|