mirror of
https://github.com/soybeanjs/soybean-admin.git
synced 2025-09-19 10:06:38 +08:00
refactor(request): unify response transformation methods and deprecate transformBackendResponse
This commit is contained in:
parent
936b834e62
commit
f83eefbc3e
@ -116,7 +116,7 @@ export function createRequest<ResponseData, ApiData, State extends Record<string
|
|||||||
const responseType = response.config?.responseType || 'json';
|
const responseType = response.config?.responseType || 'json';
|
||||||
|
|
||||||
if (responseType === 'json') {
|
if (responseType === 'json') {
|
||||||
return opts.transformBackendResponse(response);
|
return opts.transform(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
return response.data as MappedType<R, T>;
|
return response.data as MappedType<R, T>;
|
||||||
@ -152,7 +152,7 @@ export function createFlatRequest<ResponseData, ApiData, State extends Record<st
|
|||||||
const responseType = response.config?.responseType || 'json';
|
const responseType = response.config?.responseType || 'json';
|
||||||
|
|
||||||
if (responseType === 'json') {
|
if (responseType === 'json') {
|
||||||
const data = await opts.transformBackendResponse(response);
|
const data = await opts.transform(response);
|
||||||
|
|
||||||
return { data, error: null, response };
|
return { data, error: null, response };
|
||||||
}
|
}
|
||||||
|
@ -10,13 +10,21 @@ export function createDefaultOptions<
|
|||||||
State extends Record<string, unknown> = Record<string, unknown>
|
State extends Record<string, unknown> = Record<string, unknown>
|
||||||
>(options?: Partial<RequestOption<ResponseData, ApiData, State>>) {
|
>(options?: Partial<RequestOption<ResponseData, ApiData, State>>) {
|
||||||
const opts: RequestOption<ResponseData, ApiData, State> = {
|
const opts: RequestOption<ResponseData, ApiData, State> = {
|
||||||
|
defaultState: {} as State,
|
||||||
|
transform: async response => response.data as unknown as ApiData,
|
||||||
|
transformBackendResponse: async response => response.data as unknown as ApiData,
|
||||||
onRequest: async config => config,
|
onRequest: async config => config,
|
||||||
isBackendSuccess: _response => true,
|
isBackendSuccess: _response => true,
|
||||||
onBackendFail: async () => {},
|
onBackendFail: async () => {},
|
||||||
transformBackendResponse: async response => response.data as unknown as ApiData,
|
|
||||||
onError: async () => {}
|
onError: async () => {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (options?.transform) {
|
||||||
|
opts.transform = options.transform;
|
||||||
|
} else {
|
||||||
|
opts.transform = options?.transformBackendResponse || opts.transform;
|
||||||
|
}
|
||||||
|
|
||||||
Object.assign(opts, options);
|
Object.assign(opts, options);
|
||||||
|
|
||||||
return opts;
|
return opts;
|
||||||
|
@ -24,6 +24,13 @@ export interface RequestOption<
|
|||||||
*
|
*
|
||||||
* @param response Axios response
|
* @param response Axios response
|
||||||
*/
|
*/
|
||||||
|
transform: ResponseTransform<AxiosResponse<ResponseData>, ApiData>;
|
||||||
|
/**
|
||||||
|
* transform the response data to the api data
|
||||||
|
*
|
||||||
|
* @deprecated use `transform` instead, will be removed in the next major version v3
|
||||||
|
* @param response Axios response
|
||||||
|
*/
|
||||||
transformBackendResponse: ResponseTransform<AxiosResponse<ResponseData>, ApiData>;
|
transformBackendResponse: ResponseTransform<AxiosResponse<ResponseData>, ApiData>;
|
||||||
/**
|
/**
|
||||||
* The hook before request
|
* The hook before request
|
||||||
|
@ -22,7 +22,7 @@ export const request = createFlatRequest(
|
|||||||
errMsgStack: [],
|
errMsgStack: [],
|
||||||
refreshTokenPromise: null
|
refreshTokenPromise: null
|
||||||
} as RequestInstanceState,
|
} as RequestInstanceState,
|
||||||
transformBackendResponse(response: AxiosResponse<App.Service.Response<any>>) {
|
transform(response: AxiosResponse<App.Service.Response<any>>) {
|
||||||
return response.data.data;
|
return response.data.data;
|
||||||
},
|
},
|
||||||
async onRequest(config) {
|
async onRequest(config) {
|
||||||
@ -132,7 +132,7 @@ export const demoRequest = createRequest(
|
|||||||
baseURL: otherBaseURL.demo
|
baseURL: otherBaseURL.demo
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
transformBackendResponse(response: AxiosResponse<App.Service.DemoResponse>) {
|
transform(response: AxiosResponse<App.Service.DemoResponse>) {
|
||||||
return response.data.result;
|
return response.data.result;
|
||||||
},
|
},
|
||||||
async onRequest(config) {
|
async onRequest(config) {
|
||||||
|
Loading…
Reference in New Issue
Block a user