mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2024-11-21 21:09:22 +00:00
Start to create base request handler
This commit is contained in:
parent
a597e6eeb3
commit
088deb5b9d
38
src/entities/BaseRequestsHandler.ts
Normal file
38
src/entities/BaseRequestsHandler.ts
Normal file
@ -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;
|
||||
}
|
||||
}
|
@ -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 +")");
|
||||
}
|
||||
|
||||
/**
|
||||
|
11
src/main.ts
11
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"
|
||||
}
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user