Skip to content

Commit

Permalink
Add option for TTL (#7)
Browse files Browse the repository at this point in the history
Co-authored-by: Göran Nehlin <[email protected]>
  • Loading branch information
Nehlin and Göran Nehlin authored Sep 22, 2021
1 parent 98d1386 commit 3d75bb2
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import crypto from "crypto";

interface Idempotent {
client?: any;
ttl?: number;
}

const createHash = (event: any): string => {
Expand All @@ -11,7 +12,7 @@ const createHash = (event: any): string => {
.digest("base64");
};

const defaults = { client: null };
const defaults = { client: null, ttl: null };

const idempotent = ({ ...opts }: Idempotent) => {
const options = { ...defaults, ...opts };
Expand All @@ -29,7 +30,11 @@ const idempotent = ({ ...opts }: Idempotent) => {

const responseStr = JSON.stringify(request.response);

await options.client.set(hash, responseStr);
if (options.ttl) {
await options.client.set(hash, responseStr, 'ex', options.ttl);
} else {
await options.client.set(hash, responseStr);
}
};
const idempotentOnError = async (request: any) => {
console.error(request);
Expand Down

0 comments on commit 3d75bb2

Please sign in to comment.