Skip to content

Commit f83eefb

Browse files
committed
refactor(request): unify response transformation methods and deprecate transformBackendResponse
1 parent 936b834 commit f83eefb

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

packages/axios/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export function createRequest<ResponseData, ApiData, State extends Record<string
116116
const responseType = response.config?.responseType || 'json';
117117

118118
if (responseType === 'json') {
119-
return opts.transformBackendResponse(response);
119+
return opts.transform(response);
120120
}
121121

122122
return response.data as MappedType<R, T>;
@@ -152,7 +152,7 @@ export function createFlatRequest<ResponseData, ApiData, State extends Record<st
152152
const responseType = response.config?.responseType || 'json';
153153

154154
if (responseType === 'json') {
155-
const data = await opts.transformBackendResponse(response);
155+
const data = await opts.transform(response);
156156

157157
return { data, error: null, response };
158158
}

packages/axios/src/options.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,21 @@ export function createDefaultOptions<
1010
State extends Record<string, unknown> = Record<string, unknown>
1111
>(options?: Partial<RequestOption<ResponseData, ApiData, State>>) {
1212
const opts: RequestOption<ResponseData, ApiData, State> = {
13+
defaultState: {} as State,
14+
transform: async response => response.data as unknown as ApiData,
15+
transformBackendResponse: async response => response.data as unknown as ApiData,
1316
onRequest: async config => config,
1417
isBackendSuccess: _response => true,
1518
onBackendFail: async () => {},
16-
transformBackendResponse: async response => response.data as unknown as ApiData,
1719
onError: async () => {}
1820
};
1921

22+
if (options?.transform) {
23+
opts.transform = options.transform;
24+
} else {
25+
opts.transform = options?.transformBackendResponse || opts.transform;
26+
}
27+
2028
Object.assign(opts, options);
2129

2230
return opts;

packages/axios/src/type.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ export interface RequestOption<
2424
*
2525
* @param response Axios response
2626
*/
27+
transform: ResponseTransform<AxiosResponse<ResponseData>, ApiData>;
28+
/**
29+
* transform the response data to the api data
30+
*
31+
* @deprecated use `transform` instead, will be removed in the next major version v3
32+
* @param response Axios response
33+
*/
2734
transformBackendResponse: ResponseTransform<AxiosResponse<ResponseData>, ApiData>;
2835
/**
2936
* The hook before request

src/service/request/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const request = createFlatRequest(
2222
errMsgStack: [],
2323
refreshTokenPromise: null
2424
} as RequestInstanceState,
25-
transformBackendResponse(response: AxiosResponse<App.Service.Response<any>>) {
25+
transform(response: AxiosResponse<App.Service.Response<any>>) {
2626
return response.data.data;
2727
},
2828
async onRequest(config) {
@@ -132,7 +132,7 @@ export const demoRequest = createRequest(
132132
baseURL: otherBaseURL.demo
133133
},
134134
{
135-
transformBackendResponse(response: AxiosResponse<App.Service.DemoResponse>) {
135+
transform(response: AxiosResponse<App.Service.DemoResponse>) {
136136
return response.data.result;
137137
},
138138
async onRequest(config) {

0 commit comments

Comments
 (0)