# zoyi-simple-fetch

> Simple, easy to use fetch wrapping module

Latest version **0.1.19** (published 2017-11-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install zoyi-simple-fetch
pnpm add zoyi-simple-fetch
yarn add zoyi-simple-fetch
bun add zoyi-simple-fetch
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.19 |
| Published | 2017-11-01 |
| First published | 2017-01-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | blaswan |

## Links

- npm: https://www.npmjs.com/package/zoyi-simple-fetch
- npm.io page: https://npm.io/package/zoyi-simple-fetch

## Dependencies (2)

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

## Recent versions

- 0.1.19 (latest) — 2017-11-01
- 0.1.18 — 2017-08-28
- 0.1.17 — 2017-07-27
- 0.1.16 — 2017-06-14
- 0.1.15 — 2017-05-30
- 0.1.14 — 2017-04-17
- 0.1.13 — 2017-04-17
- 0.1.12 — 2017-04-14
- 0.1.11 — 2017-04-14
- 0.1.10 — 2017-04-10
- 0.1.9 — 2017-03-28
- 0.1.8 — 2017-03-28
- 0.1.7 — 2017-03-28
- 0.1.6 — 2017-03-28
- 0.1.5 — 2017-03-28
- … 5 more at https://npm.io/package/zoyi-simple-fetch/versions

## README

# simple-fetch
Simple, easy to use fetch wrapping module

## Installation
```
npm install zoyi-simple-fetch
```

## Testing
```
npm test
```

## Building
```
npm run build
```

## Usage
```
// Require
var sf = require('zoyi-simple-fetch')

// Create instance
var client = new sf.Client()
```

## Features

### Set base url depending on NODE_ENV
```
// Set base url
client.setBaseUrl('http://base.url')

// Set base url depending on NODE_ENV
client.setBaseUrl({
    staging: 'http://dev.base.url',  // if NODE_ENV is 'staging'
    production: 'http://base.url'    // if NODE_ENV is 'production'
})
```

### Set default header which is applied to every request
```
client.setDefaultHeader({
    'Accept': 'application/json',
    'Content-Type': 'application/json'
})
```

### Set header interceptor (filter) for dynamic header
```
client.setHeaderInterceptor((header) => {
    // header is defaultHeader
    header['X-Auth-Token'] = 'ABCD'
    return header
})
```

### Set credential options
```
// default value is '' (means no use)
client.credentials = ''

// Use cookies for same origin
client.credentials = 'same-origin'

// For CORS
client.credentials = 'include'
```

### Encode query string on GET method
```
const query = { key: 'value' }
client.get('url', query)
```

### Encode request body to json on POST, PUT method
```
const body = { key: 'value' }
client.post('url', body)
client.put('url', body)
```

### Encode request body to url encoded form on POST, PUT method
```
const body = { key: 'value' }
client.postByUrlEncoding('url', body)
client.putByUrlEncoding('url', body)
```

### No encode on DELETE method
```
client.delete('url')
```

### Decode response body to json
```
client.get('url').then((body: JSON) => {
    console.log(body)
})
```

### Reject if status code is not 2XX
```
client.get('url').then(() => {}).catch((err: Error) => {
    console.error(err)
})

// Error type is below.
interface Error {
    status: string;     // status code
    statusText: string; // status text
    body: JSON;         // json decoded response body
}
```

### Throw status code 0 if internet is disconnected (Actually, not reachable to server)
```
// Tested in Safari, Chrome, IE10, IE11, Edge
{
    status: 0,
    statusText: 'TypeError ~~'
}
```

## To Do

- Abstract file upload and Support uploading progress

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