Skip to content

Abstract typescript types to easily switch from http client implementations by using dependency inversion principle

License

Notifications You must be signed in to change notification settings

nbottarini/http-client-js

Repository files navigation

License: MIT CI Status

abstract-http-client: npm

axios-http-client: npm

Http Client

Abstract types to easily switch from http client implementations by using dependency inversion principle.

See Abstract Http Client Documentation.

Installation

Axios implementation:

Npm:

$ npm install --save @nbottarini/axios-http-client

Yarn:

$ yarn add @nbottarini/axios-http-client

Usage

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.

About

Abstract typescript types to easily switch from http client implementations by using dependency inversion principle

Resources

License

Stars

Watchers

Forks

Packages

No packages published