From 088deb5b9d41ada7e90b2d474a15a92c75281c4b Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 31 Mar 2020 14:03:26 +0200 Subject: [PATCH] Start to create base request handler --- src/entities/BaseRequestsHandler.ts | 38 ++++++++++++++++++++++++++ src/entities/RequestHandler.ts | 42 +++++++++-------------------- src/main.ts | 11 +++++++- 3 files changed, 60 insertions(+), 31 deletions(-) create mode 100644 src/entities/BaseRequestsHandler.ts diff --git a/src/entities/BaseRequestsHandler.ts b/src/entities/BaseRequestsHandler.ts new file mode 100644 index 0000000..dae30a4 --- /dev/null +++ b/src/entities/BaseRequestsHandler.ts @@ -0,0 +1,38 @@ +/** + * 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; + } +} \ No newline at end of file diff --git a/src/entities/RequestHandler.ts b/src/entities/RequestHandler.ts index 325f1cb..e240514 100644 --- a/src/entities/RequestHandler.ts +++ b/src/entities/RequestHandler.ts @@ -16,6 +16,7 @@ import { PostsHelper } from "../helpers/PostsHelper"; import { PostAccessLevel } from "./Post"; import { writeFileSync } from "fs"; import { CommentsHelper } from "../helpers/CommentsHelper"; +import { BaseRequestsHandler } from "./BaseRequestsHandler"; /** * Response to a request @@ -23,14 +24,20 @@ import { CommentsHelper } from "../helpers/CommentsHelper"; * @author Pierre HUBERT */ -export class RequestHandler { +export class RequestHandler extends BaseRequestsHandler { private client : APIClient = null; private userID : number = -1; private responseSent = false; - public constructor(private req : Request, private response : Response) {} + public constructor(private req : Request, private response : Response) { + super(); + } + + public get sentResponse() { + return this.responseSent; + } /** * Get remote IP address @@ -45,34 +52,10 @@ export class RequestHandler { * * @param name Name of the parameter to get */ - private getPostParam(name : string) : any { + protected getPostParam(name : string) : any { return this.req.body[name]; } - /** - * 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; - } - /** * Check out whether a post parameter is present into the request or not * @@ -579,7 +562,7 @@ export class RequestHandler { * @param code HTTP Status code * @param message The message to send */ - public error(code : number, message : string, should_throw: boolean = true) { + public error(code : number, message : string) { if(this.responseSent) return; @@ -593,8 +576,7 @@ export class RequestHandler { this.responseSent = true; - if(should_throw) - throw Error("Could not complete request! ("+ message +")"); + throw Error("Could not complete request! ("+ message +")"); } /** diff --git a/src/main.ts b/src/main.ts index 2615768..199a315 100644 --- a/src/main.ts +++ b/src/main.ts @@ -59,7 +59,16 @@ async function init() { } catch(e) { console.error(e); - handler.error(500, "Internal error.", false); + + // Send a response to the server, if it has not + // been done yet + if(!handler.sentResponse) + res.status(500).send({ + error: { + code: 500, + message: "Internal error" + } + }) } };