@bgoodman/http-client
TypeScript icon, indicating that this package has built-in type declarations

1.3.5 • Public • Published

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;

Package Sidebar

Install

npm i @bgoodman/http-client

Weekly Downloads

1

Version

1.3.5

License

MIT

Unpacked Size

11.3 kB

Total Files

11

Last publish

Collaborators

  • bgoodman