mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2024-11-29 16:56:29 +00:00
27 lines
750 B
TypeScript
27 lines
750 B
TypeScript
|
/**
|
||
|
* API limits manager
|
||
|
*
|
||
|
* @author Pierre HUBERT
|
||
|
*/
|
||
|
|
||
|
import { RequestHandler } from "../entities/RequestHandler";
|
||
|
import { Action, APILimitHelper } from "../helpers/APILimitsHelper";
|
||
|
|
||
|
/**
|
||
|
* Trigger query limiter
|
||
|
*
|
||
|
* @param h Request handler
|
||
|
* @param action The action to check
|
||
|
* @param trigger TRUE if the counter has to be increased by one / else it is a simple check
|
||
|
*/
|
||
|
export async function limit_query(h: RequestHandler, action: Action, trigger: boolean) {
|
||
|
|
||
|
// Increment the number of actions / failures done by the user
|
||
|
if(trigger) {
|
||
|
await APILimitHelper.Trigger(h.remoteIP, action)
|
||
|
}
|
||
|
|
||
|
// Check for counter
|
||
|
if(await APILimitHelper.Count(h.remoteIP, action) > 10)
|
||
|
h.error(429, "Too many request. Please try again later.")
|
||
|
}
|