1
1
/**
2
2
* @author Jason Dobry <[email protected] >
3
3
* @file js-data-http.js
4
- * @version 1.0.0-beta.1 - Homepage <http://www.js-data.io/docs/dshttpadapter>
4
+ * @version 1.0.0-beta.2 - Homepage <http://www.js-data.io/docs/dshttpadapter>
5
5
* @copyright (c) 2014 Jason Dobry
6
6
* @license MIT <https://github.com/js-data/js-data-http/blob/master/LICENSE>
7
7
*
@@ -33,9 +33,13 @@ module.exports = function xhrAdapter(resolve, reject, config) {
33
33
config . headers || { }
34
34
) ;
35
35
36
+ if ( utils . isFormData ( data ) ) {
37
+ delete headers [ 'Content-Type' ] ; // Let the browser set it
38
+ }
39
+
36
40
// Create the request
37
41
var request = new ( XMLHttpRequest || ActiveXObject ) ( 'Microsoft.XMLHTTP' ) ;
38
- request . open ( config . method , buildUrl ( config . url , config . params ) , true ) ;
42
+ request . open ( config . method . toUpperCase ( ) , buildUrl ( config . url , config . params ) , true ) ;
39
43
40
44
// Listen for ready state
41
45
request . onreadystatechange = function ( ) {
@@ -123,20 +127,22 @@ var axios = module.exports = function axios(config) {
123
127
// Don't allow overriding defaults.withCredentials
124
128
config . withCredentials = config . withCredentials || defaults . withCredentials ;
125
129
126
- var promise = new Promise ( function ( resolve , reject ) {
127
- try {
128
- // For browsers use XHR adapter
129
- if ( typeof window !== 'undefined' ) {
130
- require ( './adapters/xhr' ) ( resolve , reject , config ) ;
131
- }
132
- // For node use HTTP adapter
133
- else if ( typeof process !== 'undefined' ) {
134
- require ( './adapters/http' ) ( resolve , reject , config ) ;
130
+ var serverRequest = function ( config ) {
131
+ return new Promise ( function ( resolve , reject ) {
132
+ try {
133
+ // For browsers use XHR adapter
134
+ if ( typeof window !== 'undefined' ) {
135
+ require ( './adapters/xhr' ) ( resolve , reject , config ) ;
136
+ }
137
+ // For node use HTTP adapter
138
+ else if ( typeof process !== 'undefined' ) {
139
+ require ( './adapters/http' ) ( resolve , reject , config ) ;
140
+ }
141
+ } catch ( e ) {
142
+ reject ( e ) ;
135
143
}
136
- } catch ( e ) {
137
- reject ( e ) ;
138
- }
139
- } ) ;
144
+ } ) ;
145
+ } ;
140
146
141
147
function deprecatedMethod ( method , instead , docs ) {
142
148
try {
@@ -151,6 +157,24 @@ var axios = module.exports = function axios(config) {
151
157
} catch ( e ) { }
152
158
}
153
159
160
+ var chain = [ serverRequest , undefined ] ;
161
+ var promise = Promise . resolve ( config ) ;
162
+
163
+ utils . forEach ( axios . interceptors . request . handlers , function ( interceptor ) {
164
+ chain . unshift ( interceptor . request , interceptor . requestError ) ;
165
+ } ) ;
166
+
167
+ utils . forEach ( axios . interceptors . response . handlers , function ( interceptor ) {
168
+ chain . push ( interceptor . response , interceptor . responseError ) ;
169
+ } ) ;
170
+
171
+ while ( chain . length ) {
172
+ var thenFn = chain . shift ( ) ;
173
+ var rejectFn = chain . shift ( ) ;
174
+
175
+ promise = promise . then ( thenFn , rejectFn ) ;
176
+ }
177
+
154
178
// Provide alias for success
155
179
promise . success = function success ( fn ) {
156
180
deprecatedMethod ( 'success' , 'then' , 'https://github.com/mzabriskie/axios/blob/master/README.md#response-api' ) ;
@@ -183,6 +207,22 @@ axios.all = function (promises) {
183
207
} ;
184
208
axios . spread = require ( './helpers/spread' ) ;
185
209
210
+ // interceptors
211
+ axios . interceptors = {
212
+ request : {
213
+ handlers : [ ] ,
214
+ use : function ( thenFn , rejectFn ) {
215
+ axios . interceptors . request . handlers . push ( { request : thenFn , requestError : rejectFn } ) ;
216
+ }
217
+ } ,
218
+ response : {
219
+ handlers : [ ] ,
220
+ use : function ( thenFn , rejectFn ) {
221
+ axios . interceptors . response . handlers . push ( { response : thenFn , responseError : rejectFn } ) ;
222
+ }
223
+ }
224
+ } ;
225
+
186
226
// Provide aliases for supported request methods
187
227
createShortMethods ( 'delete' , 'get' , 'head' ) ;
188
228
createShortMethodsWithData ( 'post' , 'put' , 'patch' ) ;
@@ -209,6 +249,7 @@ function createShortMethodsWithData() {
209
249
} ;
210
250
} ) ;
211
251
}
252
+
212
253
} ) . call ( this , require ( '_process' ) )
213
254
} , { "./adapters/http" :2 , "./adapters/xhr" :2 , "./defaults" :4 , "./helpers/spread" :8 , "./utils" :11 , "_process" :22 , "es6-promise" :12 } ] , 4 :[ function ( require , module , exports ) {
214
255
'use strict' ;
@@ -503,6 +544,16 @@ function isArrayBuffer(val) {
503
544
return toString . call ( val ) === '[object ArrayBuffer]' ;
504
545
}
505
546
547
+ /**
548
+ * Determine if a value is a FormData
549
+ *
550
+ * @param {Object } val The value to test
551
+ * @returns {boolean } True if value is an FormData, otherwise false
552
+ */
553
+ function isFormData ( val ) {
554
+ return toString . call ( val ) === '[object FormData]' ;
555
+ }
556
+
506
557
/**
507
558
* Determine if a value is a view on an ArrayBuffer
508
559
*
@@ -669,6 +720,7 @@ function merge(obj1/*, obj2, obj3, ...*/) {
669
720
module . exports = {
670
721
isArray : isArray ,
671
722
isArrayBuffer : isArrayBuffer ,
723
+ isFormData : isFormData ,
672
724
isArrayBufferView : isArrayBufferView ,
673
725
isString : isString ,
674
726
isNumber : isNumber ,
0 commit comments