# fetch-dog

> http 请求库

Latest version **0.1.3** (published 2016-10-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install fetch-dog
pnpm add fetch-dog
yarn add fetch-dog
bun add fetch-dog
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.3 |
| Published | 2016-10-21 |
| First published | 2016-10-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | fzred |
| Maintainers | lred |
| Keywords | ajax, fetch |

## Links

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

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 0.1.3 (latest) — 2016-10-21
- 0.1.2 — 2016-10-20
- 0.1.1 — 2016-10-20
- 0.1.0 — 2016-10-20

## README

# fetch-dog
基于 fetch 的 HTTP 请求客户端，支持node.js，浏览器，react-native

## 特点
* 支持node.js，浏览器，react-native多个环境，可用于同构（isomorphic）应用
* 支持拦截器（interceptor）
* 支持 Promise API 
* 支持 Firefox, Chrome, Safari, Opera and IE9+
* 高扩展性
* 轻量，只有5KB(gzip后 ~3KB)

# 使用
## 添加polyfill
fetch 目前原生支持率并不高，不过引入 polyfill 后即可支持。


1. 浏览器端引入 [whatwg-fetch](https://github.com/github/fetch)
2. NodeJS引入 [node-fetch](https://github.com/bitinn/node-fetch)
3. react-native 对fetch有原生支持，不需要引入polyfill

## 安装
```bash
npm install fetch-dog
```

## 示例
### 浏览器
```javascript
import 'whatwg-fetch'
import FetchDog from 'fetch-dog'

const fd = new FetchDog({ fetch, Headers })
fd.get('/data/list', { id: 2 }).then(data => {
  console.log(data)
})
```

### NodeJS

```javascript
import fetch, { Request, Headers, Response } from 'node-fetch'
import FetchDog from 'fetch-dog'

const fd = new FetchDog({ fetch, Headers })
fd.get('http://example.com/data/list', { id: 2 }).then(data => {
  console.log(data)
})
```

### react-native
```javascript
import FetchDog from 'fetch-dog'

const fd = new FetchDog({ fetch, Headers })
fd.get('http://example.com/data/list', { id: 2 }).then(data => {
  console.log(data)
})
```

# 文档
## 拦截器
```javascript
// 请求前拦截
fd.request.push(req => {
  req.headers.set('Custom-Header', 'hello')
  return req
})

// 请求后拦截
fd.response.push(res => {
  if (res.ok) {
    return res
  }
  return {
    code: -1,
    status: res.status,
    desc: `server err【${res.status}】`,
    data: null,
  }
})
```

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