import { HttpOperationResponse } from "../httpOperationResponse" ; import { WebResourceLike } from "../webResource" ; import { BaseRequestPolicy , RequestPolicy , RequestPolicyFactory , RequestPolicyOptionsLike , } from "./requestPolicy" ; export function logPolicy ( logger = console . log ) { return { create : ( nextPolicy , options ) => { return new LogPolicy ( nextPolicy , options , logger ) ; } , } ; } export class LogPolicy extends BaseRequestPolicy { logger ? : any ; constructor ( nextPolicy , options , logger = console . log ) { super ( nextPolicy , options ) ; this . logger = logger ; } public sendRequest ( request ) { return this . _nextPolicy . sendRequest ( request ) . then ( ( response ) => logResponse ( this , response ) ) ; } } function logResponse ( policy , response ) { policy . logger ( `>> Request: ${ JSON . stringify ( response . request , undefined , 2 ) }` ) ; policy . logger ( `>> Response status code: ${ response . status }` ) ; const responseBody = response . bodyAsText ; policy . logger ( `>> Body: ${ responseBody }` ) ; return Promise . resolve ( response ) ; }