import * as Sentry from '@sentry/node' ; import { Request , Response } from 'express' ; import { cloneDeep } from 'lodash' ; import { NODE_TYPES } from 'common/enums' ; import { getViewerFromReq , toGlobalId } from 'common/utils' ; import { knex } from 'connectors' ; import { RequestContext } from 'definitions' ; const purgeSentryData = ( req ? ) : any => { const omit = ( source , target ) => { for ( const key of Object . keys ( source ) ) { if ( key === target ) { delete source [ key ] ; } else if ( typeof source [ key ] === 'object' && source [ key ] != null ) { omit ( source [ key ] , target ) ; } } } ; if ( req && req . body && typeof req . body . variables === 'object' ) { const params = cloneDeep ( req . body . variables ) ; omit ( params , 'password' ) ; return params ; } return { } ; } ; export const makeContext = async ( { req , res , connection , } ) : Promise < RequestContext > => { Sentry . configureScope ( ( scope ) => { const headers = req ? req . headers : { } ; scope . setTag ( 'action-id' , headers [ 'x-sentry-action-id' ] ) ; scope . setTag ( 'source' , 'server' ) ; scope . setExtra ( 'parameters' , purgeSentryData ( req ) ) ; } ) ; if ( connection ) { return connection . context ; } const viewer = await getViewerFromReq ( { req , res } ) ; Sentry . configureScope ( ( scope ) => { scope . setUser ( { id : viewer . id ? toGlobalId ( { type : NODE_TYPES . User , id : viewer . id } ) : viewer . id , role : viewer . role , language : viewer . language , } ) ; } ) ; return { viewer , req , res , knex , } ; } ;