diff --git a/packages/axios/src/index.ts b/packages/axios/src/index.ts index 0d91ebe3..076d2cab 100644 --- a/packages/axios/src/index.ts +++ b/packages/axios/src/index.ts @@ -116,7 +116,7 @@ export function createRequest; @@ -152,7 +152,7 @@ export function createFlatRequest = Record >(options?: Partial>) { const opts: RequestOption = { + 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, isBackendSuccess: _response => true, onBackendFail: async () => {}, - transformBackendResponse: async response => response.data as unknown as ApiData, onError: async () => {} }; + if (options?.transform) { + opts.transform = options.transform; + } else { + opts.transform = options?.transformBackendResponse || opts.transform; + } + Object.assign(opts, options); return opts; diff --git a/packages/axios/src/type.ts b/packages/axios/src/type.ts index ebc50e3b..846950c4 100644 --- a/packages/axios/src/type.ts +++ b/packages/axios/src/type.ts @@ -24,6 +24,13 @@ export interface RequestOption< * * @param response Axios response */ + transform: ResponseTransform, 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, ApiData>; /** * The hook before request diff --git a/src/service/request/index.ts b/src/service/request/index.ts index 84f3cd5c..76aef178 100644 --- a/src/service/request/index.ts +++ b/src/service/request/index.ts @@ -22,7 +22,7 @@ export const request = createFlatRequest( errMsgStack: [], refreshTokenPromise: null } as RequestInstanceState, - transformBackendResponse(response: AxiosResponse>) { + transform(response: AxiosResponse>) { return response.data.data; }, async onRequest(config) { @@ -132,7 +132,7 @@ export const demoRequest = createRequest( baseURL: otherBaseURL.demo }, { - transformBackendResponse(response: AxiosResponse) { + transform(response: AxiosResponse) { return response.data.result; }, async onRequest(config) {