diff --git a/package.json b/package.json index b6ce163..a77b224 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ainize-team/ainize-sdk", - "version": "1.0.1", + "version": "1.0.2", "main": "dist/ainize.js", "types": "dist/ainize.d.ts", "scripts": { diff --git a/src/middlewares/middleware.ts b/src/middlewares/middleware.ts index e373036..e619353 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,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); + } } \ No newline at end of file