@atlantjs/backend 1.1.5 → 1.2.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.
@@ -1,6 +1,6 @@
1
1
  import { NodeEnvs } from "@atlantjs/arch";
2
- export declare const APP_PORT: string | string[];
2
+ export declare const APP_PORT: unknown;
3
3
  export declare const LOAD_MODULES: string[];
4
- export declare const CORS_ALLOWED_ORIGINS: "*";
4
+ export declare const CORS_ALLOWED_ORIGINS: string;
5
5
  export declare const ENVIRONMENT: NodeEnvs;
6
6
  export declare const isProdOrHomolog: () => boolean;
@@ -21,7 +21,7 @@ exports.ENVIRONMENT = new env_var_1.EnvVar({
21
21
  name: "ENVIRONMENT",
22
22
  type: "enum",
23
23
  isRequired: true,
24
- referenceValues: [
24
+ enumValues: [
25
25
  arch_1.NodeEnvs.test,
26
26
  arch_1.NodeEnvs.development,
27
27
  arch_1.NodeEnvs.production,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlantjs/backend",
3
- "version": "1.1.5",
3
+ "version": "1.2.1",
4
4
  "main": "index.js",
5
5
  "license": "UNLICENSED",
6
6
  "publishConfig": {
@@ -1,8 +1,8 @@
1
- export type EnvTypes = "string" | "port-number" | "array" | "enum" | "url-string" | "int" | "float";
2
- export interface PickEnvType<Type> {
1
+ export type EnvTypes = "string" | "port-number" | "array" | "enum" | "url-https" | "int" | "boolean" | "float";
2
+ export interface PickEnvType<ValueType> {
3
3
  name: string;
4
4
  type?: EnvTypes;
5
5
  isRequired?: boolean;
6
- defaultValue?: Type;
7
- referenceValues?: Type[];
6
+ defaultValue?: ValueType;
7
+ enumValues?: string[];
8
8
  }
@@ -1,12 +1,16 @@
1
1
  import { PickEnvType } from "./env-var.type";
2
- export declare class EnvVar<Type extends string | string[]> {
2
+ export declare class EnvVar<ValueType> {
3
3
  private variableValue;
4
4
  private variableName;
5
5
  private variableType;
6
- private variableReferenceValues?;
7
- constructor({ name, type, isRequired, defaultValue, referenceValues, }: PickEnvType<Type>);
8
- get(): Type;
9
- private asArray;
10
- private asBoolean;
11
- private asEnum;
6
+ private enumValues?;
7
+ constructor({ name, type, isRequired, defaultValue, enumValues, }: PickEnvType<ValueType>);
8
+ get(): ValueType;
9
+ private urlHttpVerify;
10
+ private portNumberVerify;
11
+ private intVerify;
12
+ private floatVerify;
13
+ private arrayVerify;
14
+ private booleanVerify;
15
+ private enumVerify;
12
16
  }
@@ -4,52 +4,107 @@ 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, referenceValues, }) {
8
- this.variableValue = process.env[name] || defaultValue;
9
- this.variableName = name;
7
+ constructor({ name, type = "string", isRequired = true, defaultValue, enumValues, }) {
8
+ this.variableValue = process.env[name] || defaultValue?.toString();
10
9
  this.variableType = type;
11
- this.variableReferenceValues = referenceValues;
10
+ this.variableName = name;
11
+ this.enumValues = enumValues;
12
12
  if (arch_1.Guardian.isEmpty(this.variableValue) && isRequired.truthy()) {
13
13
  throw new env_var_error_1.PickEnvError(`A variável de ambiente ${name} tem que ter seu valor definido`);
14
14
  }
15
15
  }
16
16
  get() {
17
- if (arch_1.Guardian.isEqual(this.variableType, "boolean")) {
18
- this.asBoolean();
17
+ switch (this.variableType) {
18
+ case "boolean":
19
+ this.booleanVerify();
20
+ break;
21
+ case "array":
22
+ this.arrayVerify();
23
+ break;
24
+ case "enum":
25
+ this.enumVerify();
26
+ break;
27
+ case "float":
28
+ this.floatVerify();
29
+ break;
30
+ case "int":
31
+ this.intVerify();
32
+ break;
33
+ case "port-number":
34
+ this.portNumberVerify();
35
+ break;
36
+ case "url-https":
37
+ this.urlHttpVerify();
38
+ break;
39
+ }
40
+ return this.variableValue;
41
+ }
42
+ urlHttpVerify() {
43
+ if (arch_1.Guardian.isEmpty(this.variableValue)) {
44
+ return undefined;
45
+ }
46
+ const urlPattern = /^https?:\/\/[^\s/$.?#].[^\s]*$/i;
47
+ 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`);
19
49
  }
20
- if (arch_1.Guardian.isEqual(this.variableType, "array")) {
21
- this.asArray();
50
+ return this.variableValue;
51
+ }
52
+ portNumberVerify() {
53
+ if (arch_1.Guardian.isEmpty(this.variableValue)) {
54
+ return undefined;
22
55
  }
23
- if (arch_1.Guardian.isNotUndefined(this.variableReferenceValues) &&
24
- arch_1.Guardian.isEqual(this.variableType, "enum")) {
25
- this.asEnum(this.variableReferenceValues);
56
+ const portNumber = parseInt(this.variableValue, 10);
57
+ 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`);
26
59
  }
27
60
  return this.variableValue;
28
61
  }
29
- asArray() {
62
+ intVerify() {
30
63
  if (arch_1.Guardian.isEmpty(this.variableValue)) {
64
+ return undefined;
65
+ }
66
+ const intValue = parseInt(this.variableValue, 10);
67
+ 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
+ }
70
+ return intValue;
71
+ }
72
+ floatVerify() {
73
+ if (arch_1.Guardian.isEmpty(this.variableValue)) {
74
+ return undefined;
75
+ }
76
+ const floatValue = parseFloat(this.variableValue);
77
+ 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
+ }
80
+ return floatValue;
81
+ }
82
+ arrayVerify() {
83
+ if (arch_1.Guardian.isUndefined(this.variableValue)) {
31
84
  return [];
32
85
  }
33
- const array = this.variableValue.split(",");
34
- return array;
86
+ return this.variableValue.split(",");
35
87
  }
36
- asBoolean() {
88
+ booleanVerify() {
37
89
  if (arch_1.Guardian.isEmpty(this.variableValue)) {
38
90
  return undefined;
39
91
  }
40
- if (arch_1.Guardian.isFalsy(arch_1.Guardian.isAnyOf(this.variableValue, ["false", "true"]))) {
92
+ if (arch_1.Guardian.isAnyOf(this.variableValue, ["false", "true"]).falsy()) {
41
93
  throw new env_var_error_1.PickEnvError(`O valor da variável de ambiente ${this.variableValue} tem que ser "false" ou "true"`);
42
94
  }
43
95
  if (arch_1.Guardian.isEqual(this.variableValue, "true"))
44
96
  return true;
45
97
  return false;
46
98
  }
47
- asEnum(validValues) {
99
+ enumVerify() {
48
100
  if (arch_1.Guardian.isUndefined(this.variableValue)) {
49
- return "";
101
+ return undefined;
102
+ }
103
+ 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`);
50
105
  }
51
- if (!validValues.includes(this.variableValue)) {
52
- throw new env_var_error_1.PickEnvError(`A variável ${this.variableName} deve conter algum dos seguintes valores: [${validValues.join(", ")}]`);
106
+ 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(", ")}]`);
53
108
  }
54
109
  return this.variableValue;
55
110
  }
@@ -3,7 +3,7 @@ export declare class CorsMiddleware {
3
3
  private readonly corsSpecs;
4
4
  private readonly cors;
5
5
  constructor(corsSpecs: string);
6
- static create(corsSpecs?: "*"): RequestHandler;
6
+ static create(corsSpecs?: string): RequestHandler;
7
7
  use(request: Request, response: Response, next: NextFunction): void;
8
8
  private parseCorsAllowedOrigins;
9
9
  private stringToRegex;