import { Request } from 'express' ; import { NOT_FOUND } from 'http-status' ; import { expectationFailed } from '@hapi/boom' ; import { IResponse } from '@interfaces' ; import { getStatusCode } from '@utils/http.util' ; import { CacheService } from '@services/cache.service' ; description class Resolve { description private static instance : Resolve ; private constructor ( ) { } description static get ( ) { if ( ! Resolve . instance ) { Resolve . instance = new Resolve ( ) ; } return Resolve . instance ; } description param req param res param next async write ( req , res , next ) { const hasContent = typeof res . locals ?. data !== 'undefined' ; const hasNullContent = res . locals . data === null ; const hasStatusCodeOnResponse = typeof res . statusCode !== 'undefined' ; const status = getStatusCode ( req . method , ( hasContent && ! hasNullContent ) ) ; if ( req . method === 'DELETE' ) { if ( isNaN ( parseInt ( req . url . split ( '/' ) . pop ( ) , 10 ) ) ) { return next ( expectationFailed ( 'ID parameter must be a number' ) ) ; } res . status ( status ) ; res . end ( ) ; return ; } if ( ! hasContent ) { next ( ) ; } if ( ( hasContent && [ 'GET' , 'POST' , 'PUT' , 'PATCH' ] . includes ( req . method ) ) || ( hasStatusCodeOnResponse && res . statusCode !== NOT_FOUND ) ) { if ( CacheService . isCachable ( req ) ) { CacheService . engine . put ( CacheService . key ( req ) , res . locals . data , CacheService . duration ) ; } res . status ( status ) ; if ( res . locals . meta ) { res . json ( res . locals ) ; } else { res . json ( res . locals . data ) ; } } } } const resolve = Resolve . get ( ) ; export { resolve as Resolve } ;