@@ -5,24 +5,13 @@ SuperMap.Support = {
55 cors : ( ( window . XMLHttpRequest && 'withCredentials' in new window . XMLHttpRequest ( ) ) )
66} ;
77
8- SuperMap . Request = SuperMap . Class ( {
9-
10- initialize : function ( ) {
11- } ,
8+ SuperMap . Request = {
129
1310 get : function ( url , params , options ) {
14- options = options || { } ;
1511 var type = 'GET' ;
16- url = url . endWith ( '.json' ) ? url : url + '.json' ;
17- url = params ? SuperMap . Util . urlAppend ( url , this . getParameterString ( params || { } ) ) : url ;
18- var tokenParam = '' ;
19- var separator = url . indexOf ( "?" ) > - 1 ? "&" : "?" ;
20- if ( SuperMap . Credential . CREDENTIAL && SuperMap . Credential . CREDENTIAL . getUrlParameters ( ) ) {
21- tokenParam = SuperMap . Credential . CREDENTIAL . getUrlParameters ( ) ;
22- }
23- var requestLength = url . length ;
24- if ( requestLength <= 2000 ) {
25- url = tokenParam !== '' ? url + separator + tokenParam : url ;
12+ url = this . _appendUrlTokenParameter ( url ) ;
13+ url = SuperMap . Util . urlAppend ( url , this . _getParameterString ( params || { } ) ) ;
14+ if ( url . length <= 2000 ) {
2615 if ( SuperMap . Support . cors ) {
2716 return this . _fetch ( url , params , options , type ) ;
2817 }
@@ -31,90 +20,73 @@ SuperMap.Request = SuperMap.Class({
3120 return this . _fetchJsonp ( url , options ) ;
3221 }
3322 }
34- url = url . substring ( 0 , url . indexOf ( '?' ) ) + '_method= GET' ;
35- url = tokenParam !== '' ? url + '&' + tokenParam : url ;
36- return this . post ( url , params , options ) ;
23+ return this . _postSimulatie ( type , url . substring ( 0 , url . indexOf ( '?' ) - 1 ) , params , options ) ;
3724 } ,
3825
3926 delete : function ( url , params , options ) {
40- options = options || { } ;
41- url = url . endWith ( '.json' ) ? url : url + '.json' ;
4227 var type = 'DELETE' ;
43- var requestLength = url . length ;
44- url = params ? SuperMap . Util . urlAppend ( url , this . getParameterString ( params || { } ) ) : url ;
45- var tokenParam = '' ;
46- var separator = url . indexOf ( "?" ) > - 1 ? "&" : "?" ;
47- if ( SuperMap . Credential . CREDENTIAL && SuperMap . Credential . CREDENTIAL . getUrlParameters ( ) ) {
48- tokenParam = SuperMap . Credential . CREDENTIAL . getUrlParameters ( ) ;
49- }
50- if ( requestLength <= 2000 ) {
51- if ( SuperMap . Support . cors ) {
52- url = tokenParam !== '' ? url + separator + tokenParam : url ;
53- return this . _fetch ( url , params , options , type ) ;
54- }
28+ url = this . _appendUrlTokenParameter ( url ) ;
29+ url = SuperMap . Util . urlAppend ( url , this . _getParameterString ( params || { } ) ) ;
30+ if ( url . length <= 2000 && SuperMap . Support . cors ) {
31+ return this . _fetch ( url , params , options , type ) ;
5532 }
56- url = url . substring ( 0 , url . indexOf ( '?' ) ) + '_method= DELETE' ;
57- url = tokenParam !== '' ? url + '&' + tokenParam : url ;
58- return this . post ( url , params , options ) ;
33+ return this . _postSimulatie ( type , url . substring ( 0 , url . indexOf ( '?' ) - 1 ) , params , options ) ;
5934 } ,
6035
6136 post : function ( url , params , options ) {
62- options = options || { } ;
63- var type = 'POST' ;
64- url = url . endWith ( '.json' ) ? url : url + '.json' ;
65- var separator = url . indexOf ( "?" ) > - 1 ? "&" : "?" ;
66- if ( SuperMap . Credential . CREDENTIAL && SuperMap . Credential . CREDENTIAL . getUrlParameters ( ) ) {
67- url += separator + SuperMap . Credential . CREDENTIAL . getUrlParameters ( ) ;
68- }
69- return this . _fetch ( url , params , options , type ) ;
37+ return this . _fetch ( this . _appendUrlTokenParameter ( url ) , params , options , 'POST' ) ;
7038 } ,
7139
7240 put : function ( url , params , options ) {
73- options = options || { } ;
74- var type = 'PUT' ;
75- url = url . endWith ( '.json' ) ? url : url + '.json' ;
41+ return this . _fetch ( this . _appendUrlTokenParameter ( url ) , params , options , 'PUT' ) ;
42+ } ,
43+
44+ _postSimulatie : function ( type , url , params , options ) {
7645 var separator = url . indexOf ( "?" ) > - 1 ? "&" : "?" ;
46+ url += separator + '_method= ' + type + this . _appendUrlTokenParameter ( url ) ;
47+ return this . post ( url , params , options ) ;
48+ } ,
49+
50+ _appendUrlTokenParameter : function ( url ) {
51+ url = url . indexOf ( '.json' ) !== - 1 ? url : url + '.json' ;
7752 if ( SuperMap . Credential . CREDENTIAL && SuperMap . Credential . CREDENTIAL . getUrlParameters ( ) ) {
53+ var separator = url . indexOf ( "?" ) > - 1 ? "&" : "?" ;
7854 url += separator + SuperMap . Credential . CREDENTIAL . getUrlParameters ( ) ;
7955 }
80- return this . _fetch ( url , params , options , type ) ;
56+ return url ;
8157 } ,
8258
8359 _fetch : function ( url , params , options , type ) {
60+ options = options || { } ;
8461 if ( options . timeout ) {
85- return this . timeout ( options . timeout , fetch ( url , {
62+ return this . _timeout ( options . timeout , fetch ( url , {
8663 method : type ,
8764 headers : options . headers ,
8865 body : type === 'PUT' || type === 'POST' ? params : undefined ,
8966 credentials : options . withCredentials ? 'include' : 'omit' ,
9067 mode : 'cors'
9168 } ) . then ( function ( response ) {
92- return response . json ( ) ;
93- } ) . then ( function ( json ) {
94- return json ;
69+ return response ;
9570 } ) ) ;
9671 }
9772 return fetch ( url , {
9873 method : type ,
9974 body : type === 'PUT' || type === 'POST' ? params : undefined ,
10075 headers : options . headers
10176 } ) . then ( function ( response ) {
102- return response . json ( ) ;
103- } ) . then ( function ( json ) {
104- return json ;
105- } )
77+ return response ;
78+ } ) ;
10679 } ,
10780
10881 _fetchJsonp : function ( url , options ) {
82+ options = options || { } ;
10983 return fetchJsonp ( url , { method : 'GET' , timeout : options . timeout } )
11084 . then ( function ( response ) {
111- return response . json ( ) ;
112- } ) . then ( function ( json ) {
113- return json ;
85+ return response ;
11486 } ) ;
11587 } ,
11688
117- timeout : function ( seconds , promise ) {
89+ _timeout : function ( seconds , promise ) {
11890 return new Promise ( function ( resolve , reject ) {
11991 setTimeout ( function ( ) {
12092 reject ( new Error ( "timeout" ) )
@@ -123,7 +95,7 @@ SuperMap.Request = SuperMap.Class({
12395 } )
12496 } ,
12597
126- getParameterString : function ( params ) {
98+ _getParameterString : function ( params ) {
12799 var paramsArray = [ ] ;
128100 for ( var key in params ) {
129101 var value = params [ key ] ;
@@ -139,8 +111,7 @@ SuperMap.Request = SuperMap.Class({
139111 ) ;
140112 }
141113 encodedValue = '[' + encodedItemArray . join ( "," ) + ']' ;
142- }
143- else {
114+ } else {
144115 encodedValue = encodeURIComponent ( value ) ;
145116 }
146117 paramsArray . push ( encodeURIComponent ( key ) + "=" + encodedValue ) ;
@@ -149,18 +120,4 @@ SuperMap.Request = SuperMap.Class({
149120 return paramsArray . join ( "&" ) ;
150121 }
151122
152- } ) ;
153-
154- String . prototype . endWith = function ( s ) {
155- if ( s === null || s === "" || this . length === 0 || s . length > this . length )
156- return false ;
157- if ( this . substring ( this . length - s . length ) == s )
158- return true ;
159- else
160- return false ;
161- return true ;
162- }
163-
164- module . exports = function ( ) {
165- return new SuperMap . Request ( ) ;
166- } ;
123+ }
0 commit comments