nestjs-env-getter 0.0.0-beta2 → 0.1.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.
- package/README.md +124 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,2 +1,125 @@
|
|
|
1
1
|
# nestjs-env-getter
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
The `nestjs-env-getter` is a NestJS module designed to simplify the process of retrieving and validating environment variables in your application. It provides a robust and type-safe way to handle environment variables, ensuring that your application starts with the correct configuration.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Type-safe environment variable retrieval**: Retrieve environment variables as strings, numbers, booleans, URLs, objects, arrays, or time periods.
|
|
8
|
+
- **Validation**: Validate environment variables against allowed values or custom validation functions.
|
|
9
|
+
- **Default values**: Provide default values for optional environment variables.
|
|
10
|
+
- **Error handling**: Automatically terminates the process with a descriptive error message if required environment variables are missing or invalid.
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
Install the package using npm:
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install nestjs-env-getter
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Or using yarn:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
yarn add nestjs-env-getter
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
### Importing the Module
|
|
29
|
+
|
|
30
|
+
To use the `EnvGetterService`, import the `EnvGetterModule` into your application's module:
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
import { Module } from "@nestjs/common";
|
|
34
|
+
import { EnvGetterModule } from "nestjs-env-getter";
|
|
35
|
+
|
|
36
|
+
@Module({ imports: [EnvGetterModule] })
|
|
37
|
+
export class AppModule {}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Using the Service
|
|
41
|
+
|
|
42
|
+
Inject the `EnvGetterService` into your service or controller to retrieve environment variables:
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
import { Injectable } from "@nestjs/common";
|
|
46
|
+
import { EnvGetterService } from "nestjs-env-getter";
|
|
47
|
+
|
|
48
|
+
@Injectable()
|
|
49
|
+
export class AppService {
|
|
50
|
+
constructor(private readonly envGetter: EnvGetterService) {
|
|
51
|
+
const port = this.envGetter.getRequiredNumericEnv("PORT");
|
|
52
|
+
console.log(`Application will run on port: ${port}`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Examples
|
|
58
|
+
|
|
59
|
+
#### Retrieving Required Environment Variables
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
const dbHost = this.envGetter.getRequiredEnv("DB_HOST");
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
#### Retrieving Optional Environment Variables with Default Values
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
const logLevel = this.envGetter.getOptionalEnv("LOG_LEVEL", "info");
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
#### Validating Environment Variables Against Allowed Values
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
const environment = this.envGetter.getRequiredEnv("NODE_ENV", [
|
|
75
|
+
"development",
|
|
76
|
+
"production",
|
|
77
|
+
"test",
|
|
78
|
+
]);
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
#### Parsing Numeric Environment Variables
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
const timeout = this.envGetter.getRequiredNumericEnv("TIMEOUT");
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
#### Parsing Boolean Environment Variables
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
const isDebugMode = this.envGetter.getRequiredBooleanEnv("DEBUG_MODE");
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
#### Parsing URLs
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
const apiUrl = this.envGetter.getRequiredURL("API_URL");
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
#### Parsing Time Periods
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
const cacheDuration = this.envGetter.getRequiredTimePeriod(
|
|
103
|
+
"CACHE_DURATION",
|
|
104
|
+
"s",
|
|
105
|
+
);
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
#### Parsing JSON Objects
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
const config = this.envGetter.getRequiredObject<{ key: string }>("CONFIG");
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
#### Parsing Arrays with Validation
|
|
115
|
+
|
|
116
|
+
```typescript
|
|
117
|
+
const allowedIPs = this.envGetter.getRequiredArray<string>(
|
|
118
|
+
"ALLOWED_IPS",
|
|
119
|
+
(ip) => ip.startsWith("192.") || "IP must start with 192.",
|
|
120
|
+
);
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## License
|
|
124
|
+
|
|
125
|
+
This project is licensed under the MIT License. See the [LICENSE](./LICENSE) file for details.
|