Abstract types to easily switch from http client implementations by using dependency inversion principle.
See Abstract Http Client Documentation.
Npm:
$ npm install --save @nbottarini/axios-http-client
Yarn:
$ yarn add @nbottarini/axios-http-client
index.ts:
import { AxiosHttpClient } from '@nbottarini/axios-http-client'
import { ApiClient } from './ApiClient'
const httpClient = new AxiosHttpClient('https://myapi.com/')
const apiClient = new ApiClient(httpClient)
ApiClient.ts:
import { HttpClient } from '@nbottarini/abstract-http-client'
export class ApiClient {
constructor(private http: HttpClient) {
}
async getUsers(): Promise<User[]> {
const response = await this.http.get('/users')
return response.body
}
async createUser(user: User): Promise<void> {
await this.http.post('/users', user)
}
}
See tests for more use cases.