node-env-resolver-aws 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 +268 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.js +91 -0
- package/dist/index.js.map +1 -0
- package/package.json +68 -0
package/README.md
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
# node-env-resolver/aws
|
|
2
|
+
|
|
3
|
+
AWS integration for node-env-resolver with Secrets Manager and SSM Parameter Store support.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/node-env-resolver/aws)
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install node-env-resolver/aws
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick start
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { resolve } from 'node-env-resolver';
|
|
17
|
+
import { awsSecrets, awsSsm } from 'node-env-resolver-aws';
|
|
18
|
+
|
|
19
|
+
const config = await resolve({
|
|
20
|
+
DATABASE_URL: 'url',
|
|
21
|
+
API_KEY: 'string',
|
|
22
|
+
}, {
|
|
23
|
+
extend: [
|
|
24
|
+
awsSecrets({ secretId: 'myapp/production/secrets' }),
|
|
25
|
+
awsSsm({ path: '/myapp/config' })
|
|
26
|
+
]
|
|
27
|
+
});
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Features
|
|
31
|
+
|
|
32
|
+
- Load secrets from AWS Secrets Manager
|
|
33
|
+
- Load parameters from SSM Parameter Store
|
|
34
|
+
- Optional TTL caching
|
|
35
|
+
- Full TypeScript support
|
|
36
|
+
|
|
37
|
+
## AWS Secrets Manager
|
|
38
|
+
|
|
39
|
+
Load JSON secrets from Secrets Manager:
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { awsSecrets } from 'node-env-resolver-aws';
|
|
43
|
+
|
|
44
|
+
const config = await resolve({
|
|
45
|
+
DATABASE_URL: 'url',
|
|
46
|
+
API_KEY: 'string'
|
|
47
|
+
}, {
|
|
48
|
+
extend: [
|
|
49
|
+
awsSecrets({ secretId: 'myapp/secrets' })
|
|
50
|
+
]
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
With options:
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
awsSecrets({
|
|
58
|
+
secretId: 'myapp/production/database',
|
|
59
|
+
region: 'us-east-1',
|
|
60
|
+
parseJson: true // Default: true
|
|
61
|
+
})
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## SSM Parameter Store
|
|
65
|
+
|
|
66
|
+
Load parameters from Parameter Store:
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
import { awsSsm } from 'node-env-resolver-aws';
|
|
70
|
+
|
|
71
|
+
const config = await resolve({
|
|
72
|
+
API_ENDPOINT: 'url',
|
|
73
|
+
TIMEOUT: 30
|
|
74
|
+
}, {
|
|
75
|
+
extend: [
|
|
76
|
+
awsSsm({ path: '/myapp/config' })
|
|
77
|
+
]
|
|
78
|
+
});
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Get all parameters under a path:
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
awsSsm({
|
|
85
|
+
path: '/myapp/production',
|
|
86
|
+
region: 'us-west-2',
|
|
87
|
+
recursive: true
|
|
88
|
+
})
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Caching
|
|
92
|
+
|
|
93
|
+
Add TTL caching to reduce AWS API calls:
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
import { resolve, cached, TTL } from 'node-env-resolver';
|
|
97
|
+
import { awsSecrets } from 'node-env-resolver-aws';
|
|
98
|
+
|
|
99
|
+
const config = await resolve({
|
|
100
|
+
DATABASE_URL: 'url'
|
|
101
|
+
}, {
|
|
102
|
+
extend: [
|
|
103
|
+
cached(
|
|
104
|
+
awsSecrets({ secretId: 'myapp/secrets' }),
|
|
105
|
+
{
|
|
106
|
+
ttl: TTL.minutes5,
|
|
107
|
+
maxAge: TTL.hour,
|
|
108
|
+
staleWhileRevalidate: true
|
|
109
|
+
}
|
|
110
|
+
)
|
|
111
|
+
]
|
|
112
|
+
});
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## AWS Permissions
|
|
116
|
+
|
|
117
|
+
### Secrets Manager
|
|
118
|
+
|
|
119
|
+
```json
|
|
120
|
+
{
|
|
121
|
+
"Version": "2012-10-17",
|
|
122
|
+
"Statement": [
|
|
123
|
+
{
|
|
124
|
+
"Effect": "Allow",
|
|
125
|
+
"Action": ["secretsmanager:GetSecretValue"],
|
|
126
|
+
"Resource": ["arn:aws:secretsmanager:region:account:secret:myapp/secrets-*"]
|
|
127
|
+
}
|
|
128
|
+
]
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### SSM Parameter Store
|
|
133
|
+
|
|
134
|
+
```json
|
|
135
|
+
{
|
|
136
|
+
"Version": "2012-10-17",
|
|
137
|
+
"Statement": [
|
|
138
|
+
{
|
|
139
|
+
"Effect": "Allow",
|
|
140
|
+
"Action": [
|
|
141
|
+
"ssm:GetParameter",
|
|
142
|
+
"ssm:GetParameters",
|
|
143
|
+
"ssm:GetParametersByPath"
|
|
144
|
+
],
|
|
145
|
+
"Resource": ["arn:aws:ssm:region:account:parameter/myapp/*"]
|
|
146
|
+
}
|
|
147
|
+
]
|
|
148
|
+
}
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## Configuration
|
|
152
|
+
|
|
153
|
+
### awsSecrets options
|
|
154
|
+
|
|
155
|
+
```ts
|
|
156
|
+
interface AwsSecretsOptions {
|
|
157
|
+
secretId: string; // Secret ID or ARN
|
|
158
|
+
region?: string; // AWS region (default: AWS_REGION env var or 'us-east-1')
|
|
159
|
+
accessKeyId?: string; // AWS access key (optional)
|
|
160
|
+
secretAccessKey?: string; // AWS secret key (optional)
|
|
161
|
+
parseJson?: boolean; // Parse JSON secrets (default: true)
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### awsSsm options
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
interface AwsSsmOptions {
|
|
169
|
+
path: string; // Parameter path
|
|
170
|
+
region?: string; // AWS region (default: AWS_REGION env var or 'us-east-1')
|
|
171
|
+
accessKeyId?: string; // AWS access key (optional)
|
|
172
|
+
secretAccessKey?: string; // AWS secret key (optional)
|
|
173
|
+
recursive?: boolean; // Get all parameters under path (default: false)
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## Examples
|
|
178
|
+
|
|
179
|
+
### Production app
|
|
180
|
+
|
|
181
|
+
```ts
|
|
182
|
+
import { resolve } from 'node-env-resolver';
|
|
183
|
+
import { awsSecrets, awsSsm } from 'node-env-resolver-aws';
|
|
184
|
+
|
|
185
|
+
const config = await resolve({
|
|
186
|
+
NODE_ENV: ['development', 'production'] as const,
|
|
187
|
+
PORT: 3000,
|
|
188
|
+
DATABASE_URL: 'url',
|
|
189
|
+
JWT_SECRET: 'string',
|
|
190
|
+
}, {
|
|
191
|
+
extend: [
|
|
192
|
+
awsSecrets({ secretId: 'myapp/production/secrets' }),
|
|
193
|
+
awsSsm({ path: '/myapp/production/config' })
|
|
194
|
+
]
|
|
195
|
+
});
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### Lambda function
|
|
199
|
+
|
|
200
|
+
```ts
|
|
201
|
+
import { resolve } from 'node-env-resolver';
|
|
202
|
+
import { awsSecrets } from 'node-env-resolver-aws';
|
|
203
|
+
|
|
204
|
+
const config = await resolve({
|
|
205
|
+
API_ENDPOINT: 'url',
|
|
206
|
+
TIMEOUT: 30,
|
|
207
|
+
}, {
|
|
208
|
+
extend: [
|
|
209
|
+
awsSecrets({ secretId: 'lambda/secrets' })
|
|
210
|
+
]
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
export const handler = async (event) => {
|
|
214
|
+
// Use config
|
|
215
|
+
};
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
### Development vs production
|
|
219
|
+
|
|
220
|
+
```ts
|
|
221
|
+
import { resolve } from 'node-env-resolver';
|
|
222
|
+
import { awsSecrets } from 'node-env-resolver-aws';
|
|
223
|
+
|
|
224
|
+
const isProduction = process.env.NODE_ENV === 'production';
|
|
225
|
+
|
|
226
|
+
const config = await resolve({
|
|
227
|
+
DATABASE_URL: 'url',
|
|
228
|
+
API_KEY: 'string'
|
|
229
|
+
}, {
|
|
230
|
+
extend: isProduction ? [
|
|
231
|
+
awsSecrets({ secretId: 'myapp/production/secrets' })
|
|
232
|
+
] : []
|
|
233
|
+
});
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
## Error handling
|
|
237
|
+
|
|
238
|
+
```ts
|
|
239
|
+
try {
|
|
240
|
+
const config = await resolve({
|
|
241
|
+
DATABASE_URL: 'url'
|
|
242
|
+
}, {
|
|
243
|
+
extend: [awsSecrets({ secretId: 'myapp/secrets' })]
|
|
244
|
+
});
|
|
245
|
+
} catch (error) {
|
|
246
|
+
if (error.message.includes('AWS Secrets Manager')) {
|
|
247
|
+
console.error('Failed to load secrets from AWS');
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
## Troubleshooting
|
|
253
|
+
|
|
254
|
+
**Permission denied**
|
|
255
|
+
- Check IAM permissions match examples above
|
|
256
|
+
- Verify resource ARNs are correct
|
|
257
|
+
|
|
258
|
+
**Secret not found**
|
|
259
|
+
- Verify secret ID or path exists
|
|
260
|
+
- Check region matches where secret is stored
|
|
261
|
+
|
|
262
|
+
**Network timeout**
|
|
263
|
+
- Check VPC/security group configuration
|
|
264
|
+
- Verify internet/NAT gateway access
|
|
265
|
+
|
|
266
|
+
## License
|
|
267
|
+
|
|
268
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Resolver } from 'node-env-resolver';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* AWS resolvers for node-env-resolver
|
|
5
|
+
* Supports AWS Secrets Manager and SSM Parameter Store
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
interface AwsSecretsOptions {
|
|
9
|
+
secretId: string;
|
|
10
|
+
region?: string;
|
|
11
|
+
accessKeyId?: string;
|
|
12
|
+
secretAccessKey?: string;
|
|
13
|
+
parseJson?: boolean;
|
|
14
|
+
/** Enable caching with TTL (recommended for production) */
|
|
15
|
+
cache?: boolean | {
|
|
16
|
+
ttl?: number;
|
|
17
|
+
maxAge?: number;
|
|
18
|
+
staleWhileRevalidate?: boolean;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
declare function awsSecrets(options: AwsSecretsOptions): Resolver;
|
|
22
|
+
interface AwsSsmOptions {
|
|
23
|
+
path: string;
|
|
24
|
+
recursive?: boolean;
|
|
25
|
+
region?: string;
|
|
26
|
+
accessKeyId?: string;
|
|
27
|
+
secretAccessKey?: string;
|
|
28
|
+
/** Enable caching with TTL (recommended for production) */
|
|
29
|
+
cache?: boolean | {
|
|
30
|
+
ttl?: number;
|
|
31
|
+
maxAge?: number;
|
|
32
|
+
staleWhileRevalidate?: boolean;
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
declare function awsSsm(options: AwsSsmOptions): Resolver;
|
|
36
|
+
|
|
37
|
+
export { type AwsSecretsOptions, type AwsSsmOptions, awsSecrets, awsSsm };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { SecretsManagerClient, GetSecretValueCommand } from '@aws-sdk/client-secrets-manager';
|
|
2
|
+
import { SSMClient, GetParametersByPathCommand, GetParameterCommand } from '@aws-sdk/client-ssm';
|
|
3
|
+
|
|
4
|
+
function awsSecrets(options) {
|
|
5
|
+
return {
|
|
6
|
+
name: `aws-secrets(${options.secretId})`,
|
|
7
|
+
async load() {
|
|
8
|
+
try {
|
|
9
|
+
const client = new SecretsManagerClient({
|
|
10
|
+
region: options.region || process.env.AWS_REGION || "us-east-1",
|
|
11
|
+
credentials: options.accessKeyId && options.secretAccessKey ? {
|
|
12
|
+
accessKeyId: options.accessKeyId,
|
|
13
|
+
secretAccessKey: options.secretAccessKey
|
|
14
|
+
} : void 0
|
|
15
|
+
});
|
|
16
|
+
const command = new GetSecretValueCommand({
|
|
17
|
+
SecretId: options.secretId
|
|
18
|
+
});
|
|
19
|
+
const response = await client.send(command);
|
|
20
|
+
if (!response.SecretString) {
|
|
21
|
+
throw new Error("Secret not found or empty");
|
|
22
|
+
}
|
|
23
|
+
if (options.parseJson !== false) {
|
|
24
|
+
try {
|
|
25
|
+
return JSON.parse(response.SecretString);
|
|
26
|
+
} catch {
|
|
27
|
+
return { [options.secretId]: response.SecretString };
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return { [options.secretId]: response.SecretString };
|
|
31
|
+
} catch (error) {
|
|
32
|
+
throw new Error(`AWS Secrets Manager: ${error instanceof Error ? error.message : error}`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function awsSsm(options) {
|
|
38
|
+
return {
|
|
39
|
+
name: `aws-ssm(${options.path})`,
|
|
40
|
+
async load() {
|
|
41
|
+
try {
|
|
42
|
+
const client = new SSMClient({
|
|
43
|
+
region: options.region || process.env.AWS_REGION || "us-east-1",
|
|
44
|
+
credentials: options.accessKeyId && options.secretAccessKey ? {
|
|
45
|
+
accessKeyId: options.accessKeyId,
|
|
46
|
+
secretAccessKey: options.secretAccessKey
|
|
47
|
+
} : void 0
|
|
48
|
+
});
|
|
49
|
+
const env = {};
|
|
50
|
+
if (options.recursive) {
|
|
51
|
+
let nextToken;
|
|
52
|
+
do {
|
|
53
|
+
const command = new GetParametersByPathCommand({
|
|
54
|
+
Path: options.path,
|
|
55
|
+
Recursive: true,
|
|
56
|
+
WithDecryption: true,
|
|
57
|
+
NextToken: nextToken
|
|
58
|
+
});
|
|
59
|
+
const response = await client.send(command);
|
|
60
|
+
if (response.Parameters) {
|
|
61
|
+
for (const param of response.Parameters) {
|
|
62
|
+
if (param.Name && param.Value) {
|
|
63
|
+
const key = param.Name.split("/").pop();
|
|
64
|
+
env[key] = param.Value;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
nextToken = response.NextToken;
|
|
69
|
+
} while (nextToken);
|
|
70
|
+
} else {
|
|
71
|
+
const command = new GetParameterCommand({
|
|
72
|
+
Name: options.path,
|
|
73
|
+
WithDecryption: true
|
|
74
|
+
});
|
|
75
|
+
const response = await client.send(command);
|
|
76
|
+
if (response.Parameter?.Value) {
|
|
77
|
+
const key = options.path.split("/").pop();
|
|
78
|
+
env[key] = response.Parameter.Value;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return env;
|
|
82
|
+
} catch (error) {
|
|
83
|
+
throw new Error(`AWS SSM: ${error instanceof Error ? error.message : error}`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export { awsSecrets, awsSsm };
|
|
90
|
+
//# sourceMappingURL=index.js.map
|
|
91
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;AAoBO,SAAS,WAAW,OAAA,EAAsC;AAC/D,EAAA,OAAO;AAAA,IACL,IAAA,EAAM,CAAA,YAAA,EAAe,OAAA,CAAQ,QAAQ,CAAA,CAAA,CAAA;AAAA,IACrC,MAAM,IAAA,GAAO;AACX,MAAA,IAAI;AACF,QAAA,MAAM,MAAA,GAAS,IAAI,oBAAA,CAAqB;AAAA,UACtC,MAAA,EAAQ,OAAA,CAAQ,MAAA,IAAU,OAAA,CAAQ,IAAI,UAAA,IAAc,WAAA;AAAA,UACpD,WAAA,EAAa,OAAA,CAAQ,WAAA,IAAe,OAAA,CAAQ,eAAA,GAAkB;AAAA,YAC5D,aAAa,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,MAAM,OAAA,CAAQ,IAAA,CAAK,KAAA,CAAM,GAAG,EAAE,GAAA,EAAI;AACxC,YAAA,GAAA,CAAI,GAAG,CAAA,GAAI,QAAA,CAAS,SAAA,CAAU,KAAA;AAAA,UAChC;AAAA,QACF;AAEA,QAAA,OAAO,GAAA;AAAA,MACT,SAAS,KAAA,EAAO;AACd,QAAA,MAAM,IAAI,MAAM,CAAA,SAAA,EAAY,KAAA,YAAiB,QAAQ,KAAA,CAAM,OAAA,GAAU,KAAK,CAAA,CAAE,CAAA;AAAA,MAC9E;AAAA,IACF;AAAA,GACF;AACF","file":"index.js","sourcesContent":["/**\n * AWS resolvers for node-env-resolver\n * Supports AWS Secrets Manager and SSM Parameter Store\n */\n\nimport { SecretsManagerClient, GetSecretValueCommand } from '@aws-sdk/client-secrets-manager';\nimport { SSMClient, GetParametersByPathCommand, GetParameterCommand } from '@aws-sdk/client-ssm';\nimport type { Resolver } from 'node-env-resolver';\n\n// AWS Secrets Manager\nexport interface AwsSecretsOptions {\n secretId: string;\n region?: string;\n accessKeyId?: string;\n secretAccessKey?: string;\n parseJson?: boolean;\n /** Enable caching with TTL (recommended for production) */\n cache?: boolean | { ttl?: number; maxAge?: number; staleWhileRevalidate?: boolean };\n}\n\nexport function awsSecrets(options: AwsSecretsOptions): Resolver {\n return {\n name: `aws-secrets(${options.secretId})`,\n async load() {\n try {\n const client = new SecretsManagerClient({\n region: options.region || process.env.AWS_REGION || 'us-east-1',\n credentials: options.accessKeyId && options.secretAccessKey ? {\n accessKeyId: options.accessKeyId,\n secretAccessKey: options.secretAccessKey,\n } : undefined,\n });\n \n const command = new GetSecretValueCommand({\n SecretId: options.secretId,\n });\n \n const response = await client.send(command);\n \n if (!response.SecretString) {\n throw new Error('Secret not found or empty');\n }\n \n if (options.parseJson !== false) {\n try {\n return JSON.parse(response.SecretString);\n } catch {\n // If not valid JSON, return as single key-value\n return { [options.secretId]: response.SecretString };\n }\n }\n \n return { [options.secretId]: response.SecretString };\n } catch (error) {\n throw new Error(`AWS Secrets Manager: ${error instanceof Error ? error.message : error}`);\n }\n },\n };\n}\n\n// AWS Systems Manager Parameter Store\nexport interface AwsSsmOptions {\n path: string;\n recursive?: boolean;\n region?: string;\n accessKeyId?: string;\n secretAccessKey?: string;\n /** Enable caching with TTL (recommended for production) */\n cache?: boolean | { ttl?: number; maxAge?: number; staleWhileRevalidate?: boolean };\n}\n\nexport function awsSsm(options: AwsSsmOptions): Resolver {\n return {\n name: `aws-ssm(${options.path})`,\n async load() {\n try {\n const client = new SSMClient({\n region: options.region || process.env.AWS_REGION || 'us-east-1',\n credentials: options.accessKeyId && options.secretAccessKey ? {\n accessKeyId: options.accessKeyId,\n secretAccessKey: options.secretAccessKey,\n } : undefined,\n });\n \n const env: Record<string, string> = {};\n \n if (options.recursive) {\n // Get all parameters under path\n let nextToken: string | undefined;\n \n do {\n const command = new GetParametersByPathCommand({\n Path: options.path,\n Recursive: true,\n WithDecryption: true,\n NextToken: nextToken,\n });\n \n const response = await client.send(command);\n \n if (response.Parameters) {\n for (const param of response.Parameters) {\n if (param.Name && param.Value) {\n // Convert /app/prod/DATABASE_URL to DATABASE_URL\n const key = param.Name.split('/').pop()!;\n env[key] = param.Value;\n }\n }\n }\n \n nextToken = response.NextToken;\n } while (nextToken);\n } else {\n // Get single parameter\n const command = new GetParameterCommand({\n Name: options.path,\n WithDecryption: true,\n });\n \n const response = await client.send(command);\n \n if (response.Parameter?.Value) {\n const key = options.path.split('/').pop()!;\n env[key] = response.Parameter.Value;\n }\n }\n \n return env;\n } catch (error) {\n throw new Error(`AWS SSM: ${error instanceof Error ? error.message : error}`);\n }\n },\n };\n}"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "node-env-resolver-aws",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "AWS resolvers for node-env-resolver (Secrets Manager, SSM Parameter Store)",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"README.md"
|
|
17
|
+
],
|
|
18
|
+
"keywords": [
|
|
19
|
+
"aws",
|
|
20
|
+
"secrets-manager",
|
|
21
|
+
"ssm",
|
|
22
|
+
"parameter-store",
|
|
23
|
+
"environment",
|
|
24
|
+
"env",
|
|
25
|
+
"config",
|
|
26
|
+
"node-env-resolver"
|
|
27
|
+
],
|
|
28
|
+
"author": {
|
|
29
|
+
"name": "Jagvinder Singh Reehal",
|
|
30
|
+
"email": "jag@jagreehal.com",
|
|
31
|
+
"url": "https://jagreehal.com"
|
|
32
|
+
},
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"sideEffects": false,
|
|
35
|
+
"engines": {
|
|
36
|
+
"node": ">=18"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@aws-sdk/client-secrets-manager": "^3.901.0",
|
|
40
|
+
"@aws-sdk/client-ssm": "^3.901.0"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"node-env-resolver": "^1.0.0"
|
|
44
|
+
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@eslint/js": "^9.37.0",
|
|
50
|
+
"@types/node": "^24.7.0",
|
|
51
|
+
"@typescript-eslint/eslint-plugin": "^8.46.0",
|
|
52
|
+
"@typescript-eslint/parser": "^8.46.0",
|
|
53
|
+
"eslint": "^9.37.0",
|
|
54
|
+
"eslint-config-prettier": "^10.1.8",
|
|
55
|
+
"prettier": "^3.6.2",
|
|
56
|
+
"tsup": "^8.5.0",
|
|
57
|
+
"typescript": "^5.9.3",
|
|
58
|
+
"vitest": "^3.2.4",
|
|
59
|
+
"node-env-resolver": "1.0.0"
|
|
60
|
+
},
|
|
61
|
+
"scripts": {
|
|
62
|
+
"build": "tsup",
|
|
63
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
64
|
+
"lint": "eslint src/",
|
|
65
|
+
"test": "vitest run",
|
|
66
|
+
"format": "prettier --write \"**/*.{ts,tsx,md}\""
|
|
67
|
+
}
|
|
68
|
+
}
|