node-env-resolver 0.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 +148 -0
- package/dist/chunk-AST3HVOG.js +148 -0
- package/dist/chunk-UQYMDWOU.js +13 -0
- package/dist/chunk-XC5PWSNR.js +37 -0
- package/dist/esm/chunk-AST3HVOG.js +144 -0
- package/dist/esm/chunk-UQYMDWOU.js +11 -0
- package/dist/esm/chunk-XC5PWSNR.js +35 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/resolve.js +2 -0
- package/dist/esm/resolveZod.js +2 -0
- package/dist/index-6c4171f8.d.ts +27 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +20 -0
- package/dist/resolve.d.ts +6 -0
- package/dist/resolve.js +15 -0
- package/dist/resolveZod.d.ts +7 -0
- package/dist/resolveZod.js +15 -0
- package/package.json +49 -0
package/README.md
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# node-env-resolver
|
|
2
|
+
|
|
3
|
+
This package provides an easy way to resolve environment variables for Node.js applications.
|
|
4
|
+
|
|
5
|
+
All you need to do is provide the names of the environment variables you want to resolve, an object with defaults and the package will do the rest.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { resolve } from 'node-env-resolver';
|
|
9
|
+
|
|
10
|
+
// using array of environment variables
|
|
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',
|
|
21
|
+
PORT: 3000,
|
|
22
|
+
DATABASE_URL: 'postgres://localhost:5432/mydb',
|
|
23
|
+
REDIS_URL: '',
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
will both return an object with the following structure:
|
|
28
|
+
|
|
29
|
+
```json
|
|
30
|
+
{
|
|
31
|
+
"NODE_ENV": "local",
|
|
32
|
+
"PORT": 3000,
|
|
33
|
+
"DATABASE_URL": "postgres://localhost:5432/mydb",
|
|
34
|
+
"REDIS_URL": "redis://localhost:6379"
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
The library also supports Zod Schemas. If you provide a Zod Schema, the package will validate the resolved environment variables against the schema and throw an error if any of the environment variables are invalid.
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
const resolvedEnvs = await resolveZod(
|
|
42
|
+
z.object({
|
|
43
|
+
NODE_ENV: z.string().default('local'),
|
|
44
|
+
PORT: z.coerce.number().default(3000),
|
|
45
|
+
DATABASE_URL: z.string().url().default('postgres://localhost:5432/mydb'),
|
|
46
|
+
REDIS_URL: z.string().url().default('redis://localhost:6379'),
|
|
47
|
+
})
|
|
48
|
+
);
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Installation
|
|
52
|
+
|
|
53
|
+
```sh
|
|
54
|
+
npm install node-env-resolver
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Usage
|
|
58
|
+
|
|
59
|
+
By default the package will resolve the following environment variables from process.env.
|
|
60
|
+
|
|
61
|
+
You can configure and add additional resolvers in the second argument of the resolve function.
|
|
62
|
+
|
|
63
|
+
The default value of this argument is `local` which uses an in-built resolver that resolves environment variables from process.env.
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
const resolvedEnvs = await resolve(['PORT'], 'local');
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Environmental variables are resolved in the order of the resolvers provided in the second argument.
|
|
70
|
+
|
|
71
|
+
For example, if you provide `['local', 'aws-secrets']` as the second argument, the package will first resolve the environment variables from process.env and then from AWS Secrets Manager.
|
|
72
|
+
|
|
73
|
+
Values resolved from AWS Secrets Manager will override the values resolved from process.env.
|
|
74
|
+
|
|
75
|
+
### Adding a custom resolver
|
|
76
|
+
|
|
77
|
+
You can add a custom resolver by providing a function that returns a promise that resolves to an object with the resolved environment variables.
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
async function customResolver(): EnvValues {
|
|
81
|
+
return Promise.resolve({
|
|
82
|
+
FOO: 'bar',
|
|
83
|
+
QUZ: 'quux',
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const resolvedEnvs = await resolve(['FOO', 'QUZ'], customResolver);
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
which will return an object with the following structure:
|
|
91
|
+
|
|
92
|
+
```json
|
|
93
|
+
{
|
|
94
|
+
"FOO": "bar",
|
|
95
|
+
"QUZ": "quux"
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### AWS Secrets Manager
|
|
100
|
+
|
|
101
|
+
It also has in-built support for the resolving environment variables from aws-secrets-manager.
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
import { resolve } from 'node-env-resolver';
|
|
105
|
+
const resolvedEnvs = await resolve(
|
|
106
|
+
['NODE_ENV', 'PORT', 'DATABASE_URL', 'REDIS_URL'],
|
|
107
|
+
'aws-secrets'
|
|
108
|
+
);
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
In this case the package expects to use aws credentials from the environment variables `AWS_DEFAULT_REGION`, `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`.
|
|
112
|
+
|
|
113
|
+
In addition you need to provide the id of the secret in AWS Secrets Manager that contains the environment variables you want to resolve using the environmental variable `AWS_SECRET_ID`.
|
|
114
|
+
|
|
115
|
+
### Mixing and matching resolver types
|
|
116
|
+
|
|
117
|
+
You can mix and match the resolver types.
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
async function customResolver(): EnvValues {
|
|
121
|
+
return Promise.resolve({
|
|
122
|
+
FOO: 'bar',
|
|
123
|
+
QUZ: 'quux',
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const resolvedEnvs = await resolve(['FOO', 'QUZ'], ['local', customResolver]);
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
This will first resolve the environment variables from process.env and then from the custom resolver.
|
|
131
|
+
|
|
132
|
+
### Options
|
|
133
|
+
|
|
134
|
+
The third argument of the resolve function is an options object.
|
|
135
|
+
|
|
136
|
+
```ts
|
|
137
|
+
{
|
|
138
|
+
strict?: boolean;
|
|
139
|
+
};
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
By default the package will throw an error if any of the environment variables are not found.
|
|
143
|
+
|
|
144
|
+
You can disable this behaviour by setting the `strict` option to `false`.
|
|
145
|
+
|
|
146
|
+
```ts
|
|
147
|
+
const resolvedEnvs = await resolve(['FOO', 'QUZ'], 'local', { strict: false });
|
|
148
|
+
```
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var clientSecretsManager = require('@aws-sdk/client-secrets-manager');
|
|
4
|
+
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
9
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
10
|
+
var __spreadValues = (a, b) => {
|
|
11
|
+
for (var prop in b || (b = {}))
|
|
12
|
+
if (__hasOwnProp.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
if (__getOwnPropSymbols)
|
|
15
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
16
|
+
if (__propIsEnum.call(b, prop))
|
|
17
|
+
__defNormalProp(a, prop, b[prop]);
|
|
18
|
+
}
|
|
19
|
+
return a;
|
|
20
|
+
};
|
|
21
|
+
var __async = (__this, __arguments, generator) => {
|
|
22
|
+
return new Promise((resolve, reject) => {
|
|
23
|
+
var fulfilled = (value) => {
|
|
24
|
+
try {
|
|
25
|
+
step(generator.next(value));
|
|
26
|
+
} catch (e) {
|
|
27
|
+
reject(e);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
var rejected = (value) => {
|
|
31
|
+
try {
|
|
32
|
+
step(generator.throw(value));
|
|
33
|
+
} catch (e) {
|
|
34
|
+
reject(e);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
38
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// src/resolvers/aws-secrets/utils.ts
|
|
43
|
+
function mapAwsSecretConfigFromEnv() {
|
|
44
|
+
return {
|
|
45
|
+
secretId: process.env.AWS_SECRET_ID,
|
|
46
|
+
awsConfig: {
|
|
47
|
+
region: process.env.AWS_DEFAULT_REGION,
|
|
48
|
+
credentials: {
|
|
49
|
+
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
|
|
50
|
+
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
function validateAwsSecretsConfig(config) {
|
|
56
|
+
if (!config.secretId) {
|
|
57
|
+
throw new Error("AWS_SECRET_ID environment variable is missing");
|
|
58
|
+
}
|
|
59
|
+
if (!config.awsConfig.region) {
|
|
60
|
+
throw new Error("AWS_DEFAULT_REGION environment variable is missing");
|
|
61
|
+
}
|
|
62
|
+
if (!config.awsConfig.credentials.accessKeyId) {
|
|
63
|
+
throw new Error("AWS_ACCESS_KEY_ID environment variable is missing");
|
|
64
|
+
}
|
|
65
|
+
if (!config.awsConfig.credentials.secretAccessKey) {
|
|
66
|
+
throw new Error("AWS_SECRET_ACCESS_KEY environment variable is missing");
|
|
67
|
+
}
|
|
68
|
+
return config;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// src/resolvers/aws-secrets/index.ts
|
|
72
|
+
function resolveAwsSecrets(props) {
|
|
73
|
+
return __async(this, null, function* () {
|
|
74
|
+
const { secretId, awsConfig } = props;
|
|
75
|
+
const secretsManagerClient = new clientSecretsManager.SecretsManagerClient(awsConfig);
|
|
76
|
+
const getSecretCommand = new clientSecretsManager.GetSecretValueCommand({
|
|
77
|
+
SecretId: secretId
|
|
78
|
+
});
|
|
79
|
+
const { SecretString } = yield secretsManagerClient.send(getSecretCommand);
|
|
80
|
+
if (!SecretString) {
|
|
81
|
+
throw new Error("SecretString is undefined");
|
|
82
|
+
}
|
|
83
|
+
return JSON.parse(SecretString);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
function configResolver() {
|
|
87
|
+
return __async(this, null, function* () {
|
|
88
|
+
return resolveAwsSecrets(
|
|
89
|
+
validateAwsSecretsConfig(mapAwsSecretConfigFromEnv())
|
|
90
|
+
);
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// src/resolvers/local/index.ts
|
|
95
|
+
function configResolver2() {
|
|
96
|
+
return __async(this, null, function* () {
|
|
97
|
+
return Promise.resolve(process.env);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// src/lib/resolver.ts
|
|
102
|
+
var resolverLookUp = {
|
|
103
|
+
local: configResolver2,
|
|
104
|
+
"aws-secrets": configResolver
|
|
105
|
+
};
|
|
106
|
+
function resolveEnvs(resolveProps = "local") {
|
|
107
|
+
return __async(this, null, function* () {
|
|
108
|
+
const resolverFunctions = mapResolvePropsToResolverFunctions(resolveProps);
|
|
109
|
+
const resolversEnvs = yield Promise.all(
|
|
110
|
+
resolverFunctions.map((resolverFunction) => {
|
|
111
|
+
return resolverFunction();
|
|
112
|
+
})
|
|
113
|
+
);
|
|
114
|
+
return resolversEnvs.reduce((envs, resolverEnvs) => {
|
|
115
|
+
return __spreadValues(__spreadValues({}, envs), resolverEnvs);
|
|
116
|
+
}, {});
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
function lookUpResolver(name) {
|
|
120
|
+
const resolver = resolverLookUp[name];
|
|
121
|
+
if (!resolver) {
|
|
122
|
+
throw new Error(`Resolver function for "${name}" not found.`);
|
|
123
|
+
}
|
|
124
|
+
return resolver;
|
|
125
|
+
}
|
|
126
|
+
function mapResolvePropsToResolverFunctions(resolveProps) {
|
|
127
|
+
if (typeof resolveProps === "function") {
|
|
128
|
+
return [resolveProps];
|
|
129
|
+
}
|
|
130
|
+
if (typeof resolveProps === "string") {
|
|
131
|
+
return [lookUpResolver(resolveProps)];
|
|
132
|
+
}
|
|
133
|
+
return resolveProps.map((prop) => {
|
|
134
|
+
if (typeof prop === "function") {
|
|
135
|
+
return prop;
|
|
136
|
+
}
|
|
137
|
+
if (typeof prop === "string") {
|
|
138
|
+
return lookUpResolver(prop);
|
|
139
|
+
}
|
|
140
|
+
throw new Error(
|
|
141
|
+
`Expected a resolver function or string, but received: ${prop}`
|
|
142
|
+
);
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
exports.__async = __async;
|
|
147
|
+
exports.resolveAwsSecrets = resolveAwsSecrets;
|
|
148
|
+
exports.resolveEnvs = resolveEnvs;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkAST3HVOG_js = require('./chunk-AST3HVOG.js');
|
|
4
|
+
|
|
5
|
+
// src/resolveZod.ts
|
|
6
|
+
function resolveZod(schema, resolveProps = "local") {
|
|
7
|
+
return chunkAST3HVOG_js.__async(this, null, function* () {
|
|
8
|
+
const envValues = yield chunkAST3HVOG_js.resolveEnvs(resolveProps);
|
|
9
|
+
return schema.parse(envValues);
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
exports.resolveZod = resolveZod;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkAST3HVOG_js = require('./chunk-AST3HVOG.js');
|
|
4
|
+
|
|
5
|
+
// src/utils/index.ts
|
|
6
|
+
function findMissingEntries(envKeys, envsToResolveKeys) {
|
|
7
|
+
const intersect = envsToResolveKeys.filter(
|
|
8
|
+
(entry) => envKeys.includes(entry)
|
|
9
|
+
);
|
|
10
|
+
return envsToResolveKeys.filter((entry) => !intersect.includes(entry));
|
|
11
|
+
}
|
|
12
|
+
function resolveEnvValues(envValues, envsToResolve, resolveOptions) {
|
|
13
|
+
const envKeys = Array.isArray(envsToResolve) ? envsToResolve : Object.keys(envsToResolve);
|
|
14
|
+
if (resolveOptions == null ? void 0 : resolveOptions.strict) {
|
|
15
|
+
const missingEntries = findMissingEntries(envKeys, Object.keys(envValues));
|
|
16
|
+
if (missingEntries.length) {
|
|
17
|
+
const error = `Could not find value for the following envs: ${missingEntries.join(
|
|
18
|
+
", "
|
|
19
|
+
)}`;
|
|
20
|
+
throw new Error(error);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return envKeys.reduce((env, key) => {
|
|
24
|
+
env[key] = envValues[key];
|
|
25
|
+
return env;
|
|
26
|
+
}, /* @__PURE__ */ Object.create(null));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// src/resolve.ts
|
|
30
|
+
function resolve(_0) {
|
|
31
|
+
return chunkAST3HVOG_js.__async(this, arguments, function* (envsToResolve, resolveProps = "local", resolveOptions = { strict: true }) {
|
|
32
|
+
const envValues = yield chunkAST3HVOG_js.resolveEnvs(resolveProps);
|
|
33
|
+
return resolveEnvValues(envValues, envsToResolve, resolveOptions);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
exports.resolve = resolve;
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { SecretsManagerClient, GetSecretValueCommand } from '@aws-sdk/client-secrets-manager';
|
|
2
|
+
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __async = (__this, __arguments, generator) => {
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
var fulfilled = (value) => {
|
|
22
|
+
try {
|
|
23
|
+
step(generator.next(value));
|
|
24
|
+
} catch (e) {
|
|
25
|
+
reject(e);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
var rejected = (value) => {
|
|
29
|
+
try {
|
|
30
|
+
step(generator.throw(value));
|
|
31
|
+
} catch (e) {
|
|
32
|
+
reject(e);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
36
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// src/resolvers/aws-secrets/utils.ts
|
|
41
|
+
function mapAwsSecretConfigFromEnv() {
|
|
42
|
+
return {
|
|
43
|
+
secretId: process.env.AWS_SECRET_ID,
|
|
44
|
+
awsConfig: {
|
|
45
|
+
region: process.env.AWS_DEFAULT_REGION,
|
|
46
|
+
credentials: {
|
|
47
|
+
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
|
|
48
|
+
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function validateAwsSecretsConfig(config) {
|
|
54
|
+
if (!config.secretId) {
|
|
55
|
+
throw new Error("AWS_SECRET_ID environment variable is missing");
|
|
56
|
+
}
|
|
57
|
+
if (!config.awsConfig.region) {
|
|
58
|
+
throw new Error("AWS_DEFAULT_REGION environment variable is missing");
|
|
59
|
+
}
|
|
60
|
+
if (!config.awsConfig.credentials.accessKeyId) {
|
|
61
|
+
throw new Error("AWS_ACCESS_KEY_ID environment variable is missing");
|
|
62
|
+
}
|
|
63
|
+
if (!config.awsConfig.credentials.secretAccessKey) {
|
|
64
|
+
throw new Error("AWS_SECRET_ACCESS_KEY environment variable is missing");
|
|
65
|
+
}
|
|
66
|
+
return config;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// src/resolvers/aws-secrets/index.ts
|
|
70
|
+
function resolveAwsSecrets(props) {
|
|
71
|
+
return __async(this, null, function* () {
|
|
72
|
+
const { secretId, awsConfig } = props;
|
|
73
|
+
const secretsManagerClient = new SecretsManagerClient(awsConfig);
|
|
74
|
+
const getSecretCommand = new GetSecretValueCommand({
|
|
75
|
+
SecretId: secretId
|
|
76
|
+
});
|
|
77
|
+
const { SecretString } = yield secretsManagerClient.send(getSecretCommand);
|
|
78
|
+
if (!SecretString) {
|
|
79
|
+
throw new Error("SecretString is undefined");
|
|
80
|
+
}
|
|
81
|
+
return JSON.parse(SecretString);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
function configResolver() {
|
|
85
|
+
return __async(this, null, function* () {
|
|
86
|
+
return resolveAwsSecrets(
|
|
87
|
+
validateAwsSecretsConfig(mapAwsSecretConfigFromEnv())
|
|
88
|
+
);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// src/resolvers/local/index.ts
|
|
93
|
+
function configResolver2() {
|
|
94
|
+
return __async(this, null, function* () {
|
|
95
|
+
return Promise.resolve(process.env);
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// src/lib/resolver.ts
|
|
100
|
+
var resolverLookUp = {
|
|
101
|
+
local: configResolver2,
|
|
102
|
+
"aws-secrets": configResolver
|
|
103
|
+
};
|
|
104
|
+
function resolveEnvs(resolveProps = "local") {
|
|
105
|
+
return __async(this, null, function* () {
|
|
106
|
+
const resolverFunctions = mapResolvePropsToResolverFunctions(resolveProps);
|
|
107
|
+
const resolversEnvs = yield Promise.all(
|
|
108
|
+
resolverFunctions.map((resolverFunction) => {
|
|
109
|
+
return resolverFunction();
|
|
110
|
+
})
|
|
111
|
+
);
|
|
112
|
+
return resolversEnvs.reduce((envs, resolverEnvs) => {
|
|
113
|
+
return __spreadValues(__spreadValues({}, envs), resolverEnvs);
|
|
114
|
+
}, {});
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
function lookUpResolver(name) {
|
|
118
|
+
const resolver = resolverLookUp[name];
|
|
119
|
+
if (!resolver) {
|
|
120
|
+
throw new Error(`Resolver function for "${name}" not found.`);
|
|
121
|
+
}
|
|
122
|
+
return resolver;
|
|
123
|
+
}
|
|
124
|
+
function mapResolvePropsToResolverFunctions(resolveProps) {
|
|
125
|
+
if (typeof resolveProps === "function") {
|
|
126
|
+
return [resolveProps];
|
|
127
|
+
}
|
|
128
|
+
if (typeof resolveProps === "string") {
|
|
129
|
+
return [lookUpResolver(resolveProps)];
|
|
130
|
+
}
|
|
131
|
+
return resolveProps.map((prop) => {
|
|
132
|
+
if (typeof prop === "function") {
|
|
133
|
+
return prop;
|
|
134
|
+
}
|
|
135
|
+
if (typeof prop === "string") {
|
|
136
|
+
return lookUpResolver(prop);
|
|
137
|
+
}
|
|
138
|
+
throw new Error(
|
|
139
|
+
`Expected a resolver function or string, but received: ${prop}`
|
|
140
|
+
);
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export { __async, resolveAwsSecrets, resolveEnvs };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { __async, resolveEnvs } from './chunk-AST3HVOG.js';
|
|
2
|
+
|
|
3
|
+
// src/resolveZod.ts
|
|
4
|
+
function resolveZod(schema, resolveProps = "local") {
|
|
5
|
+
return __async(this, null, function* () {
|
|
6
|
+
const envValues = yield resolveEnvs(resolveProps);
|
|
7
|
+
return schema.parse(envValues);
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export { resolveZod };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { __async, resolveEnvs } from './chunk-AST3HVOG.js';
|
|
2
|
+
|
|
3
|
+
// src/utils/index.ts
|
|
4
|
+
function findMissingEntries(envKeys, envsToResolveKeys) {
|
|
5
|
+
const intersect = envsToResolveKeys.filter(
|
|
6
|
+
(entry) => envKeys.includes(entry)
|
|
7
|
+
);
|
|
8
|
+
return envsToResolveKeys.filter((entry) => !intersect.includes(entry));
|
|
9
|
+
}
|
|
10
|
+
function resolveEnvValues(envValues, envsToResolve, resolveOptions) {
|
|
11
|
+
const envKeys = Array.isArray(envsToResolve) ? envsToResolve : Object.keys(envsToResolve);
|
|
12
|
+
if (resolveOptions == null ? void 0 : resolveOptions.strict) {
|
|
13
|
+
const missingEntries = findMissingEntries(envKeys, Object.keys(envValues));
|
|
14
|
+
if (missingEntries.length) {
|
|
15
|
+
const error = `Could not find value for the following envs: ${missingEntries.join(
|
|
16
|
+
", "
|
|
17
|
+
)}`;
|
|
18
|
+
throw new Error(error);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return envKeys.reduce((env, key) => {
|
|
22
|
+
env[key] = envValues[key];
|
|
23
|
+
return env;
|
|
24
|
+
}, /* @__PURE__ */ Object.create(null));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// src/resolve.ts
|
|
28
|
+
function resolve(_0) {
|
|
29
|
+
return __async(this, arguments, function* (envsToResolve, resolveProps = "local", resolveOptions = { strict: true }) {
|
|
30
|
+
const envValues = yield resolveEnvs(resolveProps);
|
|
31
|
+
return resolveEnvValues(envValues, envsToResolve, resolveOptions);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export { resolve };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
interface ResolverFunction {
|
|
2
|
+
(): Promise<EnvValues>;
|
|
3
|
+
}
|
|
4
|
+
type EnvValues = {
|
|
5
|
+
[key: string]: string | undefined;
|
|
6
|
+
};
|
|
7
|
+
type ResolveOptions = {
|
|
8
|
+
strict?: boolean;
|
|
9
|
+
};
|
|
10
|
+
type EnvsToResolve = string[] | EnvValues;
|
|
11
|
+
type ResolveProps = string | ResolverFunction | (string | ResolverFunction)[];
|
|
12
|
+
|
|
13
|
+
type AwsConfig = {
|
|
14
|
+
region: string;
|
|
15
|
+
credentials: {
|
|
16
|
+
accessKeyId: string;
|
|
17
|
+
secretAccessKey: string;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
type ResolveAwsSecretsProps = {
|
|
21
|
+
secretId: string;
|
|
22
|
+
awsConfig: AwsConfig;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
declare function resolveAwsSecrets(props: ResolveAwsSecretsProps): Promise<EnvValues>;
|
|
26
|
+
|
|
27
|
+
export { EnvValues as E, ResolverFunction as R, ResolveOptions as a, EnvsToResolve as b, ResolveProps as c, resolveAwsSecrets as r };
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkXC5PWSNR_js = require('./chunk-XC5PWSNR.js');
|
|
4
|
+
var chunkUQYMDWOU_js = require('./chunk-UQYMDWOU.js');
|
|
5
|
+
var chunkAST3HVOG_js = require('./chunk-AST3HVOG.js');
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
Object.defineProperty(exports, 'resolve', {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
get: function () { return chunkXC5PWSNR_js.resolve; }
|
|
12
|
+
});
|
|
13
|
+
Object.defineProperty(exports, 'resolveZod', {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
get: function () { return chunkUQYMDWOU_js.resolveZod; }
|
|
16
|
+
});
|
|
17
|
+
Object.defineProperty(exports, 'resolveAwsSecrets', {
|
|
18
|
+
enumerable: true,
|
|
19
|
+
get: function () { return chunkAST3HVOG_js.resolveAwsSecrets; }
|
|
20
|
+
});
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { b as EnvsToResolve, c as ResolveProps, a as ResolveOptions, E as EnvValues } from './index-6c4171f8.js';
|
|
2
|
+
export { R as ResolverFunction, r as resolveAwsSecrets } from './index-6c4171f8.js';
|
|
3
|
+
|
|
4
|
+
declare function resolve(envsToResolve: EnvsToResolve, resolveProps?: ResolveProps, resolveOptions?: ResolveOptions): Promise<EnvValues>;
|
|
5
|
+
|
|
6
|
+
export { EnvValues, EnvsToResolve, ResolveOptions, ResolveProps, resolve };
|
package/dist/resolve.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkXC5PWSNR_js = require('./chunk-XC5PWSNR.js');
|
|
4
|
+
var chunkAST3HVOG_js = require('./chunk-AST3HVOG.js');
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
Object.defineProperty(exports, 'resolve', {
|
|
9
|
+
enumerable: true,
|
|
10
|
+
get: function () { return chunkXC5PWSNR_js.resolve; }
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(exports, 'resolveAwsSecrets', {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () { return chunkAST3HVOG_js.resolveAwsSecrets; }
|
|
15
|
+
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { c as ResolveProps } from './index-6c4171f8.js';
|
|
3
|
+
export { E as EnvValues, b as EnvsToResolve, a as ResolveOptions, R as ResolverFunction, r as resolveAwsSecrets } from './index-6c4171f8.js';
|
|
4
|
+
|
|
5
|
+
declare function resolveZod<T>(schema: z.ZodSchema<T>, resolveProps?: ResolveProps): Promise<T>;
|
|
6
|
+
|
|
7
|
+
export { ResolveProps, resolveZod };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkUQYMDWOU_js = require('./chunk-UQYMDWOU.js');
|
|
4
|
+
var chunkAST3HVOG_js = require('./chunk-AST3HVOG.js');
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
Object.defineProperty(exports, 'resolveZod', {
|
|
9
|
+
enumerable: true,
|
|
10
|
+
get: function () { return chunkUQYMDWOU_js.resolveZod; }
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(exports, 'resolveAwsSecrets', {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () { return chunkAST3HVOG_js.resolveAwsSecrets; }
|
|
15
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "node-env-resolver",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Resolves environment variables for Node.js applications",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"keywords": [
|
|
12
|
+
"node",
|
|
13
|
+
"aws-secrets",
|
|
14
|
+
"local-env"
|
|
15
|
+
],
|
|
16
|
+
"author": {
|
|
17
|
+
"name": "Jagvinder Singh Reehal",
|
|
18
|
+
"email": "jag@jagreehal.com",
|
|
19
|
+
"url": "https://jagreehal.com"
|
|
20
|
+
},
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@aws-sdk/client-secrets-manager": "^3.259.0",
|
|
24
|
+
"zod": "^3.20.2"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/jest": "^29.4.0",
|
|
28
|
+
"@types/node": "^18.11.18",
|
|
29
|
+
"@changesets/cli": "^2.26.0",
|
|
30
|
+
"@typescript-eslint/eslint-plugin": "^5.49.0",
|
|
31
|
+
"@typescript-eslint/parser": "^5.49.0",
|
|
32
|
+
"eslint": "^8.33.0",
|
|
33
|
+
"eslint-config-prettier": "^8.6.0",
|
|
34
|
+
"jest": "^29.4.1",
|
|
35
|
+
"prettier": "^2.8.3",
|
|
36
|
+
"ts-jest": "^29.0.5",
|
|
37
|
+
"tsup": "^6.5.0",
|
|
38
|
+
"typescript": "^4.9.4"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "tsup",
|
|
42
|
+
"changeset": "changeset",
|
|
43
|
+
"format": "prettier --ignore-path .gitignore --write \"**/*.+(js|ts|json)\"",
|
|
44
|
+
"lint": "eslint --ext .js,.ts ./src",
|
|
45
|
+
"prerelease": "npm run lint && npm run build && npm run test",
|
|
46
|
+
"release": "changeset publish",
|
|
47
|
+
"test": "jest --coverage"
|
|
48
|
+
}
|
|
49
|
+
}
|