import { NextFunction, Request, Response, Router } from 'express'; import { SubjectSubscriber } from 'rxjs/Subject'; export class ApiResult { success = false; message: string = null; data: any = null; get raw(): {} { return { success: this.success, message: this.message, data: this.data }; } constructor(private res: Response) {} setError(error) { this.success = false; if (typeof(error) === "object" && error instanceof Error) { this.message = error.message; } else if (typeof(error) === "string") { this.message = error; } else { this.message = "Unknown error object type."; } this.send(); } setSuccess(data?: any, message?: string) { this.success = true; this.message = message || "Ok"; this.data = data; this.send(); } send() { this.res.send(JSON.stringify(this.raw)); this.res.end(); } }