# uni-fetch-client

> JavaScript HTTP client using fetch API with possibility to force using XHR.

Latest version **1.2.4** (published 2019-10-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install uni-fetch-client
pnpm add uni-fetch-client
yarn add uni-fetch-client
bun add uni-fetch-client
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.4 |
| Published | 2019-10-22 |
| First published | 2018-12-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 112.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Gleb Plakhotnik |
| Maintainers | glebpl |

## Links

- npm: https://www.npmjs.com/package/uni-fetch-client
- Repository: https://github.com/glebpl/uni-fetch-client
- Homepage: https://github.com/glebpl/uni-fetch-client#readme
- Issues: https://github.com/glebpl/uni-fetch-client/issues
- npm.io page: https://npm.io/package/uni-fetch-client

## Dependencies (2)

- [whatwg-fetch](https://npm.io/package/whatwg-fetch.md) ^3.0.0
- [abortcontroller-polyfill](https://npm.io/package/abortcontroller-polyfill.md) ^1.2.1

## Recent versions

- 1.2.4 (latest) — 2019-10-22
- 1.2.3 — 2019-09-25
- 1.2.2 — 2019-09-25
- 1.2.1 — 2019-09-25
- 1.2.0 — 2019-09-13
- 1.1.1 — 2019-09-12
- 1.1.0 — 2019-09-12
- 1.0.1 — 2019-01-15
- 1.0.0 — 2018-12-14

## README

# Description
Simple JavaScript HTTP client using fetch API with possibility to force using XHR.

Has possibility to abort requests with AbortController (polyfill inside).

# Installation
`npm install --save uni-fetch-client`

# Examples
## Create client
```
import FetchClient from 'uni-fetch-client';

const client = FetchClient.create({
    host: "your.host.com"
    , secure: true
    , port: 8080
});
```

## Simple JSON request
```
client.getJson("/restapi/users").then(jsonData => {
    // process data here
}, err => {
    // process error here
});
```

## Abortable request
```
const abortController = FetchClient.createAbortController();

client.getJson("/restapi/users", {
    signal: abortController.signal
}).then(jsonData => {
    // process data here
}, err => {
    // process error here
    // Abort error is instance of DOMException and has name 'AbortError'
});

const someHandler = () => {
    abortController.abort();
};
```

## Request headers
```
client.postJson("/messages/create", {
    data: {
        message: "Some data"
    }
    , headers: {
        "Authorization": "Basic yourbase64encoded="
    }
});
```

## Error handling
```
client.getJson("/bad/resource").then(() => {}, err => {
    if(err instanceof ParseError) {
        msg = `Got ParseError ${err.code}: ${err.message}`;
    } else if(err instanceof TransportError) {
        msg = ```
            Got TransportError ${err.code}: ${err.message}: ${err.responseText}: 
            ${JSON.stringify(err.responseHeaders)}
        ```;
    } else if(err instanceof DOMException) {
        if(err.name === "AbortError") {
            // We will be here if AbortController is used (see above)
            msg = `Request aborted ${Date.now()}`;
        } else {
            console.error(err);
        }
    } else if(err instanceof TypeError) {
        // Also will happen on cors requests when 404 status comes
        // mode of fetch has no effect at this case
        msg = `Request failed due to network error ${Date.now()}`;
    }
});
```

---
_Source: https://npm.io/package/uni-fetch-client · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
