@anjianshi/utils 2.1.2 → 2.1.4
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/env-node/env-reader.d.ts +18 -3
- package/env-node/env-reader.js +40 -16
- package/package.json +3 -3
package/env-node/env-reader.d.ts
CHANGED
|
@@ -3,10 +3,25 @@
|
|
|
3
3
|
* 注意:依赖 dotenv 包
|
|
4
4
|
*/
|
|
5
5
|
export declare class EnvReader {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
protected loadedEnvs: Record<string, string>;
|
|
7
|
+
constructor(envFiles?: string | string[]);
|
|
8
|
+
protected toNumber(raw: string): number | undefined;
|
|
9
|
+
protected toBoolean(raw: string): boolean | undefined;
|
|
10
|
+
getRaw(key: string): string | undefined;
|
|
11
|
+
/**
|
|
12
|
+
* 获取指定 env 的值,并转换成与 defaults 匹配的类型。
|
|
13
|
+
* 若值不存在,返回 defaults。
|
|
14
|
+
*/
|
|
9
15
|
get(key: string, defaults: string): string;
|
|
10
16
|
get(key: string, defaults: number): number;
|
|
11
17
|
get(key: string, defaults: boolean): boolean;
|
|
18
|
+
get<T extends unknown[] | Record<string, unknown>>(key: string, defaults: T): T;
|
|
19
|
+
/**
|
|
20
|
+
* 获取指定 env 的值,并转换成指定类型,无需提供默认值。
|
|
21
|
+
* 值不存在或转换失败时,返回 undefined。
|
|
22
|
+
*/
|
|
23
|
+
getByType(key: string, type?: 'string'): string;
|
|
24
|
+
getByType(key: string, type: 'number'): number;
|
|
25
|
+
getByType(key: string, type: 'boolean'): boolean;
|
|
26
|
+
getByType<T extends unknown[] | Record<string, unknown>>(key: string, type: 'json'): T;
|
|
12
27
|
}
|
package/env-node/env-reader.js
CHANGED
|
@@ -1,31 +1,55 @@
|
|
|
1
1
|
import * as dotenv from 'dotenv';
|
|
2
|
+
import { safeParseJSON } from '../lang/string.js';
|
|
2
3
|
/**
|
|
3
4
|
* 读取 .env 文件,并获取格式化后的数据
|
|
4
5
|
* 注意:依赖 dotenv 包
|
|
5
6
|
*/
|
|
6
7
|
export class EnvReader {
|
|
7
|
-
|
|
8
|
-
envsFromFile = {};
|
|
8
|
+
loadedEnvs = {};
|
|
9
9
|
constructor(envFiles) {
|
|
10
|
-
this.envFiles = envFiles;
|
|
11
10
|
dotenv.config({
|
|
12
|
-
path:
|
|
13
|
-
processEnv: this.
|
|
11
|
+
path: envFiles,
|
|
12
|
+
processEnv: this.loadedEnvs, // 把从 .env 文件读到的内容写入此变量,而不是 process.env,以避免污染 process.env。
|
|
14
13
|
});
|
|
15
14
|
}
|
|
15
|
+
toNumber(raw) {
|
|
16
|
+
const num = parseInt(raw, 10);
|
|
17
|
+
return isFinite(num) ? num : undefined;
|
|
18
|
+
}
|
|
19
|
+
toBoolean(raw) {
|
|
20
|
+
const formatted = raw.toLowerCase().trim();
|
|
21
|
+
if (['1', 'true', 'on'].includes(formatted))
|
|
22
|
+
return true;
|
|
23
|
+
if (['0', 'false', 'off'].includes(formatted))
|
|
24
|
+
return false;
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
getRaw(key) {
|
|
28
|
+
return this.loadedEnvs[key] ?? process.env[key];
|
|
29
|
+
}
|
|
16
30
|
get(key, defaults) {
|
|
17
|
-
const
|
|
18
|
-
if (
|
|
31
|
+
const raw = this.getRaw(key);
|
|
32
|
+
if (raw === undefined)
|
|
19
33
|
return defaults;
|
|
20
|
-
if (typeof defaults === 'number')
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
else if (typeof defaults === '
|
|
25
|
-
return
|
|
26
|
-
}
|
|
27
|
-
else {
|
|
28
|
-
return value;
|
|
34
|
+
if (typeof defaults === 'number')
|
|
35
|
+
return this.toNumber(raw) ?? defaults;
|
|
36
|
+
else if (typeof defaults === 'boolean')
|
|
37
|
+
return this.toBoolean(raw) ?? defaults;
|
|
38
|
+
else if (Array.isArray(defaults) || typeof defaults === 'object') {
|
|
39
|
+
return safeParseJSON(raw) ?? defaults;
|
|
29
40
|
}
|
|
41
|
+
return raw;
|
|
42
|
+
}
|
|
43
|
+
getByType(key, type = 'string') {
|
|
44
|
+
const raw = this.getRaw(key);
|
|
45
|
+
if (raw === undefined)
|
|
46
|
+
return raw;
|
|
47
|
+
if (type === 'number')
|
|
48
|
+
return this.toNumber(raw);
|
|
49
|
+
if (type === 'boolean')
|
|
50
|
+
return this.toBoolean(raw);
|
|
51
|
+
if (type === 'json')
|
|
52
|
+
return safeParseJSON(raw);
|
|
53
|
+
return raw;
|
|
30
54
|
}
|
|
31
55
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anjianshi/utils",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.4",
|
|
4
4
|
"description": "Common JavaScript Utils",
|
|
5
5
|
"homepage": "https://github.com/anjianshi/js-packages/utils",
|
|
6
6
|
"bugs": {
|
|
@@ -28,8 +28,8 @@
|
|
|
28
28
|
"typescript": "^5.4.4",
|
|
29
29
|
"@anjianshi/presets-eslint-typescript": "4.0.4",
|
|
30
30
|
"@anjianshi/presets-eslint-node": "4.0.4",
|
|
31
|
-
"@anjianshi/presets-
|
|
32
|
-
"@anjianshi/presets-
|
|
31
|
+
"@anjianshi/presets-typescript": "3.1.4",
|
|
32
|
+
"@anjianshi/presets-prettier": "3.0.0"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
|
35
35
|
"chalk": "^5.3.0",
|