Skip to content

Commit

Permalink
WIP: adiing dynamoDB adapter for tasks #2 and #4
Browse files Browse the repository at this point in the history
  • Loading branch information
Ibrahim Cesar committed May 29, 2021
1 parent 18ecbdb commit 3c5bf2f
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 22 deletions.
2 changes: 1 addition & 1 deletion demo/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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://:[email protected]:33404",
},
});

Expand Down
5 changes: 4 additions & 1 deletion demo/api/lambda/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
})
);

Expand Down
15 changes: 8 additions & 7 deletions demo/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
57 changes: 47 additions & 10 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<string> {
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<any> {
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<any> => {
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) {
Expand All @@ -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);
Expand All @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 3c5bf2f

Please sign in to comment.