@atlantjs/backend 4.0.6 → 4.0.7
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.
|
@@ -6,27 +6,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.loadEnvs = loadEnvs;
|
|
7
7
|
const node_fs_1 = __importDefault(require("node:fs"));
|
|
8
8
|
const node_path_1 = __importDefault(require("node:path"));
|
|
9
|
-
const dotenv_1 = __importDefault(require("dotenv"));
|
|
10
9
|
const arch_1 = require("@atlantjs/arch");
|
|
10
|
+
const dotenv_1 = __importDefault(require("dotenv"));
|
|
11
11
|
function loadEnvs() {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
!node_fs_1.default.existsSync(node_path_1.default.join(__dirname, "..", "..", "..", "..", "..", "..", ".env"))) {
|
|
18
|
-
console.error("The .env file is missing with the variables and their values");
|
|
12
|
+
console.log("Loading environment variables...");
|
|
13
|
+
const envFilePath = node_path_1.default.join(__dirname, "..", "..", "..", "..", "..", "..", process.env.ENVIRONMENT === arch_1.NodeEnvs.test ? ".env.test" : ".env");
|
|
14
|
+
if (!node_fs_1.default.existsSync(envFilePath)) {
|
|
15
|
+
console.error(`The ${process.env.ENVIRONMENT === arch_1.NodeEnvs.test ? ".env.test" : ".env"} file is missing with the variables and their values`);
|
|
16
|
+
return;
|
|
19
17
|
}
|
|
20
|
-
dotenv_1.default.config({
|
|
21
|
-
path: (() => {
|
|
22
|
-
switch (process.env.ENVIRONMENT) {
|
|
23
|
-
case arch_1.NodeEnvs.test:
|
|
24
|
-
return node_path_1.default.join(__dirname, "..", "..", "..", "..", "..", "..", ".env.test");
|
|
25
|
-
case arch_1.NodeEnvs.production:
|
|
26
|
-
return node_path_1.default.join(__dirname, "..", "..", "..", "..", "..", "..", ".env");
|
|
27
|
-
default:
|
|
28
|
-
return node_path_1.default.join(__dirname, "..", "..", "..", "..", "..", "..", ".env");
|
|
29
|
-
}
|
|
30
|
-
})(),
|
|
31
|
-
});
|
|
18
|
+
dotenv_1.default.config({ path: envFilePath });
|
|
32
19
|
}
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.PickEnvError = void 0;
|
|
4
4
|
class PickEnvError extends Error {
|
|
5
5
|
constructor(variableName, variableDescription, messageError) {
|
|
6
|
-
super(`\n❯ ${messageError}:\n\n - ${variableName}: ${variableDescription}`);
|
|
6
|
+
super(`\n❯ ${process.env.ENVIRONMENT} - ${messageError}:\n\n - ${variableName}: ${variableDescription}`);
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
9
|
exports.PickEnvError = PickEnvError;
|
package/setup/env-var/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export declare class EnvVar {
|
|
|
5
5
|
private static variableType;
|
|
6
6
|
private static variableDescription;
|
|
7
7
|
private static enumValues?;
|
|
8
|
-
static get<ValueType = string>({ name, type, isRequired, default: defaultValue, values: enumValues, description }: PickEnvType<ValueType>): ValueType;
|
|
8
|
+
static get<ValueType = string>({ name, type, isRequired, default: defaultValue, values: enumValues, description, }: PickEnvType<ValueType>): ValueType;
|
|
9
9
|
private static urlHttpVerify;
|
|
10
10
|
private static portNumberVerify;
|
|
11
11
|
private static intVerify;
|
package/setup/env-var/index.js
CHANGED
|
@@ -1,117 +1,111 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.EnvVar = void 0;
|
|
4
|
-
const env_var_error_1 = require("./env-var.error");
|
|
5
4
|
const arch_1 = require("@atlantjs/arch");
|
|
5
|
+
const env_var_error_1 = require("./env-var.error");
|
|
6
6
|
class EnvVar {
|
|
7
|
-
static get({ name, type = "string", isRequired = true, default: defaultValue, values: enumValues, description }) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
// TODO: Caso o valor não seja válido com o typo da variável, informar e disponibilizar novamente o input
|
|
18
|
-
// TODO: Caso o valor seja válido, atribuir no arquivo .env e validar as demais variáveis
|
|
19
|
-
// TODO: ao finalizar todas as variáveis, caso tenha havido alguma alteração no arquivo .env, reiniciar o container
|
|
20
|
-
throw new env_var_error_1.PickEnvError(this.variableName, this.variableDescription, `A variável de ambiente tem que ter seu valor definido`);
|
|
21
|
-
}
|
|
22
|
-
switch (this.variableType) {
|
|
7
|
+
static get({ name, type = "string", isRequired = true, default: defaultValue, values: enumValues, description, }) {
|
|
8
|
+
EnvVar.variableValue = process.env[name] || defaultValue?.toString();
|
|
9
|
+
EnvVar.variableType = type;
|
|
10
|
+
EnvVar.variableName = name;
|
|
11
|
+
EnvVar.variableDescription = description;
|
|
12
|
+
EnvVar.enumValues = enumValues;
|
|
13
|
+
if (arch_1._.isEmpty(EnvVar.variableValue) && isRequired.truthy()) {
|
|
14
|
+
throw new env_var_error_1.PickEnvError(EnvVar.variableName, EnvVar.variableDescription, "The environment variable must have its value defined");
|
|
15
|
+
}
|
|
16
|
+
switch (EnvVar.variableType) {
|
|
23
17
|
case "boolean":
|
|
24
|
-
|
|
18
|
+
EnvVar.booleanVerify();
|
|
25
19
|
break;
|
|
26
20
|
case "array":
|
|
27
|
-
|
|
21
|
+
EnvVar.arrayVerify();
|
|
28
22
|
break;
|
|
29
23
|
case "enum":
|
|
30
|
-
|
|
24
|
+
EnvVar.enumVerify();
|
|
31
25
|
break;
|
|
32
26
|
case "float":
|
|
33
|
-
|
|
27
|
+
EnvVar.floatVerify();
|
|
34
28
|
break;
|
|
35
29
|
case "int":
|
|
36
|
-
|
|
30
|
+
EnvVar.intVerify();
|
|
37
31
|
break;
|
|
38
32
|
case "port-number":
|
|
39
|
-
|
|
33
|
+
EnvVar.portNumberVerify();
|
|
40
34
|
break;
|
|
41
35
|
case "url-https":
|
|
42
|
-
|
|
36
|
+
EnvVar.urlHttpVerify();
|
|
43
37
|
break;
|
|
44
38
|
}
|
|
45
|
-
return
|
|
39
|
+
return EnvVar.variableValue;
|
|
46
40
|
}
|
|
47
41
|
static urlHttpVerify() {
|
|
48
|
-
if (arch_1._.isEmpty(
|
|
42
|
+
if (arch_1._.isEmpty(EnvVar.variableValue)) {
|
|
49
43
|
return undefined;
|
|
50
44
|
}
|
|
51
45
|
const urlPattern = /^https?:\/\/[^\s/$.?#].[^\s]*$/i;
|
|
52
|
-
if (!urlPattern.test(
|
|
53
|
-
throw new env_var_error_1.PickEnvError(
|
|
46
|
+
if (!urlPattern.test(EnvVar.variableValue)) {
|
|
47
|
+
throw new env_var_error_1.PickEnvError(EnvVar.variableName, EnvVar.variableDescription, `O valor da variável de ambiente ${EnvVar.variableName} deve ser uma URL válida começando com http ou https`);
|
|
54
48
|
}
|
|
55
|
-
return
|
|
49
|
+
return EnvVar.variableValue;
|
|
56
50
|
}
|
|
57
51
|
static portNumberVerify() {
|
|
58
|
-
if (arch_1._.isEmpty(
|
|
52
|
+
if (arch_1._.isEmpty(EnvVar.variableValue)) {
|
|
59
53
|
return undefined;
|
|
60
54
|
}
|
|
61
|
-
const portNumber = parseInt(
|
|
62
|
-
if (isNaN(portNumber) || portNumber < 1 || portNumber > 65535) {
|
|
63
|
-
throw new env_var_error_1.PickEnvError(
|
|
55
|
+
const portNumber = Number.parseInt(EnvVar.variableValue, 10);
|
|
56
|
+
if (Number.isNaN(portNumber) || portNumber < 1 || portNumber > 65535) {
|
|
57
|
+
throw new env_var_error_1.PickEnvError(EnvVar.variableName, EnvVar.variableDescription, `O valor da variável de ambiente ${EnvVar.variableName} deve ser um número de porta válido entre 1 e 65535`);
|
|
64
58
|
}
|
|
65
|
-
return
|
|
59
|
+
return EnvVar.variableValue;
|
|
66
60
|
}
|
|
67
61
|
static intVerify() {
|
|
68
|
-
if (arch_1._.isEmpty(
|
|
62
|
+
if (arch_1._.isEmpty(EnvVar.variableValue)) {
|
|
69
63
|
return undefined;
|
|
70
64
|
}
|
|
71
|
-
const intValue = parseInt(
|
|
72
|
-
if (isNaN(intValue)) {
|
|
73
|
-
throw new env_var_error_1.PickEnvError(
|
|
65
|
+
const intValue = Number.parseInt(EnvVar.variableValue, 10);
|
|
66
|
+
if (Number.isNaN(intValue)) {
|
|
67
|
+
throw new env_var_error_1.PickEnvError(EnvVar.variableName, EnvVar.variableDescription, `O valor da variável de ambiente ${EnvVar.variableName} deve ser um número inteiro válido`);
|
|
74
68
|
}
|
|
75
69
|
return intValue;
|
|
76
70
|
}
|
|
77
71
|
static floatVerify() {
|
|
78
|
-
if (arch_1._.isEmpty(
|
|
72
|
+
if (arch_1._.isEmpty(EnvVar.variableValue)) {
|
|
79
73
|
return undefined;
|
|
80
74
|
}
|
|
81
|
-
const floatValue = parseFloat(
|
|
82
|
-
if (isNaN(floatValue)) {
|
|
83
|
-
throw new env_var_error_1.PickEnvError(
|
|
75
|
+
const floatValue = Number.parseFloat(EnvVar.variableValue);
|
|
76
|
+
if (Number.isNaN(floatValue)) {
|
|
77
|
+
throw new env_var_error_1.PickEnvError(EnvVar.variableName, EnvVar.variableDescription, `O valor da variável de ambiente ${EnvVar.variableName} deve ser um número float válido`);
|
|
84
78
|
}
|
|
85
79
|
return floatValue;
|
|
86
80
|
}
|
|
87
81
|
static arrayVerify() {
|
|
88
|
-
if (arch_1._.isUndefined(
|
|
82
|
+
if (arch_1._.isUndefined(EnvVar.variableValue)) {
|
|
89
83
|
return [];
|
|
90
84
|
}
|
|
91
|
-
return
|
|
85
|
+
return EnvVar.variableValue.split(",");
|
|
92
86
|
}
|
|
93
87
|
static booleanVerify() {
|
|
94
|
-
if (arch_1._.isEmpty(
|
|
88
|
+
if (arch_1._.isEmpty(EnvVar.variableValue)) {
|
|
95
89
|
return undefined;
|
|
96
90
|
}
|
|
97
|
-
if (arch_1._.isAnyOf(
|
|
98
|
-
throw new env_var_error_1.PickEnvError(
|
|
91
|
+
if (arch_1._.isAnyOf(EnvVar.variableValue, ["false", "true"]).falsy()) {
|
|
92
|
+
throw new env_var_error_1.PickEnvError(EnvVar.variableName, EnvVar.variableDescription, `O valor da variável de ambiente ${EnvVar.variableValue} tem que ser "false" ou "true"`);
|
|
99
93
|
}
|
|
100
|
-
if (arch_1._.isEqual(
|
|
94
|
+
if (arch_1._.isEqual(EnvVar.variableValue, "true"))
|
|
101
95
|
return true;
|
|
102
96
|
return false;
|
|
103
97
|
}
|
|
104
98
|
static enumVerify() {
|
|
105
|
-
if (arch_1._.isUndefined(
|
|
99
|
+
if (arch_1._.isUndefined(EnvVar.variableValue)) {
|
|
106
100
|
return undefined;
|
|
107
101
|
}
|
|
108
|
-
if (arch_1._.isUndefined(
|
|
109
|
-
throw new env_var_error_1.PickEnvError(
|
|
102
|
+
if (arch_1._.isUndefined(EnvVar.enumValues)) {
|
|
103
|
+
throw new env_var_error_1.PickEnvError(EnvVar.variableName, EnvVar.variableDescription, `A definição da variável de ambiente ${EnvVar.variableName} deve conter os valores de referencia do enum`);
|
|
110
104
|
}
|
|
111
|
-
if (
|
|
112
|
-
throw new env_var_error_1.PickEnvError(
|
|
105
|
+
if (EnvVar.enumValues.includes(EnvVar.variableValue).falsy()) {
|
|
106
|
+
throw new env_var_error_1.PickEnvError(EnvVar.variableName, EnvVar.variableDescription, `A variável ${EnvVar.variableName} deve conter algum dos seguintes valores: [${EnvVar.enumValues.join(", ")}]`);
|
|
113
107
|
}
|
|
114
|
-
return
|
|
108
|
+
return EnvVar.variableValue;
|
|
115
109
|
}
|
|
116
110
|
}
|
|
117
111
|
exports.EnvVar = EnvVar;
|