From 6763b26b1291251c2fb0e1fb76bc40a108dba6df Mon Sep 17 00:00:00 2001 From: CounterFire2023 <136581895+CounterFire2023@users.noreply.github.com> Date: Fri, 1 Mar 2024 10:22:44 +0800 Subject: [PATCH] add decorator for rate limit --- src/decorators/router.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/decorators/router.ts b/src/decorators/router.ts index d1dafca..f214e78 100644 --- a/src/decorators/router.ts +++ b/src/decorators/router.ts @@ -15,6 +15,8 @@ export class RouterMap { permissions?: string[][] data?: RouterData[] depts?: string[] + limit?: any + limitMethod?: Function } > = new Map() } @@ -140,3 +142,25 @@ export function dept(depts?: string | string[]) { } } } + +/** + * 是否需要限流 + * 使用 @fastify/rate-limit + */ +export function limit(opt?: any) { + return (target: BaseController, name: string, value: PropertyDescriptor) => { + // @ts-ignore + const key = target[name] + let limitObj = { limit: opt || true } + if (RouterMap.decoratedRouters.has(key)) { + let objCurrent = RouterMap.decoratedRouters.get(key) + Object.assign(objCurrent, limitObj) + // @ts-ignore + RouterMap.decoratedRouters.set(target[name], objCurrent) + } else { + // @ts-ignore + RouterMap.decoratedRouters.set(target[name], limitObj) + } + } +} +