import { Express , Request , Response , NextFunction } from 'express' ; import methods , { Methods , Method } from './methods' ; type ResponseFunction = { [ key in Methods ] : ( data : any , meta ? : any ) => void ; } ; declare global { namespace Express { interface Response { formatter : ResponseFunction ; } } } const responseEnhancer = ( ) => ( req , res , next ) : void => { res . formatter = _generateFormatters ( res ) ; next ( ) ; } ; const _generateFormatters = ( res ) => { const formatter = { } as ResponseFunction ; let responseBody = { } ; methods . map ( ( method ) => { if ( method . isSuccess ) { formatter [ method . name ] = ( data , meta ) => { responseBody = _generateSuccessResponse ( { data , meta } ) ; res . status ( parseInt ( method . code ) ) . json ( responseBody ) ; } ; } else { formatter [ method . name ] = ( error , meta ) => { responseBody = _generateErrorResponse ( { error , meta } ) ; res . status ( parseInt ( method . code ) ) . json ( responseBody ) ; } ; } } ) ; return formatter ; } ; interface SuccessInput { data : any ; meta : any ; } const _generateSuccessResponse = ( { data , meta } ) => ( { data , meta , } ) ; interface ErrorsInput { error : any ; meta : any ; } const _generateErrorResponse = ( { error , meta } ) => ( { error , meta , } ) ; export { responseEnhancer } ;