import { ExecutionContext , HttpException , Injectable } from '@nestjs/common' ; import { Reflector } from '@nestjs/core' ; import { style } from '@ogma/styler' ; import { OgmaInterceptorServiceOptions } from '../../interfaces/ogma-options.interface' ; import { InterceptorService } from '../interfaces/interceptor-service.interface' ; import { MetaLogObject } from '../interfaces/log.interface' ; @ Injectable ( ) export abstract class AbstractInterceptorService implements InterceptorService { constructor ( protected readonly reflector ) { } param dataLength param context param startTime param options returns getSuccessContext ( dataLength , context , startTime , options ) { return { callerAddress : this . getCallerIp ( context ) , method : this . getMethod ( context ) , callPoint : this . getCallPoint ( context ) , responseTime : this . getResponseTime ( startTime ) , contentLength : dataLength , protocol : this . getProtocol ( context ) , status : this . getStatus ( context , options . color && ! options . json ) , meta : this . getMeta ( context ) , } ; } param error param context param startTime param options returns getErrorContext ( error , context , startTime , options ) { return { callerAddress : this . getCallerIp ( context ) , method : this . getMethod ( context ) , callPoint : this . getCallPoint ( context ) , status : this . getStatus ( context , options . color && ! options . json , error ) , responseTime : this . getResponseTime ( startTime ) , contentLength : Buffer . from ( JSON . stringify ( error . message ) ) . byteLength , protocol : this . getProtocol ( context ) , meta : this . getMeta ( context ) , } ; } param context param inColor param error returns getStatus ( _context , inColor , error ? ) { const status = error ? 500 : 200 ; return inColor ? this . wrapInColor ( status ) : status . toString ( ) ; } param context returns getMeta ( _context ) { return ; } param context abstract getCallerIp ( context ) ; param context abstract getMethod ( context ) ; private getResponseTime ( startTime ) { return Date . now ( ) - startTime ; } param context abstract getProtocol ( context ) ; param context abstract getCallPoint ( context ) ; param context param requestId abstract setRequestId ( context , requestId ) ; protected wrapInColor ( status ) { let statusString ; if ( this . isBetween ( status , 100 , 300 ) ) { statusString = style . green . apply ( status ) ; } else if ( this . isBetween ( status , 300 , 400 ) ) { statusString = style . cyan . apply ( status ) ; } else if ( this . isBetween ( status , 400 , 500 ) ) { statusString = style . yellow . apply ( status ) ; } else if ( this . isBetween ( status , 500 , 600 ) ) { statusString = style . red . apply ( status ) ; } else { statusString = style . white . apply ( status ) ; } return statusString ; } protected isBetween ( comparator , bottom , top ) { return comparator >= bottom && comparator < top ; } }