11/**
22* @author Jason Dobry <jason.dobry@gmail.com>
33* @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>
55* @copyright (c) 2014 Jason Dobry
66* @license MIT <https://github.com/js-data/js-data-http/blob/master/LICENSE>
77*
@@ -33,9 +33,13 @@ module.exports = function xhrAdapter(resolve, reject, config) {
3333 config . headers || { }
3434 ) ;
3535
36+ if ( utils . isFormData ( data ) ) {
37+ delete headers [ 'Content-Type' ] ; // Let the browser set it
38+ }
39+
3640 // Create the request
3741 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 ) ;
3943
4044 // Listen for ready state
4145 request . onreadystatechange = function ( ) {
@@ -123,20 +127,22 @@ var axios = module.exports = function axios(config) {
123127 // Don't allow overriding defaults.withCredentials
124128 config . withCredentials = config . withCredentials || defaults . withCredentials ;
125129
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 ) ;
135143 }
136- } catch ( e ) {
137- reject ( e ) ;
138- }
139- } ) ;
144+ } ) ;
145+ } ;
140146
141147 function deprecatedMethod ( method , instead , docs ) {
142148 try {
@@ -151,6 +157,24 @@ var axios = module.exports = function axios(config) {
151157 } catch ( e ) { }
152158 }
153159
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+
154178 // Provide alias for success
155179 promise . success = function success ( fn ) {
156180 deprecatedMethod ( 'success' , 'then' , 'https://github.com/mzabriskie/axios/blob/master/README.md#response-api' ) ;
@@ -183,6 +207,22 @@ axios.all = function (promises) {
183207} ;
184208axios . spread = require ( './helpers/spread' ) ;
185209
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+
186226// Provide aliases for supported request methods
187227createShortMethods ( 'delete' , 'get' , 'head' ) ;
188228createShortMethodsWithData ( 'post' , 'put' , 'patch' ) ;
@@ -209,6 +249,7 @@ function createShortMethodsWithData() {
209249 } ;
210250 } ) ;
211251}
252+
212253} ) . call ( this , require ( '_process' ) )
213254} , { "./adapters/http" :2 , "./adapters/xhr" :2 , "./defaults" :4 , "./helpers/spread" :8 , "./utils" :11 , "_process" :22 , "es6-promise" :12 } ] , 4 :[ function ( require , module , exports ) {
214255'use strict' ;
@@ -503,6 +544,16 @@ function isArrayBuffer(val) {
503544 return toString . call ( val ) === '[object ArrayBuffer]' ;
504545}
505546
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+
506557/**
507558 * Determine if a value is a view on an ArrayBuffer
508559 *
@@ -669,6 +720,7 @@ function merge(obj1/*, obj2, obj3, ...*/) {
669720module . exports = {
670721 isArray : isArray ,
671722 isArrayBuffer : isArrayBuffer ,
723+ isFormData : isFormData ,
672724 isArrayBufferView : isArrayBufferView ,
673725 isString : isString ,
674726 isNumber : isNumber ,
0 commit comments