import { appendThriftObject , IStructCodec , LogFunction , ProtocolType , readThriftObject , TransportType , } from '@creditkarma/thrift-server-core' ; import { IRequestResponse , IThriftClientFilterConfig , IThriftRequest , NextFunction , } from '@creditkarma/thrift-client' ; import { defaultLogger } from './logger' ; export interface IThriftContextOptions < RequestContext , ResponseContext > { RequestCodec : IStructCodec < RequestContext , any > ; ResponseCodec : IStructCodec < ResponseContext , any > ; transportType ? : TransportType ; protocolType ? : ProtocolType ; logger ? : LogFunction ; } export function ThriftClientContextFilter < RequestContext , ResponseContext > ( { RequestCodec , ResponseCodec , transportType = 'buffered' , protocolType = 'binary' , logger = defaultLogger , } ) { return { handler ( request , next ) { return appendThriftObject ( request . context , request . data , RequestCodec , transportType , protocolType ) . then ( ( extended ) => { return next ( extended , request . context ) . then ( ( response ) : Promise < IRequestResponse > => { return readThriftObject ( response . body , ResponseCodec , transportType , protocolType ) . then ( ( result ) : IRequestResponse => { return { statusCode : response . statusCode , headers : { thriftContext : result [ 0 ] , } , body : result [ 1 ] , } ; } , ( err ) => { logger ( [ 'warn' , 'ThriftClientContextFilter' ] , `Error reading context from Thrift response: ${ err . message }` ) ; return response ; } ) ; } ) ; } ) ; } , } ; }