Skip to content

Commit

Permalink
Merge pull request #72 from ainize-team/feat/woojae/duplicateFilter
Browse files Browse the repository at this point in the history
specific api filter
  • Loading branch information
akastercomcom authored Nov 3, 2023
2 parents 7a1b599 + 3dec80e commit 1df7b27
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/middlewares/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default class Middleware {
* @returns Null if if request is duplicated.
*/
triggerDuplicateFilter = (req: Request, res: Response, next: NextFunction) => {
if (req.body.fid === undefined){
if (req.body.transaction.hash === undefined){
next();
}
const txHash = req.body.transaction.hash;
Expand All @@ -28,4 +28,24 @@ export default class Middleware {
this.cache.set(txHash, "in_progress", 500);
next();
}
/**
* Middleware for AI Network trigger call. It will filter duplicated request triggered by same transaction.
* It will pass request which is not from AI Network trigger.
* You can set filter inside specific api.
* @param {Request} request - Request data
* @param {Res} response - Response data
* @returns Null if if request is duplicated.
*/
triggerFilter = (req: Request, res: Response) => {
if (req.body.fid || req.body.transaction){
res.send("not from trigger");
return;
}
const txHash = req.body.transaction.hash;
if (this.cache.get(txHash) && this.cache.get(txHash) !== "error") {
res.send("duplicated");
return;
}
this.cache.set(txHash, "in_progress", 500);
}
}

0 comments on commit 1df7b27

Please sign in to comment.