From 075d8384210b3c13b40c49c775ebd9ecbbfd45b3 Mon Sep 17 00:00:00 2001 From: Yarin Vaknin Date: Fri, 17 Feb 2023 12:24:54 +0200 Subject: [PATCH] add support for custom axios instance --- src/api-client.ts | 28 +++++++++++++++++----------- src/fireblocks-sdk.ts | 9 +++++++-- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/api-client.ts b/src/api-client.ts index c32608a5..6672ac17 100644 --- a/src/api-client.ts +++ b/src/api-client.ts @@ -10,18 +10,24 @@ export class ApiClient { private axiosInstance: AxiosInstance; constructor(private authProvider: IAuthProvider, private apiBaseUrl: string, private options: SDKOptions) { - this.axiosInstance = axios.create({ - baseURL: this.apiBaseUrl, - proxy: this.options?.proxy, - timeout: this.options?.timeoutInMs, - headers: { - "X-API-Key": this.authProvider.getApiKey(), - "User-Agent": this.getUserAgent() - } - }); + if (options.customAxiosOptions?.axiosInstance) { + this.axiosInstance = options.customAxiosOptions.axiosInstance; + this.axiosInstance.defaults.headers.common["User-Agent"] = this.getUserAgent(); + this.axiosInstance.defaults.headers.common["X-API-Key"] = this.authProvider.getApiKey(); + } else { + this.axiosInstance = axios.create({ + baseURL: this.apiBaseUrl, + proxy: this.options?.proxy, + timeout: this.options?.timeoutInMs, + headers: { + "X-API-Key": this.authProvider.getApiKey(), + "User-Agent": this.getUserAgent() + } + }); - if (options.customAxiosOptions?.interceptors?.response) { - this.axiosInstance.interceptors.response.use(options.customAxiosOptions.interceptors.response.onFulfilled, options.customAxiosOptions.interceptors.response.onRejected); + if (options.customAxiosOptions?.interceptors?.response) { + this.axiosInstance.interceptors.response.use(options.customAxiosOptions.interceptors.response.onFulfilled, options.customAxiosOptions.interceptors.response.onRejected); + } } } diff --git a/src/fireblocks-sdk.ts b/src/fireblocks-sdk.ts index ee65965f..9fb9be3b 100644 --- a/src/fireblocks-sdk.ts +++ b/src/fireblocks-sdk.ts @@ -63,7 +63,7 @@ import { GetNFTsFilter, SettleOffExchangeAccountResponse, PublicKeyInformation, DropTransactionResponse, } from "./types"; -import { AxiosInterceptorOptions, AxiosProxyConfig, AxiosResponse } from "axios"; +import { AxiosInstance, AxiosInterceptorOptions, AxiosProxyConfig, AxiosResponse } from "axios"; export * from "./types"; @@ -89,7 +89,12 @@ export interface SDKOptions { onFulfilled: (value: AxiosResponse) => AxiosResponse | Promise>; onRejected: (error: any) => any; }; - } + }; + /** + * Providing a custom axios instance + * it will override all other Axios Related configurations + */ + axiosInstance?: AxiosInstance; }; }