1.3.5 • Published 5 years ago

@bgoodman/http-client v1.3.5

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

HTTPClient

An async HTTP Client implementing Window.XMLHttpRequest. Intended to imitate the Window.fetch API but still offering full backards compatability with legacy browsers.

Installation

npm install @bgoodman/http-client

yarn add @bgoodman/http-client

Usage

import {HTTPClient} from "@bgoodman/http-client"

interface CurrencyConversion {
    rates: {[currency: string]: number};
    base: string;
    date: string;
}

const endpointUrl = "https://api.exchangeratesapi.io/latest?symbols=USD,GBP";

const req = await HTTPClient.get(endpointUrl);
const data = HTTPClient.json<CurrencyConversion>(req);

console.log(data)
//{"rates":{"USD":1.1158,"GBP":0.86368},"base":"EUR","date":"2019-11-04"

API

get

Perform a HTTP get request.

HTTPClient.get(url: string, opts?: HTTPClient.Opts): Promise<XMLHttpRequest>

json

Conveniently parse the response from an earlier request expected to resolve to JSON data.

HTTPClient.json<T = any>(req: XMLHttpRequest): T;

post

Perform a HTTP post request.

HTTPClient.post(url: string, data: FormData, opts?: HTTPClient.Opts): Promise<XMLHttpRequest>

delete

Perform a HTTP delete request.

HTTPClient.delete(url: string, data: FormData, opts?: HTTPClient.Opts): Promise<XMLHttpRequest>

Options

withCredentials?: boolean;
responseType?: XMLHttpRequestResponseType;
requestHeaders?: Map<string, string>;
timeout?: number;
1.3.5

5 years ago

1.3.4

5 years ago

1.3.3

6 years ago

1.3.2

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago