Skip to content

Commit 5790198

Browse files
Merge pull request #100 from apptreesoftware/addingTraceFunctions
feat: Add OnCompleteCallback interceptor for HTTP call tracing
2 parents 3594e6c + 7d5a522 commit 5790198

3 files changed

Lines changed: 73 additions & 5 deletions

File tree

famis_client.ts

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import axiosRetry from 'axios-retry';
1111
import Bottleneck from 'bottleneck';
1212
import _ from 'lodash';
1313
import { ApiError, AuthorizationError } from './errors';
14-
import { Result } from './model/common';
14+
import { OnCompleteCallback, Result, SdkCallInfo } from './model/common';
1515
import {
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> {

model/common.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,17 @@ export interface Result<T> {
66
totalDuration: number;
77
averageDuration: number;
88
}
9+
10+
export interface SdkCallInfo {
11+
method: string;
12+
url: string;
13+
baseUrl: string;
14+
requestHeaders: Record<string, any>;
15+
requestBody: any;
16+
responseStatus: number | null;
17+
responseBody: any;
18+
durationMs: number;
19+
error?: string;
20+
}
21+
22+
export type OnCompleteCallback = (info: SdkCallInfo) => void;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "facility360",
3-
"version": "1.0.34",
3+
"version": "1.1.0",
44
"description": "A Node based 360Facility client SDK",
55
"main": "dist/index.js",
66
"scripts": {

0 commit comments

Comments
 (0)