@@ -11,7 +11,7 @@ import axiosRetry from 'axios-retry';
1111import Bottleneck from 'bottleneck' ;
1212import _ from 'lodash' ;
1313import { ApiError , AuthorizationError } from './errors' ;
14- import { Result } from './model/common' ;
14+ import { OnCompleteCallback , Result , SdkCallInfo } from './model/common' ;
1515import {
1616 AccountSegment ,
1717 AccountSegmentValue ,
@@ -187,6 +187,7 @@ export class FamisClient {
187187 credentials : FamisOAuthCredential ;
188188 autoRefresh : boolean ;
189189 debug : boolean ;
190+ onComplete ?: OnCompleteCallback ;
190191
191192 static async withLoginCredential ( opts : {
192193 username : string ;
@@ -195,6 +196,7 @@ export class FamisClient {
195196 autoRefresh ?: boolean ;
196197 debug ?: boolean ;
197198 autoRetry ?: boolean ;
199+ onComplete ?: OnCompleteCallback ;
198200 } ) {
199201 const cred = await this . login ( {
200202 username : opts . username ,
@@ -210,7 +212,8 @@ export class FamisClient {
210212 opts . host ,
211213 opts . autoRefresh ?? false ,
212214 opts . debug ?? false ,
213- opts . autoRetry ?? false
215+ opts . autoRetry ?? false ,
216+ opts . onComplete
214217 ) ;
215218 }
216219
@@ -219,6 +222,7 @@ export class FamisClient {
219222 host : string ;
220223 debug ?: boolean ;
221224 autoRetry ?: boolean ;
225+ onComplete ?: OnCompleteCallback ;
222226 } ) : FamisClient {
223227 return new FamisClient (
224228 {
@@ -236,7 +240,9 @@ export class FamisClient {
236240 } ,
237241 opts . host ,
238242 false ,
239- opts . debug
243+ opts . debug ?? false ,
244+ opts . autoRetry ?? false ,
245+ opts . onComplete
240246 ) ;
241247 }
242248
@@ -299,10 +305,12 @@ export class FamisClient {
299305 host : string ,
300306 autoRefresh : boolean ,
301307 debug : boolean = false ,
302- autoRetry : boolean = false
308+ autoRetry : boolean = false ,
309+ onComplete ?: OnCompleteCallback
303310 ) {
304311 this . credentials = credentials ;
305312 this . host = host ;
313+ this . onComplete = onComplete ;
306314 this . http = axios . create ( {
307315 baseURL : host ,
308316 validateStatus : status => true ,
@@ -330,6 +338,52 @@ export class FamisClient {
330338 this . http . interceptors . request . use ( AxiosLogger . requestLogger ) ;
331339 this . http . interceptors . response . use ( AxiosLogger . responseLogger ) ;
332340 }
341+
342+ this . http . interceptors . request . use ( config => {
343+ ( config as any ) . __startTime = Date . now ( ) ;
344+ return config ;
345+ } ) ;
346+
347+ this . http . interceptors . response . use (
348+ ( response ) => {
349+ if ( this . onComplete ) {
350+ const startTime = ( response . config as any ) . __startTime ;
351+ const durationMs = startTime ? Date . now ( ) - startTime : 0 ;
352+ const callInfo : SdkCallInfo = {
353+ method : ( response . config . method ?? 'GET' ) . toUpperCase ( ) ,
354+ url : response . config . url ?? '' ,
355+ baseUrl : response . config . baseURL ?? '' ,
356+ requestHeaders : response . config . headers as Record < string , any > ,
357+ requestBody : response . config . data ,
358+ responseStatus : response . status ,
359+ responseBody : response . data ,
360+ durationMs,
361+ } ;
362+ Promise . resolve ( ) . then ( ( ) => this . onComplete ! ( callInfo ) ) . catch ( ( ) => { } ) ;
363+ }
364+ return response ;
365+ } ,
366+ ( error ) => {
367+ if ( this . onComplete ) {
368+ const config = error . config ?? { } ;
369+ const startTime = ( config as any ) . __startTime ;
370+ const durationMs = startTime ? Date . now ( ) - startTime : 0 ;
371+ const callInfo : SdkCallInfo = {
372+ method : ( config . method ?? 'GET' ) . toUpperCase ( ) ,
373+ url : config . url ?? '' ,
374+ baseUrl : config . baseURL ?? '' ,
375+ requestHeaders : config . headers ?? { } ,
376+ requestBody : config . data ,
377+ responseStatus : error . response ?. status ?? null ,
378+ responseBody : error . response ?. data ?? null ,
379+ durationMs,
380+ error : error . message ,
381+ } ;
382+ Promise . resolve ( ) . then ( ( ) => this . onComplete ! ( callInfo ) ) . catch ( ( ) => { } ) ;
383+ }
384+ return Promise . reject ( error ) ;
385+ }
386+ ) ;
333387 }
334388
335389 async refreshAuthCredential ( ) : Promise < FamisOAuthCredential > {
0 commit comments