@atlantjs/backend 1.2.1 → 1.2.3
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/backend/application/env/env-vars.js +4 -2
- package/package.json +1 -1
- package/setup/env-var/env-var.error.d.ts +1 -0
- package/setup/env-var/env-var.error.js +3 -0
- package/setup/env-var/env-var.type.d.ts +10 -3
- package/setup/env-var/index.d.ts +2 -1
- package/setup/env-var/index.js +10 -9
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -6,16 +6,17 @@ const arch_1 = require("@atlantjs/arch");
|
|
|
6
6
|
exports.APP_PORT = new env_var_1.EnvVar({
|
|
7
7
|
name: "APP_PORT",
|
|
8
8
|
type: "port-number",
|
|
9
|
-
|
|
9
|
+
description: "The port number on which the application will run",
|
|
10
10
|
}).get();
|
|
11
11
|
exports.LOAD_MODULES = new env_var_1.EnvVar({
|
|
12
12
|
name: "LOAD_MODULES",
|
|
13
13
|
type: "array",
|
|
14
|
-
|
|
14
|
+
description: "List of modules, separated by commas, to be loaded by the application",
|
|
15
15
|
}).get();
|
|
16
16
|
exports.CORS_ALLOWED_ORIGINS = new env_var_1.EnvVar({
|
|
17
17
|
name: "CORS_ALLOWED_ORIGINS",
|
|
18
18
|
defaultValue: "*",
|
|
19
|
+
description: "Allowed origins for CORS",
|
|
19
20
|
}).get();
|
|
20
21
|
exports.ENVIRONMENT = new env_var_1.EnvVar({
|
|
21
22
|
name: "ENVIRONMENT",
|
|
@@ -27,6 +28,7 @@ exports.ENVIRONMENT = new env_var_1.EnvVar({
|
|
|
27
28
|
arch_1.NodeEnvs.production,
|
|
28
29
|
arch_1.NodeEnvs.homolog,
|
|
29
30
|
],
|
|
31
|
+
description: "The current environment in which the application is running",
|
|
30
32
|
}).get();
|
|
31
33
|
const isProdOrHomolog = () => [arch_1.NodeEnvs.homolog, arch_1.NodeEnvs.production].includes(exports.ENVIRONMENT);
|
|
32
34
|
exports.isProdOrHomolog = isProdOrHomolog;
|
package/package.json
CHANGED
|
@@ -2,5 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.PickEnvError = void 0;
|
|
4
4
|
class PickEnvError extends Error {
|
|
5
|
+
constructor(variableName, variableDescription, messageError) {
|
|
6
|
+
super(`${messageError}\n - ${variableName}: ${variableDescription}`);
|
|
7
|
+
}
|
|
5
8
|
}
|
|
6
9
|
exports.PickEnvError = PickEnvError;
|
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
export type EnvTypes = "string" | "port-number" | "array" | "enum" | "url-https" | "int" | "boolean" | "float";
|
|
2
|
-
|
|
2
|
+
interface BasePickEnvType<ValueType> {
|
|
3
3
|
name: string;
|
|
4
|
-
|
|
4
|
+
description: string;
|
|
5
|
+
type?: Exclude<EnvTypes, "enum">;
|
|
6
|
+
enumValues?: string[];
|
|
5
7
|
isRequired?: boolean;
|
|
6
8
|
defaultValue?: ValueType;
|
|
7
|
-
enumValues?: string[];
|
|
8
9
|
}
|
|
10
|
+
interface EnumPickEnvType<ValueType> extends Omit<BasePickEnvType<ValueType>, 'type'> {
|
|
11
|
+
type: "enum";
|
|
12
|
+
enumValues: string[];
|
|
13
|
+
}
|
|
14
|
+
export type PickEnvType<ValueType = string> = BasePickEnvType<ValueType> | EnumPickEnvType<ValueType>;
|
|
15
|
+
export {};
|
package/setup/env-var/index.d.ts
CHANGED
|
@@ -3,8 +3,9 @@ export declare class EnvVar<ValueType> {
|
|
|
3
3
|
private variableValue;
|
|
4
4
|
private variableName;
|
|
5
5
|
private variableType;
|
|
6
|
+
private variableDescription;
|
|
6
7
|
private enumValues?;
|
|
7
|
-
constructor({ name, type, isRequired, defaultValue, enumValues, }: PickEnvType<ValueType>);
|
|
8
|
+
constructor({ name, type, isRequired, defaultValue, enumValues, description }: PickEnvType<ValueType>);
|
|
8
9
|
get(): ValueType;
|
|
9
10
|
private urlHttpVerify;
|
|
10
11
|
private portNumberVerify;
|
package/setup/env-var/index.js
CHANGED
|
@@ -4,13 +4,14 @@ exports.EnvVar = void 0;
|
|
|
4
4
|
const env_var_error_1 = require("./env-var.error");
|
|
5
5
|
const arch_1 = require("@atlantjs/arch");
|
|
6
6
|
class EnvVar {
|
|
7
|
-
constructor({ name, type = "string", isRequired = true, defaultValue, enumValues, }) {
|
|
7
|
+
constructor({ name, type = "string", isRequired = true, defaultValue, enumValues, description }) {
|
|
8
8
|
this.variableValue = process.env[name] || defaultValue?.toString();
|
|
9
9
|
this.variableType = type;
|
|
10
10
|
this.variableName = name;
|
|
11
|
+
this.variableDescription = description;
|
|
11
12
|
this.enumValues = enumValues;
|
|
12
13
|
if (arch_1.Guardian.isEmpty(this.variableValue) && isRequired.truthy()) {
|
|
13
|
-
throw new env_var_error_1.PickEnvError(`A variável de ambiente
|
|
14
|
+
throw new env_var_error_1.PickEnvError(this.variableName, this.variableDescription, `A variável de ambiente tem que ter seu valor definido`);
|
|
14
15
|
}
|
|
15
16
|
}
|
|
16
17
|
get() {
|
|
@@ -45,7 +46,7 @@ class EnvVar {
|
|
|
45
46
|
}
|
|
46
47
|
const urlPattern = /^https?:\/\/[^\s/$.?#].[^\s]*$/i;
|
|
47
48
|
if (!urlPattern.test(this.variableValue)) {
|
|
48
|
-
throw new env_var_error_1.PickEnvError(`O valor da variável de ambiente ${this.variableName} deve ser uma URL válida começando com http ou https`);
|
|
49
|
+
throw new env_var_error_1.PickEnvError(this.variableName, this.variableDescription, `O valor da variável de ambiente ${this.variableName} deve ser uma URL válida começando com http ou https`);
|
|
49
50
|
}
|
|
50
51
|
return this.variableValue;
|
|
51
52
|
}
|
|
@@ -55,7 +56,7 @@ class EnvVar {
|
|
|
55
56
|
}
|
|
56
57
|
const portNumber = parseInt(this.variableValue, 10);
|
|
57
58
|
if (isNaN(portNumber) || portNumber < 1 || portNumber > 65535) {
|
|
58
|
-
throw new env_var_error_1.PickEnvError(`O valor da variável de ambiente ${this.variableName} deve ser um número de porta válido entre 1 e 65535`);
|
|
59
|
+
throw new env_var_error_1.PickEnvError(this.variableName, this.variableDescription, `O valor da variável de ambiente ${this.variableName} deve ser um número de porta válido entre 1 e 65535`);
|
|
59
60
|
}
|
|
60
61
|
return this.variableValue;
|
|
61
62
|
}
|
|
@@ -65,7 +66,7 @@ class EnvVar {
|
|
|
65
66
|
}
|
|
66
67
|
const intValue = parseInt(this.variableValue, 10);
|
|
67
68
|
if (isNaN(intValue)) {
|
|
68
|
-
throw new env_var_error_1.PickEnvError(`O valor da variável de ambiente ${this.variableName} deve ser um número inteiro válido`);
|
|
69
|
+
throw new env_var_error_1.PickEnvError(this.variableName, this.variableDescription, `O valor da variável de ambiente ${this.variableName} deve ser um número inteiro válido`);
|
|
69
70
|
}
|
|
70
71
|
return intValue;
|
|
71
72
|
}
|
|
@@ -75,7 +76,7 @@ class EnvVar {
|
|
|
75
76
|
}
|
|
76
77
|
const floatValue = parseFloat(this.variableValue);
|
|
77
78
|
if (isNaN(floatValue)) {
|
|
78
|
-
throw new env_var_error_1.PickEnvError(`O valor da variável de ambiente ${this.variableName} deve ser um número float válido`);
|
|
79
|
+
throw new env_var_error_1.PickEnvError(this.variableName, this.variableDescription, `O valor da variável de ambiente ${this.variableName} deve ser um número float válido`);
|
|
79
80
|
}
|
|
80
81
|
return floatValue;
|
|
81
82
|
}
|
|
@@ -90,7 +91,7 @@ class EnvVar {
|
|
|
90
91
|
return undefined;
|
|
91
92
|
}
|
|
92
93
|
if (arch_1.Guardian.isAnyOf(this.variableValue, ["false", "true"]).falsy()) {
|
|
93
|
-
throw new env_var_error_1.PickEnvError(`O valor da variável de ambiente ${this.variableValue} tem que ser "false" ou "true"`);
|
|
94
|
+
throw new env_var_error_1.PickEnvError(this.variableName, this.variableDescription, `O valor da variável de ambiente ${this.variableValue} tem que ser "false" ou "true"`);
|
|
94
95
|
}
|
|
95
96
|
if (arch_1.Guardian.isEqual(this.variableValue, "true"))
|
|
96
97
|
return true;
|
|
@@ -101,10 +102,10 @@ class EnvVar {
|
|
|
101
102
|
return undefined;
|
|
102
103
|
}
|
|
103
104
|
if (arch_1.Guardian.isUndefined(this.enumValues)) {
|
|
104
|
-
throw new env_var_error_1.PickEnvError(`A definição da variável de ambiente ${this.variableName} deve conter os valores de referencia do enum`);
|
|
105
|
+
throw new env_var_error_1.PickEnvError(this.variableName, this.variableDescription, `A definição da variável de ambiente ${this.variableName} deve conter os valores de referencia do enum`);
|
|
105
106
|
}
|
|
106
107
|
if (this.enumValues.includes(this.variableValue).falsy()) {
|
|
107
|
-
throw new env_var_error_1.PickEnvError(`A variável ${this.variableName} deve conter algum dos seguintes valores: [${this.enumValues.join(", ")}]`);
|
|
108
|
+
throw new env_var_error_1.PickEnvError(this.variableName, this.variableDescription, `A variável ${this.variableName} deve conter algum dos seguintes valores: [${this.enumValues.join(", ")}]`);
|
|
108
109
|
}
|
|
109
110
|
return this.variableValue;
|
|
110
111
|
}
|