1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-22 13:29:22 +00:00

Can safely get an integer

This commit is contained in:
Pierre HUBERT 2019-11-23 14:38:48 +01:00
parent e635802296
commit c1068be8f1

View File

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