@middy/dynamodb 4.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017-2023 [Luciano Mammino](https://github.com/lmammino), [will Farrell](https://github.com/willfarrell) and the [Middy team](https://github.com/middyjs/middy/graphs/contributors)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,45 @@
1
+ <div align="center">
2
+ <h1>Middy dynamodb middleware</h1>
3
+ <img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.svg"/>
4
+ <p><strong>Secrets Manager middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda</strong></p>
5
+ <p>
6
+ <a href="https://www.npmjs.com/package/@middy/dynamodb?activeTab=versions">
7
+ <img src="https://badge.fury.io/js/%40middy%2Fdynamodb.svg" alt="npm version" style="max-width:100%;">
8
+ </a>
9
+ <a href="https://packagephobia.com/result?p=@middy/dynamodb">
10
+ <img src="https://packagephobia.com/badge?p=@middy/dynamodb" alt="npm install size" style="max-width:100%;">
11
+ </a>
12
+ <a href="https://github.com/middyjs/middy/actions/workflows/tests.yml">
13
+ <img src="https://github.com/middyjs/middy/actions/workflows/tests.yml/badge.svg?branch=main&event=push" alt="GitHub Actions CI status badge" style="max-width:100%;">
14
+ </a>
15
+ <br/>
16
+ <a href="https://standardjs.com/">
17
+ <img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
18
+ </a>
19
+ <a href="https://snyk.io/test/github/middyjs/middy">
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
+ </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%;">
24
+ </a>
25
+ <a href="https://bestpractices.coreinfrastructure.org/projects/5280">
26
+ <img src="https://bestpractices.coreinfrastructure.org/projects/5280/badge" alt="Core Infrastructure Initiative (CII) Best Practices" style="max-width:100%;">
27
+ </a>
28
+ <br/>
29
+ <a href="https://gitter.im/middyjs/Lobby">
30
+ <img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
31
+ </a>
32
+ <a href="https://stackoverflow.com/questions/tagged/middy?sort=Newest&uqlId=35052">
33
+ <img src="https://img.shields.io/badge/StackOverflow-[middy]-yellow" alt="Ask questions on StackOverflow" style="max-width:100%;">
34
+ </a>
35
+ </p>
36
+ <p>You can read the documentation at: <a href="https://middy.js.org/docs/middlewares/dynamodb">https://middy.js.org/docs/middlewares/dynamodb</a></p>
37
+ </div>
38
+
39
+ ## License
40
+
41
+ Licensed under [MIT License](LICENSE). Copyright (c) 2017-2023 [Luciano Mammino](https://github.com/lmammino), [will Farrell](https://github.com/willfarrell), and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).
42
+
43
+ <a href="https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large">
44
+ <img src="https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large" alt="FOSSA Status" style="max-width:100%;">
45
+ </a>
package/index.cjs ADDED
@@ -0,0 +1,69 @@
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
+
68
+
69
+ //# sourceMappingURL=index.cjs.map
package/index.d.ts ADDED
@@ -0,0 +1,26 @@
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 } from '@aws-sdk/client-dynamodb'
5
+
6
+ export type Options<AwsDynamoDBClient = DynamoDBClient> =
7
+ Omit<MiddyOptions<AwsDynamoDBClient, DynamoDBClientConfig>, 'fetchData'>
8
+ &
9
+ {
10
+ fetchData?: {
11
+ // TODO: add more precise type
12
+ [key: string]: Record<string, any>
13
+ }
14
+ }
15
+
16
+ export type Context<TOptions extends Options | undefined> = TOptions extends {
17
+ setToContext: true
18
+ }
19
+ ? LambdaContext & Record<keyof TOptions['fetchData'], any>
20
+ : LambdaContext
21
+
22
+ declare function dynamodbMiddleware<TOptions extends Options | undefined> (
23
+ options?: TOptions
24
+ ): middy.MiddlewareObj<unknown, any, Error, Context<TOptions>>
25
+
26
+ export default dynamodbMiddleware
package/index.js ADDED
@@ -0,0 +1,61 @@
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';
4
+ const defaults = {
5
+ AwsClient: DynamoDBClient,
6
+ awsClientOptions: {},
7
+ awsClientAssumeRole: undefined,
8
+ awsClientCapture: undefined,
9
+ fetchData: {},
10
+ disablePrefetch: false,
11
+ cacheKey: 'dynamodb',
12
+ cacheExpiry: -1,
13
+ setToContext: false
14
+ };
15
+ const dynamodbMiddleware = (opts = {})=>{
16
+ const options = {
17
+ ...defaults,
18
+ ...opts
19
+ };
20
+ for(const internalKey in options.fetchData){
21
+ options.fetchData[internalKey].Key = marshall(options.fetchData[internalKey].Key);
22
+ }
23
+ const fetch = (request, cachedValues = {})=>{
24
+ const values = {};
25
+ for(const internalKey in options.fetchData){
26
+ if (cachedValues[internalKey]) continue;
27
+ const inputParameters = options.fetchData[internalKey];
28
+ values[internalKey] = client.send(new GetItemCommand(inputParameters)).then((resp)=>unmarshall(resp.Item)).catch((e)=>{
29
+ const value = getCache(options.cacheKey).value ?? {};
30
+ value[internalKey] = undefined;
31
+ modifyCache(options.cacheKey, value);
32
+ throw e;
33
+ });
34
+ }
35
+ return values;
36
+ };
37
+ let prefetch, client;
38
+ if (canPrefetch(options)) {
39
+ client = createPrefetchClient(options);
40
+ prefetch = processCache(options, fetch);
41
+ }
42
+ const dynamodbMiddlewareBefore = async (request)=>{
43
+ if (!client) {
44
+ client = await createClient(options, request);
45
+ }
46
+ const { value } = prefetch ?? processCache(options, fetch, request);
47
+ Object.assign(request.internal, value);
48
+ if (options.setToContext) {
49
+ const data = await getInternal(Object.keys(options.fetchData), request);
50
+ Object.assign(request.context, data);
51
+ }
52
+ prefetch = null;
53
+ };
54
+ return {
55
+ before: dynamodbMiddlewareBefore
56
+ };
57
+ };
58
+ export default dynamodbMiddleware;
59
+
60
+
61
+ //# sourceMappingURL=index.js.map
package/package.json ADDED
@@ -0,0 +1,73 @@
1
+ {
2
+ "name": "@middy/dynamodb",
3
+ "version": "4.1.0",
4
+ "description": "DynamoDB middleware for the middy framework",
5
+ "type": "module",
6
+ "engines": {
7
+ "node": ">=16"
8
+ },
9
+ "engineStrict": true,
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "main": "./index.cjs",
14
+ "module": "./index.js",
15
+ "exports": {
16
+ ".": {
17
+ "import": {
18
+ "types": "./index.d.ts",
19
+ "default": "./index.js"
20
+ },
21
+ "require": {
22
+ "types": "./index.d.ts",
23
+ "default": "./index.cjs"
24
+ }
25
+ }
26
+ },
27
+ "types": "index.d.ts",
28
+ "files": [
29
+ "index.js",
30
+ "index.cjs",
31
+ "index.d.ts"
32
+ ],
33
+ "scripts": {
34
+ "test": "npm run test:unit",
35
+ "test:unit": "ava",
36
+ "test:benchmark": "node __benchmarks__/index.js"
37
+ },
38
+ "license": "MIT",
39
+ "keywords": [
40
+ "Lambda",
41
+ "Middleware",
42
+ "Serverless",
43
+ "Framework",
44
+ "AWS",
45
+ "AWS Lambda",
46
+ "Middy",
47
+ "DynamoDB"
48
+ ],
49
+ "author": {
50
+ "name": "Middy contributors",
51
+ "url": "https://github.com/middyjs/middy/graphs/contributors"
52
+ },
53
+ "repository": {
54
+ "type": "git",
55
+ "url": "github:middyjs/middy",
56
+ "directory": "packages/dynamodb"
57
+ },
58
+ "bugs": {
59
+ "url": "https://github.com/middyjs/middy/issues"
60
+ },
61
+ "homepage": "https://middy.js.org",
62
+ "dependencies": {
63
+ "@middy/util": "4.1.0"
64
+ },
65
+ "devDependencies": {
66
+ "@aws-sdk/client-dynamodb": "^3.245.0",
67
+ "@aws-sdk/util-dynamodb": "^3.245.0",
68
+ "@middy/core": "4.1.0",
69
+ "@types/aws-lambda": "^8.10.101",
70
+ "aws-xray-sdk": "^3.3.3"
71
+ },
72
+ "gitHead": "c5abbac9280c3a2f30758188233a7b27367901d1"
73
+ }