mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2024-11-26 15:29:22 +00:00
38 lines
1.0 KiB
TypeScript
38 lines
1.0 KiB
TypeScript
|
/**
|
||
|
* Base requests handler
|
||
|
*
|
||
|
* @author Pierre Hubert
|
||
|
*/
|
||
|
|
||
|
export abstract class BaseRequestsHandler {
|
||
|
|
||
|
protected abstract getPostParam(name : string) : any;
|
||
|
public abstract hasPostParameter(name: string) : boolean;
|
||
|
public abstract error(code : number, message : string) : void;
|
||
|
public abstract success(message: string) : void;
|
||
|
public abstract send(data: any): void;
|
||
|
|
||
|
/**
|
||
|
* Get a String from the request
|
||
|
*
|
||
|
* @param name The name of the string to get
|
||
|
* @param minLength Minimal required size of the string
|
||
|
* @param required If set to true (true by default), an error will
|
||
|
* be thrown if the string is not included in the request
|
||
|
*/
|
||
|
public postString(name : string, minLength : number = 1, required : boolean = true) : string {
|
||
|
const param = this.getPostParam(name);
|
||
|
|
||
|
// Check if parameter was not found
|
||
|
if(param == undefined) {
|
||
|
if(required)
|
||
|
this.error(400, "Could not find required string: '"+name+"'");
|
||
|
return "";
|
||
|
}
|
||
|
|
||
|
if(param.length < minLength)
|
||
|
this.error(400, "Parameter "+name+" is too short!");
|
||
|
|
||
|
return param;
|
||
|
}
|
||
|
}
|