-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.config.ts
More file actions
119 lines (109 loc) · 4.34 KB
/
example.config.ts
File metadata and controls
119 lines (109 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import type { Level } from 'pino'
import z from 'zod'
import { EnvConfigParser } from './src'
import fs from 'node:fs'
import path from 'node:path'
const parser = new EnvConfigParser({
onValidationError: 'silent',
mode: 'schema',
})
const config = {
// Application Settings
...parser.section('============================'),
...parser.section('==== Application Settings ===='),
...parser.section('============================'),
NODE_ENV: parser.string('NODE_ENV', 'development', val => {
if (val && ['development', 'production', 'test'].includes(val))
return val
throw new Error(
'NODE_ENV must be one of "development", "production", "test"'
)
}),
PORT: parser.number('PORT', 3000),
HOST: parser.string('HOST', '0.0.0.0'),
APP_SECRET: parser.string(
'APP_SECRET',
'supersecretjwtkeythatshouldbeverylongandcomplex'
),
// Database Configuration
...parser.section(),
...parser.section('========================='),
...parser.section('==== Database Config ===='),
...parser.section('========================='),
DATABASE_URL: parser.string(
'DATABASE_URL',
'postgresql://user:password@localhost:5432/mydb'
),
DB_MAX_CONNECTIONS: parser.integer('DB_MAX_CONNECTIONS', 10),
DB_SSL_ENABLED: parser.boolean('DB_SSL_ENABLED', false),
// API Keys and External Services
...parser.section(),
...parser.section('=============================='),
...parser.section('==== External API Config ===='),
...parser.section('=============================='),
STRIPE_SECRET_KEY: parser.string('STRIPE_SECRET_KEY', ''),
GOOGLE_MAPS_API_KEY: parser.string('GOOGLE_MAPS_API_KEY', ''),
TWILIO_ACCOUNT_SID: parser.string('TWILIO_ACCOUNT_SID', ''),
TWILIO_AUTH_TOKEN: parser.string('TWILIO_AUTH_TOKEN', ''),
TWILIO_PHONE_NUMBER: parser.string('TWILIO_PHONE_NUMBER', ''),
// Logging Configuration
...parser.section(),
...parser.section('========================'),
...parser.section('==== Logging Config ===='),
...parser.section('========================'),
LOG_LEVEL: parser.string('LOG_LEVEL', 'info', inVal => {
const val = inVal ? inVal.toLowerCase() : 'info'
const valid = ['fatal', 'error', 'warn', 'info', 'debug', 'trace']
if (valid.includes(val)) return val
console.error(
`[Config Warning] Invalid log level 'LOG_LEVEL': '${inVal}'. Defaulting to 'info'`
)
return 'info'
}) as Level,
LOG_JSON_FORMAT: parser.boolean('LOG_JSON_FORMAT', false),
LOG_PRETTY_PRINT: parser.boolean('LOG_PRETTY_PRINT', true),
// Feature Flags
...parser.section(),
...parser.section('======================'),
...parser.section('==== Feature Flags ===='),
...parser.section('======================'),
FEATURE_ANALYTICS_ENABLED: parser.boolean(
'FEATURE_ANALYTICS_ENABLED',
true
),
FEATURE_NEW_DASHBOARD: parser.boolean('FEATURE_NEW_DASHBOARD', false),
// Advanced Settings
...parser.section(),
...parser.section('========================'),
...parser.section('==== Advanced Config ===='),
...parser.section('========================'),
CACHE_TTL_SECONDS: parser.number('CACHE_TTL_SECONDS', 3600),
RATE_LIMIT_ENABLED: parser.boolean('RATE_LIMIT_ENABLED', true),
ALLOWED_ORIGINS: parser.zValidator(
'ALLOWED_ORIGINS',
z
.array(z.string())
.transform(val =>
Array.isArray(val) ? val : val.split(',').map(s => s.trim())
),
['*']
),
// Example of using passThrough for sensitive data (e.g., private keys)
...parser.section(),
...parser.section('========================'),
...parser.section('==== Sensitive Data ===='),
...parser.section('========================'),
RSA_PRIVATE_KEY: parser.passThrough('RSA_PRIVATE_KEY'),
}
export default config
// Script to generate .env.example
if (require.main === module) {
try {
const template = parser.getTemplateString()
const outputPath = path.resolve(__dirname, 'example.config.env')
fs.writeFileSync(outputPath, template)
console.log(`Successfully generated .env template at ${outputPath}`) } catch (error) {
console.error('Failed to generate .env.example:', error)
process.exit(1)
}
}