# @qigy/requestor

> requestor

Latest version **0.0.8** (published 2022-11-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install @qigy/requestor
pnpm add @qigy/requestor
yarn add @qigy/requestor
bun add @qigy/requestor
```

## Health

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

Positive: has types; no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.8 |
| Published | 2022-11-04 |
| First published | 2022-11-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 50.2 KB |
| Known vulnerabilities | 0 (+24 in 1 direct dependencies) |
| Install scripts | no |
| Author | qigy |
| Maintainers | qigy |
| Keywords | requestor, axios, http |

## Links

- npm: https://www.npmjs.com/package/@qigy/requestor
- npm.io page: https://npm.io/package/@qigy/requestor

## Dependencies (1)

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

## 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.0.8 (latest) — 2022-11-04
- 0.0.7 — 2022-11-04

## README

# requestor

## 快速开始

### 安装

```shell
npm install @qigy/requestor--save

yarn install @qigy/requestor --save

pnpm install @qigy/requestor --save
```

### 导入

```javascript
/* 方式一 */
import Http from '@qigy/requestor';
/* 方式二 */
import {...} from '@qigy/requestor';
```

### 注册

axios 的注册方式，大家应该都知道的。为了进一步减少代码，加强语义化，。重新设计了注册方式。

#### 获取 Register

```javascript
/* 方式一 */
import Http from '@qigy/requestor';
Http.Register；
/* 方式二 */
import { Register } from '@qigy/requestor';
```

#### 使用

##### 复合方式

```javascript
import { Register } from '@qigy/requestor';

const options = {
  baseUrl: '/bs/protal',
  timeOut: 0,
  headers: {
    get: {},
    post: {}
  },
  defaultFilename: 'demo.md',
  requestInterceptor() {},
  requestInterceptorCatch() {},
  responseInterceptor() {},
  responseInterceptorCatch() {},
};
Register(options)；
```

##### 独立方式

```javascript
import { Register } from '@qigy/requestor';

Register.baseUrl('/bs/protal');
Register.timeOut(100);
Register.headers({});
Register.defaultFilename('demo.md');
Register.requestInterceptor(
  () => {},
  () => {}
);
Register.responseInterceptor(
  () => {},
  () => {}
);
```

##### 备注

其中请求拦截器与响应拦截器只能注册一次。其余属性均可多次注册进行覆盖。

### API

#### Get

##### 说明

get 请求封装。

##### 获取

```javascript
/* 方式一 */
import Http from '@qigy/requestor';
Http.Get；
/* 方式二 */
import { Get } from '@qigy/requestor';
```

##### 使用

```javascript
import { Get } from '@qigy/requestor';

Get('/get/user/info', { id: '9527' })
  .then((res) => {})
  .catch((err) => {});
```

#### Post

##### 说明

post 请求封装。

##### 获取

```javascript
/* 方式一 */
import Http from '@qigy/requestor';
Http.Post；
/* 方式二 */
import { Post } from '@qigy/requestor';
```

##### 使用

```javascript
import { Post } from '@qigy/requestor';

Post('/set/user/info', { id: '9527' })
  .then((res) => {})
  .catch((err) => {});
```

#### Put

##### 说明

put 请求封装。

##### 获取

```javascript
/* 方式一 */
import Http from '@qigy/requestor';
Http.Put；
/* 方式二 */
import { Put } from '@qigy/requestor';
```

##### 使用

```javascript
import { Put } from '@qigy/requestor';

Put('/put/user/info', { id: '9527' })
  .then((res) => {})
  .catch((err) => {});
```

#### Patch

##### 说明

patch 请求封装。

##### 获取

```javascript
/* 方式一 */
import Http from '@qigy/requestor';
Http.Patch；
/* 方式二 */
import { Patch } from '@qigy/requestor';
```

##### 使用

```javascript
import { Patch } from '@qigy/requestor';

Patch('/patch/user/info', { id: '9527' })
  .then((res) => {})
  .catch((err) => {});
```

#### Delete

##### 说明

delete 请求封装。

##### 获取

```javascript
/* 方式一 */
import Http from '@qigy/requestor';
Http.Delete；
/* 方式二 */
import { Delete } from '@qigy/requestor';
```

##### 使用

```javascript
import { Delete } from '@qigy/requestor';

Delete('/delete/user/info', { id: '9527' })
  .then((res) => {})
  .catch((err) => {});
```

#### Download

##### 说明

下载功能功能封装。

##### 获取

```javascript
/* 方式一 */
import Http from '@qigy/requestor';
Http.Download；
/* 方式二 */
import { Download } from '@qigy/requestor';
```

##### 使用

```javascript
import { Download } from '@qigy/requestor';

Download('/download/user/info', { method: 'POST' })
  .then((res) => {})
  .catch((err) => {});
```

##### 备注

自定义响应拦截器将会失效。

#### Request

##### 说明

请求通用实例。

##### 获取

```javascript
/* 方式一 */
import Http from '@qigy/requestor';
Http.Request；
/* 方式二 */
import { Request } from '@qigy/requestor';
```

##### 使用

```javascript
import { Request } from '@qigy/requestor';

Request('/set/user/info', { method: 'POST'， data：{ id：‘9527’ } })
.then((res) => {})
.catch((err) => {});
```

### 自定义拦截器

#### 定义

```typescript
interface RequestInterceptor{
  (res:any): any
}

interface ResponseInterceptor{
  (err:any): any
}

interface Interceptors {
  requestInterceptor: RequestInterceptor
  responseInterceptor: ResponseInterceptor
}

interface Config {
  interceptors: Interceptors
  ....
}
```

#### 使用

```typescript
import { Get } from '@qigy/requestor';

const interceptors = {
  requestInterceptor(res) {
    return res;
  },
  responseInterceptor(err) {
    return err;
  }
};
Get('/get/user/info', {}, { interceptors })
  .then((res) => {})
  .catch((err) => {});
```

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