node-env-resolver 0.0.0 → 1.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.
- package/README.md +221 -85
- package/dist/audit.d.ts +16 -0
- package/dist/audit.d.ts.map +1 -0
- package/dist/audit.js +1 -0
- package/dist/audit.test.d.ts +2 -0
- package/dist/audit.test.d.ts.map +1 -0
- package/dist/builder.d.ts +146 -0
- package/dist/builder.d.ts.map +1 -0
- package/dist/builder.js +1 -0
- package/dist/composition.test.d.ts +7 -0
- package/dist/composition.test.d.ts.map +1 -0
- package/dist/index.d.ts +99 -4
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -20
- package/dist/index.test.d.ts +2 -0
- package/dist/index.test.d.ts.map +1 -0
- package/dist/providers.d.ts +11 -0
- package/dist/providers.d.ts.map +1 -0
- package/dist/resolver.d.ts +17 -0
- package/dist/resolver.d.ts.map +1 -0
- package/dist/resolver.js +5 -0
- package/dist/resolvers.d.ts +11 -0
- package/dist/resolvers.d.ts.map +1 -0
- package/dist/resolvers.js +3 -0
- package/dist/standard-schema.d.ts +32 -0
- package/dist/standard-schema.d.ts.map +1 -0
- package/dist/standard-schema.js +3 -0
- package/dist/type-override.test.d.ts +2 -0
- package/dist/type-override.test.d.ts.map +1 -0
- package/dist/types.d.ts +66 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +0 -0
- package/dist/utils.d.ts +41 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +1 -0
- package/dist/utils.test.d.ts +2 -0
- package/dist/utils.test.d.ts.map +1 -0
- package/dist/validators.d.ts +68 -0
- package/dist/validators.d.ts.map +1 -0
- package/dist/validators.js +1 -0
- package/dist/web.d.ts +51 -0
- package/dist/web.d.ts.map +1 -0
- package/dist/web.js +6 -0
- package/dist/zod.d.ts +51 -0
- package/dist/zod.d.ts.map +1 -0
- package/dist/zod.js +1 -0
- package/package.json +88 -28
- package/dist/chunk-AST3HVOG.js +0 -148
- package/dist/chunk-UQYMDWOU.js +0 -13
- package/dist/chunk-XC5PWSNR.js +0 -37
- package/dist/esm/chunk-AST3HVOG.js +0 -144
- package/dist/esm/chunk-UQYMDWOU.js +0 -11
- package/dist/esm/chunk-XC5PWSNR.js +0 -35
- package/dist/esm/index.js +0 -3
- package/dist/esm/resolve.js +0 -2
- package/dist/esm/resolveZod.js +0 -2
- package/dist/index-6c4171f8.d.ts +0 -27
- package/dist/resolve.d.ts +0 -6
- package/dist/resolve.js +0 -15
- package/dist/resolveZod.d.ts +0 -7
- package/dist/resolveZod.js +0 -15
package/README.md
CHANGED
|
@@ -1,148 +1,284 @@
|
|
|
1
1
|
# node-env-resolver
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Type-safe environment variable handling with zero dependencies.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
[](https://www.npmjs.com/package/node-env-resolver)
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install node-env-resolver
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick start
|
|
6
14
|
|
|
7
15
|
```ts
|
|
8
16
|
import { resolve } from 'node-env-resolver';
|
|
9
17
|
|
|
10
|
-
|
|
11
|
-
const resolvedEnvs = await resolve([
|
|
12
|
-
'NODE_ENV',
|
|
13
|
-
'PORT',
|
|
14
|
-
'DATABASE_URL',
|
|
15
|
-
'REDIS_URL',
|
|
16
|
-
]);
|
|
17
|
-
|
|
18
|
-
// using object with default values
|
|
19
|
-
const resolvedEnvs = await resolve({
|
|
20
|
-
NODE_ENV: 'local',
|
|
18
|
+
const config = await resolve({
|
|
21
19
|
PORT: 3000,
|
|
22
|
-
DATABASE_URL: '
|
|
23
|
-
|
|
20
|
+
DATABASE_URL: 'url',
|
|
21
|
+
DEBUG: false,
|
|
22
|
+
API_KEY: 'string?'
|
|
24
23
|
});
|
|
24
|
+
|
|
25
|
+
// All typed correctly
|
|
26
|
+
config.PORT; // number
|
|
27
|
+
config.DATABASE_URL; // URL object
|
|
28
|
+
config.DEBUG; // boolean
|
|
29
|
+
config.API_KEY; // string | undefined
|
|
25
30
|
```
|
|
26
31
|
|
|
27
|
-
|
|
32
|
+
## Features
|
|
28
33
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
```
|
|
34
|
+
- Zero runtime dependencies
|
|
35
|
+
- Full TypeScript type inference
|
|
36
|
+
- Custom validation functions
|
|
37
|
+
- Standard Schema compliant
|
|
38
|
+
- Works with any Node.js application
|
|
39
|
+
|
|
40
|
+
## Built-in validators
|
|
37
41
|
|
|
38
|
-
The library
|
|
42
|
+
The library includes validators for common use cases:
|
|
43
|
+
|
|
44
|
+
- **Basic**: `string`, `number`, `boolean`, `json`, `port`
|
|
45
|
+
- **Network**: `url`, `http`, `https`, `email`
|
|
46
|
+
- **Database**: `postgres`, `mysql`, `mongodb`, `redis`
|
|
47
|
+
|
|
48
|
+
## Usage
|
|
49
|
+
|
|
50
|
+
### Simple schema
|
|
39
51
|
|
|
40
52
|
```ts
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
);
|
|
53
|
+
import { resolve } from 'node-env-resolver';
|
|
54
|
+
|
|
55
|
+
const config = await resolve({
|
|
56
|
+
PORT: 3000,
|
|
57
|
+
NODE_ENV: ['development', 'production'] as const,
|
|
58
|
+
DATABASE_URL: 'postgres',
|
|
59
|
+
DEBUG: false
|
|
60
|
+
});
|
|
49
61
|
```
|
|
50
62
|
|
|
51
|
-
|
|
63
|
+
### Optional values
|
|
52
64
|
|
|
53
|
-
|
|
54
|
-
|
|
65
|
+
Add `?` to make a value optional:
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
const config = await resolve({
|
|
69
|
+
API_KEY: 'string?',
|
|
70
|
+
REDIS_URL: 'url?'
|
|
71
|
+
});
|
|
55
72
|
```
|
|
56
73
|
|
|
57
|
-
|
|
74
|
+
### Custom validators
|
|
58
75
|
|
|
59
|
-
|
|
76
|
+
Write your own validation functions:
|
|
60
77
|
|
|
61
|
-
|
|
78
|
+
```ts
|
|
79
|
+
const portValidator = (value: string): number => {
|
|
80
|
+
const port = parseInt(value, 10);
|
|
81
|
+
if (isNaN(port) || port < 1 || port > 65535) {
|
|
82
|
+
throw new Error('Port must be between 1 and 65535');
|
|
83
|
+
}
|
|
84
|
+
return port;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const config = await resolve({
|
|
88
|
+
CUSTOM_PORT: portValidator,
|
|
89
|
+
DATABASE_URL: 'postgres'
|
|
90
|
+
});
|
|
91
|
+
```
|
|
62
92
|
|
|
63
|
-
|
|
93
|
+
### Multiple sources
|
|
94
|
+
|
|
95
|
+
Load from different sources with explicit mapping:
|
|
64
96
|
|
|
65
97
|
```ts
|
|
66
|
-
|
|
98
|
+
import { resolve, processEnv, dotenv } from 'node-env-resolver';
|
|
99
|
+
|
|
100
|
+
const config = await resolve.with(
|
|
101
|
+
[processEnv(), {
|
|
102
|
+
PORT: 3000,
|
|
103
|
+
NODE_ENV: ['development', 'production'] as const,
|
|
104
|
+
}],
|
|
105
|
+
[dotenv(), {
|
|
106
|
+
DATABASE_URL: 'postgres',
|
|
107
|
+
API_KEY: 'string',
|
|
108
|
+
}]
|
|
109
|
+
);
|
|
67
110
|
```
|
|
68
111
|
|
|
69
|
-
|
|
112
|
+
Later sources override earlier ones if there are conflicts.
|
|
70
113
|
|
|
71
|
-
|
|
114
|
+
### With caching
|
|
72
115
|
|
|
73
|
-
|
|
116
|
+
Add TTL caching for remote sources:
|
|
74
117
|
|
|
75
|
-
|
|
118
|
+
```ts
|
|
119
|
+
import { resolve, cached, TTL } from 'node-env-resolver';
|
|
76
120
|
|
|
77
|
-
|
|
121
|
+
const config = await resolve.with(
|
|
122
|
+
[processEnv(), { PORT: 3000 }],
|
|
123
|
+
[cached(customProvider, { ttl: TTL.minutes5 }), {
|
|
124
|
+
API_KEY: 'string',
|
|
125
|
+
}]
|
|
126
|
+
);
|
|
127
|
+
```
|
|
78
128
|
|
|
79
|
-
|
|
80
|
-
async function customResolver(): EnvValues {
|
|
81
|
-
return Promise.resolve({
|
|
82
|
-
FOO: 'bar',
|
|
83
|
-
QUZ: 'quux',
|
|
84
|
-
});
|
|
85
|
-
}
|
|
129
|
+
## API
|
|
86
130
|
|
|
87
|
-
|
|
131
|
+
### Shorthand syntax
|
|
132
|
+
|
|
133
|
+
```ts
|
|
134
|
+
'string' // Required string
|
|
135
|
+
'string?' // Optional string
|
|
136
|
+
'url' // URL (returns URL object)
|
|
137
|
+
'email' // Email address
|
|
138
|
+
'port' // Port number (1-65535)
|
|
139
|
+
'json' // JSON value
|
|
140
|
+
'postgres' // PostgreSQL connection string
|
|
141
|
+
'mysql' // MySQL connection string
|
|
142
|
+
'mongodb' // MongoDB connection string
|
|
143
|
+
'redis' // Redis connection string
|
|
144
|
+
|
|
145
|
+
// Defaults
|
|
146
|
+
3000 // number with default
|
|
147
|
+
false // boolean with default
|
|
148
|
+
['dev', 'prod'] as const // enum (requires 'as const')
|
|
88
149
|
```
|
|
89
150
|
|
|
90
|
-
|
|
151
|
+
### Object syntax
|
|
91
152
|
|
|
92
|
-
|
|
153
|
+
For advanced validation:
|
|
154
|
+
|
|
155
|
+
```ts
|
|
93
156
|
{
|
|
94
|
-
|
|
95
|
-
|
|
157
|
+
type: 'string',
|
|
158
|
+
pattern: '^sk_[a-zA-Z0-9]+$',
|
|
159
|
+
min: 32,
|
|
160
|
+
max: 64,
|
|
161
|
+
optional: true,
|
|
162
|
+
description: 'API key'
|
|
96
163
|
}
|
|
97
164
|
```
|
|
98
165
|
|
|
99
|
-
###
|
|
100
|
-
|
|
101
|
-
It also has in-built support for the resolving environment variables from aws-secrets-manager.
|
|
166
|
+
### Options
|
|
102
167
|
|
|
103
168
|
```ts
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
[
|
|
107
|
-
|
|
108
|
-
|
|
169
|
+
await resolve(schema, {
|
|
170
|
+
resolvers: [customResolver], // Custom resolvers
|
|
171
|
+
extend: [awsSecrets()], // Extend default resolvers
|
|
172
|
+
strict: true, // Strict mode
|
|
173
|
+
enableAudit: true, // Audit logging
|
|
174
|
+
policies: { // Security policies
|
|
175
|
+
enforceAllowedSources: {
|
|
176
|
+
DATABASE_URL: ['aws-secrets']
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
});
|
|
109
180
|
```
|
|
110
181
|
|
|
111
|
-
|
|
182
|
+
### Built-in resolvers
|
|
112
183
|
|
|
113
|
-
|
|
184
|
+
```ts
|
|
185
|
+
import { dotenv, processEnv } from 'node-env-resolver';
|
|
114
186
|
|
|
115
|
-
|
|
187
|
+
// Load from .env files
|
|
188
|
+
dotenv({ path: '.env', expand: true })
|
|
189
|
+
|
|
190
|
+
// Load from process.env
|
|
191
|
+
processEnv()
|
|
192
|
+
```
|
|
116
193
|
|
|
117
|
-
|
|
194
|
+
### Utility resolvers
|
|
118
195
|
|
|
119
196
|
```ts
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
197
|
+
import { cached, retry, TTL } from 'node-env-resolver';
|
|
198
|
+
|
|
199
|
+
// Cache with TTL
|
|
200
|
+
cached(resolver, { ttl: 300 })
|
|
201
|
+
|
|
202
|
+
// Retry on failure
|
|
203
|
+
retry(resolver, { attempts: 3 })
|
|
126
204
|
|
|
127
|
-
|
|
205
|
+
// TTL constants
|
|
206
|
+
TTL.minute // 60
|
|
207
|
+
TTL.minutes5 // 300
|
|
208
|
+
TTL.hour // 3600
|
|
209
|
+
TTL.day // 86400
|
|
128
210
|
```
|
|
129
211
|
|
|
130
|
-
|
|
212
|
+
## Examples
|
|
131
213
|
|
|
132
|
-
###
|
|
214
|
+
### Express app
|
|
133
215
|
|
|
134
|
-
|
|
216
|
+
```ts
|
|
217
|
+
import { resolve } from 'node-env-resolver';
|
|
218
|
+
|
|
219
|
+
const config = await resolve({
|
|
220
|
+
PORT: 3000,
|
|
221
|
+
NODE_ENV: ['development', 'production', 'test'] as const,
|
|
222
|
+
DATABASE_URL: 'postgres',
|
|
223
|
+
REDIS_URL: 'redis?',
|
|
224
|
+
JWT_SECRET: 'string',
|
|
225
|
+
JWT_EXPIRES_IN: '7d',
|
|
226
|
+
ENABLE_ANALYTICS: false
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
app.listen(config.PORT, () => {
|
|
230
|
+
console.log(`Server running on port ${config.PORT}`);
|
|
231
|
+
});
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
### AWS Lambda
|
|
135
235
|
|
|
136
236
|
```ts
|
|
137
|
-
{
|
|
138
|
-
|
|
237
|
+
import { resolve } from 'node-env-resolver';
|
|
238
|
+
import { awsSecrets } from '@node-env-resolver/aws';
|
|
239
|
+
|
|
240
|
+
const config = await resolve({
|
|
241
|
+
API_ENDPOINT: 'url',
|
|
242
|
+
TIMEOUT: 30,
|
|
243
|
+
LOG_LEVEL: ['debug', 'info', 'warn', 'error'] as const
|
|
244
|
+
}, {
|
|
245
|
+
extend: [awsSecrets({ secretId: 'lambda/secrets' })]
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
export const handler = async (event) => {
|
|
249
|
+
// Use config values
|
|
139
250
|
};
|
|
140
251
|
```
|
|
141
252
|
|
|
142
|
-
|
|
253
|
+
## Error messages
|
|
254
|
+
|
|
255
|
+
The library provides clear, actionable error messages:
|
|
256
|
+
|
|
257
|
+
```
|
|
258
|
+
❌ Environment validation failed:
|
|
259
|
+
- Missing required environment variable: DATABASE_URL
|
|
260
|
+
- PORT: Invalid port number (1-65535)
|
|
261
|
+
- NODE_ENV: must be one of: development, production (got: "staging")
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
## TypeScript support
|
|
143
265
|
|
|
144
|
-
|
|
266
|
+
Full type inference works automatically:
|
|
145
267
|
|
|
146
268
|
```ts
|
|
147
|
-
const
|
|
269
|
+
const config = await resolve({
|
|
270
|
+
PORT: 3000,
|
|
271
|
+
DATABASE_URL: 'url',
|
|
272
|
+
NODE_ENV: ['dev', 'prod'] as const,
|
|
273
|
+
DEBUG: false
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
config.PORT.toFixed(2); // ✓ number
|
|
277
|
+
config.DATABASE_URL.protocol; // ✓ URL object
|
|
278
|
+
config.NODE_ENV === 'dev'; // ✓ 'dev' | 'prod'
|
|
279
|
+
config.DEBUG ? 'yes' : 'no'; // ✓ boolean
|
|
148
280
|
```
|
|
281
|
+
|
|
282
|
+
## License
|
|
283
|
+
|
|
284
|
+
MIT © [Jagvinder Singh Reehal](https://jagreehal.com)
|
package/dist/audit.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Audit logging for environment variable resolution
|
|
3
|
+
*/
|
|
4
|
+
export type AuditEventType = 'validation_success' | 'validation_failure' | 'policy_violation' | 'env_loaded' | 'provider_error' | 'resolver_error';
|
|
5
|
+
export interface AuditEvent {
|
|
6
|
+
type: AuditEventType;
|
|
7
|
+
timestamp: number;
|
|
8
|
+
key?: string;
|
|
9
|
+
source?: string;
|
|
10
|
+
error?: string;
|
|
11
|
+
metadata?: Record<string, unknown>;
|
|
12
|
+
}
|
|
13
|
+
export declare function logAuditEvent(event: AuditEvent): void;
|
|
14
|
+
export declare function getAuditLog(): readonly AuditEvent[];
|
|
15
|
+
export declare function clearAuditLog(): void;
|
|
16
|
+
//# sourceMappingURL=audit.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"audit.d.ts","sourceRoot":"","sources":["../src/audit.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,MAAM,cAAc,GACtB,oBAAoB,GACpB,oBAAoB,GACpB,kBAAkB,GAClB,YAAY,GACZ,gBAAgB,GAChB,gBAAgB,CAAC;AAErB,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,cAAc,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;IAClB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC;AAKD,wBAAgB,aAAa,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI,CAMrD;AAED,wBAAgB,WAAW,IAAI,SAAS,UAAU,EAAE,CAEnD;AAED,wBAAgB,aAAa,IAAI,IAAI,CAEpC"}
|
package/dist/audit.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const t=[];function n(e){t.push(e),t.length>1e3&&t.shift();}function o(){return [...t]}function r(){t.length=0;}export{r as clearAuditLog,o as getAuditLog,n as logAuditEvent};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"audit.test.d.ts","sourceRoot":"","sources":["../src/audit.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* EnvBuilder - Fluent API for composing environment variables from multiple resolvers
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```typescript
|
|
6
|
+
* const config = await env({
|
|
7
|
+
* FOO: 'string',
|
|
8
|
+
* BAR: 3000,
|
|
9
|
+
* })
|
|
10
|
+
* .from(customResolver, {
|
|
11
|
+
* QUZ: 'string',
|
|
12
|
+
* })
|
|
13
|
+
* .resolve();
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
import type { Resolver, SimpleEnvSchema, InferSimpleSchema, ResolveOptions } from './types.js';
|
|
17
|
+
export type MergeSchemas<T, U> = Omit<T, keyof U> & U;
|
|
18
|
+
/**
|
|
19
|
+
* EnvBuilder class - accumulates resolvers and schemas, then resolves
|
|
20
|
+
*/
|
|
21
|
+
export declare class EnvBuilder<TAccumulated extends Record<string, unknown>> {
|
|
22
|
+
private localSchema;
|
|
23
|
+
private options;
|
|
24
|
+
private layers;
|
|
25
|
+
constructor(localSchema: SimpleEnvSchema, options?: Partial<ResolveOptions>);
|
|
26
|
+
/**
|
|
27
|
+
* Add a custom provider with its schema
|
|
28
|
+
* Later resolvers override earlier ones (last-wins)
|
|
29
|
+
*/
|
|
30
|
+
from<TNew extends SimpleEnvSchema>(resolver: Resolver, schema: TNew): EnvBuilder<MergeSchemas<TAccumulated, InferSimpleSchema<TNew>>>;
|
|
31
|
+
/**
|
|
32
|
+
* Resolve all resolvers and validate environment variables
|
|
33
|
+
*/
|
|
34
|
+
resolve(): Promise<TAccumulated>;
|
|
35
|
+
/**
|
|
36
|
+
* Build default resolvers (dotenv + process.env) for local schema
|
|
37
|
+
*/
|
|
38
|
+
private buildDefaultResolvers;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Synchronous EnvBuilder - only works with sync-capable resolvers
|
|
42
|
+
*/
|
|
43
|
+
export declare class EnvBuilderSync<TAccumulated extends Record<string, unknown>> {
|
|
44
|
+
private localSchema;
|
|
45
|
+
private options;
|
|
46
|
+
private layers;
|
|
47
|
+
constructor(localSchema: SimpleEnvSchema, options?: Partial<ResolveOptions>);
|
|
48
|
+
/**
|
|
49
|
+
* Add a custom provider with its schema (sync version)
|
|
50
|
+
*/
|
|
51
|
+
from<TNew extends SimpleEnvSchema>(resolver: Resolver, schema: TNew): EnvBuilderSync<MergeSchemas<TAccumulated, InferSimpleSchema<TNew>>>;
|
|
52
|
+
/**
|
|
53
|
+
* Synchronously resolve all resolvers
|
|
54
|
+
*/
|
|
55
|
+
resolveSync(): TAccumulated;
|
|
56
|
+
private buildDefaultResolvers;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Create an environment builder with local schema
|
|
60
|
+
* Use .from() to add custom resolvers, then .resolve() to execute
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* const config = await env({
|
|
65
|
+
* FOO: 'string',
|
|
66
|
+
* BAR: 3000,
|
|
67
|
+
* })
|
|
68
|
+
* .from(customResolver, { QUZ: 'string' })
|
|
69
|
+
* .resolve();
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
export declare function env<T extends SimpleEnvSchema>(localSchema: T, options?: Partial<ResolveOptions>): EnvBuilder<InferSimpleSchema<T>>;
|
|
73
|
+
/**
|
|
74
|
+
* Synchronous version of env() - only works with sync-capable resolvers
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```typescript
|
|
78
|
+
* const config = envSync({
|
|
79
|
+
* FOO: 'string',
|
|
80
|
+
* BAR: 3000,
|
|
81
|
+
* }).resolveSync();
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export declare function envSync<T extends SimpleEnvSchema>(localSchema: T, options?: Partial<ResolveOptions>): EnvBuilderSync<InferSimpleSchema<T>>;
|
|
85
|
+
/**
|
|
86
|
+
* ResolvableBuilder - A builder that is both awaitable AND chainable
|
|
87
|
+
* Simple usage: await resolve({...})
|
|
88
|
+
* Composition: await resolve({...}).from(...).compose()
|
|
89
|
+
*/
|
|
90
|
+
export declare class ResolvableBuilder<TAccumulated extends Record<string, unknown>> implements PromiseLike<TAccumulated> {
|
|
91
|
+
private builder;
|
|
92
|
+
private simpleResolve;
|
|
93
|
+
constructor(localSchema: SimpleEnvSchema, options?: Partial<ResolveOptions>);
|
|
94
|
+
/**
|
|
95
|
+
* Add a custom provider with its schema
|
|
96
|
+
*/
|
|
97
|
+
from<TNew extends SimpleEnvSchema>(resolver: Resolver, schema: TNew): ResolvableBuilder<MergeSchemas<TAccumulated, InferSimpleSchema<TNew>>>;
|
|
98
|
+
/**
|
|
99
|
+
* Terminal operation for composition - resolves all resolvers
|
|
100
|
+
* Only needed when using .from() - otherwise just await directly
|
|
101
|
+
*/
|
|
102
|
+
compose(): Promise<TAccumulated>;
|
|
103
|
+
/**
|
|
104
|
+
* Make this object awaitable (PromiseLike)
|
|
105
|
+
* Allows: const config = await resolve({...})
|
|
106
|
+
*/
|
|
107
|
+
then<TResult1 = TAccumulated, TResult2 = never>(onfulfilled?: ((value: TAccumulated) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null): PromiseLike<TResult1 | TResult2>;
|
|
108
|
+
/**
|
|
109
|
+
* Catch errors (Promise-like interface)
|
|
110
|
+
*/
|
|
111
|
+
catch<TResult = never>(onrejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | null): Promise<TAccumulated | TResult>;
|
|
112
|
+
/**
|
|
113
|
+
* Finally handler (Promise-like interface)
|
|
114
|
+
*/
|
|
115
|
+
finally(onfinally?: (() => void) | null): Promise<TAccumulated>;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* ResolvableBuilderSync - Synchronous version
|
|
119
|
+
* Returns the resolved value directly, but also chainable
|
|
120
|
+
*
|
|
121
|
+
* The returned object has all properties from TAccumulated plus builder methods
|
|
122
|
+
*/
|
|
123
|
+
export declare class ResolvableBuilderSync<TAccumulated extends Record<string, unknown>> {
|
|
124
|
+
private builder;
|
|
125
|
+
private _resolved;
|
|
126
|
+
private _hasComposition;
|
|
127
|
+
constructor(localSchema: SimpleEnvSchema, options?: Partial<ResolveOptions>);
|
|
128
|
+
/**
|
|
129
|
+
* Add a custom provider with its schema
|
|
130
|
+
*/
|
|
131
|
+
from<TNew extends SimpleEnvSchema>(resolver: Resolver, schema: TNew): ResolvableBuilderSync<MergeSchemas<TAccumulated, InferSimpleSchema<TNew>>>;
|
|
132
|
+
/**
|
|
133
|
+
* Terminal operation for composition - resolves all resolvers synchronously
|
|
134
|
+
* Only needed when using .from() - otherwise properties are already available
|
|
135
|
+
*/
|
|
136
|
+
compose(): TAccumulated;
|
|
137
|
+
/**
|
|
138
|
+
* Convert to plain object
|
|
139
|
+
*/
|
|
140
|
+
valueOf(): TAccumulated;
|
|
141
|
+
/**
|
|
142
|
+
* For JSON serialization
|
|
143
|
+
*/
|
|
144
|
+
toJSON(): TAccumulated;
|
|
145
|
+
}
|
|
146
|
+
//# sourceMappingURL=builder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builder.d.ts","sourceRoot":"","sources":["../src/builder.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,EACV,QAAQ,EACR,eAAe,EAEf,iBAAiB,EACjB,cAAc,EACf,MAAM,YAAY,CAAC;AAIpB,MAAM,MAAM,YAAY,CAAC,CAAC,EAAE,CAAC,IAAI,IAAI,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC;AAUtD;;GAEG;AACH,qBAAa,UAAU,CAAC,YAAY,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAIhE,OAAO,CAAC,WAAW;IACnB,OAAO,CAAC,OAAO;IAJjB,OAAO,CAAC,MAAM,CAAwC;gBAG5C,WAAW,EAAE,eAAe,EAC5B,OAAO,GAAE,OAAO,CAAC,cAAc,CAAM;IAG/C;;;OAGG;IACH,IAAI,CAAC,IAAI,SAAS,eAAe,EAC/B,QAAQ,EAAE,QAAQ,EAClB,MAAM,EAAE,IAAI,GACX,UAAU,CAAC,YAAY,CAAC,YAAY,EAAE,iBAAiB,CAAC,IAAI,CAAC,CAAC,CAAC;IAMlE;;OAEG;IACG,OAAO,IAAI,OAAO,CAAC,YAAY,CAAC;IAqCtC;;OAEG;IACH,OAAO,CAAC,qBAAqB;CAgB9B;AAED;;GAEG;AACH,qBAAa,cAAc,CAAC,YAAY,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAIpE,OAAO,CAAC,WAAW;IACnB,OAAO,CAAC,OAAO;IAJjB,OAAO,CAAC,MAAM,CAAwC;gBAG5C,WAAW,EAAE,eAAe,EAC5B,OAAO,GAAE,OAAO,CAAC,cAAc,CAAM;IAG/C;;OA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EnvInternalSync}from'./resolver.js';import {dotenv,processEnv}from'./resolvers.js';class v{constructor(e,s={}){this.localSchema=e;this.options=s;}layers=[];from(e,s){return this.layers.push({resolver:e,schema:s}),this}async resolve(){const e=normalizeSchema(this.localSchema);for(const l of this.layers){const c=normalizeSchema(l.schema);Object.assign(e,c);}const s=this.buildDefaultResolvers(),r=this.layers.map(l=>l.resolver),i=this.options.extend||[],n=this.options.resolvers?this.options.resolvers:[...s,...r,...i],a=this.options.policies??{};return await resolveEnvInternal(e,{...this.options,resolvers:n,policies:a,interpolate:this.options.interpolate??true,strict:this.options.strict??true})}buildDefaultResolvers(){const e=process.env.NODE_ENV==="production",s=[];return e||s.push(dotenv({expand:true})),s.push(processEnv()),s}}class p{constructor(e,s={}){this.localSchema=e;this.options=s;}layers=[];from(e,s){return this.layers.push({resolver:e,schema:s}),this}resolveSync(){const e=normalizeSchema(this.localSchema);for(const l of this.layers){const c=normalizeSchema(l.schema);Object.assign(e,c);}const s=this.buildDefaultResolvers(),r=this.layers.map(l=>l.resolver),i=this.options.extend||[],n=this.options.resolvers?this.options.resolvers:[...s,...r,...i],a=this.options.policies??{};return resolveEnvInternalSync(e,{...this.options,resolvers:n,policies:a,interpolate:this.options.interpolate??true,strict:this.options.strict??true})}buildDefaultResolvers(){const e=process.env.NODE_ENV==="production",s=[];return e||s.push(dotenv({expand:true})),s.push(processEnv()),s}}function f(t,e){return new v(t,e)}function y(t,e){return new p(t,e)}class b{builder;simpleResolve=null;constructor(e,s={}){this.builder=new v(e,s);}from(e,s){return this.builder.from(e,s),this}async compose(){return this.builder.resolve()}then(e,s){return this.simpleResolve||(this.simpleResolve=this.builder.resolve()),this.simpleResolve.then(e,s)}catch(e){return this.simpleResolve||(this.simpleResolve=this.builder.resolve()),this.simpleResolve.catch(e)}finally(e){return this.simpleResolve||(this.simpleResolve=this.builder.resolve()),this.simpleResolve.finally(e)}}class E{builder;_resolved=null;_hasComposition=false;constructor(e,s={}){Object.defineProperty(this,"builder",{value:new p(e,s),enumerable:false,writable:true}),Object.defineProperty(this,"_hasComposition",{value:false,enumerable:false,writable:true});const r=this.builder.resolveSync();Object.defineProperty(this,"_resolved",{value:r,enumerable:false,writable:true}),Object.assign(this,r);}from(e,s){return Object.defineProperty(this,"_hasComposition",{value:true,enumerable:false,writable:true}),this.builder.from(e,s),this}compose(){return this._hasComposition&&(this._resolved=this.builder.resolveSync(),Object.assign(this,this._resolved)),this._resolved}valueOf(){return this._resolved}toJSON(){return this._resolved}}export{v as EnvBuilder,p as EnvBuilderSync,b as ResolvableBuilder,E as ResolvableBuilderSync,f as env,y as envSync};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"composition.test.d.ts","sourceRoot":"","sources":["../src/composition.test.ts"],"names":[],"mappings":"AAAA;;;;GAIG"}
|