1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-24 06:19:22 +00:00
comunicapiv2/src/controllers/APILimitsController.ts

27 lines
750 B
TypeScript
Raw Normal View History

2020-03-25 08:04:04 +00:00
/**
* 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.")
}