From d391a847794425fe29ce1bafc6830fcad1b23eeb Mon Sep 17 00:00:00 2001 From: dyh_a Date: Sun, 1 May 2022 19:50:55 +0800 Subject: [PATCH] feat: simplifyMethodFactory --- __test__/AxiosRequestTemplate.test.ts | 27 ++++++++++++++++ src/AxiosRequestTemplate.ts | 45 +++++++++++---------------- 2 files changed, 46 insertions(+), 26 deletions(-) diff --git a/__test__/AxiosRequestTemplate.test.ts b/__test__/AxiosRequestTemplate.test.ts index 85866ac..2a340ff 100644 --- a/__test__/AxiosRequestTemplate.test.ts +++ b/__test__/AxiosRequestTemplate.test.ts @@ -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 () => { diff --git a/src/AxiosRequestTemplate.ts b/src/AxiosRequestTemplate.ts index 5b2be60..d1c8f77 100644 --- a/src/AxiosRequestTemplate.ts +++ b/src/AxiosRequestTemplate.ts @@ -294,31 +294,6 @@ export class AxiosRequestTemplate { } } - // 模板方法,请求入口。 - // 可子类覆盖,如非必要不建议子类覆盖 - /* request( - url: string, - data?: {}, - customConfig?: DynamicCustomConfig, - requestConfig?: Omit, - ): Promise> : ResType>; - async request( - url: string, - data: {} = {}, - customConfig = {} as CC, - requestConfig: AxiosRequestConfig = {}, - ): Promise { - 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( requestConfig: Omit & { url: string }, @@ -387,7 +362,25 @@ export class AxiosRequestTemplate { }; } - // 本质上跟methodFactory其实是一样的 + // 简化版请求方法工厂 忽略data还是params,url前缀,只改axios到url,data,method;及自定义配置 + simplifyMethodFactory(method: Method, urlPrefix = '') { + return ( + url: string, + data = {}, + customConfig = {} as DynamicCustomConfig, + ) => { + const requestConfig: AxiosRequestConfig = {}; + if (method === 'get') { + requestConfig.params = data; + } else { + requestConfig.data = data; + } + requestConfig.url = urlPrefix + url; + return this.request(requestConfig as any, customConfig); + }; + } + + // 本质上跟methodFactory是一样的 use(configs: Partial>) { const { customConfig: custom = {}, requestConfig: request = {} } = configs; return (