@atlantjs/backend 4.1.0 → 5.0.0

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,15 +0,0 @@
1
- export type EnvTypes = "string" | "port-number" | "array" | "enum" | "url-https" | "int" | "boolean" | "float";
2
- interface BasePickEnvType<ValueType> {
3
- name: string;
4
- description: string;
5
- type?: Exclude<EnvTypes, "enum">;
6
- values?: string[];
7
- isRequired?: boolean;
8
- default?: ValueType;
9
- }
10
- interface EnumPickEnvType<ValueType> extends Omit<BasePickEnvType<ValueType>, 'type'> {
11
- type: "enum";
12
- values: string[];
13
- }
14
- export type PickEnvType<ValueType = string> = BasePickEnvType<ValueType> | EnumPickEnvType<ValueType>;
15
- export {};
@@ -1,16 +0,0 @@
1
- import { PickEnvType } from "./env-var.type";
2
- export declare class EnvVar {
3
- private static variableValue;
4
- private static variableName;
5
- private static variableType;
6
- private static variableDescription;
7
- private static enumValues?;
8
- static get<ValueType = string>({ name, type, isRequired, default: defaultValue, values: enumValues, description, }: PickEnvType<ValueType>): ValueType;
9
- private static urlHttpVerify;
10
- private static portNumberVerify;
11
- private static intVerify;
12
- private static floatVerify;
13
- private static arrayVerify;
14
- private static booleanVerify;
15
- private static enumVerify;
16
- }
@@ -1,111 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.EnvVar = void 0;
4
- const arch_1 = require("@atlantjs/arch");
5
- const env_var_error_1 = require("./env-var.error");
6
- class EnvVar {
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) {
17
- case "boolean":
18
- EnvVar.booleanVerify();
19
- break;
20
- case "array":
21
- EnvVar.arrayVerify();
22
- break;
23
- case "enum":
24
- EnvVar.enumVerify();
25
- break;
26
- case "float":
27
- EnvVar.floatVerify();
28
- break;
29
- case "int":
30
- EnvVar.intVerify();
31
- break;
32
- case "port-number":
33
- EnvVar.portNumberVerify();
34
- break;
35
- case "url-https":
36
- EnvVar.urlHttpVerify();
37
- break;
38
- }
39
- return EnvVar.variableValue;
40
- }
41
- static urlHttpVerify() {
42
- if (arch_1._.isEmpty(EnvVar.variableValue)) {
43
- return undefined;
44
- }
45
- const urlPattern = /^https?:\/\/[^\s/$.?#].[^\s]*$/i;
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`);
48
- }
49
- return EnvVar.variableValue;
50
- }
51
- static portNumberVerify() {
52
- if (arch_1._.isEmpty(EnvVar.variableValue)) {
53
- return undefined;
54
- }
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`);
58
- }
59
- return EnvVar.variableValue;
60
- }
61
- static intVerify() {
62
- if (arch_1._.isEmpty(EnvVar.variableValue)) {
63
- return undefined;
64
- }
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`);
68
- }
69
- return intValue;
70
- }
71
- static floatVerify() {
72
- if (arch_1._.isEmpty(EnvVar.variableValue)) {
73
- return undefined;
74
- }
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`);
78
- }
79
- return floatValue;
80
- }
81
- static arrayVerify() {
82
- if (arch_1._.isUndefined(EnvVar.variableValue)) {
83
- return [];
84
- }
85
- return EnvVar.variableValue.split(",");
86
- }
87
- static booleanVerify() {
88
- if (arch_1._.isEmpty(EnvVar.variableValue)) {
89
- return undefined;
90
- }
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"`);
93
- }
94
- if (arch_1._.isEqual(EnvVar.variableValue, "true"))
95
- return true;
96
- return false;
97
- }
98
- static enumVerify() {
99
- if (arch_1._.isUndefined(EnvVar.variableValue)) {
100
- return undefined;
101
- }
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`);
104
- }
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(", ")}]`);
107
- }
108
- return EnvVar.variableValue;
109
- }
110
- }
111
- exports.EnvVar = EnvVar;