@middy/dynamodb 6.1.5 → 6.2.0

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 +57 -39
  2. package/index.js +74 -74
  3. package/package.json +66 -69
package/index.d.ts CHANGED
@@ -1,46 +1,64 @@
1
- import middy from '@middy/core'
2
- import { Options as MiddyOptions } from '@middy/util'
3
- import { Context as LambdaContext } from 'aws-lambda'
4
- import { DynamoDBClient, DynamoDBClientConfig, GetItemCommandInput } from '@aws-sdk/client-dynamodb'
5
- import { NativeAttributeValue } from '@aws-sdk/util-dynamodb'
1
+ import type {
2
+ DynamoDBClient,
3
+ DynamoDBClientConfig,
4
+ GetItemCommandInput,
5
+ } from "@aws-sdk/client-dynamodb";
6
+ import type { NativeAttributeValue } from "@aws-sdk/util-dynamodb";
7
+ import type middy from "@middy/core";
8
+ import type { Options as MiddyOptions } from "@middy/util";
9
+ import type { Context as LambdaContext } from "aws-lambda";
6
10
 
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>
11
+ export type ParamType<T extends Record<string, NativeAttributeValue>> =
12
+ GetItemCommandInput & { __returnType?: T };
13
+ export declare function dynamoDbReq<
14
+ T extends Record<string, NativeAttributeValue>,
15
+ >(req: GetItemCommandInput): ParamType<T>;
9
16
 
10
- export type DynamoDbOptions<AwsDynamoDBClient = DynamoDBClient> =
11
- Omit<MiddyOptions<AwsDynamoDBClient, DynamoDBClientConfig>, 'fetchData'>
12
- &
13
- {
14
- fetchData?: {
15
- [key: string]: GetItemCommandInput | ParamType<Record<string, NativeAttributeValue>>
16
- }
17
- }
17
+ export type DynamoDbOptions<AwsDynamoDBClient = DynamoDBClient> = Omit<
18
+ MiddyOptions<AwsDynamoDBClient, DynamoDBClientConfig>,
19
+ "fetchData"
20
+ > & {
21
+ fetchData?: {
22
+ [key: string]:
23
+ | GetItemCommandInput
24
+ | ParamType<Record<string, NativeAttributeValue>>;
25
+ };
26
+ };
18
27
 
19
- export type Context<TOptions extends DynamoDbOptions | undefined> = TOptions extends {
20
- setToContext: true
21
- }
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
29
- : LambdaContext
28
+ export type Context<TOptions extends DynamoDbOptions | undefined> =
29
+ TOptions extends {
30
+ setToContext: true;
31
+ }
32
+ ? TOptions extends { fetchData: infer TFetchData }
33
+ ? LambdaContext & {
34
+ [Key in keyof TFetchData]: TFetchData[Key] extends ParamType<infer T>
35
+ ? T
36
+ : NativeAttributeValue;
37
+ }
38
+ : never
39
+ : LambdaContext;
30
40
 
31
41
  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
- : {}
42
+ TOptions extends DynamoDbOptions
43
+ ? TOptions extends { fetchData: infer TFetchData }
44
+ ? {
45
+ [Key in keyof TFetchData]: TFetchData[Key] extends ParamType<infer T>
46
+ ? T
47
+ : NativeAttributeValue;
48
+ }
49
+ : {}
50
+ : {};
41
51
 
42
- declare function dynamodbMiddleware<TOptions extends DynamoDbOptions | undefined> (
43
- options?: TOptions
44
- ): middy.MiddlewareObj<unknown, any, Error, Context<TOptions>, Internal<TOptions>>
52
+ declare function dynamodbMiddleware<
53
+ TOptions extends DynamoDbOptions | undefined,
54
+ >(
55
+ options?: TOptions,
56
+ ): middy.MiddlewareObj<
57
+ unknown,
58
+ any,
59
+ Error,
60
+ Context<TOptions>,
61
+ Internal<TOptions>
62
+ >;
45
63
 
46
- export default dynamodbMiddleware
64
+ export default dynamodbMiddleware;
package/index.js CHANGED
@@ -1,85 +1,85 @@
1
+ import { DynamoDBClient, GetItemCommand } from "@aws-sdk/client-dynamodb";
2
+ import { marshall, unmarshall } from "@aws-sdk/util-dynamodb";
1
3
  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'
4
+ canPrefetch,
5
+ catchInvalidSignatureException,
6
+ createClient,
7
+ createPrefetchClient,
8
+ getCache,
9
+ getInternal,
10
+ modifyCache,
11
+ processCache,
12
+ } from "@middy/util";
13
13
 
14
14
  const defaults = {
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
- }
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
26
  const dynamodbMiddleware = (opts = {}) => {
27
- const options = {
28
- ...defaults,
29
- ...opts
30
- }
27
+ const options = {
28
+ ...defaults,
29
+ ...opts,
30
+ };
31
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
- }
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
38
 
39
- const fetchRequest = (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
- })
55
- }
56
- return values
57
- }
39
+ const fetchRequest = (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
+ });
55
+ }
56
+ return values;
57
+ };
58
58
 
59
- let client
60
- if (canPrefetch(options)) {
61
- client = createPrefetchClient(options)
62
- processCache(options, fetchRequest)
63
- }
64
- const dynamodbMiddlewareBefore = async (request) => {
65
- if (!client) {
66
- client = await createClient(options, request)
67
- }
68
- const { value } = processCache(options, fetchRequest, 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)
73
- }
74
- }
75
- return {
76
- before: dynamodbMiddlewareBefore
77
- }
78
- }
59
+ let client;
60
+ if (canPrefetch(options)) {
61
+ client = createPrefetchClient(options);
62
+ processCache(options, fetchRequest);
63
+ }
64
+ const dynamodbMiddlewareBefore = async (request) => {
65
+ if (!client) {
66
+ client = await createClient(options, request);
67
+ }
68
+ const { value } = processCache(options, fetchRequest, 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);
73
+ }
74
+ };
75
+ return {
76
+ before: dynamodbMiddlewareBefore,
77
+ };
78
+ };
79
79
 
80
80
  // used for TS type inference (see index.d.ts)
81
- export function dynamoDbReq (req) {
82
- return req
81
+ export function dynamoDbReq(req) {
82
+ return req;
83
83
  }
84
84
 
85
- export default dynamodbMiddleware
85
+ export default dynamodbMiddleware;
package/package.json CHANGED
@@ -1,71 +1,68 @@
1
1
  {
2
- "name": "@middy/dynamodb",
3
- "version": "6.1.5",
4
- "description": "DynamoDB middleware for the middy framework",
5
- "type": "module",
6
- "engines": {
7
- "node": ">=20"
8
- },
9
- "engineStrict": true,
10
- "publishConfig": {
11
- "access": "public"
12
- },
13
- "module": "./index.js",
14
- "exports": {
15
- ".": {
16
- "import": {
17
- "types": "./index.d.ts",
18
- "default": "./index.js"
19
- },
20
- "require": {
21
- "default": "./index.js"
22
- }
23
- }
24
- },
25
- "types": "index.d.ts",
26
- "files": [
27
- "index.js",
28
- "index.d.ts"
29
- ],
30
- "scripts": {
31
- "test": "npm run test:unit && npm run test:fuzz",
32
- "test:unit": "node --test __tests__/index.js",
33
- "test:fuzz": "node --test __tests__/fuzz.js",
34
- "test:benchmark": "node __benchmarks__/index.js"
35
- },
36
- "license": "MIT",
37
- "keywords": [
38
- "Lambda",
39
- "Middleware",
40
- "Serverless",
41
- "Framework",
42
- "AWS",
43
- "AWS Lambda",
44
- "Middy",
45
- "DynamoDB"
46
- ],
47
- "author": {
48
- "name": "Middy contributors",
49
- "url": "https://github.com/middyjs/middy/graphs/contributors"
50
- },
51
- "repository": {
52
- "type": "git",
53
- "url": "git+https://github.com/middyjs/middy.git",
54
- "directory": "packages/dynamodb"
55
- },
56
- "bugs": {
57
- "url": "https://github.com/middyjs/middy/issues"
58
- },
59
- "homepage": "https://middy.js.org",
60
- "funding": {
61
- "type": "github",
62
- "url": "https://github.com/sponsors/willfarrell"
63
- },
64
- "devDependencies": {
65
- "@aws-sdk/client-dynamodb": "^3.245.0",
66
- "@aws-sdk/util-dynamodb": "^3.245.0",
67
- "@types/aws-lambda": "^8.10.101",
68
- "aws-xray-sdk": "^3.3.3"
69
- },
70
- "gitHead": "4780887d818be31d9b6f1e2ced99c954d11a4a81"
2
+ "name": "@middy/dynamodb",
3
+ "version": "6.2.0",
4
+ "description": "DynamoDB middleware for the middy framework",
5
+ "type": "module",
6
+ "engines": {
7
+ "node": ">=20"
8
+ },
9
+ "engineStrict": true,
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "module": "./index.js",
14
+ "exports": {
15
+ ".": {
16
+ "import": {
17
+ "types": "./index.d.ts",
18
+ "default": "./index.js"
19
+ },
20
+ "require": {
21
+ "default": "./index.js"
22
+ }
23
+ }
24
+ },
25
+ "types": "index.d.ts",
26
+ "files": ["index.js", "index.d.ts"],
27
+ "scripts": {
28
+ "test": "npm run test:unit && npm run test:fuzz",
29
+ "test:unit": "node --test",
30
+ "test:fuzz": "node --test index.fuzz.js",
31
+ "test:perf": "node --test index.perf.js"
32
+ },
33
+ "license": "MIT",
34
+ "keywords": [
35
+ "Lambda",
36
+ "Middleware",
37
+ "Serverless",
38
+ "Framework",
39
+ "AWS",
40
+ "AWS Lambda",
41
+ "Middy",
42
+ "DynamoDB"
43
+ ],
44
+ "author": {
45
+ "name": "Middy contributors",
46
+ "url": "https://github.com/middyjs/middy/graphs/contributors"
47
+ },
48
+ "repository": {
49
+ "type": "git",
50
+ "url": "git+https://github.com/middyjs/middy.git",
51
+ "directory": "packages/dynamodb"
52
+ },
53
+ "bugs": {
54
+ "url": "https://github.com/middyjs/middy/issues"
55
+ },
56
+ "homepage": "https://middy.js.org",
57
+ "funding": {
58
+ "type": "github",
59
+ "url": "https://github.com/sponsors/willfarrell"
60
+ },
61
+ "devDependencies": {
62
+ "@aws-sdk/client-dynamodb": "^3.245.0",
63
+ "@aws-sdk/util-dynamodb": "^3.245.0",
64
+ "@types/aws-lambda": "^8.10.101",
65
+ "aws-xray-sdk": "^3.3.3"
66
+ },
67
+ "gitHead": "4780887d818be31d9b6f1e2ced99c954d11a4a81"
71
68
  }