44import 'promise-polyfill/dist/polyfill' ;
55import 'fetch-ie8' ;
66import fetchJsonp from 'fetch-jsonp' ;
7- import {
8- SuperMap
9- } from '../SuperMap' ;
10- import {
11- Util
12- } from '../commontypes/Util' ;
7+ import { SuperMap } from '../SuperMap' ;
8+ import { Util } from '../commontypes/Util' ;
139
14- const fetch = window . fetch ;
10+ let fetch = window . fetch ;
11+ export var setFetch = function ( newFetch ) {
12+ fetch = newFetch ;
13+ }
1514/**
1615 * @function SuperMap.setCORS
1716 * @description 设置是否允许跨域请求,全局配置,优先级低于 service 下的 crossOring 参数。
@@ -64,12 +63,13 @@ export var FetchRequest = SuperMap.FetchRequest = {
6463 }
6564 } ,
6665 supportDirectRequest : function ( url , options ) {
67- if ( Util . isInTheSameDomain ( url ) ) {
68- return true ;
69- } if ( options . crossOrigin != undefined ) {
70- return options . crossOrigin ;
71- } else {
72- return isCORS ( ) || options . proxy
66+ if ( Util . isInTheSameDomain ( url ) ) {
67+ return true ;
68+ }
69+ if ( options . crossOrigin != undefined ) {
70+ return options . crossOrigin ;
71+ } else {
72+ return isCORS ( ) || options . proxy ;
7373 }
7474 } ,
7575 get : function ( url , params , options ) {
@@ -90,7 +90,6 @@ export var FetchRequest = SuperMap.FetchRequest = {
9090 } else {
9191 return this . _postSimulatie ( type , url . substring ( 0 , url . indexOf ( '?' ) - 1 ) , params , options ) ;
9292 }
93-
9493 } ,
9594
9695 delete : function ( url , params , options ) {
@@ -110,7 +109,6 @@ export var FetchRequest = SuperMap.FetchRequest = {
110109 return this . _postSimulatie ( type , url . substring ( 0 , url . indexOf ( '?' ) - 1 ) , params , options ) ;
111110 }
112111 return this . _fetch ( url , params , options , type ) ;
113-
114112 } ,
115113 post : function ( url , params , options ) {
116114 options = options || { } ;
@@ -123,7 +121,6 @@ export var FetchRequest = SuperMap.FetchRequest = {
123121 return SuperMap . Util . RequestJSONPPromise . POST ( config ) ;
124122 }
125123 return this . _fetch ( this . _processUrl ( url , options ) , params , options , 'POST' ) ;
126-
127124 } ,
128125
129126 put : function ( url , params , options ) {
@@ -136,10 +133,8 @@ export var FetchRequest = SuperMap.FetchRequest = {
136133 data : params
137134 } ;
138135 return SuperMap . Util . RequestJSONPPromise . PUT ( config ) ;
139-
140136 }
141137 return this . _fetch ( url , params , options , 'PUT' ) ;
142-
143138 } ,
144139 urlIsLong : function ( url ) {
145140 //当前url的字节长度。
@@ -156,10 +151,10 @@ export var FetchRequest = SuperMap.FetchRequest = {
156151 totalLength += 3 ;
157152 }
158153 }
159- return ( totalLength < 2000 ) ? false : true ;
154+ return totalLength < 2000 ? false : true ;
160155 } ,
161156 _postSimulatie : function ( type , url , params , options ) {
162- var separator = url . indexOf ( "?" ) > - 1 ? "&" : "?" ;
157+ var separator = url . indexOf ( '?' ) > - 1 ? '&' : '?' ;
163158 url += separator + '_method=' + type ;
164159 if ( typeof params !== 'string' ) {
165160 params = JSON . stringify ( params ) ;
@@ -173,17 +168,17 @@ export var FetchRequest = SuperMap.FetchRequest = {
173168 }
174169
175170 if ( url . indexOf ( '.json' ) === - 1 && ! options . withoutFormatSuffix ) {
176- if ( url . indexOf ( "?" ) < 0 ) {
177- url += '.json'
171+ if ( url . indexOf ( '?' ) < 0 ) {
172+ url += '.json' ;
178173 } else {
179- var urlArrays = url . split ( "?" ) ;
174+ var urlArrays = url . split ( '?' ) ;
180175 if ( urlArrays . length === 2 ) {
181- url = urlArrays [ 0 ] + " .json?" + urlArrays [ 1 ]
176+ url = urlArrays [ 0 ] + ' .json?' + urlArrays [ 1 ] ;
182177 }
183178 }
184179 }
185180 if ( options && options . proxy ) {
186- if ( typeof options . proxy === " function" ) {
181+ if ( typeof options . proxy === ' function' ) {
187182 url = options . proxy ( url ) ;
188183 } else {
189184 url = decodeURIComponent ( url ) ;
@@ -200,70 +195,82 @@ export var FetchRequest = SuperMap.FetchRequest = {
200195 options . headers [ 'Content-Type' ] = 'application/x-www-form-urlencoded;charset=UTF-8' ;
201196 }
202197 if ( options . timeout ) {
203- return this . _timeout ( options . timeout , fetch ( url , {
204- method : type ,
205- headers : options . headers ,
206- body : type === 'PUT' || type === 'POST' ? params : undefined ,
207- credentials : options . withCredentials ? 'include' : 'omit' ,
208- mode : 'cors' ,
209- timeout : getRequestTimeout ( )
210- } ) . then ( function ( response ) {
211- return response ;
212- } ) ) ;
198+ return this . _timeout (
199+ options . timeout ,
200+ fetch ( url , {
201+ method : type ,
202+ headers : options . headers ,
203+ body : type === 'PUT' || type === 'POST' ? params : undefined ,
204+ credentials : this . _getWithCredentials ( options ) ,
205+ mode : 'cors' ,
206+ timeout : getRequestTimeout ( )
207+ } ) . then ( function ( response ) {
208+ return response ;
209+ } )
210+ ) ;
213211 }
214212 return fetch ( url , {
215213 method : type ,
216214 body : type === 'PUT' || type === 'POST' ? params : undefined ,
217215 headers : options . headers ,
218- credentials : options . withCredentials ? 'include' : 'omit' ,
216+ credentials : this . _getWithCredentials ( options ) ,
219217 mode : 'cors' ,
220218 timeout : getRequestTimeout ( )
221219 } ) . then ( function ( response ) {
222220 return response ;
223221 } ) ;
224222 } ,
225223
224+ _getWithCredentials : function ( options ) {
225+ if ( options . withCredentials === true ) {
226+ return 'include' ;
227+ }
228+ if ( options . withCredentials === false ) {
229+ return 'omit' ;
230+ }
231+ return 'same-origin' ;
232+ } ,
233+
226234 _fetchJsonp : function ( url , options ) {
227235 options = options || { } ;
228236 return fetchJsonp ( url , {
229- method : 'GET' ,
230- timeout : options . timeout
231- } )
232- . then ( function ( response ) {
233- return response ;
234- } ) ;
237+ method : 'GET' ,
238+ timeout : options . timeout
239+ } ) . then ( function ( response ) {
240+ return response ;
241+ } ) ;
235242 } ,
236243
237244 _timeout : function ( seconds , promise ) {
238245 return new Promise ( function ( resolve , reject ) {
239246 setTimeout ( function ( ) {
240- reject ( new Error ( " timeout" ) )
241- } , seconds )
242- promise . then ( resolve , reject )
243- } )
247+ reject ( new Error ( ' timeout' ) ) ;
248+ } , seconds ) ;
249+ promise . then ( resolve , reject ) ;
250+ } ) ;
244251 } ,
245252
246253 _getParameterString : function ( params ) {
247254 var paramsArray = [ ] ;
248255 for ( var key in params ) {
249256 var value = params [ key ] ;
250- if ( ( value != null ) && ( typeof value !== 'function' ) ) {
257+ if ( value != null && typeof value !== 'function' ) {
251258 var encodedValue ;
252259 if ( Array . isArray ( value ) || value . toString ( ) === '[object Object]' ) {
253260 encodedValue = encodeURIComponent ( JSON . stringify ( value ) ) ;
254261 } else {
255262 encodedValue = encodeURIComponent ( value ) ;
256263 }
257- paramsArray . push ( encodeURIComponent ( key ) + "=" + encodedValue ) ;
264+ paramsArray . push ( encodeURIComponent ( key ) + '=' + encodedValue ) ;
258265 }
259266 }
260- return paramsArray . join ( "&" ) ;
267+ return paramsArray . join ( '&' ) ;
261268 } ,
262269
263270 _isMVTRequest : function ( url ) {
264- return ( url . indexOf ( '.mvt' ) > - 1 || url . indexOf ( '.pbf' ) > - 1 ) ;
271+ return url . indexOf ( '.mvt' ) > - 1 || url . indexOf ( '.pbf' ) > - 1 ;
265272 }
266- } ;
273+ }
267274SuperMap . Util . RequestJSONPPromise = {
268275 limitLength : 1500 ,
269276 queryKeys : [ ] ,
@@ -273,7 +280,7 @@ SuperMap.Util.RequestJSONPPromise = {
273280 var me = this ;
274281 for ( var key in values ) {
275282 me . queryKeys . push ( key ) ;
276- if ( typeof values [ key ] !== " string" ) {
283+ if ( typeof values [ key ] !== ' string' ) {
277284 values [ key ] = SuperMap . Util . toJSON ( values [ key ] ) ;
278285 }
279286 var tempValue = encodeURIComponent ( values [ key ] ) ;
@@ -299,7 +306,8 @@ SuperMap.Util.RequestJSONPPromise = {
299306 keysCount = 0 ; //此次sectionURL中有多少个key
300307 var length = me . queryKeys ? me . queryKeys . length : 0 ;
301308 for ( var i = 0 ; i < length ; i ++ ) {
302- if ( sectionURL . length + me . queryKeys [ i ] . length + 2 >= me . limitLength ) { //+2 for ("&"or"?")and"="
309+ if ( sectionURL . length + me . queryKeys [ i ] . length + 2 >= me . limitLength ) {
310+ //+2 for ("&"or"?")and"="
303311 if ( keysCount == 0 ) {
304312 return false ;
305313 }
@@ -312,22 +320,22 @@ SuperMap.Util.RequestJSONPPromise = {
312320 var leftValue = me . queryValues [ i ] ;
313321 while ( leftValue . length > 0 ) {
314322 var leftLength = me . limitLength - sectionURL . length - me . queryKeys [ i ] . length - 2 ; //+2 for ("&"or"?")and"="
315- if ( sectionURL . indexOf ( "?" ) > - 1 ) {
316- sectionURL += "&" ;
323+ if ( sectionURL . indexOf ( '?' ) > - 1 ) {
324+ sectionURL += '&' ;
317325 } else {
318- sectionURL += "?" ;
326+ sectionURL += '?' ;
319327 }
320328 var tempLeftValue = leftValue . substring ( 0 , leftLength ) ;
321329 //避免 截断sectionURL时,将类似于%22这样的符号截成两半,从而导致服务端组装sectionURL时发生错误
322- if ( tempLeftValue . substring ( leftLength - 1 , leftLength ) === "%" ) {
330+ if ( tempLeftValue . substring ( leftLength - 1 , leftLength ) === '%' ) {
323331 leftLength -= 1 ;
324332 tempLeftValue = leftValue . substring ( 0 , leftLength ) ;
325- } else if ( tempLeftValue . substring ( leftLength - 2 , leftLength - 1 ) === "%" ) {
333+ } else if ( tempLeftValue . substring ( leftLength - 2 , leftLength - 1 ) === '%' ) {
326334 leftLength -= 2 ;
327335 tempLeftValue = leftValue . substring ( 0 , leftLength ) ;
328336 }
329337
330- sectionURL += me . queryKeys [ i ] + "=" + tempLeftValue ;
338+ sectionURL += me . queryKeys [ i ] + '=' + tempLeftValue ;
331339 leftValue = leftValue . substring ( leftLength ) ;
332340 if ( tempLeftValue . length > 0 ) {
333341 splitQuestUrl . push ( sectionURL ) ;
@@ -337,21 +345,24 @@ SuperMap.Util.RequestJSONPPromise = {
337345 }
338346 } else {
339347 keysCount ++ ;
340- if ( sectionURL . indexOf ( "?" ) > - 1 ) {
341- sectionURL += "&" ;
348+ if ( sectionURL . indexOf ( '?' ) > - 1 ) {
349+ sectionURL += '&' ;
342350 } else {
343- sectionURL += "?" ;
351+ sectionURL += '?' ;
344352 }
345- sectionURL += me . queryKeys [ i ] + "=" + me . queryValues [ i ] ;
353+ sectionURL += me . queryKeys [ i ] + '=' + me . queryValues [ i ] ;
346354 }
347355 }
348356 }
349357 splitQuestUrl . push ( sectionURL ) ;
350- me . send ( splitQuestUrl , "SuperMap.Util.RequestJSONPPromise.supermap_callbacks[" + uid + "]" , config && config . proxy ) ;
358+ me . send (
359+ splitQuestUrl ,
360+ 'SuperMap.Util.RequestJSONPPromise.supermap_callbacks[' + uid + ']' ,
361+ config && config . proxy
362+ ) ;
351363 return p ;
352364 } ,
353365
354-
355366 getUid : function ( ) {
356367 var uid = new Date ( ) . getTime ( ) ,
357368 random = Math . floor ( Math . random ( ) * 1e17 ) ;
@@ -364,22 +375,22 @@ SuperMap.Util.RequestJSONPPromise = {
364375 var jsonpUserID = new Date ( ) . getTime ( ) ;
365376 for ( var i = 0 ; i < len ; i ++ ) {
366377 var url = splitQuestUrl [ i ] ;
367- if ( url . indexOf ( "?" ) > - 1 ) {
368- url += "&" ;
378+ if ( url . indexOf ( '?' ) > - 1 ) {
379+ url += '&' ;
369380 } else {
370- url += "?" ;
381+ url += '?' ;
371382 }
372- url += " sectionCount=" + len ;
373- url += " §ionIndex=" + i ;
374- url += " &jsonpUserID=" + jsonpUserID ;
383+ url += ' sectionCount=' + len ;
384+ url += ' §ionIndex=' + i ;
385+ url += ' &jsonpUserID=' + jsonpUserID ;
375386 if ( proxy ) {
376387 url = decodeURIComponent ( url ) ;
377388 url = proxy + encodeURIComponent ( url ) ;
378389 }
379390 fetchJsonp ( url , {
380391 jsonpCallbackFunction : callback ,
381392 timeout : 30000
382- } )
393+ } ) ;
383394 }
384395 }
385396 } ,
0 commit comments