node-env-resolver 3.0.1 → 5.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 +396 -246
- package/dist/audit.cjs +1 -0
- package/dist/audit.d.cts +29 -0
- package/dist/audit.d.ts +19 -6
- package/dist/audit.js +1 -1
- package/dist/builder.cjs +1 -0
- package/dist/builder.d.cts +149 -0
- package/dist/builder.d.ts +17 -14
- package/dist/builder.js +1 -1
- package/dist/chunk-4YVXFL3I.cjs +9 -0
- package/dist/chunk-4ZOF2PI4.cjs +1 -0
- package/dist/chunk-5PELJRUQ.js +1 -0
- package/dist/chunk-6FXCXA4R.cjs +11 -0
- package/dist/chunk-BFLXBLZ3.cjs +1 -0
- package/dist/chunk-BJ2QNYTS.js +11 -0
- package/dist/chunk-DIXI6MUB.js +9 -0
- package/dist/chunk-KIQXH265.cjs +1 -0
- package/dist/chunk-P3TTYVEW.js +16 -0
- package/dist/chunk-UCLEPPLS.cjs +16 -0
- package/dist/chunk-YOHHFBZN.js +1 -0
- package/dist/chunk-ZMRT37K3.js +1 -0
- package/dist/cli.cjs +1 -0
- package/dist/cli.d.cts +52 -0
- package/dist/cli.d.ts +9 -6
- package/dist/cli.js +1 -1
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +150 -0
- package/dist/index.d.ts +122 -40
- package/dist/index.js +1 -1
- package/dist/resolver.cjs +1 -0
- package/dist/resolver.d.cts +20 -0
- package/dist/resolver.d.ts +8 -5
- package/dist/resolver.js +1 -1
- package/dist/resolvers.cjs +1 -0
- package/dist/resolvers.d.cts +407 -0
- package/dist/resolvers.d.ts +289 -22
- package/dist/resolvers.js +1 -1
- package/dist/types.cjs +1 -0
- package/dist/types.d.cts +74 -0
- package/dist/types.d.ts +58 -164
- package/dist/types.js +1 -0
- package/dist/utils.cjs +1 -0
- package/dist/utils.d.cts +213 -0
- package/dist/utils.d.ts +37 -33
- package/dist/utils.js +1 -1
- package/dist/valibot.cjs +1 -0
- package/dist/valibot.d.cts +156 -0
- package/dist/valibot.d.ts +11 -8
- package/dist/valibot.js +1 -1
- package/dist/validation-types.cjs +1 -0
- package/dist/validation-types.d.cts +29 -0
- package/dist/validation-types.d.ts +4 -3
- package/dist/validators.cjs +2 -0
- package/dist/validators.d.cts +116 -0
- package/dist/validators.d.ts +46 -18
- package/dist/validators.js +2 -1
- package/dist/web.cjs +6 -0
- package/dist/web.d.cts +69 -0
- package/dist/web.d.ts +28 -10
- package/dist/web.js +4 -4
- package/dist/zod.cjs +1 -0
- package/dist/zod.d.cts +135 -0
- package/dist/zod.d.ts +11 -8
- package/dist/zod.js +1 -1
- package/package.json +31 -19
- package/dist/advanced-features.test.d.ts +0 -2
- package/dist/advanced-features.test.d.ts.map +0 -1
- package/dist/audit.d.ts.map +0 -1
- package/dist/audit.test.d.ts +0 -2
- package/dist/audit.test.d.ts.map +0 -1
- package/dist/builder.d.ts.map +0 -1
- package/dist/chunk-476ZUILE.js +0 -1
- package/dist/chunk-DAGPFV3V.js +0 -1
- package/dist/chunk-MGTYH57K.js +0 -9
- package/dist/chunk-MWG7FWHN.js +0 -5
- package/dist/chunk-QWDK3BUB.js +0 -15
- package/dist/chunk-VJZZNSRI.js +0 -1
- package/dist/cli.d.ts.map +0 -1
- package/dist/cli.test.d.ts +0 -2
- package/dist/cli.test.d.ts.map +0 -1
- package/dist/composition.test.d.ts +0 -7
- package/dist/composition.test.d.ts.map +0 -1
- package/dist/computed.test.d.ts +0 -2
- package/dist/computed.test.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.test.d.ts +0 -2
- package/dist/index.test.d.ts.map +0 -1
- package/dist/nested.test.d.ts +0 -2
- package/dist/nested.test.d.ts.map +0 -1
- package/dist/performance.test.d.ts +0 -7
- package/dist/performance.test.d.ts.map +0 -1
- package/dist/priority.test.d.ts +0 -6
- package/dist/priority.test.d.ts.map +0 -1
- package/dist/providers.d.ts +0 -11
- package/dist/providers.d.ts.map +0 -1
- package/dist/resolver.d.ts.map +0 -1
- package/dist/resolvers.d.ts.map +0 -1
- package/dist/resolvers.test.d.ts +0 -2
- package/dist/resolvers.test.d.ts.map +0 -1
- package/dist/type-inference.test.d.ts +0 -2
- package/dist/type-inference.test.d.ts.map +0 -1
- package/dist/type-override.test.d.ts +0 -2
- package/dist/type-override.test.d.ts.map +0 -1
- package/dist/types.d.ts.map +0 -1
- package/dist/utils.d.ts.map +0 -1
- package/dist/utils.test.d.ts +0 -2
- package/dist/utils.test.d.ts.map +0 -1
- package/dist/valibot.d.ts.map +0 -1
- package/dist/validation-types.d.ts.map +0 -1
- package/dist/validators.d.ts.map +0 -1
- package/dist/web.d.ts.map +0 -1
- package/dist/zod.d.ts.map +0 -1
package/dist/types.d.ts
CHANGED
|
@@ -1,180 +1,74 @@
|
|
|
1
|
-
|
|
2
|
-
* Shared types for node-env-resolver
|
|
3
|
-
*/
|
|
4
|
-
export interface EnvDefinition {
|
|
5
|
-
/**
|
|
6
|
-
* Type of the environment variable
|
|
7
|
-
*
|
|
8
|
-
* Basic types (inline validation, ~2KB core):
|
|
9
|
-
* - 'string': Any string value (default)
|
|
10
|
-
* - 'number': Coerced to number
|
|
11
|
-
* - 'boolean': Coerced to boolean
|
|
12
|
-
* - 'custom': Custom validator function
|
|
13
|
-
*
|
|
14
|
-
* Array types (comma-separated by default):
|
|
15
|
-
* - 'string[]': Array of strings (e.g., 'a,b,c' → ['a', 'b', 'c'])
|
|
16
|
-
* - 'number[]': Array of numbers (e.g., '1,2,3' → [1, 2, 3])
|
|
17
|
-
* - 'url[]': Array of URLs
|
|
18
|
-
*
|
|
19
|
-
* Advanced types (lazy-loaded validators, tree-shakeable):
|
|
20
|
-
* - 'postgres'/'postgresql': PostgreSQL connection URL
|
|
21
|
-
* - 'mysql': MySQL connection URL
|
|
22
|
-
* - 'mongodb': MongoDB connection URL (supports replica sets)
|
|
23
|
-
* - 'redis': Redis connection URL
|
|
24
|
-
* - 'http': HTTP or HTTPS URL
|
|
25
|
-
* - 'https': HTTPS-only URL
|
|
26
|
-
* - 'url': Generic URL
|
|
27
|
-
* - 'email': Email address
|
|
28
|
-
* - 'port': Port number 1-65535
|
|
29
|
-
* - 'json': JSON value (returns parsed object)
|
|
30
|
-
* - 'date': ISO 8601 date
|
|
31
|
-
* - 'timestamp': Unix timestamp (seconds)
|
|
32
|
-
* - 'duration': Time duration (e.g., '5s', '2h', '30m' → milliseconds)
|
|
33
|
-
* - 'file': Read content from file path
|
|
34
|
-
*/
|
|
35
|
-
type?: 'string' | 'number' | 'boolean' | 'custom' | 'string[]' | 'number[]' | 'url[]' | 'postgres' | 'postgresql' | 'mysql' | 'mongodb' | 'redis' | 'http' | 'https' | 'url' | 'email' | 'port' | 'json' | 'date' | 'timestamp' | 'duration' | 'file';
|
|
36
|
-
enum?: readonly string[];
|
|
37
|
-
default?: string | number | boolean | string[] | number[];
|
|
1
|
+
type Validator<T> = ((value: string) => T) & {
|
|
38
2
|
optional?: boolean;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
3
|
+
default?: T;
|
|
4
|
+
};
|
|
5
|
+
type SimpleEnvSchema = Record<string, Validator<unknown> | string | number | boolean | readonly string[]>;
|
|
6
|
+
type InferSimpleValue<V> = V extends Validator<infer R> ? V extends {
|
|
7
|
+
optional: true;
|
|
8
|
+
} ? R | undefined : R : V extends number ? number : V extends boolean ? boolean : V extends readonly (infer U)[] ? U : V extends string ? string : never;
|
|
9
|
+
type InferSimpleSchema<T extends SimpleEnvSchema> = {
|
|
10
|
+
[K in keyof T]: InferSimpleValue<T[K]>;
|
|
11
|
+
};
|
|
12
|
+
type InferNestedSchema<T extends SimpleEnvSchema> = InferSimpleSchema<T>;
|
|
13
|
+
type InferResolveResult<T extends SimpleEnvSchema, O extends ResolveOptions | undefined> = O extends {
|
|
14
|
+
nestedDelimiter: string;
|
|
15
|
+
} ? InferNestedSchema<T> : InferSimpleSchema<T>;
|
|
16
|
+
interface SyncResolver {
|
|
17
|
+
loadSync(): Record<string, string>;
|
|
18
|
+
load?(): Promise<Record<string, string>>;
|
|
19
|
+
name?: string;
|
|
20
|
+
metadata?: Record<string, unknown>;
|
|
53
21
|
}
|
|
54
|
-
|
|
55
|
-
name: string;
|
|
22
|
+
interface AsyncResolver {
|
|
56
23
|
load(): Promise<Record<string, string>>;
|
|
57
24
|
loadSync?(): Record<string, string>;
|
|
25
|
+
name?: string;
|
|
58
26
|
metadata?: Record<string, unknown>;
|
|
59
27
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
enableAudit?: boolean;
|
|
65
|
-
/**
|
|
66
|
-
* Control merge behavior when multiple resolvers provide the same variable.
|
|
67
|
-
*
|
|
68
|
-
* - `'last'` (default): Later resolvers override earlier ones
|
|
69
|
-
* - `'first'`: Earlier resolvers take precedence, later ones are skipped if value already set
|
|
70
|
-
*
|
|
71
|
-
* @example
|
|
72
|
-
* // Local .env overrides AWS secrets (for development)
|
|
73
|
-
* resolve.with(
|
|
74
|
-
* [dotenv(), { DATABASE_URL: 'url' }],
|
|
75
|
-
* [awsSecrets(), { DATABASE_URL: 'url' }],
|
|
76
|
-
* { priority: 'first' } // dotenv wins
|
|
77
|
-
* );
|
|
78
|
-
*
|
|
79
|
-
* @example
|
|
80
|
-
* // AWS secrets override process.env (for production, default)
|
|
81
|
-
* resolve.with(
|
|
82
|
-
* [processEnv(), { DATABASE_URL: 'url' }],
|
|
83
|
-
* [awsSecrets(), { DATABASE_URL: 'url' }]
|
|
84
|
-
* // priority: 'last' is default - AWS wins
|
|
85
|
-
* );
|
|
86
|
-
*/
|
|
28
|
+
type Resolver = SyncResolver | AsyncResolver;
|
|
29
|
+
declare const isSyncResolver: (resolver: Resolver) => resolver is SyncResolver;
|
|
30
|
+
declare const isAsyncOnlyResolver: (resolver: Resolver) => resolver is AsyncResolver;
|
|
31
|
+
interface ResolveOptions {
|
|
87
32
|
priority?: 'first' | 'last';
|
|
88
|
-
/**
|
|
89
|
-
* Transform flat environment variables into nested objects using a delimiter.
|
|
90
|
-
*
|
|
91
|
-
* When specified, keys like `DATABASE__HOST` become nested as `{ database: { host: '...' } }`
|
|
92
|
-
*
|
|
93
|
-
* @example
|
|
94
|
-
* ```ts
|
|
95
|
-
* // With delimiter '__'
|
|
96
|
-
* process.env.DATABASE__HOST = 'localhost';
|
|
97
|
-
* process.env.DATABASE__PORT = '5432';
|
|
98
|
-
*
|
|
99
|
-
* const config = resolve({
|
|
100
|
-
* 'DATABASE__HOST': 'string',
|
|
101
|
-
* 'DATABASE__PORT': 'port'
|
|
102
|
-
* }, { nestedDelimiter: '__' });
|
|
103
|
-
*
|
|
104
|
-
* // Result: { database: { host: 'localhost', port: 5432 } }
|
|
105
|
-
* ```
|
|
106
|
-
*/
|
|
107
33
|
nestedDelimiter?: string;
|
|
108
|
-
/**
|
|
109
|
-
* Validate default values using the same validators as environment values.
|
|
110
|
-
*
|
|
111
|
-
* When `true`, default values are validated to ensure they match the specified type.
|
|
112
|
-
* This catches configuration errors at startup instead of runtime.
|
|
113
|
-
*
|
|
114
|
-
* @default false
|
|
115
|
-
*
|
|
116
|
-
* @example
|
|
117
|
-
* ```ts
|
|
118
|
-
* const config = resolve({
|
|
119
|
-
* PORT: 99999, // Invalid port number!
|
|
120
|
-
* DATABASE_URL: 'not-a-url' // Invalid URL!
|
|
121
|
-
* }, { validateDefaults: true });
|
|
122
|
-
* // Throws: Port must be between 1 and 65535
|
|
123
|
-
* ```
|
|
124
|
-
*/
|
|
125
34
|
validateDefaults?: boolean;
|
|
126
|
-
/**
|
|
127
|
-
* Global secrets directory for file reading (type: 'file').
|
|
128
|
-
*
|
|
129
|
-
* When specified, variables with `type: 'file'` will read from `secretsDir/lowercase_key_name`
|
|
130
|
-
* if no env var value is provided. Useful for Docker/Kubernetes secrets.
|
|
131
|
-
*
|
|
132
|
-
* Can be overridden per-field using the `secretsDir` property in EnvDefinition.
|
|
133
|
-
*
|
|
134
|
-
* @example
|
|
135
|
-
* ```ts
|
|
136
|
-
* const config = resolve({
|
|
137
|
-
* DB_PASSWORD: 'file', // Reads from /run/secrets/db_password
|
|
138
|
-
* API_KEY: 'file' // Reads from /run/secrets/api_key
|
|
139
|
-
* }, { secretsDir: '/run/secrets' });
|
|
140
|
-
* ```
|
|
141
|
-
*/
|
|
142
35
|
secretsDir?: string;
|
|
36
|
+
arraySeparator?: string;
|
|
37
|
+
interpolate?: boolean;
|
|
38
|
+
strict?: boolean;
|
|
39
|
+
policies?: Record<string, unknown>;
|
|
40
|
+
enableAudit?: boolean;
|
|
143
41
|
}
|
|
144
|
-
|
|
42
|
+
type SafeResolveResultType<T> = {
|
|
43
|
+
success: true;
|
|
44
|
+
data: T;
|
|
45
|
+
} | {
|
|
46
|
+
success: false;
|
|
47
|
+
error: string;
|
|
48
|
+
details?: unknown;
|
|
49
|
+
};
|
|
50
|
+
type Provenance = {
|
|
145
51
|
source: string;
|
|
146
52
|
timestamp: number;
|
|
53
|
+
metadata?: Record<string, unknown>;
|
|
147
54
|
cached?: boolean;
|
|
148
|
-
}
|
|
149
|
-
export interface PolicyOptions {
|
|
150
|
-
/**
|
|
151
|
-
* Control loading from .env files in production.
|
|
152
|
-
*
|
|
153
|
-
* - `undefined` (default): .env files completely ignored in production (secure default)
|
|
154
|
-
* - `true`: Allow all vars from .env in production (NOT recommended)
|
|
155
|
-
* - `string[]`: Allow only specific vars from .env in production
|
|
156
|
-
*
|
|
157
|
-
* Note: `process.env` is ALWAYS used regardless of this setting.
|
|
158
|
-
* Production platforms (Vercel, AWS, Docker, etc.) inject via process.env, NOT .env files.
|
|
159
|
-
*
|
|
160
|
-
* @example allowDotenvInProduction: true // Allow all (not recommended)
|
|
161
|
-
* @example allowDotenvInProduction: ['PORT', 'NODE_ENV'] // Allow specific vars
|
|
162
|
-
*/
|
|
163
|
-
allowDotenvInProduction?: boolean | string[];
|
|
164
|
-
enforceAllowedSources?: Record<string, string[]>;
|
|
165
|
-
}
|
|
166
|
-
export type SimpleEnvValue = string | number | boolean | readonly string[] | EnvDefinition | ((value: string) => unknown);
|
|
167
|
-
export interface SimpleEnvSchema {
|
|
168
|
-
[key: string]: SimpleEnvValue;
|
|
169
|
-
}
|
|
170
|
-
export type InferType<T extends EnvDefinition> = T['enum'] extends readonly (infer U)[] ? U : T['type'] extends 'number' ? number : T['type'] extends 'boolean' ? boolean : T['type'] extends 'port' ? number : T['type'] extends 'timestamp' ? number : T['type'] extends 'duration' ? number : T['type'] extends 'string[]' ? string[] : T['type'] extends 'number[]' ? number[] : T['type'] extends 'url[]' ? string[] : T['type'] extends 'custom' ? unknown : string;
|
|
171
|
-
export type InferSchema<T extends EnvSchema> = {
|
|
172
|
-
[K in keyof T]: T[K]['optional'] extends true ? InferType<T[K]> | undefined : T[K]['default'] extends unknown ? InferType<T[K]> : InferType<T[K]>;
|
|
173
55
|
};
|
|
174
|
-
type
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
56
|
+
type PolicyOptions = {
|
|
57
|
+
allowDotenvInProduction?: boolean;
|
|
58
|
+
requireSecretsForSensitive?: boolean;
|
|
59
|
+
blockInsecureProtocols?: boolean;
|
|
60
|
+
enforceAllowedSources?: boolean;
|
|
178
61
|
};
|
|
179
|
-
|
|
180
|
-
|
|
62
|
+
type SimpleEnvValue = Validator<unknown> | string | number | boolean | readonly string[];
|
|
63
|
+
type EnvSchema = Record<string, unknown>;
|
|
64
|
+
interface EnvDefinition {
|
|
65
|
+
type?: string;
|
|
66
|
+
validator?: (value: string, key?: string) => unknown;
|
|
67
|
+
default?: unknown;
|
|
68
|
+
optional?: boolean;
|
|
69
|
+
secretsDir?: string;
|
|
70
|
+
}
|
|
71
|
+
type InferType = unknown;
|
|
72
|
+
type InferSchema = unknown;
|
|
73
|
+
|
|
74
|
+
export { type AsyncResolver, type EnvDefinition, type EnvSchema, type InferNestedSchema, type InferResolveResult, type InferSchema, type InferSimpleSchema, type InferSimpleValue, type InferType, type PolicyOptions, type Provenance, type ResolveOptions, type Resolver, type SafeResolveResultType, type SimpleEnvSchema, type SimpleEnvValue, type SyncResolver, type Validator, isAsyncOnlyResolver, isSyncResolver };
|
package/dist/types.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export{b as isAsyncOnlyResolver,a as isSyncResolver}from'./chunk-ZMRT37K3.js';import'./chunk-5PELJRUQ.js';
|
package/dist/utils.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
'use strict';var chunk6FXCXA4R_cjs=require('./chunk-6FXCXA4R.cjs');require('./chunk-4YVXFL3I.cjs'),require('./chunk-4ZOF2PI4.cjs'),require('./chunk-BFLXBLZ3.cjs');var chunkKIQXH265_cjs=require('./chunk-KIQXH265.cjs');function C(e,n={}){let{ttl:r=3e5,maxAge:a=36e5,staleWhileRevalidate:o=false}=n,t=null,s={name:`cached(${e.name})`,metadata:{},async load(){let i=Date.now();if(!t||i-t.timestamp>a){let l=e.load?await e.load():{};return t={data:l,timestamp:i},s.metadata={cached:false},l}if(i-t.timestamp<r)return s.metadata={cached:true},t.data;if(o&&!t.refreshPromise){let l=Promise.resolve(e.load?e.load():Promise.resolve({}));return t.refreshPromise=l.then(u=>(t.data=u,t.timestamp=Date.now(),t.refreshPromise=void 0,u)).catch(()=>(t.refreshPromise=void 0,t.data)),s.metadata={cached:true,stale:true},t.data}let d=e.load?await e.load():{};return t={data:d,timestamp:i},s.metadata={cached:false},d}};return s}var p={short:30*1e3,minute:60*1e3,minutes5:300*1e3,minutes15:900*1e3,hour:3600*1e3,hours6:360*60*1e3,day:1440*60*1e3};function b(e={}){return {ttl:e.ttl??p.minutes5,maxAge:e.maxAge??p.hour,staleWhileRevalidate:e.staleWhileRevalidate??true,key:"aws-secrets"}}function x(e,n=3,r=1e3){return {name:`retry(${e.name})`,async load(){let a;for(let o=0;o<=n;o++)try{return e.load?await e.load():{}}catch(t){a=t instanceof Error?t:new Error(String(t)),o<n&&await new Promise(s=>setTimeout(s,r*Math.pow(2,o)));}throw a}}}function P(e,n){let r=n.toUpperCase();return {name:`withPrefix(${e.name}, ${n})`,async load(){let a=e.load?await e.load():{},o={};for(let[t,s]of Object.entries(a))if(t.toUpperCase().startsWith(r)){let i=t.slice(n.length);o[i]=s;}else o[t]=s;return o},loadSync(){if(!e.loadSync)throw new Error(`Resolver ${e.name} does not support sync loading`);let a=e.loadSync(),o={};for(let[t,s]of Object.entries(a))if(t.toUpperCase().startsWith(r)){let i=t.slice(n.length);o[i]=s;}else o[t]=s;return o}}}function $(e,n){let r={...e};for(let[a,o]of Object.entries(n))Object.defineProperty(r,a,{get(){return o(e)},enumerable:true,configurable:true});return r}function S(e,n){return {name:`withAliases(${e.name})`,async load(){let r=e.load?await e.load():{};return g(r,n)},loadSync(){if(!e.loadSync)throw new Error(`Resolver ${e.name} does not support sync loading`);let r=e.loadSync();return g(r,n)}}}function g(e,n){let r={...e};for(let[a,o]of Object.entries(n))for(let t of o)if(e[t]!==void 0){r[a]=e[t];break}return r}function k(e,n){return {name:`withTransform(${e.name})`,async load(){let r=e.load?await e.load():{};return h(r,n)},loadSync(){if(!e.loadSync)throw new Error(`Resolver ${e.name} does not support sync loading`);let r=e.loadSync();return h(r,n)}}}function h(e,n){let r={...e};for(let[a,o]of Object.entries(n))if(e[a]!==void 0)try{r[a]=o(e[a]);}catch(t){throw new Error(`Transform failed for ${a}: ${t instanceof Error?t.message:String(t)}`)}return r}function v(e,n){let a=`${n.toUpperCase()}_`;return {name:`withNamespace(${e.name}, ${n})`,async load(){let o=e.load?await e.load():{},t={};for(let[s,i]of Object.entries(o))if(s.toUpperCase().startsWith(a)){let d=s.slice(a.length);t[d]=i;}return t},loadSync(){if(!e.loadSync)throw new Error(`Resolver ${e.name} does not support sync loading`);let o=e.loadSync(),t={};for(let[s,i]of Object.entries(o))if(s.toUpperCase().startsWith(a)){let d=s.slice(a.length);t[d]=i;}return t}}}function A(e,n,r){let{onChange:a,debounce:o=100,files:t=[".env"]}=r??{},s,i=null,d=[],l=async()=>{try{return await import('./index.cjs'),s=await chunk6FXCXA4R_cjs.c(e,n),s}catch(c){throw console.error("[watch] Failed to load config:",c),c}},u=()=>{i&&clearTimeout(i),i=setTimeout(async()=>{try{let c=await l();a?.(c);}catch(c){console.error("[watch] Failed to reload config:",c);}},o);},{watch:y}=chunkKIQXH265_cjs.a("fs"),{join:w}=chunkKIQXH265_cjs.a("path");for(let c of t)try{let R=y(w(process.cwd(),c),u);d.push(R);}catch{}return l().catch(console.error),{getConfig:()=>s,stop:()=>{i&&clearTimeout(i);for(let c of d)c.close();}}}exports.TTL=p;exports.awsCache=b;exports.cached=C;exports.retry=x;exports.watch=A;exports.withAliases=S;exports.withComputed=$;exports.withNamespace=v;exports.withPrefix=P;exports.withTransform=k;
|
package/dist/utils.d.cts
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { resolveAsync } from './index.cjs';
|
|
2
|
+
import { Resolver } from './types.cjs';
|
|
3
|
+
import './validation-types.cjs';
|
|
4
|
+
import './audit.cjs';
|
|
5
|
+
|
|
6
|
+
interface CacheOptions {
|
|
7
|
+
/** Time to live in milliseconds (default: 5 minutes) */
|
|
8
|
+
ttl?: number;
|
|
9
|
+
/** Maximum age in milliseconds before forcing refresh (default: 1 hour) */
|
|
10
|
+
maxAge?: number;
|
|
11
|
+
/** Enable stale-while-revalidate: serve stale data while refreshing in background */
|
|
12
|
+
staleWhileRevalidate?: boolean;
|
|
13
|
+
/** Custom cache key (useful for debugging or manual cache invalidation) */
|
|
14
|
+
key?: string;
|
|
15
|
+
}
|
|
16
|
+
declare function cached(resolver: Resolver, options?: CacheOptions): Resolver;
|
|
17
|
+
declare const TTL: {
|
|
18
|
+
/** 30 seconds */
|
|
19
|
+
readonly short: number;
|
|
20
|
+
/** 1 minute */
|
|
21
|
+
readonly minute: number;
|
|
22
|
+
/** 5 minutes */
|
|
23
|
+
readonly minutes5: number;
|
|
24
|
+
/** 15 minutes */
|
|
25
|
+
readonly minutes15: number;
|
|
26
|
+
/** 1 hour */
|
|
27
|
+
readonly hour: number;
|
|
28
|
+
/** 6 hours */
|
|
29
|
+
readonly hours6: number;
|
|
30
|
+
/** 24 hours */
|
|
31
|
+
readonly day: number;
|
|
32
|
+
};
|
|
33
|
+
declare function awsCache(options?: {
|
|
34
|
+
/** Cache duration for AWS secrets/parameters (default: 5 minutes) */
|
|
35
|
+
ttl?: number;
|
|
36
|
+
/** Maximum age before forcing refresh (default: 1 hour) */
|
|
37
|
+
maxAge?: number;
|
|
38
|
+
/** Enable stale-while-revalidate for better performance (default: true) */
|
|
39
|
+
staleWhileRevalidate?: boolean;
|
|
40
|
+
}): CacheOptions;
|
|
41
|
+
declare function retry(resolver: Resolver, maxRetries?: number, delayMs?: number): Resolver;
|
|
42
|
+
/**
|
|
43
|
+
* Wrap a resolver to strip a prefix from environment variable names
|
|
44
|
+
* Useful for scoping environment variables (e.g., APP_PORT → PORT)
|
|
45
|
+
*
|
|
46
|
+
* @param resolver The resolver to wrap
|
|
47
|
+
* @param prefix The prefix to strip (case-insensitive)
|
|
48
|
+
* @returns Wrapped resolver that strips the prefix
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* import { resolve, processEnv } from 'node-env-resolver';
|
|
53
|
+
* import { withPrefix } from 'node-env-resolver/utils';
|
|
54
|
+
*
|
|
55
|
+
* // Maps APP_PORT → PORT, APP_DATABASE_URL → DATABASE_URL
|
|
56
|
+
* const config = await resolveAsync(
|
|
57
|
+
* [withPrefix(processEnv(), 'APP_'), { PORT: 3000, DATABASE_URL: postgres() }]
|
|
58
|
+
* );
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
declare function withPrefix(resolver: Resolver, prefix: string): Resolver;
|
|
62
|
+
/**
|
|
63
|
+
* Add computed/derived properties to resolved config
|
|
64
|
+
*
|
|
65
|
+
* Useful for deriving values from multiple config fields (e.g., building URLs,
|
|
66
|
+
* computing feature flags, creating connection objects).
|
|
67
|
+
*
|
|
68
|
+
* @param config Resolved configuration object
|
|
69
|
+
* @param computed Map of computed property names to functions that derive values
|
|
70
|
+
* @returns Config with computed properties as getters
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```ts
|
|
74
|
+
* import { resolve } from 'node-env-resolver';
|
|
75
|
+
* import { withComputed } from 'node-env-resolver/utils';
|
|
76
|
+
*
|
|
77
|
+
* const config = withComputed(
|
|
78
|
+
* resolve({ HOST: 'localhost', PORT: 3000, NODE_ENV: 'development' }),
|
|
79
|
+
* {
|
|
80
|
+
* url: (c) => `http://${c.HOST}:${c.PORT}`,
|
|
81
|
+
* isDev: (c) => c.NODE_ENV === 'development',
|
|
82
|
+
* isSecure: (c) => c.HOST !== 'localhost'
|
|
83
|
+
* }
|
|
84
|
+
* );
|
|
85
|
+
*
|
|
86
|
+
* console.log(config.url); // 'http://localhost:3000'
|
|
87
|
+
* console.log(config.isDev); // true
|
|
88
|
+
* console.log(config.isSecure); // false
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
declare function withComputed<TConfig extends Record<string, unknown>, TComputed extends Record<string, (config: TConfig) => unknown>>(config: TConfig, computed: TComputed): TConfig & {
|
|
92
|
+
[K in keyof TComputed]: ReturnType<TComputed[K]>;
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Wrap a resolver to support field aliases (multiple possible names for a config value)
|
|
96
|
+
* Tries each alias in order and uses the first one found
|
|
97
|
+
*
|
|
98
|
+
* @param resolver The resolver to wrap
|
|
99
|
+
* @param aliases Map of canonical key to array of possible aliases
|
|
100
|
+
* @returns Wrapped resolver that supports aliases
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```ts
|
|
104
|
+
* import { resolve, processEnv } from 'node-env-resolver';
|
|
105
|
+
* import { withAliases } from 'node-env-resolver/utils';
|
|
106
|
+
*
|
|
107
|
+
* // Tries PORT, then HTTP_PORT, then SERVER_PORT (first found wins)
|
|
108
|
+
* const config = await resolveAsync(
|
|
109
|
+
* [withAliases(processEnv(), {
|
|
110
|
+
* PORT: ['PORT', 'HTTP_PORT', 'SERVER_PORT'],
|
|
111
|
+
* DATABASE_URL: ['DATABASE_URL', 'DB_URL', 'POSTGRES_URL']
|
|
112
|
+
* }), { PORT: 3000, DATABASE_URL: postgres() }]
|
|
113
|
+
* );
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
declare function withAliases(resolver: Resolver, aliases: Record<string, string[]>): Resolver;
|
|
117
|
+
/**
|
|
118
|
+
* Wrap a resolver to apply custom transformations to values
|
|
119
|
+
* Powerful utility for parsing, coercing, or enriching config values
|
|
120
|
+
*
|
|
121
|
+
* @param resolver The resolver to wrap
|
|
122
|
+
* @param transforms Map of keys to transformation functions
|
|
123
|
+
* @returns Wrapped resolver that applies transforms
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```ts
|
|
127
|
+
* import { resolve, processEnv } from 'node-env-resolver';
|
|
128
|
+
* import { withTransform } from 'node-env-resolver/utils';
|
|
129
|
+
*
|
|
130
|
+
* const config = await resolveAsync(
|
|
131
|
+
* [withTransform(processEnv(), {
|
|
132
|
+
* // Parse comma-separated values
|
|
133
|
+
* TAGS: (val) => val.split(',').map(s => s.trim()),
|
|
134
|
+
* // Convert to URL object
|
|
135
|
+
* API_URL: (val) => new URL(val),
|
|
136
|
+
* // Custom parsing
|
|
137
|
+
* MAX_RETRIES: (val) => Math.min(parseInt(val, 10), 10)
|
|
138
|
+
* }), {
|
|
139
|
+
* TAGS: string(),
|
|
140
|
+
* API_URL: url(),
|
|
141
|
+
* MAX_RETRIES: 'number'
|
|
142
|
+
* }]
|
|
143
|
+
* );
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
declare function withTransform(resolver: Resolver, transforms: Record<string, (value: string) => string>): Resolver;
|
|
147
|
+
/**
|
|
148
|
+
* Wrap a resolver to scope all keys to a namespace/prefix
|
|
149
|
+
* Similar to withPrefix but adds the namespace instead of removing it
|
|
150
|
+
*
|
|
151
|
+
* @param resolver The resolver to wrap
|
|
152
|
+
* @param namespace The namespace to scope keys under
|
|
153
|
+
* @returns Wrapped resolver with scoped keys
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```ts
|
|
157
|
+
* import { resolve, processEnv } from 'node-env-resolver';
|
|
158
|
+
* import { withNamespace } from 'node-env-resolver/utils';
|
|
159
|
+
*
|
|
160
|
+
* // Reads DATABASE_HOST, DATABASE_PORT from env
|
|
161
|
+
* // But schema only needs HOST, PORT
|
|
162
|
+
* const dbConfig = await resolveAsync(
|
|
163
|
+
* [withNamespace(processEnv(), 'DATABASE'), {
|
|
164
|
+
* HOST: string(),
|
|
165
|
+
* PORT: 'port'
|
|
166
|
+
* }]
|
|
167
|
+
* );
|
|
168
|
+
* // dbConfig: { HOST: '...', PORT: ... }
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
171
|
+
declare function withNamespace(resolver: Resolver, namespace: string): Resolver;
|
|
172
|
+
/**
|
|
173
|
+
* Watch configuration files and reload automatically
|
|
174
|
+
* Perfect for development - auto-reload when .env files change
|
|
175
|
+
*
|
|
176
|
+
* @param schema Environment schema definition
|
|
177
|
+
* @param resolvers Array of resolvers to use
|
|
178
|
+
* @param options Watch options
|
|
179
|
+
* @returns Function to get current config + cleanup function
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* ```ts
|
|
183
|
+
* import { watch } from 'node-env-resolver/utils';
|
|
184
|
+
* import { dotenv, processEnv } from 'node-env-resolver/resolvers';
|
|
185
|
+
*
|
|
186
|
+
* const { getConfig, stop } = watch(
|
|
187
|
+
* { PORT: 3000, API_KEY: string() },
|
|
188
|
+
* [processEnv(), dotenv()],
|
|
189
|
+
* {
|
|
190
|
+
* onChange: (config) => console.log('Config reloaded!', config)
|
|
191
|
+
* }
|
|
192
|
+
* );
|
|
193
|
+
*
|
|
194
|
+
* // Use in your app
|
|
195
|
+
* app.get('/config', () => getConfig());
|
|
196
|
+
*
|
|
197
|
+
* // Cleanup when done
|
|
198
|
+
* process.on('SIGINT', () => stop());
|
|
199
|
+
* ```
|
|
200
|
+
*/
|
|
201
|
+
declare function watch<T>(schema: Parameters<typeof resolveAsync>[0], resolvers: Parameters<typeof resolveAsync>[1], options?: {
|
|
202
|
+
/** Callback when config changes */
|
|
203
|
+
onChange?: (config: T) => void;
|
|
204
|
+
/** Debounce delay in ms (default: 100) */
|
|
205
|
+
debounce?: number;
|
|
206
|
+
/** Files to watch (default: ['.env']) */
|
|
207
|
+
files?: string[];
|
|
208
|
+
}): {
|
|
209
|
+
getConfig: () => T;
|
|
210
|
+
stop: () => void;
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
export { type CacheOptions, TTL, awsCache, cached, retry, watch, withAliases, withComputed, withNamespace, withPrefix, withTransform };
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import
|
|
5
|
-
|
|
1
|
+
import { resolveAsync } from './index.js';
|
|
2
|
+
import { Resolver } from './types.js';
|
|
3
|
+
import './validation-types.js';
|
|
4
|
+
import './audit.js';
|
|
5
|
+
|
|
6
|
+
interface CacheOptions {
|
|
6
7
|
/** Time to live in milliseconds (default: 5 minutes) */
|
|
7
8
|
ttl?: number;
|
|
8
9
|
/** Maximum age in milliseconds before forcing refresh (default: 1 hour) */
|
|
@@ -12,8 +13,8 @@ export interface CacheOptions {
|
|
|
12
13
|
/** Custom cache key (useful for debugging or manual cache invalidation) */
|
|
13
14
|
key?: string;
|
|
14
15
|
}
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
declare function cached(resolver: Resolver, options?: CacheOptions): Resolver;
|
|
17
|
+
declare const TTL: {
|
|
17
18
|
/** 30 seconds */
|
|
18
19
|
readonly short: number;
|
|
19
20
|
/** 1 minute */
|
|
@@ -29,7 +30,7 @@ export declare const TTL: {
|
|
|
29
30
|
/** 24 hours */
|
|
30
31
|
readonly day: number;
|
|
31
32
|
};
|
|
32
|
-
|
|
33
|
+
declare function awsCache(options?: {
|
|
33
34
|
/** Cache duration for AWS secrets/parameters (default: 5 minutes) */
|
|
34
35
|
ttl?: number;
|
|
35
36
|
/** Maximum age before forcing refresh (default: 1 hour) */
|
|
@@ -37,7 +38,7 @@ export declare function awsCache(options?: {
|
|
|
37
38
|
/** Enable stale-while-revalidate for better performance (default: true) */
|
|
38
39
|
staleWhileRevalidate?: boolean;
|
|
39
40
|
}): CacheOptions;
|
|
40
|
-
|
|
41
|
+
declare function retry(resolver: Resolver, maxRetries?: number, delayMs?: number): Resolver;
|
|
41
42
|
/**
|
|
42
43
|
* Wrap a resolver to strip a prefix from environment variable names
|
|
43
44
|
* Useful for scoping environment variables (e.g., APP_PORT → PORT)
|
|
@@ -52,12 +53,12 @@ export declare function retry(resolver: Resolver, maxRetries?: number, delayMs?:
|
|
|
52
53
|
* import { withPrefix } from 'node-env-resolver/utils';
|
|
53
54
|
*
|
|
54
55
|
* // Maps APP_PORT → PORT, APP_DATABASE_URL → DATABASE_URL
|
|
55
|
-
* const config = await
|
|
56
|
-
* [withPrefix(processEnv(), 'APP_'), { PORT: 3000, DATABASE_URL:
|
|
56
|
+
* const config = await resolveAsync(
|
|
57
|
+
* [withPrefix(processEnv(), 'APP_'), { PORT: 3000, DATABASE_URL: postgres() }]
|
|
57
58
|
* );
|
|
58
59
|
* ```
|
|
59
60
|
*/
|
|
60
|
-
|
|
61
|
+
declare function withPrefix(resolver: Resolver, prefix: string): Resolver;
|
|
61
62
|
/**
|
|
62
63
|
* Add computed/derived properties to resolved config
|
|
63
64
|
*
|
|
@@ -87,7 +88,7 @@ export declare function withPrefix(resolver: Resolver, prefix: string): Resolver
|
|
|
87
88
|
* console.log(config.isSecure); // false
|
|
88
89
|
* ```
|
|
89
90
|
*/
|
|
90
|
-
|
|
91
|
+
declare function withComputed<TConfig extends Record<string, unknown>, TComputed extends Record<string, (config: TConfig) => unknown>>(config: TConfig, computed: TComputed): TConfig & {
|
|
91
92
|
[K in keyof TComputed]: ReturnType<TComputed[K]>;
|
|
92
93
|
};
|
|
93
94
|
/**
|
|
@@ -104,15 +105,15 @@ export declare function withComputed<TConfig extends Record<string, unknown>, TC
|
|
|
104
105
|
* import { withAliases } from 'node-env-resolver/utils';
|
|
105
106
|
*
|
|
106
107
|
* // Tries PORT, then HTTP_PORT, then SERVER_PORT (first found wins)
|
|
107
|
-
* const config = await
|
|
108
|
+
* const config = await resolveAsync(
|
|
108
109
|
* [withAliases(processEnv(), {
|
|
109
110
|
* PORT: ['PORT', 'HTTP_PORT', 'SERVER_PORT'],
|
|
110
111
|
* DATABASE_URL: ['DATABASE_URL', 'DB_URL', 'POSTGRES_URL']
|
|
111
|
-
* }), { PORT: 3000, DATABASE_URL:
|
|
112
|
+
* }), { PORT: 3000, DATABASE_URL: postgres() }]
|
|
112
113
|
* );
|
|
113
114
|
* ```
|
|
114
115
|
*/
|
|
115
|
-
|
|
116
|
+
declare function withAliases(resolver: Resolver, aliases: Record<string, string[]>): Resolver;
|
|
116
117
|
/**
|
|
117
118
|
* Wrap a resolver to apply custom transformations to values
|
|
118
119
|
* Powerful utility for parsing, coercing, or enriching config values
|
|
@@ -126,7 +127,7 @@ export declare function withAliases(resolver: Resolver, aliases: Record<string,
|
|
|
126
127
|
* import { resolve, processEnv } from 'node-env-resolver';
|
|
127
128
|
* import { withTransform } from 'node-env-resolver/utils';
|
|
128
129
|
*
|
|
129
|
-
* const config = await
|
|
130
|
+
* const config = await resolveAsync(
|
|
130
131
|
* [withTransform(processEnv(), {
|
|
131
132
|
* // Parse comma-separated values
|
|
132
133
|
* TAGS: (val) => val.split(',').map(s => s.trim()),
|
|
@@ -135,14 +136,14 @@ export declare function withAliases(resolver: Resolver, aliases: Record<string,
|
|
|
135
136
|
* // Custom parsing
|
|
136
137
|
* MAX_RETRIES: (val) => Math.min(parseInt(val, 10), 10)
|
|
137
138
|
* }), {
|
|
138
|
-
* TAGS:
|
|
139
|
-
* API_URL:
|
|
139
|
+
* TAGS: string(),
|
|
140
|
+
* API_URL: url(),
|
|
140
141
|
* MAX_RETRIES: 'number'
|
|
141
142
|
* }]
|
|
142
143
|
* );
|
|
143
144
|
* ```
|
|
144
145
|
*/
|
|
145
|
-
|
|
146
|
+
declare function withTransform(resolver: Resolver, transforms: Record<string, (value: string) => string>): Resolver;
|
|
146
147
|
/**
|
|
147
148
|
* Wrap a resolver to scope all keys to a namespace/prefix
|
|
148
149
|
* Similar to withPrefix but adds the namespace instead of removing it
|
|
@@ -158,35 +159,37 @@ export declare function withTransform(resolver: Resolver, transforms: Record<str
|
|
|
158
159
|
*
|
|
159
160
|
* // Reads DATABASE_HOST, DATABASE_PORT from env
|
|
160
161
|
* // But schema only needs HOST, PORT
|
|
161
|
-
* const dbConfig = await
|
|
162
|
+
* const dbConfig = await resolveAsync(
|
|
162
163
|
* [withNamespace(processEnv(), 'DATABASE'), {
|
|
163
|
-
* HOST:
|
|
164
|
+
* HOST: string(),
|
|
164
165
|
* PORT: 'port'
|
|
165
166
|
* }]
|
|
166
167
|
* );
|
|
167
168
|
* // dbConfig: { HOST: '...', PORT: ... }
|
|
168
169
|
* ```
|
|
169
170
|
*/
|
|
170
|
-
|
|
171
|
+
declare function withNamespace(resolver: Resolver, namespace: string): Resolver;
|
|
171
172
|
/**
|
|
172
173
|
* Watch configuration files and reload automatically
|
|
173
174
|
* Perfect for development - auto-reload when .env files change
|
|
174
175
|
*
|
|
175
|
-
* @param
|
|
176
|
+
* @param schema Environment schema definition
|
|
177
|
+
* @param resolvers Array of resolvers to use
|
|
176
178
|
* @param options Watch options
|
|
177
179
|
* @returns Function to get current config + cleanup function
|
|
178
180
|
*
|
|
179
181
|
* @example
|
|
180
182
|
* ```ts
|
|
181
183
|
* import { watch } from 'node-env-resolver/utils';
|
|
182
|
-
* import { dotenv } from 'node-env-resolver/resolvers';
|
|
184
|
+
* import { dotenv, processEnv } from 'node-env-resolver/resolvers';
|
|
183
185
|
*
|
|
184
|
-
* const { getConfig, stop } = watch(
|
|
185
|
-
*
|
|
186
|
-
*
|
|
187
|
-
*
|
|
188
|
-
*
|
|
189
|
-
*
|
|
186
|
+
* const { getConfig, stop } = watch(
|
|
187
|
+
* { PORT: 3000, API_KEY: string() },
|
|
188
|
+
* [processEnv(), dotenv()],
|
|
189
|
+
* {
|
|
190
|
+
* onChange: (config) => console.log('Config reloaded!', config)
|
|
191
|
+
* }
|
|
192
|
+
* );
|
|
190
193
|
*
|
|
191
194
|
* // Use in your app
|
|
192
195
|
* app.get('/config', () => getConfig());
|
|
@@ -195,7 +198,7 @@ export declare function withNamespace(resolver: Resolver, namespace: string): Re
|
|
|
195
198
|
* process.on('SIGINT', () => stop());
|
|
196
199
|
* ```
|
|
197
200
|
*/
|
|
198
|
-
|
|
201
|
+
declare function watch<T>(schema: Parameters<typeof resolveAsync>[0], resolvers: Parameters<typeof resolveAsync>[1], options?: {
|
|
199
202
|
/** Callback when config changes */
|
|
200
203
|
onChange?: (config: T) => void;
|
|
201
204
|
/** Debounce delay in ms (default: 100) */
|
|
@@ -206,4 +209,5 @@ export declare function watch<T>(config: Parameters<typeof import('./index').res
|
|
|
206
209
|
getConfig: () => T;
|
|
207
210
|
stop: () => void;
|
|
208
211
|
};
|
|
209
|
-
|
|
212
|
+
|
|
213
|
+
export { type CacheOptions, TTL, awsCache, cached, retry, watch, withAliases, withComputed, withNamespace, withPrefix, withTransform };
|