Skip to content

Commit

Permalink
feat: simplifyMethodFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
mengxinssfd committed May 1, 2022
1 parent 748ea7a commit d391a84
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 26 deletions.
27 changes: 27 additions & 0 deletions __test__/AxiosRequestTemplate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,33 @@ describe('AxiosRequestTemplate', () => {
expect(e).toEqual({ code: 0, msg: '账号或密码错误' });
}
});
test('simplifyMethodFactory', async () => {
const get = req.simplifyMethodFactory('get');
const post = req.simplifyMethodFactory('post');
expect.assertions(4);
// console.log((axios.create({ url: 'test' }) as any)(1, 2, 3), Req);
const res = await get<{ username: string; id: number }>('/user');
expect(res).toEqual({ code: 200, data: { username: 'get', id: 1 }, msg: 'success' });

const res2 = await get<{ username: string; id: number }, true>(
'/user',
{},
{ returnRes: true },
);
expect(res2).toEqual({
status: 200,
data: { code: 200, data: { username: 'get', id: 1 }, msg: 'success' },
});

const res3 = await post('/login', { username: 'foo', password: 'bar' });
expect(res3).toEqual({ code: 200, msg: 'success' });

try {
await post('/login', { username: 'foo' });
} catch (e) {
expect(e).toEqual({ code: 0, msg: '账号或密码错误' });
}
});

describe('AxiosWrapper Cache', () => {
test('no cache', async () => {
Expand Down
45 changes: 19 additions & 26 deletions src/AxiosRequestTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,31 +294,6 @@ export class AxiosRequestTemplate<CC extends CustomConfig = CustomConfig> {
}
}

// 模板方法,请求入口。
// 可子类覆盖,如非必要不建议子类覆盖
/* request<T = never, RC extends boolean = false>(
url: string,
data?: {},
customConfig?: DynamicCustomConfig<CC, RC>,
requestConfig?: Omit<AxiosRequestConfig, 'data' | 'cancelToken'>,
): Promise<RC extends true ? AxiosResponse<ResType<T>> : ResType<T>>;
async request(
url: string,
data: {} = {},
customConfig = {} as CC,
requestConfig: AxiosRequestConfig = {},
): Promise<any> {
const ctx = this.generateContext(url, data, customConfig, requestConfig);
this.beforeRequest(ctx);
try {
return await this.execRequest(ctx);
} catch (e: any) {
return await this.handleError(ctx, e);
} finally {
this.afterRequest(ctx);
}
}*/

// 模板方法,请求入口。
request<T = never, RC extends boolean = false>(
requestConfig: Omit<AxiosRequestConfig, 'cancelToken' | 'url'> & { url: string },
Expand Down Expand Up @@ -387,7 +362,25 @@ export class AxiosRequestTemplate<CC extends CustomConfig = CustomConfig> {
};
}

// 本质上跟methodFactory其实是一样的
// 简化版请求方法工厂 忽略data还是params,url前缀,只改axios到url,data,method;及自定义配置
simplifyMethodFactory(method: Method, urlPrefix = '') {
return <T = never, RC extends boolean = false>(
url: string,
data = {},
customConfig = {} as DynamicCustomConfig<CC, RC>,
) => {
const requestConfig: AxiosRequestConfig = {};
if (method === 'get') {
requestConfig.params = data;
} else {
requestConfig.data = data;
}
requestConfig.url = urlPrefix + url;
return this.request<T, RC>(requestConfig as any, customConfig);
};
}

// 本质上跟methodFactory是一样的
use(configs: Partial<Configs<CC>>) {
const { customConfig: custom = {}, requestConfig: request = {} } = configs;
return <T = never, RC extends boolean = false>(
Expand Down

0 comments on commit d391a84

Please sign in to comment.