From 2b8e7e31c699a0e33146167ad150865c1f4b38ac Mon Sep 17 00:00:00 2001 From: akaster99 Date: Fri, 3 Nov 2023 14:54:54 +0900 Subject: [PATCH 1/2] filter specific --- src/middlewares/middleware.ts | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/middlewares/middleware.ts b/src/middlewares/middleware.ts index e373036..2a55ff0 100644 --- a/src/middlewares/middleware.ts +++ b/src/middlewares/middleware.ts @@ -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; @@ -28,4 +28,28 @@ 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 === undefined){ + res.send("not from trigger"); + return; + } + if (req.body.transaction === undefined){ + 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); + } } \ No newline at end of file From 3dec80e573b70a07744d9751bc099004b1ab6112 Mon Sep 17 00:00:00 2001 From: akaster99 Date: Fri, 3 Nov 2023 15:33:57 +0900 Subject: [PATCH 2/2] refactor: merge --- src/middlewares/middleware.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/middlewares/middleware.ts b/src/middlewares/middleware.ts index 2a55ff0..e619353 100644 --- a/src/middlewares/middleware.ts +++ b/src/middlewares/middleware.ts @@ -37,11 +37,7 @@ export default class Middleware { * @returns Null if if request is duplicated. */ triggerFilter = (req: Request, res: Response) => { - if (req.body.fid === undefined){ - res.send("not from trigger"); - return; - } - if (req.body.transaction === undefined){ + if (req.body.fid || req.body.transaction){ res.send("not from trigger"); return; }