import { ContextType , ExecutionContext , Injectable } from '@nestjs/common' ; import { OgmaInterceptorServiceOptions } from '../../interfaces' ; import { DelegatorContextReturn , LogObject , MetaLogObject } from '../interfaces/log.interface' ; import { HttpInterceptorService } from './http-interceptor.service' ; import { GqlInterceptorService } from './gql-interceptor.service' ; import { WebsocketInterceptorService } from './websocket-interceptor.service' ; import { RpcInterceptorService } from './rpc-interceptor.service' ; type Parser = 'httpParser' | 'gqlParser' | 'wsParser' | 'rpcParser' ; @ Injectable ( ) export class DelegatorService { constructor ( private readonly httpParser , private readonly wsParser , private readonly rpcParser , private readonly gqlParser ) { } setRequestId ( context , requestId ) { const parser = this . getParser ( context . getType ( ) ) ; this [ parser ] . setRequestId ( context , requestId ) ; } getContextSuccessString ( data , context , startTime , options ) { data = data ? JSON . stringify ( data ) : '' ; data = Buffer . from ( data ) . byteLength ; const parser = this . getParser ( context . getType ( ) ) ; const { meta , ... logObject } = this . getContextString ( { method : 'getSuccessContext' , data , context , startTime , options , parser , } ) ; return { meta , log : this . getStringOrObject ( logObject , { json : options . json } ) } ; } private getParser ( type ) { let parser ; switch ( type ) { case 'http' : parser = 'httpParser' ; break ; case 'graphql' : parser = 'gqlParser' ; break ; case 'ws' : parser = 'wsParser' ; break ; case 'rpc' : parser = 'rpcParser' ; break ; } return parser ; } getContextErrorString ( error , context , startTime , options ) { const parser = this . getParser ( context . getType ( ) ) ; const { meta , ... logObject } = this . getContextString ( { method : 'getErrorContext' , data : error , context , startTime , options , parser , } ) ; return { meta , log : this . getStringOrObject ( logObject , { json : options . json } ) } ; } private getContextString ( { method , data , context , startTime , options , parser , } ) { return this [ parser ] [ method ] ( data , context , startTime , options ) ; } private getStringOrObject ( data , options ) { return options . json ? data : `${ data . callerAddress } - ${ data . method } ${ data . callPoint } ${ data . protocol } ${ data . status } ${ data . responseTime }ms - ${ data . contentLength }` ; } }