@qigy/requestor
v0.0.8
Published
requestor
Downloads
5
Readme
requestor
Get Start
Install
npm install @qigy/requestor--save
yarn install @qigy/requestor --save
pnpm install @qigy/requestor --save
Import
/* 方式一 */
import Http from '@qigy/requestor';
/* 方式二 */
import {...} from '@qigy/requestor';
Register
new api;
GET Register
/* 方式一 */
import Http from '@qigy/requestor';
Http.Register;
/* 方式二 */
import { Register } from '@qigy/requestor';
Use
Composite mode
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
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
/* 方式一 */
import Http from '@qigy/requestor';
Http.Get;
/* 方式二 */
import { Get } from '@qigy/requestor';
Use
import { Get } from '@qigy/requestor';
Get('/get/user/info', { id: '9527' })
.then((res) => {})
.catch((err) => {});
Post
Explain
Post request encapsulation.
Get
/* 方式一 */
import Http from '@qigy/requestor';
Http.Post;
/* 方式二 */
import { Post } from '@qigy/requestor';
Use
import { Post } from '@qigy/requestor';
Post('/set/user/info', { id: '9527' })
.then((res) => {})
.catch((err) => {});
Put
Explain
Put request encapsulation.
Get
/* 方式一 */
import Http from '@qigy/requestor';
Http.Put;
/* 方式二 */
import { Put } from '@qigy/requestor';
Use
import { Put } from '@qigy/requestor';
Put('/put/user/info', { id: '9527' })
.then((res) => {})
.catch((err) => {});
Patch
Explain
The patch request is encapsulated.
Get
/* 方式一 */
import Http from '@qigy/requestor';
Http.Patch;
/* 方式二 */
import { Patch } from '@qigy/requestor';
Use
import { Patch } from '@qigy/requestor';
Patch('/patch/user/info', { id: '9527' })
.then((res) => {})
.catch((err) => {});
Delete
Explain
Delete request encapsulation.
Get
/* 方式一 */
import Http from '@qigy/requestor';
Http.Delete;
/* 方式二 */
import { Delete } from '@qigy/requestor';
Use
import { Delete } from '@qigy/requestor';
Delete('/delete/user/info', { id: '9527' })
.then((res) => {})
.catch((err) => {});
Download
Explain
Download function encapsulation.
Get
/* 方式一 */
import Http from '@qigy/requestor';
Http.Download;
/* 方式二 */
import { Download } from '@qigy/requestor';
Use
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
/* 方式一 */
import Http from '@qigy/requestor';
Http.Request;
/* 方式二 */
import { Request } from '@qigy/requestor';
Use
import { Request } from '@qigy/requestor';
Request('/set/user/info', { method: 'POST', data:{ id:‘9527’ } })
.then((res) => {})
.catch((err) => {});
Custom Interceptor
Definition
interface RequestInterceptor{
(res:any): any
}
interface ResponseInterceptor{
(err:any): any
}
interface Interceptors {
requestInterceptor: RequestInterceptor
responseInterceptor: ResponseInterceptor
}
interface Config {
interceptors: Interceptors
....
}
Use
import { Get } from '@qigy/requestor';
const interceptors = {
requestInterceptor(res) {
return res;
},
responseInterceptor(err) {
return err;
}
};
Get('/get/user/info', {}, { interceptors })
.then((res) => {})
.catch((err) => {});