import { RESPONSE } from '@nguniversal/express-engine/tokens' ; import { Inject , Injectable , Optional } from '@angular/core' ; import { Response } from 'express' ; import ms = require ( 'ms' ) ; export interface IServerResponseService { getHeader ( key ) : string ; setHeader ( key , value ) : this ; setHeaders ( dictionary ) : this ; appendHeader ( key , value , delimiter ? ) : this ; setStatus ( code , message ? ) : this ; setNotFound ( message ? ) : this ; setError ( message ? ) : this ; } @ Injectable ( ) export class ServerResponseService implements IServerResponseService { private response : Response ; constructor ( @ Optional ( ) @ Inject ( RESPONSE ) response ) { this . response = response ; } getHeader ( key ) { return this . response . getHeader ( key ) as string ; } setHeader ( key , value ) { if ( this . response ) this . response . header ( key , value ) ; return this ; } appendHeader ( key , value , delimiter = ',' ) { if ( this . response ) { const current = this . getHeader ( key ) ; if ( ! current ) return this . setHeader ( key , value ) ; const newValue = [ ... current . split ( delimiter ) , value ] . filter ( ( el , i , a ) => i === a . indexOf ( el ) ) . join ( delimiter ) ; this . response . header ( key , newValue ) ; } return this ; } setHeaders ( dictionary ) { if ( this . response ) Object . keys ( dictionary ) . forEach ( key => this . setHeader ( key , dictionary [ key ] ) ) ; return this ; } setStatus ( code , message ? ) { if ( this . response ) { this . response . statusCode = code ; if ( message ) this . response . statusMessage = message ; } return this ; } setNotFound ( message = 'not found' ) { if ( this . response ) { this . response . statusCode = 404 ; this . response . statusMessage = message ; } return this ; } setUnauthorized ( message = 'Unauthorized' ) { if ( this . response ) { this . response . statusCode = 401 ; this . response . statusMessage = message ; } return this ; } setCachePrivate ( ) { if ( this . response ) { this . setCache ( 'private' ) ; } return this ; } setCacheNone ( ) { if ( this . response ) { this . setHeader ( 'Cache-Control' , 'no-cache, no-store, must-revalidate' ) ; this . setHeader ( 'Pragma' , 'no-cache' ) ; } return this ; } setCache ( directive , maxAge ? , smaxAge ? ) { if ( this . response ) { if ( smaxAge ) { this . setHeader ( 'Cache-Control' , `${ directive }, max-age=${ maxAge ? ms ( maxAge ) / 1000 : 0 }, s-maxage=${ ms ( smaxAge ) / 1000 }` ) ; } else { this . setHeader ( 'Cache-Control' , `${ directive }, max-age=${ maxAge ? ms ( maxAge ) / 1000 : 0 }` ) ; } this . setHeader ( 'Expires' , maxAge ? new Date ( Date . now ( ) + ms ( maxAge ) ) . toUTCString ( ) : new Date ( Date . now ( ) ) . toUTCString ( ) ) ; } return this ; } setError ( message = 'internal server error' ) { if ( this . response ) { this . response . statusCode = 500 ; this . response . statusMessage = message ; } return this ; } } export type HttpCacheDirective = 'public' | 'private' | 'no-store' | 'no-cache' | 'must-revalidate' | 'no-transform' | 'proxy-revalidate' ;