22 isEmptyArray ,
33 isFunction ,
44 isPlainObject ,
5+ isString ,
56 isUndefined ,
67} from './helpers/isTypes' ;
78import { assert } from './helpers/error' ;
@@ -57,6 +58,19 @@ export interface AxiosAdapterResponseError extends AnyObject {
5758 * 响应头
5859 */
5960 headers : AnyObject ;
61+ /**
62+ * 错误数据
63+ */
64+ data ?: {
65+ /**
66+ * 错误信息
67+ */
68+ errMsg : string ;
69+ /**
70+ * Errno错误码
71+ */
72+ errno : number ;
73+ } ;
6074}
6175
6276export interface AxiosAdapterRequestConfig extends AnyObject {
@@ -253,22 +267,25 @@ export function createAdapter(platform: AxiosPlatform) {
253267 return download ( options ) ;
254268 }
255269
256- function transformResult ( result : AnyObject ) : void {
257- result . status =
258- result . status ??
259- result . statusCode ??
260- ( isUndefined ( result . data ) ? 400 : 200 ) ;
261- result . statusText =
262- result . status === 200
263- ? 'OK'
264- : result . status === 400
265- ? 'Bad Adapter'
266- : result . errMsg ;
267- result . headers = result . headers || result . header ;
268-
269- if ( result . statusCode ) delete result . statusCode ;
270- if ( result . errMsg ) delete result . errMsg ;
271- if ( result . header ) delete result . header ;
270+ function transformResponse ( response : AnyObject ) : void {
271+ response . status = response . status ?? response . statusCode ;
272+ response . statusText = 'OK' ;
273+
274+ if ( isUndefined ( response . status ) ) {
275+ response . status = 400 ;
276+ response . statusText = 'Fail Adapter' ;
277+ }
278+
279+ response . headers = response . headers ?? response . header ?? { } ;
280+
281+ if ( isUndefined ( response . data ) && isString ( response . errMsg ) ) {
282+ response . data = {
283+ errMsg : response . errMsg ,
284+ errno : response . errno ,
285+ } ;
286+ }
287+
288+ cleanResponse ( response , [ 'statusCode' , 'errMsg' , 'errno' , 'header' ] ) ;
272289 }
273290
274291 function transformOptions (
@@ -278,11 +295,11 @@ export function createAdapter(platform: AxiosPlatform) {
278295 ...config ,
279296 header : config . headers ,
280297 success ( response ) : void {
281- transformResult ( response ) ;
298+ transformResponse ( response ) ;
282299 config . success ( response ) ;
283300 } ,
284301 fail ( error : AxiosAdapterResponseError ) : void {
285- transformResult ( error ) ;
302+ transformResponse ( error ) ;
286303 config . fail ( error ) ;
287304 } ,
288305 } ;
@@ -297,9 +314,16 @@ export function createAdapter(platform: AxiosPlatform) {
297314 response . apFilePath ,
298315 } ;
299316
300- if ( response . tempFilePath ) delete response . tempFilePath ;
301- if ( response . apFilePath ) delete response . apFilePath ;
302- if ( response . filePath ) delete response . filePath ;
317+ cleanResponse ( response , [ 'tempFilePath' , 'apFilePath' , 'filePath' ] ) ;
318+ }
319+
320+ /**
321+ * 清理 response 上多余的 key
322+ */
323+ function cleanResponse ( response : AnyObject , keys : string [ ] ) {
324+ for ( const key of keys ) {
325+ if ( key in response ) delete response [ key ] ;
326+ }
303327 }
304328
305329 return adapter ;
0 commit comments