@middy/dynamodb 5.1.0 → 5.2.1

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 (2) hide show
  1. package/index.js +80 -57
  2. package/package.json +4 -4
package/index.js CHANGED
@@ -1,62 +1,85 @@
1
- import { canPrefetch, createPrefetchClient, createClient, getCache, getInternal, processCache, modifyCache } from '@middy/util';
2
- import { DynamoDBClient, GetItemCommand } from '@aws-sdk/client-dynamodb';
3
- import { marshall, unmarshall } from '@aws-sdk/util-dynamodb';
1
+ import {
2
+ canPrefetch,
3
+ createPrefetchClient,
4
+ createClient,
5
+ getCache,
6
+ getInternal,
7
+ processCache,
8
+ modifyCache,
9
+ catchInvalidSignatureException
10
+ } from '@middy/util'
11
+ import { DynamoDBClient, GetItemCommand } from '@aws-sdk/client-dynamodb'
12
+ import { marshall, unmarshall } from '@aws-sdk/util-dynamodb'
13
+
4
14
  const defaults = {
5
- AwsClient: DynamoDBClient,
6
- awsClientOptions: {},
7
- awsClientAssumeRole: undefined,
8
- awsClientCapture: undefined,
9
- fetchData: {},
10
- disablePrefetch: false,
11
- cacheKey: 'dynamodb',
12
- cacheKeyExpiry: {},
13
- cacheExpiry: -1,
14
- setToContext: false
15
- };
16
- const dynamodbMiddleware = (opts = {})=>{
17
- const options = {
18
- ...defaults,
19
- ...opts
20
- };
21
- for(const internalKey in options.fetchData){
22
- options.fetchData[internalKey].Key = marshall(options.fetchData[internalKey].Key);
15
+ AwsClient: DynamoDBClient,
16
+ awsClientOptions: {},
17
+ awsClientAssumeRole: undefined,
18
+ awsClientCapture: undefined,
19
+ fetchData: {},
20
+ disablePrefetch: false,
21
+ cacheKey: 'dynamodb',
22
+ cacheKeyExpiry: {},
23
+ cacheExpiry: -1,
24
+ setToContext: false
25
+ }
26
+ const dynamodbMiddleware = (opts = {}) => {
27
+ const options = {
28
+ ...defaults,
29
+ ...opts
30
+ }
31
+
32
+ // force marshall of Key during cold start
33
+ for (const internalKey in options.fetchData) {
34
+ options.fetchData[internalKey].Key = marshall(
35
+ options.fetchData[internalKey].Key
36
+ )
37
+ }
38
+
39
+ const fetch = (request, cachedValues = {}) => {
40
+ const values = {}
41
+ for (const internalKey in options.fetchData) {
42
+ if (cachedValues[internalKey]) continue
43
+ const inputParameters = options.fetchData[internalKey]
44
+ const command = new GetItemCommand(inputParameters)
45
+ values[internalKey] = client
46
+ .send(command)
47
+ .catch((e) => catchInvalidSignatureException(e, client, command))
48
+ .then((resp) => unmarshall(resp.Item))
49
+ .catch((e) => {
50
+ const value = getCache(options.cacheKey).value ?? {}
51
+ value[internalKey] = undefined
52
+ modifyCache(options.cacheKey, value)
53
+ throw e
54
+ })
23
55
  }
24
- const fetch = (request, cachedValues = {})=>{
25
- const values = {};
26
- for(const internalKey in options.fetchData){
27
- if (cachedValues[internalKey]) continue;
28
- const inputParameters = options.fetchData[internalKey];
29
- values[internalKey] = client.send(new GetItemCommand(inputParameters)).then((resp)=>unmarshall(resp.Item)).catch((e)=>{
30
- const value = getCache(options.cacheKey).value ?? {};
31
- value[internalKey] = undefined;
32
- modifyCache(options.cacheKey, value);
33
- throw e;
34
- });
35
- }
36
- return values;
37
- };
38
- let client;
39
- if (canPrefetch(options)) {
40
- client = createPrefetchClient(options);
41
- processCache(options, fetch);
56
+ return values
57
+ }
58
+
59
+ let client
60
+ if (canPrefetch(options)) {
61
+ client = createPrefetchClient(options)
62
+ processCache(options, fetch)
63
+ }
64
+ const dynamodbMiddlewareBefore = async (request) => {
65
+ if (!client) {
66
+ client = await createClient(options, request)
67
+ }
68
+ const { value } = processCache(options, fetch, request)
69
+ Object.assign(request.internal, value)
70
+ if (options.setToContext) {
71
+ const data = await getInternal(Object.keys(options.fetchData), request)
72
+ Object.assign(request.context, data)
42
73
  }
43
- const dynamodbMiddlewareBefore = async (request)=>{
44
- if (!client) {
45
- client = await createClient(options, request);
46
- }
47
- const { value } = processCache(options, fetch, request);
48
- Object.assign(request.internal, value);
49
- if (options.setToContext) {
50
- const data = await getInternal(Object.keys(options.fetchData), request);
51
- Object.assign(request.context, data);
52
- }
53
- };
54
- return {
55
- before: dynamodbMiddlewareBefore
56
- };
57
- };
58
- export function dynamoDbReq(req) {
59
- return req;
74
+ }
75
+ return {
76
+ before: dynamodbMiddlewareBefore
77
+ }
78
+ }
79
+
80
+ // used for TS type inference (see index.d.ts)
81
+ export function dynamoDbReq (req) {
82
+ return req
60
83
  }
61
- export default dynamodbMiddleware;
62
84
 
85
+ export default dynamodbMiddleware
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/dynamodb",
3
- "version": "5.1.0",
3
+ "version": "5.2.1",
4
4
  "description": "DynamoDB middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -58,14 +58,14 @@
58
58
  "url": "https://github.com/sponsors/willfarrell"
59
59
  },
60
60
  "dependencies": {
61
- "@middy/util": "5.1.0"
61
+ "@middy/util": "5.2.1"
62
62
  },
63
63
  "devDependencies": {
64
64
  "@aws-sdk/client-dynamodb": "^3.245.0",
65
65
  "@aws-sdk/util-dynamodb": "^3.245.0",
66
- "@middy/core": "5.1.0",
66
+ "@middy/core": "5.2.1",
67
67
  "@types/aws-lambda": "^8.10.101",
68
68
  "aws-xray-sdk": "^3.3.3"
69
69
  },
70
- "gitHead": "bbdaf5843914921804ba085dd58117273febe6b5"
70
+ "gitHead": "4d55da221b9165b4b3e59a12632fd40a149a1e92"
71
71
  }