@ooneex/app-env 0.0.1 → 0.0.4
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 +306 -0
- package/package.json +12 -7
- package/dist/ooneex-app-env-0.0.1.tgz +0 -0
package/README.md
CHANGED
|
@@ -1 +1,307 @@
|
|
|
1
1
|
# @ooneex/app-env
|
|
2
|
+
|
|
3
|
+
A lightweight application environment configuration and management utility for TypeScript applications. This package provides type-safe environment detection with boolean flags for all common deployment environments, making it easy to write environment-specific logic.
|
|
4
|
+
|
|
5
|
+

|
|
6
|
+

|
|
7
|
+

|
|
8
|
+

|
|
9
|
+

|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
✅ **15 Environment Types** - Support for local, development, staging, production, and more
|
|
14
|
+
|
|
15
|
+
✅ **Boolean Flags** - Quick environment checks with `isProduction`, `isDevelopment`, etc.
|
|
16
|
+
|
|
17
|
+
✅ **Type-Safe** - Full TypeScript support with proper type definitions
|
|
18
|
+
|
|
19
|
+
✅ **Zero Dependencies** - Lightweight with no external runtime dependencies
|
|
20
|
+
|
|
21
|
+
✅ **Validation** - Throws descriptive errors for invalid environment configurations
|
|
22
|
+
|
|
23
|
+
✅ **Immutable** - All properties are readonly for safety
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
|
|
27
|
+
### Bun
|
|
28
|
+
```bash
|
|
29
|
+
bun add @ooneex/app-env
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### pnpm
|
|
33
|
+
```bash
|
|
34
|
+
pnpm add @ooneex/app-env
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Yarn
|
|
38
|
+
```bash
|
|
39
|
+
yarn add @ooneex/app-env
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### npm
|
|
43
|
+
```bash
|
|
44
|
+
npm install @ooneex/app-env
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Usage
|
|
48
|
+
|
|
49
|
+
### Basic Usage
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
import { AppEnv } from '@ooneex/app-env';
|
|
53
|
+
|
|
54
|
+
const env = new AppEnv('production');
|
|
55
|
+
|
|
56
|
+
if (env.isProduction) {
|
|
57
|
+
console.log('Running in production mode');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (env.isDevelopment || env.isLocal) {
|
|
61
|
+
console.log('Debug mode enabled');
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### With Environment Variable
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
import { AppEnv, type EnvType } from '@ooneex/app-env';
|
|
69
|
+
|
|
70
|
+
const env = new AppEnv(process.env.APP_ENV as EnvType);
|
|
71
|
+
|
|
72
|
+
console.log(`Current environment: ${env.env}`);
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Environment-Specific Configuration
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
import { AppEnv } from '@ooneex/app-env';
|
|
79
|
+
|
|
80
|
+
const env = new AppEnv('staging');
|
|
81
|
+
|
|
82
|
+
const config = {
|
|
83
|
+
apiUrl: env.isProduction
|
|
84
|
+
? 'https://api.example.com'
|
|
85
|
+
: env.isStaging
|
|
86
|
+
? 'https://staging-api.example.com'
|
|
87
|
+
: 'http://localhost:3000',
|
|
88
|
+
debug: env.isLocal || env.isDevelopment,
|
|
89
|
+
caching: env.isProduction || env.isStaging
|
|
90
|
+
};
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Integration with Ooneex App
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
import { App } from '@ooneex/app';
|
|
97
|
+
import { AppEnv, type EnvType } from '@ooneex/app-env';
|
|
98
|
+
|
|
99
|
+
const app = new App({
|
|
100
|
+
env: new AppEnv(process.env.APP_ENV as EnvType),
|
|
101
|
+
// ... other config
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
await app.run();
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## API Reference
|
|
108
|
+
|
|
109
|
+
### Classes
|
|
110
|
+
|
|
111
|
+
#### `AppEnv`
|
|
112
|
+
|
|
113
|
+
Main class for environment configuration and detection.
|
|
114
|
+
|
|
115
|
+
**Constructor:**
|
|
116
|
+
```typescript
|
|
117
|
+
new AppEnv(env: EnvType)
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
**Parameters:**
|
|
121
|
+
- `env` - The environment type string
|
|
122
|
+
|
|
123
|
+
**Throws:** `AppEnvException` if env is not provided or invalid
|
|
124
|
+
|
|
125
|
+
**Properties:**
|
|
126
|
+
|
|
127
|
+
| Property | Type | Description |
|
|
128
|
+
|----------|------|-------------|
|
|
129
|
+
| `env` | `EnvType` | The current environment string |
|
|
130
|
+
| `isLocal` | `boolean` | True if environment is "local" |
|
|
131
|
+
| `isDevelopment` | `boolean` | True if environment is "development" |
|
|
132
|
+
| `isStaging` | `boolean` | True if environment is "staging" |
|
|
133
|
+
| `isTesting` | `boolean` | True if environment is "testing" |
|
|
134
|
+
| `isTest` | `boolean` | True if environment is "test" |
|
|
135
|
+
| `isQa` | `boolean` | True if environment is "qa" |
|
|
136
|
+
| `isUat` | `boolean` | True if environment is "uat" |
|
|
137
|
+
| `isIntegration` | `boolean` | True if environment is "integration" |
|
|
138
|
+
| `isPreview` | `boolean` | True if environment is "preview" |
|
|
139
|
+
| `isDemo` | `boolean` | True if environment is "demo" |
|
|
140
|
+
| `isSandbox` | `boolean` | True if environment is "sandbox" |
|
|
141
|
+
| `isBeta` | `boolean` | True if environment is "beta" |
|
|
142
|
+
| `isCanary` | `boolean` | True if environment is "canary" |
|
|
143
|
+
| `isHotfix` | `boolean` | True if environment is "hotfix" |
|
|
144
|
+
| `isProduction` | `boolean` | True if environment is "production" |
|
|
145
|
+
|
|
146
|
+
**Example:**
|
|
147
|
+
```typescript
|
|
148
|
+
const env = new AppEnv('production');
|
|
149
|
+
|
|
150
|
+
console.log(env.env); // "production"
|
|
151
|
+
console.log(env.isProduction); // true
|
|
152
|
+
console.log(env.isDevelopment); // false
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### Enums
|
|
156
|
+
|
|
157
|
+
#### `Environment`
|
|
158
|
+
|
|
159
|
+
Enum containing all valid environment values.
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
enum Environment {
|
|
163
|
+
LOCAL = "local",
|
|
164
|
+
DEVELOPMENT = "development",
|
|
165
|
+
STAGING = "staging",
|
|
166
|
+
TESTING = "testing",
|
|
167
|
+
TEST = "test",
|
|
168
|
+
QA = "qa",
|
|
169
|
+
UAT = "uat",
|
|
170
|
+
INTEGRATION = "integration",
|
|
171
|
+
PREVIEW = "preview",
|
|
172
|
+
DEMO = "demo",
|
|
173
|
+
SANDBOX = "sandbox",
|
|
174
|
+
BETA = "beta",
|
|
175
|
+
CANARY = "canary",
|
|
176
|
+
HOTFIX = "hotfix",
|
|
177
|
+
PRODUCTION = "production",
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### Types
|
|
182
|
+
|
|
183
|
+
#### `EnvType`
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
type EnvType = `${Environment}`;
|
|
187
|
+
// Equivalent to: "local" | "development" | "staging" | "testing" | "test" | "qa" | "uat" | "integration" | "preview" | "demo" | "sandbox" | "beta" | "canary" | "hotfix" | "production"
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
#### `IAppEnv`
|
|
191
|
+
|
|
192
|
+
```typescript
|
|
193
|
+
interface IAppEnv {
|
|
194
|
+
readonly env: EnvType;
|
|
195
|
+
readonly isLocal: boolean;
|
|
196
|
+
readonly isDevelopment: boolean;
|
|
197
|
+
readonly isStaging: boolean;
|
|
198
|
+
readonly isTesting: boolean;
|
|
199
|
+
readonly isTest: boolean;
|
|
200
|
+
readonly isQa: boolean;
|
|
201
|
+
readonly isUat: boolean;
|
|
202
|
+
readonly isIntegration: boolean;
|
|
203
|
+
readonly isPreview: boolean;
|
|
204
|
+
readonly isDemo: boolean;
|
|
205
|
+
readonly isSandbox: boolean;
|
|
206
|
+
readonly isBeta: boolean;
|
|
207
|
+
readonly isCanary: boolean;
|
|
208
|
+
readonly isHotfix: boolean;
|
|
209
|
+
readonly isProduction: boolean;
|
|
210
|
+
}
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
#### `AppEnvClassType`
|
|
214
|
+
|
|
215
|
+
```typescript
|
|
216
|
+
type AppEnvClassType = new (env: EnvType) => IAppEnv;
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
## Advanced Usage
|
|
220
|
+
|
|
221
|
+
### Custom Environment Helper
|
|
222
|
+
|
|
223
|
+
```typescript
|
|
224
|
+
import { AppEnv, type IAppEnv } from '@ooneex/app-env';
|
|
225
|
+
|
|
226
|
+
function isProductionLike(env: IAppEnv): boolean {
|
|
227
|
+
return env.isProduction || env.isStaging || env.isUat;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function isDevelopmentLike(env: IAppEnv): boolean {
|
|
231
|
+
return env.isLocal || env.isDevelopment || env.isTest;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const env = new AppEnv('staging');
|
|
235
|
+
console.log(isProductionLike(env)); // true
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
### Error Handling
|
|
239
|
+
|
|
240
|
+
```typescript
|
|
241
|
+
import { AppEnv, AppEnvException } from '@ooneex/app-env';
|
|
242
|
+
|
|
243
|
+
try {
|
|
244
|
+
const env = new AppEnv('' as any);
|
|
245
|
+
} catch (error) {
|
|
246
|
+
if (error instanceof AppEnvException) {
|
|
247
|
+
console.error('Invalid environment:', error.message);
|
|
248
|
+
// Handle missing or invalid environment
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
### Feature Flags Based on Environment
|
|
254
|
+
|
|
255
|
+
```typescript
|
|
256
|
+
import { AppEnv } from '@ooneex/app-env';
|
|
257
|
+
|
|
258
|
+
const env = new AppEnv('beta');
|
|
259
|
+
|
|
260
|
+
const features = {
|
|
261
|
+
newDashboard: env.isBeta || env.isCanary,
|
|
262
|
+
experimentalApi: env.isLocal || env.isDevelopment,
|
|
263
|
+
analytics: env.isProduction || env.isStaging,
|
|
264
|
+
debugToolbar: !env.isProduction,
|
|
265
|
+
maintenanceMode: env.isHotfix
|
|
266
|
+
};
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
### Logging Configuration
|
|
270
|
+
|
|
271
|
+
```typescript
|
|
272
|
+
import { AppEnv } from '@ooneex/app-env';
|
|
273
|
+
|
|
274
|
+
const env = new AppEnv('development');
|
|
275
|
+
|
|
276
|
+
const logConfig = {
|
|
277
|
+
level: env.isProduction ? 'error' : env.isStaging ? 'warn' : 'debug',
|
|
278
|
+
prettyPrint: env.isLocal || env.isDevelopment,
|
|
279
|
+
includeStackTrace: !env.isProduction
|
|
280
|
+
};
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
## License
|
|
284
|
+
|
|
285
|
+
This project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details.
|
|
286
|
+
|
|
287
|
+
## Contributing
|
|
288
|
+
|
|
289
|
+
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
|
|
290
|
+
|
|
291
|
+
### Development Setup
|
|
292
|
+
|
|
293
|
+
1. Clone the repository
|
|
294
|
+
2. Install dependencies: `bun install`
|
|
295
|
+
3. Run tests: `bun run test`
|
|
296
|
+
4. Build the project: `bun run build`
|
|
297
|
+
|
|
298
|
+
### Guidelines
|
|
299
|
+
|
|
300
|
+
- Write tests for new features
|
|
301
|
+
- Follow the existing code style
|
|
302
|
+
- Update documentation for API changes
|
|
303
|
+
- Ensure all tests pass before submitting PR
|
|
304
|
+
|
|
305
|
+
---
|
|
306
|
+
|
|
307
|
+
Made with ❤️ by the Ooneex team
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ooneex/app-env",
|
|
3
|
-
"description": "",
|
|
4
|
-
"version": "0.0.
|
|
3
|
+
"description": "Application environment configuration and management utilities for Ooneex framework",
|
|
4
|
+
"version": "0.0.4",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist",
|
|
@@ -25,15 +25,20 @@
|
|
|
25
25
|
"test": "bun test tests",
|
|
26
26
|
"build": "bunup",
|
|
27
27
|
"lint": "tsgo --noEmit && bunx biome lint",
|
|
28
|
-
"publish
|
|
29
|
-
"publish:pack": "bun pm pack --destination ./dist",
|
|
30
|
-
"publish:dry": "bun publish --dry-run",
|
|
31
|
-
"dev": "bun run src/dev.ts"
|
|
28
|
+
"publish": "bun publish --access public || true"
|
|
32
29
|
},
|
|
33
30
|
"dependencies": {
|
|
34
31
|
"@ooneex/exception": "0.0.1",
|
|
35
32
|
"@ooneex/http-status": "0.0.1"
|
|
36
33
|
},
|
|
37
34
|
"devDependencies": {},
|
|
38
|
-
"
|
|
35
|
+
"keywords": [
|
|
36
|
+
"bun",
|
|
37
|
+
"config",
|
|
38
|
+
"configuration",
|
|
39
|
+
"env",
|
|
40
|
+
"environment",
|
|
41
|
+
"ooneex",
|
|
42
|
+
"typescript"
|
|
43
|
+
]
|
|
39
44
|
}
|
|
Binary file
|