Skip to content

Commit

Permalink
feat: 改名为RequestTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
mengxinssfd committed Apr 21, 2022
1 parent ad6272a commit e5899c5
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 26 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# axios-wrapper
# request-template

基于状态处理实现的 axios 请求封装

Expand All @@ -16,15 +16,15 @@
### 安装

```shell
pnpm add @mxssfd/axios-wrapper
pnpm add request-template
```

### 使用

首先定义一个封装模板

```ts
import { StatusHandlers, CustomConfig, HttpStatus, AxiosWrapper } from '@mxssfd/AxiosWrapper';
import { StatusHandlers, CustomConfig, HttpStatus, AxiosRequestTemplate } from 'request-template';

// 通用错误Handler
const errorHandler: StatusHandler<CustomConfig> = (res, data, customConfig) => {
Expand Down Expand Up @@ -60,22 +60,22 @@ const statusHandlers: StatusHandlers = {
};
```

直接使用 AxiosWrapper
直接使用 AxiosRequestTemplate

```ts
const req = new AxiosWrapper<CustomConfig>({ baseURL: '/' }, { statusHandlers });
const req = new AxiosRequestTemplate<CustomConfig>({ baseURL: '/' }, { statusHandlers });
// 使用methodFactory生成请求方法
const get = req.methodFactory('get');
const post = req.methodFactory('post');
```

或者继承 AxiosWrapper 作为一个固定模板(推荐)
或者继承 AxiosRequestTemplate 作为一个固定模板(推荐)

```ts
/**
* 主域名请求类
*/
export default class PrimaryRequest extends AxiosWrapper {
export default class PrimaryRequest extends AxiosRequestTemplate {
static readonly ins = new PrimaryRequest();
static readonly get = PrimaryRequest.ins.methodFactory('get');
static readonly post = PrimaryRequest.ins.methodFactory('post');
Expand Down
12 changes: 6 additions & 6 deletions __test__/AxiosWrapper.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios';
import { routers } from './mock-server';
import { StatusHandlers, AxiosWrapper, CustomConfig } from '../src';
import { StatusHandlers, AxiosRequestTemplate, CustomConfig } from '../src';

jest.mock('axios');
const map = new Map<string, Function>();
Expand Down Expand Up @@ -34,7 +34,7 @@ describe('AxiosWrapper', () => {
return customConfig.returnRes ? res : data;
},
};
const req = new AxiosWrapper<CustomConfig>({ baseURL: '/' }, { statusHandlers });
const req = new AxiosRequestTemplate<CustomConfig>({ baseURL: '/' }, { statusHandlers });
const get = req.methodFactory('get');
const post = req.methodFactory('post');

Expand Down Expand Up @@ -90,7 +90,7 @@ describe('AxiosWrapper', () => {
expect(res2).toEqual({ code: 300 });
});
test('default constructor params', async () => {
const req = new AxiosWrapper();
const req = new AxiosRequestTemplate();
const get = req.methodFactory('get');
const res = await get<{ username: string; id: number }>(
'/user',
Expand Down Expand Up @@ -159,7 +159,7 @@ describe('AxiosWrapper', () => {
}
});
test('global customConfig', async () => {
const req = new AxiosWrapper({}, { returnRes: true, statusHandlers });
const req = new AxiosRequestTemplate({}, { returnRes: true, statusHandlers });
const get = req.methodFactory('get');
const res = await get<{ username: string; id: number }, true>('/user', undefined);
expect(res).toEqual({
Expand All @@ -168,7 +168,7 @@ describe('AxiosWrapper', () => {
});
});
test('cancel all', async () => {
const req = new AxiosWrapper();
const req = new AxiosRequestTemplate();
const get = req.methodFactory('get');
const reqList = [get('/user'), get('/user'), get('/user')];
req.cancelAll('test');
Expand All @@ -182,7 +182,7 @@ describe('AxiosWrapper', () => {
]);
});
test('cancel current', async () => {
const req = new AxiosWrapper();
const req = new AxiosRequestTemplate();
const get = req.methodFactory('get');
const res1 = get('/user');
req.cancelCurrentRequest('cancel1');
Expand Down
4 changes: 2 additions & 2 deletions __test__/Other.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// 其他域名请求
import { StatusHandlers, AxiosWrapper, CustomConfig } from '../src';
import { StatusHandlers, AxiosRequestTemplate, CustomConfig } from '../src';

interface MyCustomConfig extends CustomConfig {
p1?: number;
Expand All @@ -12,7 +12,7 @@ const statusHandlers: StatusHandlers<MyCustomConfig> = {
},
};

export default class Other extends AxiosWrapper<MyCustomConfig> {
export default class Other extends AxiosRequestTemplate<MyCustomConfig> {
static readonly ins = new Other();
static readonly get = Other.ins.methodFactory('get');
static readonly post = Other.ins.methodFactory('post');
Expand Down
4 changes: 2 additions & 2 deletions __test__/Primary.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// 主域名请求
import { StatusHandlers, AxiosWrapper } from '../src';
import { StatusHandlers, AxiosRequestTemplate } from '../src';

const statusHandlers: StatusHandlers = {
200: (res, data, customConfig) => {
return customConfig.returnRes ? res : data;
},
};
export default class Primary extends AxiosWrapper {
export default class Primary extends AxiosRequestTemplate {
static readonly ins = new Primary();
static readonly get = Primary.ins.methodFactory('get');
static readonly post = Primary.ins.methodFactory('post');
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@mxssfd/axios-wrapper",
"version": "0.0.6",
"description": "axios封装类库",
"name": "request-template",
"version": "0.0.1",
"description": "请求模板封装",
"main": "dist/index.js",
"typings": "./types/",
"sideEffects": false,
Expand All @@ -17,22 +17,22 @@
"build": "run-s compile babel pub",
"type-check": "tsc --noEmit",
"pub": "npm publish",
"compub": "run-s lint-check compile",
"compub": "run-s lint-check compile pub",
"eslint": "eslint",
"prettier-fix": "prettier src __test__ --write",
"prettier-check": "prettier src __test__ --check",
"lint-check": "run-p eslint prettier-check"
},
"repository": {
"type": "git",
"url": "git+https://github.com/mengxinssfd/axios-wrapper.git"
"url": "git+https://github.com/mengxinssfd/request-template.git"
},
"author": "DYH",
"license": "MIT",
"bugs": {
"url": "https://github.com/mengxinssfd/axios-wrapper/issues"
"url": "https://github.com/mengxinssfd/request-template/issues"
},
"homepage": "https://github.com/mengxinssfd/axios-wrapper#readme",
"homepage": "https://github.com/mengxinssfd/request-template#readme",
"dependencies": {
"axios": "^0.26.1",
"qs": "^6.10.3"
Expand Down
2 changes: 1 addition & 1 deletion src/AxiosWrapper.ts → src/AxiosRequestTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Qs from 'qs';
import { Cache } from './Cache';

// 使用模板方法模式处理axios请求, 具体类可实现protected方法替换掉原有方法
export class AxiosWrapper<CC extends CustomConfig = CustomConfig> {
export class AxiosRequestTemplate<CC extends CustomConfig = CustomConfig> {
// 为了提高子类的拓展性,子类可以访问并使用该实例,但如果没必要不要去访问该axios实例
protected readonly axiosIns: AxiosInstance;
// 子类不可访问缓存
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './AxiosWrapper';
export * from './AxiosRequestTemplate';
export * from './Cache';
export * from './HttpStatus';
export * from './types';

0 comments on commit e5899c5

Please sign in to comment.