Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for custom axios instance #146

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
28 changes: 17 additions & 11 deletions src/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down
9 changes: 7 additions & 2 deletions src/fireblocks-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -89,7 +89,12 @@ export interface SDKOptions {
onFulfilled: (value: AxiosResponse<any, any>) => AxiosResponse<any, any> | Promise<AxiosResponse<any, any>>;
onRejected: (error: any) => any;
};
}
};
/**
* Providing a custom axios instance
* it will override all other Axios Related configurations
*/
axiosInstance?: AxiosInstance;
};
}

Expand Down