@middy/secrets-manager 7.1.1 → 7.1.3

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 CHANGED
@@ -30,6 +30,23 @@
30
30
  <p>You can read the documentation at: <a href="https://middy.js.org/docs/middlewares/secrets-manager">https://middy.js.org/docs/middlewares/secrets-manager</a></p>
31
31
  </div>
32
32
 
33
+ ## Install
34
+
35
+ ```bash
36
+ npm install --save @middy/secrets-manager @aws-sdk/client-secrets-manager
37
+ ```
38
+
39
+
40
+ ## Documentation and examples
41
+
42
+ For documentation and examples, refer to the main [Middy monorepo on GitHub](https://github.com/middyjs/middy) or [Middy official website](https://middy.js.org/docs/middlewares/secrets-manager).
43
+
44
+
45
+ ## Contributing
46
+
47
+ Everyone is very welcome to contribute to this repository. Feel free to [raise issues](https://github.com/middyjs/middy/issues) or to [submit Pull Requests](https://github.com/middyjs/middy/pulls).
48
+
49
+
33
50
  ## License
34
51
 
35
52
  Licensed under [MIT License](LICENSE). Copyright (c) 2017-2026 [will Farrell](https://github.com/willfarrell), [Luciano Mammino](https://github.com/lmammino), and [Middy contributors](https://github.com/middyjs/middy/graphs/contributors).
package/index.d.ts CHANGED
@@ -11,12 +11,14 @@ import type { Context as LambdaContext } from "aws-lambda";
11
11
  export type SecretType<T> = string & { __returnType?: T };
12
12
  export declare function secretsManagerParam<T>(path: string): SecretType<T>;
13
13
 
14
- interface SecretsManagerOptions<AwsSecretsManagerClient = SecretsManagerClient>
15
- extends Omit<
14
+ export interface SecretsManagerOptions<
15
+ AwsSecretsManagerClient = SecretsManagerClient,
16
+ > extends Omit<
16
17
  MiddyOptions<AwsSecretsManagerClient, SecretsManagerClientConfig>,
17
18
  "fetchData"
18
19
  > {
19
20
  fetchData?: { [key: string]: string | SecretType<unknown> };
21
+ fetchRotationDate?: boolean | Record<string, boolean>;
20
22
  }
21
23
 
22
24
  export type Context<TOptions extends SecretsManagerOptions | undefined> =
@@ -47,7 +49,7 @@ declare function secretsManager<
47
49
  options?: TOptions,
48
50
  ): middy.MiddlewareObj<
49
51
  unknown,
50
- any,
52
+ unknown,
51
53
  Error,
52
54
  Context<TOptions>,
53
55
  Internal<TOptions>
package/index.js CHANGED
@@ -38,54 +38,64 @@ const secretsManagerMiddleware = (opts = {}) => {
38
38
  cacheKeyExpiry: { ...defaults.cacheKeyExpiry, ...opts.cacheKeyExpiry },
39
39
  };
40
40
 
41
+ const fetchDataKeys = Object.keys(options.fetchData);
41
42
  const fetchRequest = (request, cachedValues = {}) => {
42
43
  const values = {};
43
44
 
44
- for (const internalKey of Object.keys(options.fetchData)) {
45
+ for (const internalKey of fetchDataKeys) {
45
46
  if (cachedValues[internalKey]) continue;
46
47
 
47
- values[internalKey] = Promise.resolve()
48
- .then(() => {
49
- if (
50
- options.fetchRotationDate === true ||
51
- options.fetchRotationDate?.[internalKey]
52
- ) {
53
- const command = new DescribeSecretCommand({
54
- SecretId: options.fetchData[internalKey],
55
- });
56
- return client
57
- .send(command)
58
- .catch((e) => catchInvalidSignatureException(e, client, command))
59
- .then((resp) => {
60
- if (options.cacheExpiry < 0) {
61
- options.cacheKeyExpiry[internalKey] =
62
- resp.NextRotationDate * 1000;
63
- } else {
64
- options.cacheKeyExpiry[internalKey] = Math.min(
65
- Math.max(resp.LastRotationDate, resp.LastChangedDate) *
66
- 1000 +
67
- options.cacheExpiry,
68
- resp.NextRotationDate * 1000,
69
- );
70
- }
71
- });
72
- }
73
- })
74
- .then(() => {
75
- const command = new GetSecretValueCommand({
76
- SecretId: options.fetchData[internalKey],
77
- });
78
- return client
79
- .send(command)
80
- .catch((e) => catchInvalidSignatureException(e, client, command));
81
- })
82
- .then((resp) => jsonSafeParse(resp.SecretString))
83
- .catch((e) => {
84
- const value = getCache(options.cacheKey).value ?? {};
85
- value[internalKey] = undefined;
86
- modifyCache(options.cacheKey, value);
87
- throw e;
48
+ const fetchRotation =
49
+ options.fetchRotationDate === true ||
50
+ options.fetchRotationDate?.[internalKey];
51
+ const rotationPromise = fetchRotation
52
+ ? client
53
+ .send(
54
+ new DescribeSecretCommand({
55
+ SecretId: options.fetchData[internalKey],
56
+ }),
57
+ )
58
+ .catch((e) =>
59
+ catchInvalidSignatureException(
60
+ e,
61
+ client,
62
+ new DescribeSecretCommand({
63
+ SecretId: options.fetchData[internalKey],
64
+ }),
65
+ ),
66
+ )
67
+ .then((resp) => {
68
+ if (options.cacheExpiry < 0) {
69
+ options.cacheKeyExpiry[internalKey] =
70
+ resp.NextRotationDate * 1000;
71
+ } else {
72
+ options.cacheKeyExpiry[internalKey] = Math.min(
73
+ Math.max(resp.LastRotationDate, resp.LastChangedDate) * 1000 +
74
+ options.cacheExpiry,
75
+ resp.NextRotationDate * 1000,
76
+ );
77
+ }
78
+ })
79
+ : undefined;
80
+
81
+ const fetchSecret = () => {
82
+ const command = new GetSecretValueCommand({
83
+ SecretId: options.fetchData[internalKey],
88
84
  });
85
+ return client
86
+ .send(command)
87
+ .catch((e) => catchInvalidSignatureException(e, client, command))
88
+ .then((resp) => jsonSafeParse(resp.SecretString));
89
+ };
90
+
91
+ values[internalKey] = (
92
+ rotationPromise ? rotationPromise.then(fetchSecret) : fetchSecret()
93
+ ).catch((e) => {
94
+ const value = getCache(options.cacheKey).value ?? {};
95
+ value[internalKey] = undefined;
96
+ modifyCache(options.cacheKey, value);
97
+ throw e;
98
+ });
89
99
  }
90
100
  return values;
91
101
  };
@@ -106,7 +116,7 @@ const secretsManagerMiddleware = (opts = {}) => {
106
116
  Object.assign(request.internal, value);
107
117
 
108
118
  if (options.setToContext) {
109
- const data = await getInternal(Object.keys(options.fetchData), request);
119
+ const data = await getInternal(fetchDataKeys, request);
110
120
  Object.assign(request.context, data);
111
121
  }
112
122
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/secrets-manager",
3
- "version": "7.1.1",
3
+ "version": "7.1.3",
4
4
  "description": "Secrets Manager middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -63,7 +63,7 @@
63
63
  "url": "https://github.com/sponsors/willfarrell"
64
64
  },
65
65
  "dependencies": {
66
- "@middy/util": "7.1.1"
66
+ "@middy/util": "7.1.3"
67
67
  },
68
68
  "peerDependencies": {
69
69
  "@aws-sdk/client-secrets-manager": "^3.0.0"
@@ -75,9 +75,9 @@
75
75
  },
76
76
  "devDependencies": {
77
77
  "@aws-sdk/client-secrets-manager": "^3.0.0",
78
- "@middy/core": "7.1.1",
78
+ "@middy/core": "7.1.3",
79
79
  "@types/aws-lambda": "^8.0.0",
80
+ "@types/node": "^22.0.0",
80
81
  "aws-xray-sdk": "^3.3.3"
81
- },
82
- "gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"
82
+ }
83
83
  }