https://github.com/b-goodman/http-client
An async HTTP Client implementing Window.XMLHttpRequest.
https://github.com/b-goodman/http-client
xmlhttprequest-wrapper
Last synced: 2 months ago
JSON representation
An async HTTP Client implementing Window.XMLHttpRequest.
- Host: GitHub
- URL: https://github.com/b-goodman/http-client
- Owner: b-goodman
- Created: 2019-11-05T05:50:58.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-05T00:09:30.000Z (over 2 years ago)
- Last Synced: 2025-01-24T18:35:05.996Z (4 months ago)
- Topics: xmlhttprequest-wrapper
- Language: TypeScript
- Size: 899 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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
```bash
npm install @bgoodman/http-clientyarn add @bgoodman/http-client
```## Usage
```typescript
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(req);console.log(data)
//{"rates":{"USD":1.1158,"GBP":0.86368},"base":"EUR","date":"2019-11-04"
```### API
#### `get`
Perform a HTTP `get` request.
```typescript
HTTPClient.get(url: string, opts?: HTTPClient.Opts): Promise
```#### `json`
Conveniently parse the response from an earlier request expected to resolve to JSON data.
```typescript
HTTPClient.json(req: XMLHttpRequest): T;
```#### `post`
Perform a HTTP `post` request.
```typescript
HTTPClient.post(url: string, data: FormData, opts?: HTTPClient.Opts): Promise
```#### `delete`
Perform a HTTP `delete` request.
```typescript
HTTPClient.delete(url: string, data: FormData, opts?: HTTPClient.Opts): Promise
```---
#### Options
```typescript
withCredentials?: boolean;
responseType?: XMLHttpRequestResponseType;
requestHeaders?: Map;
timeout?: number;
```