# barbarojs-http

> fetch wrapper

Latest version **1.0.9** (published 2017-02-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install barbarojs-http
pnpm add barbarojs-http
yarn add barbarojs-http
bun add barbarojs-http
```

## 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.0.9 |
| Published | 2017-02-04 |
| First published | 2017-01-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Marco Sangalli |
| Maintainers | sangallimarco |

## Links

- npm: https://www.npmjs.com/package/barbarojs-http
- Repository: https://github.com/barbarojs/barbarojs-http
- Homepage: https://github.com/barbarojs/barbarojs-http#readme
- Issues: https://github.com/barbarojs/barbarojs-http/issues
- npm.io page: https://npm.io/package/barbarojs-http

## Dependencies (1)

- [whatwg-fetch](https://npm.io/package/whatwg-fetch.md) ^2.0.2

## Recent versions

- 1.0.9 (latest) — 2017-02-04
- 1.0.8 — 2017-01-30
- 1.0.7 — 2017-01-25
- 1.0.6 — 2017-01-25
- 1.0.5 — 2017-01-25
- 1.0.4 — 2017-01-25
- 1.0.3 — 2017-01-23
- 1.0.2 — 2017-01-21
- 1.0.1 — 2017-01-21
- 1.0.0 — 2017-01-20

## README

# barbarojs-http
Wrapper of github fetch lib: https://github.com/github/fetch
It uses native Promise to chain Middlewares

# Installation

npm install barbarojs-http

# Usage

```javascript
import {http} from 'barbarojs-http';
```

## How To

Lib interpolates variables as described here below
Supported verbs are: GET, POST, PUT, PATCH, DELETE


```javascript
// define params to be interpolated
let conn = new http('/test/:id');

// send request
conn.get({id: 123, q: 1}).then(x => console.log(x)); // /test/123?q=1

// post
conn.post({id: 123, q: 1, name: 'me'}).then(x => console.log(x)); // /test/123 -> body {q:1, name: 'me'}
```

## Special Features

Set development server

```javascript
import {httpProvider} from 'barbarojs-http';
httpProvider.setHostname('http://localhost:3000');
```

### Set JWT Token

```javascript
import {httpProvider} from 'barbarojs-http';
...
...
// set
httpProvider.setJwtToken(tokenHere);
// remove 
httpProvider.removeJwtToken();
```

### Middlewares

```javascript
import {httpProvider} from 'barbarojs-http';
...
...
// set middleware, always return a promise in order to chain other middlewares 
// or your promise
httpProvider.use((req) => {
	if (req.status === 403) {
		// redirect to home
		return Promise.reject(req);
	} else if (req.status >= 400) {
		return Promise.reject(req);
	} else {
		return Promise.resolve(req);
	}
});

// myClass.js
...
...
let conn = new http('/test/:id');
conn.get({id: 345})
.then( // this will happen after middlewares
    x => console.log(x), // all good here
    err => console.log(err) // rejection can come from middleware
);
...
...
```

### Options

```javascript
import {httpProvider} from 'barbarojs-http';
...
...
// set custom options
httpProvider.setOptions({
    headers:{
        'x-Auth....': 'mytoken'
        }
    }
);

```

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