import url from "url" ; export default class AxiosErrorEventDecorator { event : any | undefined ; hint : any | undefined ; constructor ( event , hint ) { this . event = event ; this . hint = hint ; } get error ( ) : any | undefined { return this . hint ?. originalException ; } get response ( ) : any | undefined { return this . error ?. response ; } get request ( ) : any | undefined { return this . response ?. request ; } static decorate ( event , hint ) { return new AxiosErrorEventDecorator ( event , hint ) . decorate ( ) ; } validError ( ) { return this . error && this . response && this . request ; } decorate ( ) { if ( ! this . validError ( ) ) { return this . event ; } this . event . extra . response = this . responseMetadata ( ) ; this . event . extra . request = this . requestMetadata ( ) ; this . event . fingerprint = this . generateFingerprint ( ) ; return this . event ; } requestMetadata ( ) { return { method : this . request ?. method , path : this . request ?. path , host : this . request ?. getHeader ( "host" ) , headers : this . request ?. getHeaders ( ) , body : this . parseRequestBody ( this . response ?. config ?. data ) } ; } responseMetadata ( ) { return { status : this . response ?. status , headers : this . response ?. headers , body : this . response ?. data ? this . response . data . toString ( ) . slice ( 0 , 255 ) : undefined } ; } generateFingerprint ( ) { const { pathname } = url . parse ( this . request ?. path || "" ) ; return [ "{{ default }}" , this . response ?. status , `${ this . request ?. method } ${ pathname }` ] ; } parseRequestBody ( body ) { if ( body && this . isJsonRequest ( ) ) { try { return JSON . parse ( body ) ; } catch ( error ) { if ( error . name !== "SyntaxError" ) { throw error ; } } } return body ; } isJsonRequest ( ) { return this . request ?. getHeader ( "content-type" ) ?. startsWith ( "application/json" ) ; } }