1.0.4 • Published 4 years ago

@kingga/kc-http v1.0.4

Weekly downloads
5
License
MIT
Repository
github
Last release
4 years ago

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;
}
1.0.4

4 years ago

1.0.2

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago