11/*!
2- * axios-miniprogram-adapter 0.3.1 (https://github.com/bigMeow/axios-miniprogram-adapter)
2+ * axios-miniprogram-adapter 1.0.0 (https://github.com/bigMeow/axios-miniprogram-adapter)
33 * API https://github.com/bigMeow/axios-miniprogram-adapter/blob/master/doc/api.md
4- * Copyright 2018-2020 bigMeow. All Rights Reserved
4+ * Copyright 2018-2022 bigMeow. All Rights Reserved
55 * Licensed under MIT (https://github.com/bigMeow/axios-miniprogram-adapter/blob/master/LICENSE)
66 */
77
@@ -41,28 +41,31 @@ function encoder(input) {
4141 return output ;
4242}
4343
44- var platFormName = ' wechat' ;
44+ var platFormName = " wechat" /* 微信 */ ;
4545/**
4646 * 获取各个平台的请求函数
4747 */
4848function getRequest ( ) {
4949 switch ( true ) {
5050 case typeof wx === 'object' :
51- platFormName = ' wechat' ;
51+ platFormName = " wechat" /* 微信 */ ;
5252 return wx . request . bind ( wx ) ;
5353 case typeof swan === 'object' :
54- platFormName = ' baidu' ;
54+ platFormName = " baidu" /* 百度 */ ;
5555 return swan . request . bind ( swan ) ;
56+ case typeof dd === 'object' :
57+ platFormName = "dd" /* 钉钉 */ ;
58+ // https://open.dingtalk.com/document/orgapp-client/send-network-requests
59+ return dd . httpRequest . bind ( dd ) ;
5660 case typeof my === 'object' :
5761 /**
5862 * remark:
5963 * 支付宝客户端已不再维护 my.httpRequest,建议使用 my.request。另外,钉钉客户端尚不支持 my.request。若在钉钉客户端开发小程序,则需要使用 my.httpRequest。
6064 * my.httpRequest的请求头默认值为{'content-type': 'application/x-www-form-urlencoded'}。
6165 * my.request的请求头默认值为{'content-type': 'application/json'}。
62- * TODO: 区分支付宝和钉钉环境
63- * 还有个 dd.httpRequest WFK!!! https://ding-doc.dingtalk.com/doc#/dev/httprequest
66+ * 还有个 dd.httpRequest
6467 */
65- platFormName = ' alipay' ;
68+ platFormName = " alipay" /* 支付宝 */ ;
6669 return ( my . request || my . httpRequest ) . bind ( my ) ;
6770 default :
6871 return wx . request . bind ( wx ) ;
@@ -102,7 +105,7 @@ function transformResponse(mpResponse, config, mpRequestOption) {
102105 */
103106function transformError ( error , reject , config ) {
104107 switch ( platFormName ) {
105- case ' wechat' :
108+ case " wechat" /* 微信 */ :
106109 if ( error . errMsg . indexOf ( 'request:fail abort' ) !== - 1 ) {
107110 // Handle request cancellation (as opposed to a manual cancellation)
108111 reject ( createError ( 'Request aborted' , config , 'ECONNABORTED' , '' ) ) ;
@@ -116,21 +119,22 @@ function transformError(error, reject, config) {
116119 reject ( createError ( 'Network Error' , config , null , '' ) ) ;
117120 }
118121 break ;
119- case 'alipay' :
122+ case "dd" /* 钉钉 */ :
123+ case "alipay" /* 支付宝 */ :
120124 // https://docs.alipay.com/mini/api/network
121125 if ( [ 14 , 19 ] . includes ( error . error ) ) {
122- reject ( createError ( 'Request aborted' , config , 'ECONNABORTED' , '' ) ) ;
126+ reject ( createError ( 'Request aborted' , config , 'ECONNABORTED' , '' , error ) ) ;
123127 }
124128 else if ( [ 13 ] . includes ( error . error ) ) {
125129 // timeout
126- reject ( createError ( 'timeout of ' + config . timeout + 'ms exceeded' , config , 'ECONNABORTED' , '' ) ) ;
130+ reject ( createError ( 'timeout of ' + config . timeout + 'ms exceeded' , config , 'ECONNABORTED' , '' , error ) ) ;
127131 }
128132 else {
129133 // NetWordError
130- reject ( createError ( 'Network Error' , config , null , '' ) ) ;
134+ reject ( createError ( 'Network Error' , config , null , '' , error ) ) ;
131135 }
132136 break ;
133- case ' baidu' :
137+ case " baidu" /* 百度 */ :
134138 // TODO error.errCode
135139 reject ( createError ( 'Network Error' , config , null , '' ) ) ;
136140 break ;
@@ -141,14 +145,18 @@ function transformError(error, reject, config) {
141145 * @param config
142146 */
143147function transformConfig ( config ) {
144- if ( platFormName === 'alipay' ) {
148+ var _a ;
149+ if ( [ "alipay" /* 支付宝 */ , "dd" /* 钉钉 */ ] . includes ( platFormName ) ) {
145150 config . headers = config . header ;
146151 delete config . header ;
152+ if ( "dd" /* 钉钉 */ === platFormName && ( ( _a = config . headers ) === null || _a === void 0 ? void 0 : _a [ "Content-Type" ] ) === "application/json" && Object . prototype . toString . call ( config . data ) === '[object Object]' ) {
153+ // Content-Type为application/json时,data参数只支持json字符串,需要手动调用JSON.stringify进行序列化
154+ config . data = JSON . stringify ( config . data ) ;
155+ }
147156 }
148157 return config ;
149158}
150159
151- var warn = console . warn ;
152160var isJSONstr = function ( str ) {
153161 try {
154162 return typeof str === 'string' && str . length && ( str = JSON . parse ( str ) ) && Object . prototype . toString . call ( str ) === '[object Object]' ;
@@ -169,6 +177,7 @@ function mpAdapter(config) {
169177 var mpRequestOption = {
170178 method : requestMethod ,
171179 url : buildURL ( buildFullPath ( config . baseURL , config . url ) , config . params , config . paramsSerializer ) ,
180+ timeout : config . timeout ,
172181 // Listen for success
173182 success : function ( mpResponse ) {
174183 var response = transformResponse ( mpResponse , config , mpRequestOption ) ;
@@ -187,10 +196,6 @@ function mpAdapter(config) {
187196 var _a = [ config . auth . username || '' , config . auth . password || '' ] , username = _a [ 0 ] , password = _a [ 1 ] ;
188197 requestHeaders . Authorization = 'Basic ' + encoder ( username + ':' + password ) ;
189198 }
190- // Set the request timeout
191- if ( config . timeout !== 0 ) {
192- warn ( 'The "timeout" option is not supported by miniprogram. For more information about usage see "https://developers.weixin.qq.com/miniprogram/dev/framework/config.html#全局配置"' ) ;
193- }
194199 // Add headers to the request
195200 utils . forEach ( requestHeaders , function setRequestHeader ( val , key ) {
196201 var _header = key . toLowerCase ( ) ;
0 commit comments