@middy/secrets-manager 2.5.2 → 2.5.6

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.
Files changed (3) hide show
  1. package/index.d.ts +2 -13
  2. package/index.js +38 -52
  3. package/package.json +4 -4
package/index.d.ts CHANGED
@@ -1,19 +1,8 @@
1
1
  import { SecretsManager } from 'aws-sdk'
2
- import { captureAWSClient } from 'aws-xray-sdk'
3
2
  import middy from '@middy/core'
3
+ import { Options as MiddyOptions } from '@middy/util'
4
4
 
5
- interface Options<SM = SecretsManager> {
6
- AwsClient?: new() => SM
7
- awsClientOptions?: Partial<SecretsManager.Types.ClientConfiguration>
8
- awsClientAssumeRole?: string
9
- awsClientCapture?: typeof captureAWSClient
10
- fetchData?: { [key: string]: string }
11
- disablePrefetch?: boolean
12
- cacheKey?: string
13
- cacheExpiry?: number
14
- setToEnv?: boolean
15
- setToContext?: boolean
16
- }
5
+ interface Options<SM = SecretsManager> extends MiddyOptions<SM, SecretsManager.Types.ClientConfiguration> {}
17
6
 
18
7
  declare function secretsManager (options?: Options): middy.MiddlewareObj
19
8
 
package/index.js CHANGED
@@ -1,5 +1,3 @@
1
- "use strict";
2
-
3
1
  const {
4
2
  canPrefetch,
5
3
  createPrefetchClient,
@@ -9,87 +7,75 @@ const {
9
7
  modifyCache,
10
8
  jsonSafeParse,
11
9
  getInternal
12
- } = require('@middy/util');
13
-
14
- const SecretsManager = require('aws-sdk/clients/secretsmanager'); // v2
10
+ } = require('@middy/util')
11
+ const SecretsManager = require('aws-sdk/clients/secretsmanager') // v2
15
12
  // const { SecretsManager } = require('@aws-sdk/client-secrets-manager') // v3
16
13
 
17
-
18
14
  const defaults = {
19
15
  AwsClient: SecretsManager,
20
16
  awsClientOptions: {},
21
17
  awsClientAssumeRole: undefined,
22
18
  awsClientCapture: undefined,
23
- fetchData: {},
24
- // If more than 2, consider writing own using ListSecrets
19
+ fetchData: {}, // If more than 2, consider writing own using ListSecrets
25
20
  disablePrefetch: false,
26
21
  cacheKey: 'secrets-manager',
27
22
  cacheExpiry: -1,
28
- setToEnv: false,
29
- // can return object when requesting db credentials, cannot set to process.env
23
+ setToEnv: false, // can return object when requesting db credentials, cannot set to process.env
30
24
  setToContext: false
31
- };
25
+ }
32
26
 
33
27
  const secretsManagerMiddleware = (opts = {}) => {
34
- const options = { ...defaults,
35
- ...opts
36
- };
28
+ const options = { ...defaults, ...opts }
37
29
 
38
30
  const fetch = (request, cachedValues = {}) => {
39
- const values = {}; // Multiple secrets can be requested in a single requests,
31
+ const values = {}
32
+
33
+ // Multiple secrets can be requested in a single requests,
40
34
  // however this is likely uncommon IRL, increases complexity to handle,
41
35
  // and will require recursive promise resolution impacting performance.
42
36
  // See https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SecretsManager.html#listSecrets-property
43
-
44
37
  for (const internalKey of Object.keys(options.fetchData)) {
45
- if (cachedValues[internalKey]) continue;
46
- values[internalKey] = client.getSecretValue({
47
- SecretId: options.fetchData[internalKey]
48
- }).promise() // Required for aws-sdk v2
49
- .then(resp => jsonSafeParse(resp.SecretString)).catch(e => {
50
- var _getCache$value, _getCache;
51
-
52
- const value = (_getCache$value = (_getCache = getCache(options.cacheKey)) === null || _getCache === void 0 ? void 0 : _getCache.value) !== null && _getCache$value !== void 0 ? _getCache$value : {};
53
- value[internalKey] = undefined;
54
- modifyCache(options.cacheKey, value);
55
- throw e;
56
- });
38
+ if (cachedValues[internalKey]) continue
39
+ values[internalKey] = client
40
+ .getSecretValue({ SecretId: options.fetchData[internalKey] })
41
+ .promise() // Required for aws-sdk v2
42
+ .then((resp) => jsonSafeParse(resp.SecretString))
43
+ .catch((e) => {
44
+ const value = getCache(options.cacheKey)?.value ?? {}
45
+ value[internalKey] = undefined
46
+ modifyCache(options.cacheKey, value)
47
+ throw e
48
+ })
57
49
  }
50
+ return values
51
+ }
58
52
 
59
- return values;
60
- };
61
-
62
- let prefetch, client;
63
-
53
+ let prefetch, client
64
54
  if (canPrefetch(options)) {
65
- client = createPrefetchClient(options);
66
- prefetch = processCache(options, fetch);
55
+ client = createPrefetchClient(options)
56
+ prefetch = processCache(options, fetch)
67
57
  }
68
58
 
69
- const secretsManagerMiddlewareBefore = async request => {
70
- var _prefetch;
71
-
59
+ const secretsManagerMiddlewareBefore = async (request) => {
72
60
  if (!client) {
73
- client = await createClient(options, request);
61
+ client = await createClient(options, request)
74
62
  }
75
63
 
76
- const {
77
- value
78
- } = (_prefetch = prefetch) !== null && _prefetch !== void 0 ? _prefetch : processCache(options, fetch, request);
79
- Object.assign(request.internal, value);
64
+ const { value } = prefetch ?? processCache(options, fetch, request)
65
+
66
+ Object.assign(request.internal, value)
80
67
 
81
68
  if (options.setToContext || options.setToEnv) {
82
- const data = await getInternal(Object.keys(options.fetchData), request);
83
- if (options.setToEnv) Object.assign(process.env, data);
84
- if (options.setToContext) Object.assign(request.context, data);
69
+ const data = await getInternal(Object.keys(options.fetchData), request)
70
+ if (options.setToEnv) Object.assign(process.env, data)
71
+ if (options.setToContext) Object.assign(request.context, data)
85
72
  }
86
73
 
87
- prefetch = null;
88
- };
74
+ prefetch = null
75
+ }
89
76
 
90
77
  return {
91
78
  before: secretsManagerMiddlewareBefore
92
- };
93
- };
94
-
95
- module.exports = secretsManagerMiddleware;
79
+ }
80
+ }
81
+ module.exports = secretsManagerMiddleware
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/secrets-manager",
3
- "version": "2.5.2",
3
+ "version": "2.5.6",
4
4
  "description": "Secrets Manager middleware for the middy framework",
5
5
  "type": "commonjs",
6
6
  "engines": {
@@ -44,12 +44,12 @@
44
44
  },
45
45
  "homepage": "https://github.com/middyjs/middy#readme",
46
46
  "dependencies": {
47
- "@middy/util": "^2.5.2"
47
+ "@middy/util": "^2.5.6"
48
48
  },
49
49
  "devDependencies": {
50
- "@middy/core": "^2.5.2",
50
+ "@middy/core": "^2.5.6",
51
51
  "aws-sdk": "^2.939.0",
52
52
  "aws-xray-sdk": "^3.3.3"
53
53
  },
54
- "gitHead": "a2bb757a7a13638ae64277f8eecfcf11c1af17d4"
54
+ "gitHead": "0c789f55b4adf691f977b0d9904d1a805bb3bb2b"
55
55
  }