From 3c5bf2fc3c49010db2c66cf09813c168a56657ba Mon Sep 17 00:00:00 2001 From: Ibrahim Cesar Date: Sat, 29 May 2021 20:46:25 -0300 Subject: [PATCH] WIP: adiing dynamoDB adapter for tasks #2 and #4 --- demo/api/index.ts | 2 +- demo/api/lambda/api.ts | 5 +++- demo/package-lock.json | 15 +++++------ demo/package.json | 2 +- lib/index.ts | 57 ++++++++++++++++++++++++++++++++++-------- package.json | 5 ++-- 6 files changed, 64 insertions(+), 22 deletions(-) diff --git a/demo/api/index.ts b/demo/api/index.ts index 6b3ad0e..e729d05 100644 --- a/demo/api/index.ts +++ b/demo/api/index.ts @@ -16,7 +16,7 @@ export class ApiStack extends cdk.Stack { description: "An lambda to test", tracing: lambda.Tracing.ACTIVE, environment: { - UPSTASH_REDISS: "", + UPSTASH_REDISS: "rediss://:5faa9cfe16d44ed1adbe366ab22623a7@us1-merry-worm-33404.upstash.io:33404", }, }); diff --git a/demo/api/lambda/api.ts b/demo/api/lambda/api.ts index cdfeb2c..2a7e5f3 100644 --- a/demo/api/lambda/api.ts +++ b/demo/api/lambda/api.ts @@ -28,7 +28,10 @@ async function baseHandler( let handler = middy(baseHandler); handler.use(jsonBodyParser()).use( idempotent({ - client: new Redis(process.env.UPSTASH_REDISS), + adapter: { + port: "redis" + client: new Redis(process.env.UPSTASH_REDISS), + } }) ); diff --git a/demo/package-lock.json b/demo/package-lock.json index d15b92c..e6c2e7e 100644 --- a/demo/package-lock.json +++ b/demo/package-lock.json @@ -42,7 +42,7 @@ "css-loader": "^5.2.0", "esbuild": "^0.8.44", "eslint": "^7.23.0", - "middy-idempotent": "0.0.11", + "middy-idempotent": "0.0.21", "prettier": "^2.2.1", "rollup-plugin-banner2": "^1.2.2", "rollup-plugin-copy": "^3.4.0", @@ -8110,10 +8110,11 @@ } }, "node_modules/middy-idempotent": { - "version": "0.0.11", - "resolved": "https://registry.npmjs.org/middy-idempotent/-/middy-idempotent-0.0.11.tgz", - "integrity": "sha512-7XDdyd9l6mJIIdDO6yJqAhfu0ly/sidI3Zj6ozdMGCkknHAnYufPxIWweAHBzjhnY5jx7fd0Y5A6HaVnVa63Rw==", + "version": "0.0.21", + "resolved": "https://registry.npmjs.org/middy-idempotent/-/middy-idempotent-0.0.21.tgz", + "integrity": "sha512-J0ByDyQjbYjOCmhndSP25pdo2ukpFskTssvS1dTqKRKs4OGcr62oHa44yQ78Y7ZG4qcFOzgaZVIjgrFVaQi0xQ==", "dev": true, + "license": "MIT", "peerDependencies": { "@middy/core": ">=2.0.0", "@middy/http-json-body-parser": ">=2.0.0", @@ -17259,9 +17260,9 @@ } }, "middy-idempotent": { - "version": "0.0.11", - "resolved": "https://registry.npmjs.org/middy-idempotent/-/middy-idempotent-0.0.11.tgz", - "integrity": "sha512-7XDdyd9l6mJIIdDO6yJqAhfu0ly/sidI3Zj6ozdMGCkknHAnYufPxIWweAHBzjhnY5jx7fd0Y5A6HaVnVa63Rw==", + "version": "0.0.21", + "resolved": "https://registry.npmjs.org/middy-idempotent/-/middy-idempotent-0.0.21.tgz", + "integrity": "sha512-J0ByDyQjbYjOCmhndSP25pdo2ukpFskTssvS1dTqKRKs4OGcr62oHa44yQ78Y7ZG4qcFOzgaZVIjgrFVaQi0xQ==", "dev": true, "requires": {} }, diff --git a/demo/package.json b/demo/package.json index fdddc44..ea9ac19 100644 --- a/demo/package.json +++ b/demo/package.json @@ -34,7 +34,7 @@ "css-loader": "^5.2.0", "esbuild": "^0.8.44", "eslint": "^7.23.0", - "middy-idempotent": "0.0.11", + "middy-idempotent": "0.0.21", "prettier": "^2.2.1", "rollup-plugin-banner2": "^1.2.2", "rollup-plugin-copy": "^3.4.0", diff --git a/lib/index.ts b/lib/index.ts index 28d430d..b9e5b54 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -1,7 +1,10 @@ import crypto from "crypto"; - +type Adapters = 'redis' | 'dynamoDB' | 'upstash' | 'DynamoDB' interface Idempotent { - client?: any; + adapter: { + client: any, + port: Adapters + } path?: "rawPath" | "rawQueryString"; header?: string; body?: true | string; @@ -14,35 +17,62 @@ const createHash = (event: any): string => { .digest("base64"); }; -class Persistence { +const portError = `Adapter not found. Did you check you are passing one valid client and a port name? +For client check if is the correct one and if is being shipped in the lambda. +For port, possibles values are: 'redis', 'dynamoDB', 'upstash','DynamoDB'` + +class Port { adapter: any; + port: Adapters; hash: string; response?: string; - constructor(adapter: any, hash: string, response?: string) { + constructor(adapter: any, port: Adapters, hash: string, response?: string) { this.adapter = adapter, this.hash = hash this.response = response + this.port = port } - + async get(): Promise { - return await this.adapter.get(this.hash); + + if (this.port === 'DynamoDB' || 'dynamodb') { + // TODO: Logic for DynamoDB + } + + if (this.port === 'upstash' || 'redis') { + return await this.adapter.get(this.hash); + } + + throw new Error(portError) } async set(): Promise { - return await this.adapter.set(this.hash, this.response) + if (this.port === 'DynamoDB' || 'dynamodb') { + // TODO: Logic for DynamoDB + } + + if (this.port === 'upstash' || 'redis' ) { + return await this.adapter.set(this.hash, this.response) + } + + throw new Error(portError) } } -const defaults = { body: null, client: null, header: null, path: null }; +const defaults = { body: null, port: null, header: null, path: null }; const idempotent = ({ ...opts }: Idempotent) => { const options = { ...defaults, ...opts }; const idempotentBefore = async (request: any): Promise => { let hash = ""; + if (options.adapter.port !== 'redis' || 'dynamoDB' || 'upstash' || 'DynamoDB') { + throw new Error(`You need to pass a valid value for adapter name. possibles values are: 'redis', 'dynamoDB', 'upstash','DynamoDB'. And you need use their respectives clients`) + } + hash = createHash(request.event); if (options.path) { @@ -61,7 +91,10 @@ const idempotent = ({ ...opts }: Idempotent) => { } } - const getByHash = await new Persistence(options.client, hash).get(); + const getByHash = await new Port( + options.adapter.client, + options.adapter.port, + hash).get(); if (getByHash) { return JSON.parse(getByHash); @@ -72,7 +105,11 @@ const idempotent = ({ ...opts }: Idempotent) => { const responseStr = JSON.stringify(request.response); - await new Persistence(options.client, hash, responseStr).set(); + await new Port( + options.adapter.client, + options.adapter.port, + hash, + responseStr).set(); }; const idempotentOnError = async (request: any) => { console.error(request); diff --git a/package.json b/package.json index eee2e4b..bc09399 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,14 @@ { "name": "middy-idempotent", - "version": "0.0.13", + "version": "0.0.21", "description": "An Idempotent Middleware for Middy", "main": "dist/index.js", "module": "dist/index.es.js", "types": "dist/index.d.ts", "scripts": { "build": "npx rollup -c", - "pub": "npx rollup -c && npm publish" + "pub": "npx rollup -c && npm publish", + "beta": "npx rollup -c && npm publish --tag beta" }, "repository": { "type": "git",