add decorator for rate limit

This commit is contained in:
CounterFire2023 2024-03-01 10:22:44 +08:00
parent c1946bbe7d
commit 6763b26b12

View File

@ -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)
}
}
}