import { Provider , Scope } from '@nestjs/common' ; import { REQUEST as CONTEXT , Reflector } from '@nestjs/core' ; import { Ogma , OgmaOptions } from '@ogma/logger' ; import { OGMA_INSTANCE , OGMA_REQUEST_SCOPED_SERVICE_TOKEN , OGMA_SERVICE_TOKEN , } from './ogma.constants' ; import { OgmaService } from './ogma.service' ; import { AbstractInterceptorService } from './interceptor/providers' ; import { OgmaInterceptorOptions , OgmaModuleOptions , OgmaServiceOptions , Type } from './interfaces' ; import { RequestContext } from './interfaces/request-context.interface' ; internal export function createOgmaProvider ( options ? ) { return new Ogma ( { ... options , application : options ?. application || 'Nest' , } ) ; } function mergeInterceptorDefaults ( options ) { const mergedOptions = { ... { http : false , ws : false , rpc : false , gql : false } , ... options , } ; return mergedOptions ; } export function createOgmaInterceptorOptionsFactory ( options ) { const intOpts = options ?. interceptor ; if ( intOpts === false ) { return intOpts ; } return mergeInterceptorDefaults ( intOpts ) ; } export function createOgmaServiceOptions ( options ) { return options . service ; } export function createProviderToken ( topic ) { return OGMA_SERVICE_TOKEN + ':' + topic ; } export function createRequestScopedProviderToken ( topic ) { return OGMA_REQUEST_SCOPED_SERVICE_TOKEN + ':' + topic ; } export function createLoggerProviders ( topic ) { topic = typeof topic === 'function' ? topic . name : topic ; const token = createProviderToken ( topic ) ; return [ { inject : [ OGMA_INSTANCE ] , provide : token , useFactory : ( ogmaInstance ) : OgmaService => { return new OgmaService ( ogmaInstance , topic as string ) ; } , } , ] ; } export function createRequestScopedLoggerProviders ( topic ) { topic = typeof topic === 'function' ? topic . name : topic ; const token = createRequestScopedProviderToken ( topic ) ; return [ { inject : [ OGMA_INSTANCE , CONTEXT ] , provide : token , scope : Scope . REQUEST , useFactory : ( ogmaInstance , requestContext ) : OgmaService => { return new OgmaService ( ogmaInstance , topic as string , requestContext ) ; } , } , ] ; } export const interceptorProviderFactory = ( type , backupClass ) : ( ( opt : OgmaInterceptorOptions , reflector : Reflector ) => Type < AbstractInterceptorService > ) => ( intOpts , reflector ) : Type < AbstractInterceptorService > => intOpts [ type ] ? new ( intOpts as Type < AbstractInterceptorService > ) [ type ] ( reflector ) : new backupClass ( reflector ) ;