@hkdigital/lib-core 0.4.18 → 0.4.20
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/dist/services/README.md +254 -47
- package/dist/services/manager-plugins/ConfigPlugin.d.ts +68 -0
- package/dist/services/manager-plugins/ConfigPlugin.js +329 -0
- package/dist/services/service-base/ServiceBase.d.ts +59 -22
- package/dist/services/service-base/ServiceBase.js +139 -61
- package/dist/services/service-base/constants.d.ts +30 -14
- package/dist/services/service-base/constants.js +42 -14
- package/dist/services/service-base/typedef.d.ts +15 -9
- package/dist/services/service-base/typedef.js +33 -11
- package/dist/services/service-manager/ServiceManager.d.ts +25 -5
- package/dist/services/service-manager/ServiceManager.js +103 -20
- package/dist/services/service-manager/constants.js +0 -1
- package/dist/services/service-manager/typedef.d.ts +36 -14
- package/dist/services/service-manager/typedef.js +30 -13
- package/dist/util/sveltekit/env/README.md +424 -0
- package/dist/util/sveltekit/env/all.d.ts +54 -0
- package/dist/util/sveltekit/env/all.js +97 -0
- package/dist/util/sveltekit/env/parsers.d.ts +135 -0
- package/dist/util/sveltekit/env/parsers.js +257 -0
- package/dist/util/sveltekit/env/private.d.ts +56 -0
- package/dist/util/sveltekit/env/private.js +87 -0
- package/dist/util/sveltekit/env/public.d.ts +52 -0
- package/dist/util/sveltekit/env/public.js +82 -0
- package/dist/util/sveltekit/env-all.d.ts +1 -0
- package/dist/util/sveltekit/env-all.js +19 -0
- package/dist/util/sveltekit/env-private.d.ts +1 -0
- package/dist/util/sveltekit/env-private.js +18 -0
- package/dist/util/sveltekit/env-public.d.ts +1 -0
- package/dist/util/sveltekit/env-public.js +18 -0
- package/package.json +1 -1
- package/dist/util/index.js__ +0 -20
- package/dist/util/sveltekit/index.d.ts +0 -0
- package/dist/util/sveltekit/index.js +0 -0
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
# SvelteKit Environment Utilities
|
|
2
|
+
|
|
3
|
+
A collection of utilities for working with SvelteKit environment variables with automatic grouping, type parsing, and intelligent organization.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
These utilities automatically organize your environment variables into logical groups based on their prefixes, parse values to appropriate types, and provide a clean API for accessing configuration in both client and server contexts.
|
|
8
|
+
|
|
9
|
+
## Key Features
|
|
10
|
+
|
|
11
|
+
- 🎯 **Simple & Predictable**: Variables with underscores get grouped by prefix
|
|
12
|
+
- 🔄 **Automatic Type Parsing**: Converts strings to numbers, booleans, null, etc.
|
|
13
|
+
- 🏗️ **Smart Grouping**: `DATABASE_HOST` + `DATABASE_PORT` → `{ database: { host, port } }`
|
|
14
|
+
- 📦 **SvelteKit Integration**: Works with public/private environment variables
|
|
15
|
+
- 🔒 **Type Safe**: Full TypeScript support with proper environment separation
|
|
16
|
+
|
|
17
|
+
## Basic Examples
|
|
18
|
+
|
|
19
|
+
### Simple Prefix Grouping
|
|
20
|
+
|
|
21
|
+
```javascript
|
|
22
|
+
import { autoGroupEnvByPrefix } from '$lib/util/sveltekit/env/parsers.js';
|
|
23
|
+
|
|
24
|
+
// Environment variables:
|
|
25
|
+
// DATABASE_HOST=localhost
|
|
26
|
+
// DATABASE_PORT=5432
|
|
27
|
+
// REDIS_URL=redis://cache
|
|
28
|
+
// DEBUG_MODE=true
|
|
29
|
+
// API_KEY=secret123
|
|
30
|
+
|
|
31
|
+
const env = {
|
|
32
|
+
'DATABASE_HOST': 'localhost',
|
|
33
|
+
'DATABASE_PORT': '5432',
|
|
34
|
+
'REDIS_URL': 'redis://cache',
|
|
35
|
+
'DEBUG_MODE': 'true',
|
|
36
|
+
'API_KEY': 'secret123'
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const config = autoGroupEnvByPrefix(env);
|
|
40
|
+
console.log(config);
|
|
41
|
+
// Output:
|
|
42
|
+
// {
|
|
43
|
+
// database: {
|
|
44
|
+
// host: 'localhost',
|
|
45
|
+
// port: 5432 // ← Parsed as number
|
|
46
|
+
// },
|
|
47
|
+
// redis: {
|
|
48
|
+
// url: 'redis://cache'
|
|
49
|
+
// },
|
|
50
|
+
// debug: {
|
|
51
|
+
// mode: true // ← Parsed as boolean
|
|
52
|
+
// },
|
|
53
|
+
// api: {
|
|
54
|
+
// key: 'secret123'
|
|
55
|
+
// }
|
|
56
|
+
// }
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Variables Without Underscores
|
|
60
|
+
|
|
61
|
+
```javascript
|
|
62
|
+
const env = {
|
|
63
|
+
'DATABASE_HOST': 'localhost', // → database.host (grouped)
|
|
64
|
+
'API_KEY': 'secret', // → api.key (grouped)
|
|
65
|
+
'HOST': 'localhost', // → host (top-level, no underscore)
|
|
66
|
+
'PORT': '3000' // → port (top-level, no underscore)
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const config = autoGroupEnvByPrefix(env);
|
|
70
|
+
// Output:
|
|
71
|
+
// {
|
|
72
|
+
// database: { host: 'localhost' },
|
|
73
|
+
// api: { key: 'secret' },
|
|
74
|
+
// host: 'localhost', // ← Stays top-level
|
|
75
|
+
// port: 3000 // ← Stays top-level
|
|
76
|
+
// }
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## SvelteKit Integration
|
|
80
|
+
|
|
81
|
+
### Public Environment Variables
|
|
82
|
+
|
|
83
|
+
```javascript
|
|
84
|
+
// .env file:
|
|
85
|
+
// PUBLIC_API_URL=https://api.example.com
|
|
86
|
+
// PUBLIC_API_TIMEOUT=5000
|
|
87
|
+
// PUBLIC_FEATURE_FLAGS=true
|
|
88
|
+
|
|
89
|
+
import { getPublicEnv } from '$lib/util/sveltekit/env/public.js';
|
|
90
|
+
|
|
91
|
+
// ✅ Safe to use on client and server
|
|
92
|
+
const config = getPublicEnv();
|
|
93
|
+
console.log(config);
|
|
94
|
+
// Output:
|
|
95
|
+
// {
|
|
96
|
+
// public: {
|
|
97
|
+
// apiUrl: 'https://api.example.com',
|
|
98
|
+
// apiTimeout: 5000,
|
|
99
|
+
// featureFlags: true
|
|
100
|
+
// }
|
|
101
|
+
// }
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Private Environment Variables
|
|
105
|
+
|
|
106
|
+
```javascript
|
|
107
|
+
// .env file:
|
|
108
|
+
// DATABASE_HOST=localhost
|
|
109
|
+
// DATABASE_PORT=5432
|
|
110
|
+
// JWT_SECRET=super-secret-key
|
|
111
|
+
// REDIS_URL=redis://localhost:6379
|
|
112
|
+
|
|
113
|
+
import { getPrivateEnv } from '$lib/util/sveltekit/env/private.js';
|
|
114
|
+
|
|
115
|
+
// ⚠️ Server-side only!
|
|
116
|
+
const config = getPrivateEnv();
|
|
117
|
+
console.log(config);
|
|
118
|
+
// Output:
|
|
119
|
+
// {
|
|
120
|
+
// database: {
|
|
121
|
+
// host: 'localhost',
|
|
122
|
+
// port: 5432
|
|
123
|
+
// },
|
|
124
|
+
// jwt: {
|
|
125
|
+
// secret: 'super-secret-key'
|
|
126
|
+
// },
|
|
127
|
+
// redis: {
|
|
128
|
+
// url: 'redis://localhost:6379'
|
|
129
|
+
// }
|
|
130
|
+
// }
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### Combined Environment (Public + Private)
|
|
134
|
+
|
|
135
|
+
```javascript
|
|
136
|
+
// .env file:
|
|
137
|
+
// PUBLIC_API_URL=https://api.example.com
|
|
138
|
+
// DATABASE_HOST=localhost
|
|
139
|
+
// DATABASE_PORT=5432
|
|
140
|
+
|
|
141
|
+
import { getAllEnv } from '$lib/util/sveltekit/env/all.js';
|
|
142
|
+
|
|
143
|
+
// ⚠️ Server-side only! (contains private vars)
|
|
144
|
+
const config = getAllEnv();
|
|
145
|
+
console.log(config);
|
|
146
|
+
// Output:
|
|
147
|
+
// {
|
|
148
|
+
// database: {
|
|
149
|
+
// host: 'localhost',
|
|
150
|
+
// port: 5432
|
|
151
|
+
// },
|
|
152
|
+
// public: {
|
|
153
|
+
// apiUrl: 'https://api.example.com'
|
|
154
|
+
// }
|
|
155
|
+
// }
|
|
156
|
+
|
|
157
|
+
// Private variables override public ones with same name
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## Prefix-Specific Access
|
|
161
|
+
|
|
162
|
+
### Get Variables by Specific Prefix
|
|
163
|
+
|
|
164
|
+
```javascript
|
|
165
|
+
import { getPrivateEnvByPrefix } from '$lib/util/sveltekit/env/private.js';
|
|
166
|
+
|
|
167
|
+
// Only get DATABASE_* variables
|
|
168
|
+
const dbConfig = getPrivateEnvByPrefix('DATABASE');
|
|
169
|
+
console.log(dbConfig);
|
|
170
|
+
// Output:
|
|
171
|
+
// {
|
|
172
|
+
// host: 'localhost', // DATABASE_HOST (prefix removed)
|
|
173
|
+
// port: 5432, // DATABASE_PORT (prefix removed)
|
|
174
|
+
// name: 'myapp' // DATABASE_NAME (prefix removed)
|
|
175
|
+
// }
|
|
176
|
+
|
|
177
|
+
// Works with or without trailing underscore
|
|
178
|
+
const dbConfig2 = getPrivateEnvByPrefix('DATABASE_'); // Same result
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## Type Parsing
|
|
182
|
+
|
|
183
|
+
### Automatic Value Conversion
|
|
184
|
+
|
|
185
|
+
```javascript
|
|
186
|
+
const env = {
|
|
187
|
+
'SERVER_PORT': '3000', // → 3000 (number)
|
|
188
|
+
'ENABLE_LOGGING': 'true', // → true (boolean)
|
|
189
|
+
'DISABLE_CACHE': 'false', // → false (boolean)
|
|
190
|
+
'NULL_VALUE': 'null', // → null
|
|
191
|
+
'UNDEFINED_VALUE': 'undefined', // → undefined
|
|
192
|
+
'API_URL': 'https://api.com', // → 'https://api.com' (string)
|
|
193
|
+
'MIXED_VALUE': '123abc' // → '123abc' (string, can't parse as number)
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
const config = autoGroupEnvByPrefix(env);
|
|
197
|
+
// All values are parsed to appropriate types automatically
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### Disable Type Parsing
|
|
201
|
+
|
|
202
|
+
```javascript
|
|
203
|
+
const config = autoGroupEnvByPrefix(env, {
|
|
204
|
+
parseValues: false
|
|
205
|
+
});
|
|
206
|
+
// All values remain as strings
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
## Naming Conventions
|
|
210
|
+
|
|
211
|
+
### CamelCase Conversion (Default)
|
|
212
|
+
|
|
213
|
+
```javascript
|
|
214
|
+
const env = {
|
|
215
|
+
'DATABASE_HOST': 'localhost',
|
|
216
|
+
'API_BASE_URL': 'https://api.com',
|
|
217
|
+
'JWT_EXPIRES_IN': '24h'
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
const config = autoGroupEnvByPrefix(env, { camelCase: true }); // default
|
|
221
|
+
// Output:
|
|
222
|
+
// {
|
|
223
|
+
// database: { host: 'localhost' },
|
|
224
|
+
// api: { baseUrl: 'https://api.com' },
|
|
225
|
+
// jwt: { expiresIn: '24h' }
|
|
226
|
+
// }
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
### Keep Original Case
|
|
230
|
+
|
|
231
|
+
```javascript
|
|
232
|
+
const config = autoGroupEnvByPrefix(env, { camelCase: false });
|
|
233
|
+
// Output:
|
|
234
|
+
// {
|
|
235
|
+
// database: { host: 'localhost' },
|
|
236
|
+
// api: { base_url: 'https://api.com' },
|
|
237
|
+
// jwt: { expires_in: '24h' }
|
|
238
|
+
// }
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
## Real-World Usage Examples
|
|
242
|
+
|
|
243
|
+
### Database Configuration
|
|
244
|
+
|
|
245
|
+
```javascript
|
|
246
|
+
// .env
|
|
247
|
+
// DATABASE_HOST=localhost
|
|
248
|
+
// DATABASE_PORT=5432
|
|
249
|
+
// DATABASE_NAME=myapp
|
|
250
|
+
// DATABASE_USER=admin
|
|
251
|
+
// DATABASE_PASSWORD=secret
|
|
252
|
+
// DATABASE_SSL=true
|
|
253
|
+
|
|
254
|
+
import { getPrivateEnvByPrefix } from '$lib/util/sveltekit/env/private.js';
|
|
255
|
+
|
|
256
|
+
const dbConfig = getPrivateEnvByPrefix('DATABASE');
|
|
257
|
+
console.log(dbConfig);
|
|
258
|
+
// Output:
|
|
259
|
+
// {
|
|
260
|
+
// host: 'localhost',
|
|
261
|
+
// port: 5432,
|
|
262
|
+
// name: 'myapp',
|
|
263
|
+
// user: 'admin',
|
|
264
|
+
// password: 'secret',
|
|
265
|
+
// ssl: true
|
|
266
|
+
// }
|
|
267
|
+
|
|
268
|
+
// Use directly with database client
|
|
269
|
+
import pg from 'pg';
|
|
270
|
+
const client = new pg.Client(dbConfig);
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
### API Configuration
|
|
274
|
+
|
|
275
|
+
```javascript
|
|
276
|
+
// .env
|
|
277
|
+
// PUBLIC_API_URL=https://api.example.com
|
|
278
|
+
// PUBLIC_API_TIMEOUT=5000
|
|
279
|
+
// PUBLIC_API_RETRIES=3
|
|
280
|
+
// API_SECRET=private-key
|
|
281
|
+
|
|
282
|
+
import { getAllEnv } from '$lib/util/sveltekit/env/all.js';
|
|
283
|
+
|
|
284
|
+
const config = getAllEnv();
|
|
285
|
+
const apiConfig = {
|
|
286
|
+
...config.public, // Public API settings
|
|
287
|
+
secret: config.api.secret // Private API key
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
console.log(apiConfig);
|
|
291
|
+
// Output:
|
|
292
|
+
// {
|
|
293
|
+
// url: 'https://api.example.com',
|
|
294
|
+
// timeout: 5000,
|
|
295
|
+
// retries: 3,
|
|
296
|
+
// secret: 'private-key'
|
|
297
|
+
// }
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
### Feature Flags
|
|
301
|
+
|
|
302
|
+
```javascript
|
|
303
|
+
// .env
|
|
304
|
+
// PUBLIC_FEATURE_NEW_UI=true
|
|
305
|
+
// PUBLIC_FEATURE_BETA_DASHBOARD=false
|
|
306
|
+
// PUBLIC_FEATURE_ANALYTICS=true
|
|
307
|
+
|
|
308
|
+
import { getPublicEnvByPrefix } from '$lib/util/sveltekit/env/public.js';
|
|
309
|
+
|
|
310
|
+
const features = getPublicEnvByPrefix('PUBLIC_FEATURE');
|
|
311
|
+
console.log(features);
|
|
312
|
+
// Output:
|
|
313
|
+
// {
|
|
314
|
+
// newUi: true,
|
|
315
|
+
// betaDashboard: false,
|
|
316
|
+
// analytics: true
|
|
317
|
+
// }
|
|
318
|
+
|
|
319
|
+
// Use in components
|
|
320
|
+
{#if features.newUi}
|
|
321
|
+
<NewUIComponent />
|
|
322
|
+
{:else}
|
|
323
|
+
<LegacyUIComponent />
|
|
324
|
+
{/if}
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
## API Reference
|
|
328
|
+
|
|
329
|
+
### Core Functions
|
|
330
|
+
|
|
331
|
+
- `autoGroupEnvByPrefix(env, options)` - Groups environment variables by prefix
|
|
332
|
+
- `parseEnv(env, options)` - Parses environment variables with options
|
|
333
|
+
- `parseEnvByPrefix(env, prefix, options)` - Parses variables with specific prefix
|
|
334
|
+
|
|
335
|
+
### SvelteKit Functions
|
|
336
|
+
|
|
337
|
+
- `getPublicEnv(options)` - Get public environment variables (client + server safe)
|
|
338
|
+
- `getPrivateEnv(options)` - Get private environment variables (server only)
|
|
339
|
+
- `getAllEnv(options)` - Get combined environment variables (server only)
|
|
340
|
+
- `getPublicEnvByPrefix(prefix, options)` - Get public variables by prefix
|
|
341
|
+
- `getPrivateEnvByPrefix(prefix, options)` - Get private variables by prefix
|
|
342
|
+
- `getAllEnvByPrefix(prefix, options)` - Get combined variables by prefix
|
|
343
|
+
|
|
344
|
+
### Raw Access
|
|
345
|
+
|
|
346
|
+
- `getRawPublicEnv()` - Get raw public environment object
|
|
347
|
+
- `getRawPrivateEnv()` - Get raw private environment object
|
|
348
|
+
- `getRawAllEnv()` - Get raw combined environment object
|
|
349
|
+
|
|
350
|
+
### Options
|
|
351
|
+
|
|
352
|
+
All functions accept these options:
|
|
353
|
+
|
|
354
|
+
```javascript
|
|
355
|
+
{
|
|
356
|
+
camelCase: true, // Convert SNAKE_CASE to camelCase
|
|
357
|
+
parseValues: true, // Parse strings to numbers/booleans/null
|
|
358
|
+
autoGroup: true // Enable automatic prefix grouping (SvelteKit functions only)
|
|
359
|
+
}
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
## Security Notes
|
|
363
|
+
|
|
364
|
+
- **Public variables** (`PUBLIC_*`) are safe to use on client and server
|
|
365
|
+
- **Private variables** are server-side only and will cause build errors if imported on client
|
|
366
|
+
- **Combined functions** (`getAllEnv*`) are server-side only
|
|
367
|
+
- Private variables take precedence over public ones when names conflict
|
|
368
|
+
|
|
369
|
+
## Migration from Other Solutions
|
|
370
|
+
|
|
371
|
+
### From Manual Environment Access
|
|
372
|
+
|
|
373
|
+
```javascript
|
|
374
|
+
// Before ❌
|
|
375
|
+
const dbHost = process.env.DATABASE_HOST;
|
|
376
|
+
const dbPort = parseInt(process.env.DATABASE_PORT);
|
|
377
|
+
const dbSsl = process.env.DATABASE_SSL === 'true';
|
|
378
|
+
|
|
379
|
+
// After ✅
|
|
380
|
+
const { database } = getPrivateEnv();
|
|
381
|
+
const { host, port, ssl } = database;
|
|
382
|
+
```
|
|
383
|
+
|
|
384
|
+
### From dotenv Libraries
|
|
385
|
+
|
|
386
|
+
```javascript
|
|
387
|
+
// Before ❌
|
|
388
|
+
import dotenv from 'dotenv';
|
|
389
|
+
dotenv.config();
|
|
390
|
+
|
|
391
|
+
const config = {
|
|
392
|
+
database: {
|
|
393
|
+
host: process.env.DATABASE_HOST,
|
|
394
|
+
port: parseInt(process.env.DATABASE_PORT)
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
// After ✅
|
|
399
|
+
import { getPrivateEnv } from '$lib/util/sveltekit/env/private.js';
|
|
400
|
+
const { database } = getPrivateEnv();
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
## Best Practices
|
|
404
|
+
|
|
405
|
+
1. **Use descriptive prefixes**: `DATABASE_`, `REDIS_`, `JWT_`, `API_`
|
|
406
|
+
2. **Group related settings**: Put all database settings under `DATABASE_*`
|
|
407
|
+
3. **Use appropriate access level**: Public for client-safe config, private for secrets
|
|
408
|
+
4. **Leverage type parsing**: Use `'true'/'false'` for booleans, numbers as strings
|
|
409
|
+
5. **Consistent naming**: Stick to `UPPER_SNAKE_CASE` for environment variables
|
|
410
|
+
6. **Document your variables**: Comment your `.env` files with examples
|
|
411
|
+
|
|
412
|
+
## TypeScript Support
|
|
413
|
+
|
|
414
|
+
All functions include full TypeScript definitions. For better type safety, consider defining interfaces for your configuration:
|
|
415
|
+
|
|
416
|
+
```typescript
|
|
417
|
+
interface DatabaseConfig {
|
|
418
|
+
host: string;
|
|
419
|
+
port: number;
|
|
420
|
+
ssl: boolean;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
const { database }: { database: DatabaseConfig } = getPrivateEnv();
|
|
424
|
+
```
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get all environment variables (public + private) with automatic grouping
|
|
3
|
+
*
|
|
4
|
+
* Combines and automatically groups both public and private environment
|
|
5
|
+
* variables. Private variables take precedence over public ones when
|
|
6
|
+
* there are conflicts.
|
|
7
|
+
*
|
|
8
|
+
* @param {Object} [options={}] - Parsing options
|
|
9
|
+
* @param {boolean} [options.camelCase=true]
|
|
10
|
+
* Convert env var names to camelCase object keys
|
|
11
|
+
* @param {boolean} [options.parseValues=true]
|
|
12
|
+
* Parse string values to numbers/booleans when possible
|
|
13
|
+
* @param {boolean} [options.autoGroup=true]
|
|
14
|
+
* Enable automatic prefix grouping
|
|
15
|
+
*
|
|
16
|
+
* @returns {Object} Grouped and parsed combined environment variables
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* // Environment variables:
|
|
20
|
+
* // PUBLIC_API_URL=https://api.example.com
|
|
21
|
+
* // DATABASE_HOST=localhost
|
|
22
|
+
* // DATABASE_PORT=5432
|
|
23
|
+
* // SINGLE_FLAG=true
|
|
24
|
+
*
|
|
25
|
+
* const config = getAllEnv();
|
|
26
|
+
* // Returns:
|
|
27
|
+
* // {
|
|
28
|
+
* // database: { host: 'localhost', port: 5432 },
|
|
29
|
+
* // publicApiUrl: 'https://api.example.com',
|
|
30
|
+
* // singleFlag: true
|
|
31
|
+
* // }
|
|
32
|
+
*/
|
|
33
|
+
export function getAllEnv(options?: {
|
|
34
|
+
camelCase?: boolean;
|
|
35
|
+
parseValues?: boolean;
|
|
36
|
+
autoGroup?: boolean;
|
|
37
|
+
}): any;
|
|
38
|
+
/**
|
|
39
|
+
* Get combined environment variables by prefix
|
|
40
|
+
*
|
|
41
|
+
* @param {string} prefix - Environment variable prefix (e.g., 'DATABASE')
|
|
42
|
+
* @param {Object} [options={}] - Parsing options
|
|
43
|
+
*
|
|
44
|
+
* @returns {Object} Parsed configuration object
|
|
45
|
+
*/
|
|
46
|
+
export function getAllEnvByPrefix(prefix: string, options?: any): any;
|
|
47
|
+
/**
|
|
48
|
+
* Get raw combined environment variables (no parsing)
|
|
49
|
+
*
|
|
50
|
+
* @returns {Object<string, string>} Raw combined environment variables
|
|
51
|
+
*/
|
|
52
|
+
export function getRawAllEnv(): {
|
|
53
|
+
[x: string]: string;
|
|
54
|
+
};
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Combined environment variables access (server-side only)
|
|
3
|
+
*
|
|
4
|
+
* Provides access to both public and private SvelteKit environment variables.
|
|
5
|
+
* IMPORTANT: This module can only be imported on the server side since it
|
|
6
|
+
* imports private environment variables.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* import { getAllEnv } from './all.js';
|
|
10
|
+
*
|
|
11
|
+
* const allVars = getAllEnv();
|
|
12
|
+
* const dbConfig = getAllEnv({ prefix: 'DATABASE' });
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { getPublicEnv, getRawPublicEnv } from './public.js';
|
|
16
|
+
import { getPrivateEnv, getRawPrivateEnv } from './private.js';
|
|
17
|
+
import { autoGroupEnvByPrefix, parseEnv } from './parsers.js';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Get all environment variables (public + private) with automatic grouping
|
|
21
|
+
*
|
|
22
|
+
* Combines and automatically groups both public and private environment
|
|
23
|
+
* variables. Private variables take precedence over public ones when
|
|
24
|
+
* there are conflicts.
|
|
25
|
+
*
|
|
26
|
+
* @param {Object} [options={}] - Parsing options
|
|
27
|
+
* @param {boolean} [options.camelCase=true]
|
|
28
|
+
* Convert env var names to camelCase object keys
|
|
29
|
+
* @param {boolean} [options.parseValues=true]
|
|
30
|
+
* Parse string values to numbers/booleans when possible
|
|
31
|
+
* @param {boolean} [options.autoGroup=true]
|
|
32
|
+
* Enable automatic prefix grouping
|
|
33
|
+
*
|
|
34
|
+
* @returns {Object} Grouped and parsed combined environment variables
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* // Environment variables:
|
|
38
|
+
* // PUBLIC_API_URL=https://api.example.com
|
|
39
|
+
* // DATABASE_HOST=localhost
|
|
40
|
+
* // DATABASE_PORT=5432
|
|
41
|
+
* // SINGLE_FLAG=true
|
|
42
|
+
*
|
|
43
|
+
* const config = getAllEnv();
|
|
44
|
+
* // Returns:
|
|
45
|
+
* // {
|
|
46
|
+
* // database: { host: 'localhost', port: 5432 },
|
|
47
|
+
* // publicApiUrl: 'https://api.example.com',
|
|
48
|
+
* // singleFlag: true
|
|
49
|
+
* // }
|
|
50
|
+
*/
|
|
51
|
+
export function getAllEnv(options = {}) {
|
|
52
|
+
const { autoGroup = true, ...parseOptions } = options;
|
|
53
|
+
|
|
54
|
+
if (autoGroup) {
|
|
55
|
+
// Combine raw env vars first, then group
|
|
56
|
+
const combinedRawEnv = { ...getRawPublicEnv(), ...getRawPrivateEnv() };
|
|
57
|
+
return autoGroupEnvByPrefix(combinedRawEnv, parseOptions);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// No grouping - just combine parsed results
|
|
61
|
+
const publicVars = getPublicEnv({ ...parseOptions, autoGroup: false });
|
|
62
|
+
const privateVars = getPrivateEnv({ ...parseOptions, autoGroup: false });
|
|
63
|
+
|
|
64
|
+
// Private variables take precedence over public ones
|
|
65
|
+
return { ...publicVars, ...privateVars };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Get combined environment variables by prefix
|
|
70
|
+
*
|
|
71
|
+
* @param {string} prefix - Environment variable prefix (e.g., 'DATABASE')
|
|
72
|
+
* @param {Object} [options={}] - Parsing options
|
|
73
|
+
*
|
|
74
|
+
* @returns {Object} Parsed configuration object
|
|
75
|
+
*/
|
|
76
|
+
export function getAllEnvByPrefix(prefix, options = {}) {
|
|
77
|
+
const prefixWithUnderscore = prefix.endsWith('_') ? prefix : `${prefix}_`;
|
|
78
|
+
|
|
79
|
+
return getAllEnv({
|
|
80
|
+
...options,
|
|
81
|
+
prefix: prefixWithUnderscore,
|
|
82
|
+
removePrefix: true
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Get raw combined environment variables (no parsing)
|
|
88
|
+
*
|
|
89
|
+
* @returns {Object<string, string>} Raw combined environment variables
|
|
90
|
+
*/
|
|
91
|
+
export function getRawAllEnv() {
|
|
92
|
+
const publicVars = getRawPublicEnv();
|
|
93
|
+
const privateVars = getRawPrivateEnv();
|
|
94
|
+
|
|
95
|
+
// Private variables take precedence over public ones
|
|
96
|
+
return { ...publicVars, ...privateVars };
|
|
97
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Environment variable parsing utilities
|
|
3
|
+
*
|
|
4
|
+
* Provides functions for parsing and transforming environment variables
|
|
5
|
+
* with type conversion, key transformation, and prefix handling.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* import { parseEnv, parseEnvByPrefix } from './parsers.js';
|
|
9
|
+
*
|
|
10
|
+
* const parsed = parseEnv(envObject, {
|
|
11
|
+
* camelCase: true,
|
|
12
|
+
* parseValues: true
|
|
13
|
+
* });
|
|
14
|
+
*
|
|
15
|
+
* const dbConfig = parseEnvByPrefix(envObject, 'DATABASE');
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* Parse environment object with type conversion and key transformation
|
|
19
|
+
*
|
|
20
|
+
* @param {Object<string, string>} env - Raw environment variables
|
|
21
|
+
* @param {Object} [options={}] - Parsing options
|
|
22
|
+
* @param {boolean} [options.camelCase=true]
|
|
23
|
+
* Convert env var names to camelCase object keys
|
|
24
|
+
* @param {boolean} [options.parseValues=true]
|
|
25
|
+
* Parse string values to numbers/booleans when possible
|
|
26
|
+
* @param {string} [options.prefix] - Only include vars with this prefix
|
|
27
|
+
* @param {boolean} [options.removePrefix=true]
|
|
28
|
+
* Remove prefix from resulting keys
|
|
29
|
+
*
|
|
30
|
+
* @returns {Object} Parsed environment object
|
|
31
|
+
*/
|
|
32
|
+
export function parseEnv(env: {
|
|
33
|
+
[x: string]: string;
|
|
34
|
+
}, options?: {
|
|
35
|
+
camelCase?: boolean;
|
|
36
|
+
parseValues?: boolean;
|
|
37
|
+
prefix?: string;
|
|
38
|
+
removePrefix?: boolean;
|
|
39
|
+
}): any;
|
|
40
|
+
/**
|
|
41
|
+
* Parse environment variables by prefix
|
|
42
|
+
*
|
|
43
|
+
* @param {Object<string, string>} env - Raw environment variables
|
|
44
|
+
* @param {string} prefix - Environment variable prefix (e.g., 'DATABASE')
|
|
45
|
+
* @param {Object} [options={}] - Parsing options
|
|
46
|
+
*
|
|
47
|
+
* @returns {Object} Parsed configuration object
|
|
48
|
+
*/
|
|
49
|
+
export function parseEnvByPrefix(env: {
|
|
50
|
+
[x: string]: string;
|
|
51
|
+
}, prefix: string, options?: any): any;
|
|
52
|
+
/**
|
|
53
|
+
* Convert SCREAMING_SNAKE_CASE to camelCase
|
|
54
|
+
*
|
|
55
|
+
* @param {string} str - String to convert
|
|
56
|
+
*
|
|
57
|
+
* @returns {string} camelCase string
|
|
58
|
+
*/
|
|
59
|
+
export function toCamelCase(str: string): string;
|
|
60
|
+
/**
|
|
61
|
+
* Parse string value to appropriate type
|
|
62
|
+
*
|
|
63
|
+
* @param {string} value - String value to parse
|
|
64
|
+
*
|
|
65
|
+
* @returns {*} Parsed value (string, number, boolean, or null)
|
|
66
|
+
*/
|
|
67
|
+
export function parseValue(value: string): any;
|
|
68
|
+
/**
|
|
69
|
+
* Auto-detect and group environment variables by prefixes
|
|
70
|
+
*
|
|
71
|
+
* Automatically detects prefixes from environment variable names and groups
|
|
72
|
+
* them into configuration objects. All variables with underscores are
|
|
73
|
+
* grouped by their prefix (the part before the first underscore).
|
|
74
|
+
*
|
|
75
|
+
* @param {Object<string, string>} env - Raw environment variables
|
|
76
|
+
* @param {Object} [options={}] - Parsing options
|
|
77
|
+
* @param {boolean} [options.camelCase=true]
|
|
78
|
+
* Convert env var names to camelCase object keys
|
|
79
|
+
* @param {boolean} [options.parseValues=true]
|
|
80
|
+
* Parse string values to numbers/booleans when possible
|
|
81
|
+
*
|
|
82
|
+
* @returns {Object<string, Object>} Grouped environment variables
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* // Input env vars:
|
|
86
|
+
* // DATABASE_HOST=localhost, DATABASE_PORT=5432
|
|
87
|
+
* // REDIS_HOST=cache, API_KEY=secret, SINGLE=value
|
|
88
|
+
*
|
|
89
|
+
* const grouped = autoGroupEnvByPrefix(env);
|
|
90
|
+
* // Returns:
|
|
91
|
+
* // {
|
|
92
|
+
* // database: { host: 'localhost', port: 5432 },
|
|
93
|
+
* // redis: { host: 'cache' },
|
|
94
|
+
* // api: { key: 'secret' },
|
|
95
|
+
* // single: 'value' // No underscore, stays top-level
|
|
96
|
+
* // }
|
|
97
|
+
*/
|
|
98
|
+
export function autoGroupEnvByPrefix(env: {
|
|
99
|
+
[x: string]: string;
|
|
100
|
+
}, options?: {
|
|
101
|
+
camelCase?: boolean;
|
|
102
|
+
parseValues?: boolean;
|
|
103
|
+
}): {
|
|
104
|
+
[x: string]: any;
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* Group environment variables by specific prefixes
|
|
108
|
+
*
|
|
109
|
+
* @param {Object<string, string>} env - Raw environment variables
|
|
110
|
+
* @param {string[]} prefixes - Array of prefixes to group by
|
|
111
|
+
* @param {Object} [options={}] - Parsing options
|
|
112
|
+
*
|
|
113
|
+
* @returns {Object<string, Object>} Grouped environment variables
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* const grouped = groupEnvByPrefixes(env, ['DATABASE', 'REDIS', 'JWT']);
|
|
117
|
+
* // Returns: { database: {...}, redis: {...}, jwt: {...} }
|
|
118
|
+
*/
|
|
119
|
+
export function groupEnvByPrefixes(env: {
|
|
120
|
+
[x: string]: string;
|
|
121
|
+
}, prefixes: string[], options?: any): {
|
|
122
|
+
[x: string]: any;
|
|
123
|
+
};
|
|
124
|
+
/**
|
|
125
|
+
* Filter environment variables by pattern
|
|
126
|
+
*
|
|
127
|
+
* @param {Object<string, string>} env - Raw environment variables
|
|
128
|
+
* @param {RegExp|string} pattern - Pattern to match against keys
|
|
129
|
+
* @param {Object} [options={}] - Parsing options
|
|
130
|
+
*
|
|
131
|
+
* @returns {Object} Filtered and parsed environment variables
|
|
132
|
+
*/
|
|
133
|
+
export function filterEnvByPattern(env: {
|
|
134
|
+
[x: string]: string;
|
|
135
|
+
}, pattern: RegExp | string, options?: any): any;
|