npm package: @qigy/requestor
Description: requestor
Author: qigy
Latest version: 0.0.8

# requestor

## Get Start

### Install

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

yarn install @qigy/requestor --save

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

### Import

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

### Register

new api;

#### GET Register

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

#### Use

##### Composite mode

```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)；
```

##### Independent approach

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

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

##### Remark

The request interceptor and response interceptor can only be registered once. Other attributes can be registered multiple times to overwrite.

### API

#### Get

##### Explain

Get request encapsulation.

##### Get

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

##### Use

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

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

#### Post

##### Explain

Post request encapsulation.

##### Get

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

##### Use

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

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

#### Put

##### Explain

Put request encapsulation.

##### Get

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

##### Use

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

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

#### Patch

##### Explain

The patch request is encapsulated.

##### Get

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

##### Use

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

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

#### Delete

##### Explain

Delete request encapsulation.

##### Get

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

##### Use

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

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

#### Download

##### Explain

Download function encapsulation.

##### Get

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

##### Use

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

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

##### Remark

The custom response interceptor will fail.

#### Request

##### Explain

Request a generic instance.

##### Get

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

##### Use

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

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

### Custom Interceptor

#### Definition

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

interface ResponseInterceptor{
  (err:any): any
}

interface Interceptors {
  requestInterceptor: RequestInterceptor
  responseInterceptor: ResponseInterceptor
}

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

#### Use

```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) => {});
```