@middy/dynamodb 5.0.0-alpha.0 → 5.0.0-alpha.2

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
@@ -19,8 +19,9 @@
19
19
  <a href="https://snyk.io/test/github/middyjs/middy">
20
20
  <img src="https://snyk.io/test/github/middyjs/middy/badge.svg" alt="Known Vulnerabilities" data-canonical-src="https://snyk.io/test/github/middyjs/middy" style="max-width:100%;">
21
21
  </a>
22
- <a href="https://lgtm.com/projects/g/middyjs/middy/context:javascript">
23
- <img src="https://img.shields.io/lgtm/grade/javascript/g/middyjs/middy.svg?logo=lgtm&logoWidth=18" alt="Language grade: JavaScript" style="max-width:100%;">
22
+ <a href="https://github.com/middyjs/middy/actions/workflows/sast.yml">
23
+ <img src="https://github.com/middyjs/middy/actions/workflows/sast.yml/badge.svg
24
+ ?branch=main&event=push" alt="CodeQL" style="max-width:100%;">
24
25
  </a>
25
26
  <a href="https://bestpractices.coreinfrastructure.org/projects/5280">
26
27
  <img src="https://bestpractices.coreinfrastructure.org/projects/5280/badge" alt="Core Infrastructure Initiative (CII) Best Practices" style="max-width:100%;">
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
@@ -9,6 +9,7 @@ const defaults = {
9
9
  fetchData: {},
10
10
  disablePrefetch: false,
11
11
  cacheKey: 'dynamodb',
12
+ cacheKeyExpiry: {},
12
13
  cacheExpiry: -1,
13
14
  setToContext: false
14
15
  };
@@ -17,6 +18,7 @@ const dynamodbMiddleware = (opts = {})=>{
17
18
  ...defaults,
18
19
  ...opts
19
20
  };
21
+ // force marshall of Key during cold start
20
22
  for(const internalKey in options.fetchData){
21
23
  options.fetchData[internalKey].Key = marshall(options.fetchData[internalKey].Key);
22
24
  }
@@ -34,26 +36,29 @@ const dynamodbMiddleware = (opts = {})=>{
34
36
  }
35
37
  return values;
36
38
  };
37
- let prefetch, client;
39
+ let client;
38
40
  if (canPrefetch(options)) {
39
41
  client = createPrefetchClient(options);
40
- prefetch = processCache(options, fetch);
42
+ processCache(options, fetch);
41
43
  }
42
44
  const dynamodbMiddlewareBefore = async (request)=>{
43
45
  if (!client) {
44
46
  client = await createClient(options, request);
45
47
  }
46
- const { value } = prefetch ?? processCache(options, fetch, request);
48
+ const { value } = processCache(options, fetch, request);
47
49
  Object.assign(request.internal, value);
48
50
  if (options.setToContext) {
49
51
  const data = await getInternal(Object.keys(options.fetchData), request);
50
52
  Object.assign(request.context, data);
51
53
  }
52
- prefetch = null;
53
54
  };
54
55
  return {
55
56
  before: dynamodbMiddlewareBefore
56
57
  };
57
58
  };
59
+ // used for TS type inference (see index.d.ts)
60
+ export function dynamoDbReq(req) {
61
+ return req;
62
+ }
58
63
  export default dynamodbMiddleware;
59
64
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/dynamodb",
3
- "version": "5.0.0-alpha.0",
3
+ "version": "5.0.0-alpha.2",
4
4
  "description": "DynamoDB middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -10,24 +10,18 @@
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": "5.0.0-alpha.0"
61
+ "@middy/util": "5.0.0-alpha.2"
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": "5.0.0-alpha.0",
66
+ "@middy/core": "5.0.0-alpha.2",
73
67
  "@types/aws-lambda": "^8.10.101",
74
68
  "aws-xray-sdk": "^3.3.3"
75
69
  },
76
- "gitHead": "08c35e3dba9efdad0b86666ce206ce302cc65d07"
70
+ "gitHead": "ebce8d5df8783077fa49ba62ee9be20e8486a7f1"
77
71
  }
package/index.cjs DELETED
@@ -1,67 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(module, "exports", {
6
- enumerable: true,
7
- get: ()=>_default
8
- });
9
- const _util = require("@middy/util");
10
- const _clientDynamodb = require("@aws-sdk/client-dynamodb");
11
- const _utilDynamodb = require("@aws-sdk/util-dynamodb");
12
- const defaults = {
13
- AwsClient: _clientDynamodb.DynamoDBClient,
14
- awsClientOptions: {},
15
- awsClientAssumeRole: undefined,
16
- awsClientCapture: undefined,
17
- fetchData: {},
18
- disablePrefetch: false,
19
- cacheKey: 'dynamodb',
20
- cacheExpiry: -1,
21
- setToContext: false
22
- };
23
- const dynamodbMiddleware = (opts = {})=>{
24
- const options = {
25
- ...defaults,
26
- ...opts
27
- };
28
- for(const internalKey in options.fetchData){
29
- options.fetchData[internalKey].Key = (0, _utilDynamodb.marshall)(options.fetchData[internalKey].Key);
30
- }
31
- const fetch = (request, cachedValues = {})=>{
32
- const values = {};
33
- for(const internalKey in options.fetchData){
34
- if (cachedValues[internalKey]) continue;
35
- const inputParameters = options.fetchData[internalKey];
36
- values[internalKey] = client.send(new _clientDynamodb.GetItemCommand(inputParameters)).then((resp)=>(0, _utilDynamodb.unmarshall)(resp.Item)).catch((e)=>{
37
- const value = (0, _util.getCache)(options.cacheKey).value ?? {};
38
- value[internalKey] = undefined;
39
- (0, _util.modifyCache)(options.cacheKey, value);
40
- throw e;
41
- });
42
- }
43
- return values;
44
- };
45
- let prefetch, client;
46
- if ((0, _util.canPrefetch)(options)) {
47
- client = (0, _util.createPrefetchClient)(options);
48
- prefetch = (0, _util.processCache)(options, fetch);
49
- }
50
- const dynamodbMiddlewareBefore = async (request)=>{
51
- if (!client) {
52
- client = await (0, _util.createClient)(options, request);
53
- }
54
- const { value } = prefetch ?? (0, _util.processCache)(options, fetch, request);
55
- Object.assign(request.internal, value);
56
- if (options.setToContext) {
57
- const data = await (0, _util.getInternal)(Object.keys(options.fetchData), request);
58
- Object.assign(request.context, data);
59
- }
60
- prefetch = null;
61
- };
62
- return {
63
- before: dynamodbMiddlewareBefore
64
- };
65
- };
66
- const _default = dynamodbMiddleware;
67
-