diff --git a/src/entities/RequestHandler.ts b/src/entities/RequestHandler.ts index 73562ba..b4a750c 100644 --- a/src/entities/RequestHandler.ts +++ b/src/entities/RequestHandler.ts @@ -66,6 +66,29 @@ export class RequestHandler { return email; } + /** + * Get an integer included in the request + * + * @param name Name of POST field + * @param fallback Fallback value (if none, throw an error) + * @returns The number (throws in case of error) + */ + public postInt(name: string, fallback ?: number) : number { + const param = this.getPostParam(name); + + if(param == undefined) { + if(!fallback) + this.error(400, "Missing integer '"+name+"' in the request!"); + return fallback; + } + + // Check number + if(Number.parseInt(param).toString() !== param.toString()) + this.error(400, "'"+name+"' is an invalid integer!"); + + return Number.parseInt(param); + } + /** * Validate API tokens