UNPKG

bitso-ts

Version:

Simple TypeScript Bitso API Wrapper for Public and Private end points. [![MIT License](https://img.shields.io/apm/l/atomic-design-ui.svg?)](https://github.com/amircp/bitso-ts/blob/master/LICENSE) ## Installation

42 lines (33 loc) 1.02 kB
import axios, { AxiosInstance } from 'axios'; declare module 'axios' { interface AxiosResponse<T = any> extends Promise<T> {} } export default abstract class HttpClient { protected readonly _httpClient: AxiosInstance; constructor( private baseUrl: string, private headers?: any, private timeout?: number ) { this._httpClient = this.getInstance(); } private openConnection(): AxiosInstance { return axios.create({ baseURL: this.baseUrl, timeout: this.timeout ?? 5000, headers: this.headers ?? {}, }); } public setAuthToken(token: string): void { this._httpClient.defaults.headers.common['Authorization'] = token; } public setHeaders(customHeaders: object): void { Object.assign(this._httpClient.defaults.headers, customHeaders); } public setAPIBase(url: string): void { this._httpClient.defaults['baseURL'] = url; } public getInstance(): AxiosInstance { return this._httpClient ? this._httpClient : this.openConnection(); } }