Skip to content

Commit

Permalink
refactor(优化): 移除qs
Browse files Browse the repository at this point in the history
  • Loading branch information
mengxinssfd committed Apr 24, 2022
1 parent 76e326e commit baf8811
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 20 deletions.
4 changes: 4 additions & 0 deletions __test__/Primary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export default class Primary extends AxiosRequestTemplate {
super({ baseURL: 'http://test.test' }, { statusHandlers });
}

protected handleRequestData(data: {}, requestConfig) {
super.handleRequestData({ ...data, token: 123 }, requestConfig);
}

protected setInterceptors() {
this.interceptors.request.use((config) => {
if (!config.headers) config.headers = {};
Expand Down
3 changes: 1 addition & 2 deletions __test__/cacheConfig.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useMockAxios, routers } from './mock-server';
useMockAxios(routers);
import { AxiosRequestTemplate } from '../src';
import { CustomConfig } from '../types';
import { AxiosRequestTemplate, CustomConfig } from '../src';

describe('mock cacheConfig', () => {
test('empty global', async () => {
Expand Down
10 changes: 4 additions & 6 deletions __test__/mock-server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Qs from 'qs';
import axios, { AxiosRequestConfig } from 'axios';

function buildRes(res: { code: number; data?: any; msg: string }) {
Expand All @@ -13,24 +12,23 @@ export const routers = {
return buildRes({ code: 200, data: { username: 'get', id: 1 }, msg: 'success' });
},
'/login'(data: any) {
const d: any = typeof data === 'string' ? Qs.parse(data) : data;
if (
data instanceof FormData &&
data.get('username') === 'foo' &&
data.get('password') === 'bar'
) {
return buildRes({ code: 200, msg: 'success' });
}
if (d.username === 'foo' && d.password === 'bar') {
if (data.username === 'foo' && data.password === 'bar') {
return buildRes({ code: 200, msg: 'success' });
}
return Promise.reject({ code: 0, msg: '账号或密码错误' });
},
'404'(d: string) {
if (d === 'returnRes=1') {
'404'(d: any) {
if (d.returnRes === 1) {
return Promise.reject({ status: 404, response: { data: { code: 200 } } });
}
if (d === 'returnRes=2') {
if (d.returnRes === 2) {
return Promise.reject({ status: 404, response: { data: { code: 300 } } });
}
return Promise.reject('404');
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
},
"homepage": "https://github.com/mengxinssfd/request-template#readme",
"dependencies": {
"axios": "^0.26.1",
"qs": "^6.10.3"
"axios": "^0.26.1"
},
"devDependencies": {
"@types/jest": "^27.4.1",
Expand Down
10 changes: 0 additions & 10 deletions src/AxiosRequestTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@ import axios, {
CancelToken,
AxiosError,
} from 'axios';
import Qs from 'qs';
import { Cache } from './Cache';
import { CustomCacheConfig } from './types';

const root = Function('return this')();

// 使用模板方法模式处理axios请求, 具体类可实现protected方法替换掉原有方法
export class AxiosRequestTemplate<CC extends CustomConfig = CustomConfig> {
// 为了提高子类的拓展性,子类可以访问并使用该实例,但如果没必要不要去访问该axios实例
Expand Down Expand Up @@ -141,13 +138,6 @@ export class AxiosRequestTemplate<CC extends CustomConfig = CustomConfig> {
requestConfig.params = data;
return;
}
// node里面没有FormData
if (!(root as typeof window).FormData || !(data instanceof FormData)) {
// 使用Qs.stringify处理过的数据不会有{}包裹
// 使用Qs.stringify其实就是转成url的参数形式:a=1&b=2&c=3
// 格式化模式有三种:indices、brackets、repeat
data = Qs.stringify(data, { arrayFormat: 'repeat' });
}
requestConfig.data = data;
}
// 处理响应结果
Expand Down

0 comments on commit baf8811

Please sign in to comment.