# tf-request

> request模块

Latest version **0.2.0** (published 2020-03-30) · ISC license · 0 weekly downloads

## Install

```sh
npm install tf-request
pnpm add tf-request
yarn add tf-request
bun add tf-request
```

## 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.2.0 |
| Published | 2020-03-30 |
| First published | 2018-11-12 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 76.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | tffed |

## Links

- npm: https://www.npmjs.com/package/tf-request
- Repository: http://gitlab.tf56.lo/tf-components/tf-request
- npm.io page: https://npm.io/package/tf-request

## Dependencies (4)

- [qs](https://npm.io/package/qs.md) ^6.5.2
- [fetch-jsonp](https://npm.io/package/fetch-jsonp.md) ^1.1.3
- [whatwg-fetch](https://npm.io/package/whatwg-fetch.md) ^3.0.0
- [lodash.isobject](https://npm.io/package/lodash.isobject.md) ^3.0.2

## Recent versions

- 0.2.0 (latest) — 2020-03-30
- 0.1.9 — 2020-03-04
- 0.1.5 — 2019-03-28
- 0.1.4 — 2019-03-28
- 0.1.3 — 2018-11-12

## README

## 请求模块


```javascript
// 引入
import request, { setCsrfUrl, setCsrf } from "tf-request";

// 使用
request('url地址', {
  body: String | Object, // 传给后端的参数
  dataType: 'jsonp',  // 后端返回的数据格式, 一般不传, 目前只支持传入jsonp
  type: '', // 前端传给后端的数据格式, 一般不传, 默认form类型, 只支持传入json
  method: 'get | post',  // 请求类型, 默认post

  hasCsrf: Boolean | Fucntion(url, options),  // 是否需要先发csrf请求, 默认false
  csrfUrl: String,  // 设置csrf请求地址
  csrf(data, csrfType) { // 可选, 返回请求需要拼接的csrf参数, 默认为:
    return { csrfType, csrfToken: data.data }
  }

  interceptRequest(data) { // 请求发送前做处理, 可选参数
    // ...对data做处理, 有下面2种返回数据方式: 

    // 方式1: 直接返回处理后的data
    return data;

    // 方式2: 返回一个Promise
    return new Promise(resolve => {
      setTimeout(() => { // 模拟异步请求
          console.log(data)
          // data.body  = data.body + '&c=ccc'  // 如果是post请求方式, 请拼接到body上
          data.url = data.url + '?x=xxxx' // 如果是get请求方式, 请拼接到url上
          resolve(data) // 返回数据
      }, 1000)
    })
  },

  interceptResponse(data) { //  拦截返回的请求做处理, 可选参数
    // 对data做处理
    return data // 一定要返回数据
  }

  // ps: 如果是jsonp, options还支持参数: timeout, jsonpCallbackFunction, jsonpCallback等参数
  //     文档地址: https://www.npmjs.com/package/fetch-jsonp
})
.then(data => {
  console.log(data) // 返回的数据
})
.catch(e) {
  console.log(e) // 错误信息
}

// cors请求
request('https://cnodejs.org/api/v1/topics', {
    method: 'get',
    mode: 'cors',
    // credentials: 'include',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    body: 'a=aa'
}).then(function(response) {
    console.log(response);
});


setCsrfUrl('url地址')
setCsrf(true)
```

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