# think-fetch

> Fetch for ThinkJS 3.x

Latest version **1.1.2** (published 2019-12-23) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2019-12-23 |
| First published | 2017-04-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 8 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 10 |
| Author | Berwin |
| Maintainers | berwin, bezos, huangxiaolu, lizheming, lushijie, sijiecai, toxicjohann, welefen |
| Keywords | thinkjs3, fetch, think-extend |

## Links

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

## Dependencies (1)

- [node-fetch](https://npm.io/package/node-fetch.md) ^1.6.3

## Alternatives

- [gamedig](https://npm.io/package/gamedig.md) — 29.3K weekly downloads
- [join-monster](https://npm.io/package/join-monster.md) — 12.8K weekly downloads
- [masked](https://npm.io/package/masked.md) — 5.5K weekly downloads
- [@comunica/actor-query-process-explain-logical](https://npm.io/package/@comunica/actor-query-process-explain-logical.md) — 4.7K weekly downloads
- [@veracity/vui](https://npm.io/package/@veracity/vui.md) — 4.6K weekly downloads

## Recent versions

- 1.1.2 (latest) — 2019-12-23
- 1.1.1 — 2019-04-28
- 1.1.0 — 2017-07-27
- 1.0.1 — 2017-07-16
- 1.0.0 — 2017-04-13

## README

# think-fetch
[![npm](https://img.shields.io/npm/v/think-fetch.svg)](https://www.npmjs.com/package/think-fetch)
[![Build Status](https://travis-ci.org/thinkjs/think-fetch.svg?branch=master)](https://travis-ci.org/thinkjs/think-fetch)
[![Coverage Status](https://coveralls.io/repos/github/thinkjs/think-fetch/badge.svg?branch=master)](https://coveralls.io/github/thinkjs/think-fetch?branch=master)

Fetch for ThinkJS 3.x

## Install

```
$ npm install think-fetch --save
```

## How to use

config file `src/config/extend.js`

```javascript
const fetch = require('think-fetch');

module.exports = [
  fetch, // HTTP request client.
];
```

## Methods in Controller

```javascript
module.exports = class extends think.Controller {
  async indexAction () {

    // plain text or html
    const text = await this.fetch('https://github.com/').then(res => res.text());

    // json
    const json = await this.fetch('https://api.github.com/repos/thinkjs/think-fetch').then(res => res.json());

    // post
    const body = await this.fetch('http://httpbin.org/post', { method: 'POST', body: 'a=1' }).then(res => res.json());

    // stream
    const res = await this.fetch('https://assets-cdn.github.com/images/modules/logos_page/Octocat.png');
    const dest = fs.createWriteStream('./octocat.png');
    res.body.pipe(dest);

    // post with stream from file
    const stream = fs.createReadStream('input.txt');
    const result = this.fetch('http://httpbin.org/post', { method: 'POST', body: stream }).then(res => res.json());
  }
}
```

## API

### fetch(url[, options])

- `url` A string representing the URL for fetching
- `options` [Options](#options) for the HTTP(S) request
- Returns: `Promise<Response>`

Perform an HTTP(S) fetch.

url should be an absolute url, such as `http://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected promise.

#### Options

The default values are shown after each option key.

```javascript
{
  // These properties are part of the Fetch Standard
  method: 'GET',
  headers: {},        // request headers. format is the identical to that accepted by the Headers constructor (see below)
  body: null,         // request body. can be null, a string, a Buffer, a Blob, or a Node.js Readable stream
  redirect: 'follow', // set to `manual` to extract redirect headers, `error` to reject redirect

  // The following properties are node-fetch extensions
  follow: 20,         // maximum redirect count. 0 to not follow redirect
  timeout: 0,         // req/res timeout in ms, it resets on redirect. 0 to disable (OS limit applies)
  compress: true,     // support gzip/deflate content encoding. false to disable
  size: 0,            // maximum response body size in bytes. 0 to disable
  agent: null         // http(s).Agent instance, allows custom proxy, certificate etc.
}
```

##### Default Headers

If no values are set, the following request headers will be sent automatically:

| Header | Value |
| :------: | :------: |
| `Accept-Encoding` | `gzip,deflate` (when `options.compress === true`) |
| `Accept` | `*/*` |
| `Connection` | `close` (when no `options.agent` is present) |
| `Content-Length` | (automatically calculated, if possible) |
| `User-Agent` | `node-fetch/1.0 (+https://github.com/bitinn/node-fetch)` |



[More Methods Click](https://github.com/bitinn/node-fetch)

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