import * as express from 'express' ; export abstract class BaseController { protected request : express . Request ; protected response : express . Response ; protected abstract executeImpl ( ) ; public execute ( request , response ) { this . request = request ; this . response = response ; this . executeImpl ( ) ; } public static jsonResponse ( response , code , message ) { return response . status ( code ) . json ( { message } ) ; } public ok < T > ( dto ? ) { if ( dto ) { return this . response . status ( 200 ) . json ( dto ) ; } return this . response . sendStatus ( 200 ) ; } public created ( ) { return this . response . sendStatus ( 201 ) ; } public clientError ( message ? ) { return BaseController . jsonResponse ( this . response , 400 , message || 'Unauthorized' ) ; } public unauthorized ( message ? ) { return BaseController . jsonResponse ( this . response , 401 , message || 'Unauthorized' ) ; } public forbidden ( message ? ) { return BaseController . jsonResponse ( this . response , 403 , message || 'Forbidden' ) ; } public notFound ( message ? ) { return BaseController . jsonResponse ( this . response , 404 , message || 'Not found' ) ; } public conflict ( message ? ) { return BaseController . jsonResponse ( this . response , 409 , message || 'Conflict' ) ; } public tooMany ( message ? ) { return BaseController . jsonResponse ( this . response , 429 , message || 'Too many requests' ) ; } public fail ( error ) { console . log ( error ) ; return this . response . status ( 500 ) . json ( { message : error . toString ( ) , } ) ; } }