Skip to content

An async HTTP Client implementing Window.XMLHttpRequest.

Notifications You must be signed in to change notification settings

b-goodman/http-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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;

About

An async HTTP Client implementing Window.XMLHttpRequest.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published