Skip to content
/ kc-http Public

An wrapper for common HTTP libraries such as Axios and Fetch.

Notifications You must be signed in to change notification settings

kingga/kc-http

Repository files navigation

KC HTTP

A wrapper around common HTTP libs

Build Status

Change HTTP library without any hassle, switching between different HTTP libs could be as easy as changing one line.

Installation

yarn add @kingga/kc-http
# OR
npm i @kingga/kc-http

Usage

import Container from '@kingga/kc-container'; // Not needed, requires yarn add @kingga/kc-container.
import { FetchHttp, IHttp } from '@kingga/kc-http';

const container = new Container();

container.bind<IHttp>('IHttp', () => new FetchHttp());

// Anywhere else in your application.
import { IHttp, IResponse, IErrorResponse } from '@kingga/kc-http';

const http = container.make<IHttp>('IHttp');
http.get({ url: '/user' })
    .then((response: IResponse) => {
        console.log(response.data);
    })
    .catch((error: IErrorResponse) => {
        console.error(error);
    });

Short Docs

Interfaces

IRequest

interface IRequest {
    url: string;
    method?: RequestMethod; // E.g. GET | POST | PUT | ...
    body?: any;
    headers?: Record<string, string>;
}

IResponse

interface IResponse {
    data: any;
    status: number;
    statusText: string;
    headers: Record<string, string>;
    request: IRequest;
}

IErrorResponse

interface IErrorResponse {
    code?: number,
    message: string;
    name: string;
    response?: IResponse;
    request: IRequest;
}