# data-request

> 数据请求封装

Latest version **1.1.1** (published 2020-09-07) · ISC license · 0 weekly downloads

## Install

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

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2020-09-07 |
| First published | 2020-09-01 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 15.2 KB |
| Known vulnerabilities | 0 (+25 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | sdh |
| Maintainers | milulu0812 |
| Keywords | 数据请求 |

## Links

- npm: https://www.npmjs.com/package/data-request
- Repository: https://github.com/Zql-mll/data-request
- Homepage: https://github.com/Zql-mll/data-request#readme
- Issues: https://github.com/Zql-mll/data-request/issues
- npm.io page: https://npm.io/package/data-request

## Dependencies (1)

- [axios](https://npm.io/package/axios.md) ^0.20.0

## Recent versions

- 1.1.1 (latest) — 2020-09-07
- 1.1.0 — 2020-09-07
- 1.0.9 — 2020-09-03
- 1.0.8 — 2020-09-02
- 1.0.7 — 2020-09-01
- 1.0.6 — 2020-09-01
- 1.0.5 — 2020-09-01
- 1.0.4 — 2020-09-01
- 1.0.3 — 2020-09-01
- 1.0.2 — 2020-09-01
- 1.0.1 — 2020-09-01

## README

# 数据请求

### 参数泛型：

T： params参数类型
R： 返回数据类型
D： data数据类型

### 参数释义：

|   参数名    |      参数类型       | 参数描述 |
| :---------: | :-----------------: | :------: |
|   **url**   |       string        | 接口url  |
| **params**  |          T          |   参数   |
|  **data**   |          D          | body传值 |
| **timeout** | number \| undefined | 超时时间 |

### 方法描述：

|      方法名       | 携带泛型 |       参数        |  方法描述  |
| :---------------: | :------: | :---------------: | :--------: |
|  **getRequest**   |   R，T   |    url，params，timeout    |  get请求   |
| **deleteRequest** |   R，T   |    url，params，timeout    | delete请求 |
|  **postRequest**  | R, D, T  | url，data，params，timeout |  post请求  |
|  **putRequest**   | R, D, T  | url，data，params，timeout |  put请求   |
| **patchRequest**  | R, D, T  | url，data，params，timeout | patch请求  |

### 设计描述：

#### 1. 为什么单例中要求传入Axios实例？

源代码单例截图：

![image-20200902172538792](https://picbucket-1301820142.cos.ap-shanghai.myqcloud.com/img/image-20200902172538792.png)

传入的request是当前项目进行的基础的axios封装，比如拦截器，鉴于项目的不同，后期更新的不同，基础封装的axios作为参数传入。

**request文件示例：**

```typescript
import axios from "axios";
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API // url = base url + request url
});

// Request interceptors
service.interceptors.request.use(
  config => {
    // // Add Authorization header to every request, you can add other custom headers here
    // if (UserModule.token) {
    //   config.headers["Authorization"] = `Bearer ${UserModule.token}`;
    // }
      ...//
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

// Response interceptors
service.interceptors.response.use(
  response => {
    if (response.status === 200) {
      return response.data;
    } else {
        ...//
      console.error(`request failed error： ${response}`);
    }
  },
  error => {
    const response = error.response;
    ....//
    return Promise.reject(error);
  }
);

export default service;

```

#### 2. 项目如何引用

##### 2.1 安装插件

```
npm i data-request --save
```

##### 2.2 引入使用

![image-20200902173547131](https://picbucket-1301820142.cos.ap-shanghai.myqcloud.com/img/image-20200902173547131.png)

在业务的入口管理处进行定义。

使用示例：

![image-20200903140859808](https://picbucket-1301820142.cos.ap-shanghai.myqcloud.com/img/image-20200903140859808.png)

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