@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.
- package/index.d.ts +57 -39
- package/index.js +74 -74
- package/package.json +66 -69
package/index.d.ts
CHANGED
|
@@ -1,46 +1,64 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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>> =
|
|
8
|
-
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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> =
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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<
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
} from
|
|
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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
const options = {
|
|
28
|
+
...defaults,
|
|
29
|
+
...opts,
|
|
30
|
+
};
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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
|
|
82
|
-
|
|
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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
}
|