@middy/dynamodb 4.6.5 → 5.0.0-alpha.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 (4) hide show
  1. package/index.d.ts +28 -8
  2. package/index.js +78 -56
  3. package/package.json +5 -11
  4. package/index.cjs +0 -70
package/index.d.ts CHANGED
@@ -1,26 +1,46 @@
1
1
  import middy from '@middy/core'
2
2
  import { Options as MiddyOptions } from '@middy/util'
3
3
  import { Context as LambdaContext } from 'aws-lambda'
4
- import { DynamoDBClient, DynamoDBClientConfig } from '@aws-sdk/client-dynamodb'
4
+ import { DynamoDBClient, DynamoDBClientConfig, GetItemCommandInput } from '@aws-sdk/client-dynamodb'
5
+ import { NativeAttributeValue } from '@aws-sdk/util-dynamodb'
5
6
 
6
- export type Options<AwsDynamoDBClient = DynamoDBClient> =
7
+ export type ParamType<T extends Record<string, NativeAttributeValue>> = GetItemCommandInput & { __returnType?: T }
8
+ export declare function dynamoDbReq<T extends Record<string, NativeAttributeValue>> (req: GetItemCommandInput): ParamType<T>
9
+
10
+ export type DynamoDbOptions<AwsDynamoDBClient = DynamoDBClient> =
7
11
  Omit<MiddyOptions<AwsDynamoDBClient, DynamoDBClientConfig>, 'fetchData'>
8
12
  &
9
13
  {
10
14
  fetchData?: {
11
- // TODO: add more precise type
12
- [key: string]: Record<string, any>
15
+ [key: string]: GetItemCommandInput | ParamType<Record<string, NativeAttributeValue>>
13
16
  }
14
17
  }
15
18
 
16
- export type Context<TOptions extends Options | undefined> = TOptions extends {
19
+ export type Context<TOptions extends DynamoDbOptions | undefined> = TOptions extends {
17
20
  setToContext: true
18
21
  }
19
- ? LambdaContext & Record<keyof TOptions['fetchData'], any>
22
+ ? TOptions extends { fetchData: infer TFetchData }
23
+ ? LambdaContext & {
24
+ [Key in keyof TFetchData]: TFetchData[Key] extends ParamType<infer T>
25
+ ? T
26
+ : NativeAttributeValue
27
+ }
28
+ : never
20
29
  : LambdaContext
21
30
 
22
- declare function dynamodbMiddleware<TOptions extends Options | undefined> (
31
+ export type Internal<TOptions extends DynamoDbOptions | undefined> =
32
+ TOptions extends DynamoDbOptions
33
+ ? TOptions extends { fetchData: infer TFetchData }
34
+ ? {
35
+ [Key in keyof TFetchData]: TFetchData[Key] extends ParamType<infer T>
36
+ ? T
37
+ : NativeAttributeValue
38
+ }
39
+ : {}
40
+ : {}
41
+
42
+ declare function dynamodbMiddleware<TOptions extends DynamoDbOptions | undefined> (
23
43
  options?: TOptions
24
- ): middy.MiddlewareObj<unknown, any, Error, Context<TOptions>>
44
+ ): middy.MiddlewareObj<unknown, any, Error, Context<TOptions>, Internal<TOptions>>
25
45
 
26
46
  export default dynamodbMiddleware
package/index.js CHANGED
@@ -1,60 +1,82 @@
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
+ } from '@middy/util'
10
+ import { DynamoDBClient, GetItemCommand } from '@aws-sdk/client-dynamodb'
11
+ import { marshall, unmarshall } from '@aws-sdk/util-dynamodb'
12
+
4
13
  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
- // force marshall of Key during cold start
22
- for(const internalKey in options.fetchData){
23
- options.fetchData[internalKey].Key = marshall(options.fetchData[internalKey].Key);
14
+ AwsClient: DynamoDBClient,
15
+ awsClientOptions: {},
16
+ awsClientAssumeRole: undefined,
17
+ awsClientCapture: undefined,
18
+ fetchData: {},
19
+ disablePrefetch: false,
20
+ cacheKey: 'dynamodb',
21
+ cacheKeyExpiry: {},
22
+ cacheExpiry: -1,
23
+ setToContext: false
24
+ }
25
+ const dynamodbMiddleware = (opts = {}) => {
26
+ const options = {
27
+ ...defaults,
28
+ ...opts
29
+ }
30
+
31
+ // force marshall of Key during cold start
32
+ for (const internalKey in options.fetchData) {
33
+ options.fetchData[internalKey].Key = marshall(
34
+ options.fetchData[internalKey].Key
35
+ )
36
+ }
37
+
38
+ const fetch = (request, cachedValues = {}) => {
39
+ const values = {}
40
+ for (const internalKey in options.fetchData) {
41
+ if (cachedValues[internalKey]) continue
42
+ const inputParameters = options.fetchData[internalKey]
43
+ values[internalKey] = client
44
+ .send(new GetItemCommand(inputParameters))
45
+ .then((resp) => unmarshall(resp.Item))
46
+ .catch((e) => {
47
+ const value = getCache(options.cacheKey).value ?? {}
48
+ value[internalKey] = undefined
49
+ modifyCache(options.cacheKey, value)
50
+ throw e
51
+ })
24
52
  }
25
- const fetch = (request, cachedValues = {})=>{
26
- const values = {};
27
- for(const internalKey in options.fetchData){
28
- if (cachedValues[internalKey]) continue;
29
- const inputParameters = options.fetchData[internalKey];
30
- values[internalKey] = client.send(new GetItemCommand(inputParameters)).then((resp)=>unmarshall(resp.Item)).catch((e)=>{
31
- const value = getCache(options.cacheKey).value ?? {};
32
- value[internalKey] = undefined;
33
- modifyCache(options.cacheKey, value);
34
- throw e;
35
- });
36
- }
37
- return values;
38
- };
39
- let client;
40
- if (canPrefetch(options)) {
41
- client = createPrefetchClient(options);
42
- processCache(options, fetch);
53
+ return values
54
+ }
55
+
56
+ let client
57
+ if (canPrefetch(options)) {
58
+ client = createPrefetchClient(options)
59
+ processCache(options, fetch)
60
+ }
61
+ const dynamodbMiddlewareBefore = async (request) => {
62
+ if (!client) {
63
+ client = await createClient(options, request)
64
+ }
65
+ const { value } = processCache(options, fetch, request)
66
+ Object.assign(request.internal, value)
67
+ if (options.setToContext) {
68
+ const data = await getInternal(Object.keys(options.fetchData), request)
69
+ Object.assign(request.context, data)
43
70
  }
44
- const dynamodbMiddlewareBefore = async (request)=>{
45
- if (!client) {
46
- client = await createClient(options, request);
47
- }
48
- const { value } = processCache(options, fetch, request);
49
- Object.assign(request.internal, value);
50
- if (options.setToContext) {
51
- const data = await getInternal(Object.keys(options.fetchData), request);
52
- Object.assign(request.context, data);
53
- }
54
- };
55
- return {
56
- before: dynamodbMiddlewareBefore
57
- };
58
- };
59
- export default dynamodbMiddleware;
71
+ }
72
+ return {
73
+ before: dynamodbMiddlewareBefore
74
+ }
75
+ }
76
+
77
+ // used for TS type inference (see index.d.ts)
78
+ export function dynamoDbReq (req) {
79
+ return req
80
+ }
60
81
 
82
+ export default dynamodbMiddleware
package/package.json CHANGED
@@ -1,33 +1,27 @@
1
1
  {
2
2
  "name": "@middy/dynamodb",
3
- "version": "4.6.5",
3
+ "version": "5.0.0-alpha.1",
4
4
  "description": "DynamoDB middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
7
- "node": ">=16"
7
+ "node": ">=18"
8
8
  },
9
9
  "engineStrict": true,
10
10
  "publishConfig": {
11
11
  "access": "public"
12
12
  },
13
- "main": "./index.cjs",
14
13
  "module": "./index.js",
15
14
  "exports": {
16
15
  ".": {
17
16
  "import": {
18
17
  "types": "./index.d.ts",
19
18
  "default": "./index.js"
20
- },
21
- "require": {
22
- "types": "./index.d.ts",
23
- "default": "./index.cjs"
24
19
  }
25
20
  }
26
21
  },
27
22
  "types": "index.d.ts",
28
23
  "files": [
29
24
  "index.js",
30
- "index.cjs",
31
25
  "index.d.ts"
32
26
  ],
33
27
  "scripts": {
@@ -64,14 +58,14 @@
64
58
  "url": "https://github.com/sponsors/willfarrell"
65
59
  },
66
60
  "dependencies": {
67
- "@middy/util": "4.6.5"
61
+ "@middy/util": "5.0.0-alpha.1"
68
62
  },
69
63
  "devDependencies": {
70
64
  "@aws-sdk/client-dynamodb": "^3.245.0",
71
65
  "@aws-sdk/util-dynamodb": "^3.245.0",
72
- "@middy/core": "4.6.5",
66
+ "@middy/core": "5.0.0-alpha.1",
73
67
  "@types/aws-lambda": "^8.10.101",
74
68
  "aws-xray-sdk": "^3.3.3"
75
69
  },
76
- "gitHead": "573d7b0bb243d8c5a9bcb00cf29d031aa7a0c606"
70
+ "gitHead": "ebce8d5df8783077fa49ba62ee9be20e8486a7f1"
77
71
  }
package/index.cjs DELETED
@@ -1,70 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(module, "exports", {
6
- enumerable: true,
7
- get: function() {
8
- return _default;
9
- }
10
- });
11
- const _util = require("@middy/util");
12
- const _clientdynamodb = require("@aws-sdk/client-dynamodb");
13
- const _utildynamodb = require("@aws-sdk/util-dynamodb");
14
- const defaults = {
15
- AwsClient: _clientdynamodb.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
- // force marshall of Key during cold start
32
- for(const internalKey in options.fetchData){
33
- options.fetchData[internalKey].Key = (0, _utildynamodb.marshall)(options.fetchData[internalKey].Key);
34
- }
35
- const fetch = (request, cachedValues = {})=>{
36
- const values = {};
37
- for(const internalKey in options.fetchData){
38
- if (cachedValues[internalKey]) continue;
39
- const inputParameters = options.fetchData[internalKey];
40
- values[internalKey] = client.send(new _clientdynamodb.GetItemCommand(inputParameters)).then((resp)=>(0, _utildynamodb.unmarshall)(resp.Item)).catch((e)=>{
41
- const value = (0, _util.getCache)(options.cacheKey).value ?? {};
42
- value[internalKey] = undefined;
43
- (0, _util.modifyCache)(options.cacheKey, value);
44
- throw e;
45
- });
46
- }
47
- return values;
48
- };
49
- let client;
50
- if ((0, _util.canPrefetch)(options)) {
51
- client = (0, _util.createPrefetchClient)(options);
52
- (0, _util.processCache)(options, fetch);
53
- }
54
- const dynamodbMiddlewareBefore = async (request)=>{
55
- if (!client) {
56
- client = await (0, _util.createClient)(options, request);
57
- }
58
- const { value } = (0, _util.processCache)(options, fetch, request);
59
- Object.assign(request.internal, value);
60
- if (options.setToContext) {
61
- const data = await (0, _util.getInternal)(Object.keys(options.fetchData), request);
62
- Object.assign(request.context, data);
63
- }
64
- };
65
- return {
66
- before: dynamodbMiddlewareBefore
67
- };
68
- };
69
- const _default = dynamodbMiddleware;
70
-