Skip to content

Commit a821d9a

Browse files
committed
perf: imporve axios logic
1 parent 7c2f851 commit a821d9a

File tree

13 files changed

+77
-74
lines changed

13 files changed

+77
-74
lines changed

src/api/demo/account.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import { defHttp } from '/@/utils/http/axios';
22
import { GetAccountInfoModel } from './model/accountModel';
33

4+
const { get } = defHttp;
5+
46
enum Api {
57
ACCOUNT_INFO = '/account/getAccountInfo',
68
}
79

810
// Get personal center-basic settings
9-
export function accountInfoApi() {
10-
return defHttp.request<GetAccountInfoModel>({
11-
url: Api.ACCOUNT_INFO,
12-
method: 'GET',
13-
});
14-
}
11+
12+
export const accountInfoApi = () => get<GetAccountInfoModel>({ url: Api.ACCOUNT_INFO });

src/api/demo/error.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { defHttp } from '/@/utils/http/axios';
22

3+
const { get } = defHttp;
4+
35
enum Api {
46
// The address does not exist
57
Error = '/error',
@@ -8,9 +10,5 @@ enum Api {
810
/**
911
* @description: Trigger ajax error
1012
*/
11-
export function fireErrorApi() {
12-
return defHttp.request({
13-
url: Api.Error,
14-
method: 'GET',
15-
});
16-
}
13+
14+
export const fireErrorApi = () => get({ url: Api.Error });

src/api/demo/select.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { defHttp } from '/@/utils/http/axios';
22
import { DemoOptionsGetResultModel } from './model/optionsModel';
3+
const { get } = defHttp;
34

45
enum Api {
56
OPTIONS_LIST = '/select/getDemoOptions',
@@ -8,9 +9,4 @@ enum Api {
89
/**
910
* @description: Get sample options value
1011
*/
11-
export function optionsListApi() {
12-
return defHttp.request<DemoOptionsGetResultModel>({
13-
url: Api.OPTIONS_LIST,
14-
method: 'GET',
15-
});
16-
}
12+
export const optionsListApi = () => get<DemoOptionsGetResultModel>({ url: Api.OPTIONS_LIST });

src/api/demo/table.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import { defHttp } from '/@/utils/http/axios';
22
import { DemoParams, DemoListGetResultModel } from './model/tableModel';
33

4+
const { get } = defHttp;
5+
46
enum Api {
57
DEMO_LIST = '/table/getDemoList',
68
}
79

810
/**
911
* @description: Get sample list value
1012
*/
11-
export function demoListApi(params: DemoParams) {
12-
return defHttp.request<DemoListGetResultModel>({
13+
14+
export const demoListApi = (params: DemoParams) =>
15+
get<DemoListGetResultModel>({
1316
url: Api.DEMO_LIST,
14-
method: 'GET',
1517
params,
1618
headers: {
1719
ignoreCancelToken: true,
1820
},
1921
});
20-
}

src/api/sys/menu.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import { defHttp } from '/@/utils/http/axios';
2-
32
import { getMenuListByIdParams, getMenuListByIdParamsResultModel } from './model/menuModel';
43

4+
const { get } = defHttp;
5+
56
enum Api {
67
GetMenuListById = '/getMenuListById',
78
}
89

910
/**
1011
* @description: Get user menu based on id
1112
*/
12-
export function getMenuListById(params: getMenuListByIdParams) {
13-
return defHttp.request<getMenuListByIdParamsResultModel>({
14-
url: Api.GetMenuListById,
15-
method: 'GET',
16-
params,
17-
});
18-
}
13+
14+
export const getMenuListById = (params: getMenuListByIdParams) => {
15+
return get<getMenuListByIdParamsResultModel>({ url: Api.GetMenuListById, params });
16+
};

src/api/sys/user.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from './model/userModel';
88
import { ErrorMessageMode } from '/@/utils/http/axios/types';
99

10+
const { post, get } = defHttp;
1011
enum Api {
1112
Login = '/login',
1213
GetUserInfoById = '/getUserInfoById',
@@ -17,10 +18,9 @@ enum Api {
1718
* @description: user login api
1819
*/
1920
export function loginApi(params: LoginParams, mode: ErrorMessageMode = 'modal') {
20-
return defHttp.request<LoginResultModel>(
21+
return post<LoginResultModel>(
2122
{
2223
url: Api.Login,
23-
method: 'POST',
2424
params,
2525
},
2626
{
@@ -33,17 +33,15 @@ export function loginApi(params: LoginParams, mode: ErrorMessageMode = 'modal')
3333
* @description: getUserInfoById
3434
*/
3535
export function getUserInfoById(params: GetUserInfoByUserIdParams) {
36-
return defHttp.request<GetUserInfoByUserIdModel>({
36+
return get<GetUserInfoByUserIdModel>({
3737
url: Api.GetUserInfoById,
38-
method: 'GET',
3938
params,
4039
});
4140
}
4241

4342
export function getPermCodeByUserId(params: GetUserInfoByUserIdParams) {
44-
return defHttp.request<string[]>({
43+
return get<string[]>({
4544
url: Api.GetPermCodeByUserId,
46-
method: 'GET',
4745
params,
4846
});
4947
}

src/utils/http/axios/Axios.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,22 @@ export class VAxios {
165165
};
166166
}
167167

168+
get<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T> {
169+
return this.request({ ...config, method: 'GET' }, options);
170+
}
171+
172+
post<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T> {
173+
return this.request({ ...config, method: 'POST' }, options);
174+
}
175+
176+
put<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T> {
177+
return this.request({ ...config, method: 'PUT' }, options);
178+
}
179+
180+
delete<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T> {
181+
return this.request({ ...config, method: 'DELETE' }, options);
182+
}
183+
168184
request<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T> {
169185
let conf: AxiosRequestConfig = cloneDeep(config);
170186
const transform = this.getTransform();
@@ -173,7 +189,7 @@ export class VAxios {
173189

174190
const opt: RequestOptions = Object.assign({}, requestOptions, options);
175191

176-
const { beforeRequestHook, requestCatch, transformRequestData } = transform || {};
192+
const { beforeRequestHook, requestCatchHook, transformRequestHook } = transform || {};
177193
if (beforeRequestHook && isFunction(beforeRequestHook)) {
178194
conf = beforeRequestHook(conf, opt);
179195
}
@@ -183,16 +199,16 @@ export class VAxios {
183199
this.axiosInstance
184200
.request<any, AxiosResponse<Result>>(conf)
185201
.then((res: AxiosResponse<Result>) => {
186-
if (transformRequestData && isFunction(transformRequestData)) {
187-
const ret = transformRequestData(res, opt);
202+
if (transformRequestHook && isFunction(transformRequestHook)) {
203+
const ret = transformRequestHook(res, opt);
188204
ret !== errorResult ? resolve(ret) : reject(new Error('request error!'));
189205
return;
190206
}
191207
resolve((res as unknown) as Promise<T>);
192208
})
193209
.catch((e: Error) => {
194-
if (requestCatch && isFunction(requestCatch)) {
195-
reject(requestCatch(e));
210+
if (requestCatchHook && isFunction(requestCatchHook)) {
211+
reject(requestCatchHook(e));
196212
return;
197213
}
198214
reject(e);

src/utils/http/axios/axiosCancel.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import axios from 'axios';
33

44
import { isFunction } from '/@/utils/is';
55

6-
// 声明一个 Map 用于存储每个请求的标识 和 取消函数
6+
// Used to store the identification and cancellation function of each request
77
let pendingMap = new Map<string, Canceler>();
88

99
export const getPendingUrl = (config: AxiosRequestConfig) => [config.method, config.url].join('&');
1010

1111
export class AxiosCanceler {
1212
/**
13-
* 添加请求
13+
* Add request
1414
* @param {Object} config
1515
*/
1616
addPending(config: AxiosRequestConfig) {
@@ -20,14 +20,14 @@ export class AxiosCanceler {
2020
config.cancelToken ||
2121
new axios.CancelToken((cancel) => {
2222
if (!pendingMap.has(url)) {
23-
// 如果 pending 中不存在当前请求,则添加进去
23+
// If there is no current request in pending, add it
2424
pendingMap.set(url, cancel);
2525
}
2626
});
2727
}
2828

2929
/**
30-
* @description: 清空所有pending
30+
* @description: Clear all pending
3131
*/
3232
removeAllPending() {
3333
pendingMap.forEach((cancel) => {
@@ -37,22 +37,23 @@ export class AxiosCanceler {
3737
}
3838

3939
/**
40-
* 移除请求
40+
* Removal request
4141
* @param {Object} config
4242
*/
4343
removePending(config: AxiosRequestConfig) {
4444
const url = getPendingUrl(config);
4545

4646
if (pendingMap.has(url)) {
47-
// 如果在 pending 中存在当前请求标识,需要取消当前请求,并且移除
47+
// If there is a current request identifier in pending,
48+
// the current request needs to be cancelled and removed
4849
const cancel = pendingMap.get(url);
4950
cancel && cancel(url);
5051
pendingMap.delete(url);
5152
}
5253
}
5354

5455
/**
55-
* @description: 重置
56+
* @description: reset
5657
*/
5758
reset(): void {
5859
pendingMap = new Map<string, Canceler>();

src/utils/http/axios/axiosTransform.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
/**
2-
* 数据处理类,可以根据项目自行配置
2+
* Data processing class, can be configured according to the project
33
*/
44
import type { AxiosRequestConfig, AxiosResponse } from 'axios';
55
import type { RequestOptions, Result } from './types';
66

77
export abstract class AxiosTransform {
88
/**
9-
* @description: 请求之前处理配置
9+
* @description: Process configuration before request
1010
* @description: Process configuration before request
1111
*/
1212
beforeRequestHook?: (config: AxiosRequestConfig, options: RequestOptions) => AxiosRequestConfig;
1313

1414
/**
15-
* @description: 请求成功处理
15+
* @description: Request successfully processed
1616
*/
17-
transformRequestData?: (res: AxiosResponse<Result>, options: RequestOptions) => any;
17+
transformRequestHook?: (res: AxiosResponse<Result>, options: RequestOptions) => any;
1818

1919
/**
2020
* @description: 请求失败处理
2121
*/
22-
requestCatch?: (e: Error) => Promise<any>;
22+
requestCatchHook?: (e: Error) => Promise<any>;
2323

2424
/**
2525
* @description: 请求之前的拦截器

src/utils/http/axios/const.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
// 接口返回值data不能为这个,否则会判为请求失败
1+
// The interface return value data cannot be this, otherwise the request will be judged as a failure
22
export const errorResult = '__ERROR_RESULT__';

0 commit comments

Comments
 (0)