Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 58 additions & 4 deletions famis_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import axiosRetry from 'axios-retry';
import Bottleneck from 'bottleneck';
import _ from 'lodash';
import { ApiError, AuthorizationError } from './errors';
import { Result } from './model/common';
import { OnCompleteCallback, Result, SdkCallInfo } from './model/common';
import {
AccountSegment,
AccountSegmentValue,
Expand Down Expand Up @@ -187,6 +187,7 @@ export class FamisClient {
credentials: FamisOAuthCredential;
autoRefresh: boolean;
debug: boolean;
onComplete?: OnCompleteCallback;

static async withLoginCredential(opts: {
username: string;
Expand All @@ -195,6 +196,7 @@ export class FamisClient {
autoRefresh?: boolean;
debug?: boolean;
autoRetry?: boolean;
onComplete?: OnCompleteCallback;
}) {
const cred = await this.login({
username: opts.username,
Expand All @@ -210,7 +212,8 @@ export class FamisClient {
opts.host,
opts.autoRefresh ?? false,
opts.debug ?? false,
opts.autoRetry ?? false
opts.autoRetry ?? false,
opts.onComplete
);
}

Expand All @@ -219,6 +222,7 @@ export class FamisClient {
host: string;
debug?: boolean;
autoRetry?: boolean;
onComplete?: OnCompleteCallback;
}): FamisClient {
return new FamisClient(
{
Expand All @@ -236,7 +240,9 @@ export class FamisClient {
},
opts.host,
false,
opts.debug
opts.debug ?? false,
opts.autoRetry ?? false,
opts.onComplete
);
}

Expand Down Expand Up @@ -299,10 +305,12 @@ export class FamisClient {
host: string,
autoRefresh: boolean,
debug: boolean = false,
autoRetry: boolean = false
autoRetry: boolean = false,
onComplete?: OnCompleteCallback
) {
this.credentials = credentials;
this.host = host;
this.onComplete = onComplete;
this.http = axios.create({
baseURL: host,
validateStatus: status => true,
Expand Down Expand Up @@ -330,6 +338,52 @@ export class FamisClient {
this.http.interceptors.request.use(AxiosLogger.requestLogger);
this.http.interceptors.response.use(AxiosLogger.responseLogger);
}

this.http.interceptors.request.use(config => {
(config as any).__startTime = Date.now();
return config;
});

this.http.interceptors.response.use(
(response) => {
if (this.onComplete) {
const startTime = (response.config as any).__startTime;
const durationMs = startTime ? Date.now() - startTime : 0;
const callInfo: SdkCallInfo = {
method: (response.config.method ?? 'GET').toUpperCase(),
url: response.config.url ?? '',
baseUrl: response.config.baseURL ?? '',
requestHeaders: response.config.headers as Record<string, any>,
requestBody: response.config.data,
responseStatus: response.status,
responseBody: response.data,
durationMs,
};
Promise.resolve().then(() => this.onComplete!(callInfo)).catch(() => {});
}
return response;
},
(error) => {
if (this.onComplete) {
const config = error.config ?? {};
const startTime = (config as any).__startTime;
const durationMs = startTime ? Date.now() - startTime : 0;
const callInfo: SdkCallInfo = {
method: (config.method ?? 'GET').toUpperCase(),
url: config.url ?? '',
baseUrl: config.baseURL ?? '',
requestHeaders: config.headers ?? {},
requestBody: config.data,
responseStatus: error.response?.status ?? null,
responseBody: error.response?.data ?? null,
durationMs,
error: error.message,
};
Promise.resolve().then(() => this.onComplete!(callInfo)).catch(() => {});
}
return Promise.reject(error);
}
);
}

async refreshAuthCredential(): Promise<FamisOAuthCredential> {
Expand Down
14 changes: 14 additions & 0 deletions model/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,17 @@ export interface Result<T> {
totalDuration: number;
averageDuration: number;
}

export interface SdkCallInfo {
method: string;
url: string;
baseUrl: string;
requestHeaders: Record<string, any>;
requestBody: any;
responseStatus: number | null;
responseBody: any;
durationMs: number;
error?: string;
}

export type OnCompleteCallback = (info: SdkCallInfo) => void;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "facility360",
"version": "1.0.34",
"version": "1.1.0",
"description": "A Node based 360Facility client SDK",
"main": "dist/index.js",
"scripts": {
Expand Down
Loading