Skip to content
Permalink
Browse files

Move transform response logic from adapters to dispatcher

  • Loading branch information...
mzabriskie committed Jun 23, 2016
1 parent e833a2f commit 10eb23865101f9347570552c04e9d6211376e25e
Showing with 20 additions and 16 deletions.
  1. +3 −7 lib/adapters/README.md
  2. +3 −2 lib/adapters/http.js
  3. +5 −7 lib/adapters/xhr.js
  4. +9 −0 lib/core/dispatchRequest.js
@@ -6,23 +6,18 @@ The modules under `adapters/` are modules that handle dispatching a request and

```js
var settle = require('./../core/settle');
var transformData = require('./../core/transformData');
module.exports myAdapter(resolve, reject, config) {
// At this point:
// - config has been merged with defaults
// - request transformers have already run
// - request interceptors have already run
// Make the request using config provided/
// Make the request using config provided
// Upon response settle the Promise
var response = {
data: transformData(
responseData,
responseHeaders,
config.transformResponse
),
data: responseData,
status: request.status,
statusText: request.statusText,
headers: responseHeaders,
@@ -33,6 +28,7 @@ module.exports myAdapter(resolve, reject, config) {
settle(resolve, reject, response);
// From here:
// - response transformers will run
// - response interceptors will run
}
```
@@ -2,7 +2,6 @@

var utils = require('./../utils');
var settle = require('./../core/settle');
var transformData = require('./../core/transformData');
var buildURL = require('./../helpers/buildURL');
var http = require('http');
var https = require('https');
@@ -117,6 +116,7 @@ module.exports = function httpAdapter(resolve, reject, config) {
config: config,
request: req
};

if (config.responseType === 'stream') {
response.data = stream;
settle(resolve, reject, response);
@@ -141,7 +141,8 @@ module.exports = function httpAdapter(resolve, reject, config) {
if (config.responseType !== 'arraybuffer') {
responseData = responseData.toString('utf8');
}
response.data = transformData(responseData, res.headers, config.transformResponse);

response.data = responseData;
settle(resolve, reject, response);
});
}
@@ -2,7 +2,6 @@

var utils = require('./../utils');
var settle = require('./../core/settle');
var transformData = require('./../core/transformData');
var buildURL = require('./../helpers/buildURL');
var parseHeaders = require('./../helpers/parseHeaders');
var isURLSameOrigin = require('./../helpers/isURLSameOrigin');
@@ -24,7 +23,10 @@ module.exports = function xhrAdapter(resolve, reject, config) {
// For IE 8/9 CORS support
// Only supports POST and GET calls and doesn't returns the response headers.
// DON'T do this for testing b/c XMLHttpRequest is mocked, not XDomainRequest.
if (process.env.NODE_ENV !== 'test' && typeof window !== 'undefined' && window.XDomainRequest && !('withCredentials' in request) && !isURLSameOrigin(config.url)) {
if (process.env.NODE_ENV !== 'test' &&
typeof window !== 'undefined' &&
window.XDomainRequest && !('withCredentials' in request) &&
!isURLSameOrigin(config.url)) {
request = new window.XDomainRequest();
loadEvent = 'onload';
xDomain = true;
@@ -60,11 +62,7 @@ module.exports = function xhrAdapter(resolve, reject, config) {
var responseHeaders = 'getAllResponseHeaders' in request ? parseHeaders(request.getAllResponseHeaders()) : null;
var responseData = !config.responseType || config.responseType === 'text' ? request.responseText : request.response;
var response = {
data: transformData(
responseData,
responseHeaders,
config.transformResponse
),
data: responseData,
// IE sends 1223 instead of 204 (https://github.com/mzabriskie/axios/issues/201)
status: request.status === 1223 ? 204 : request.status,
statusText: request.status === 1223 ? 'No Content' : request.statusText,
@@ -56,6 +56,15 @@ module.exports = function dispatchRequest(config) {
} catch (e) {
reject(e);
}
}).then(function onFulfilled(response) {
// Transform response data
response.data = transformData(
response.data,
response.headers,
config.transformResponse
);

return response;
});
};

0 comments on commit 10eb238

Please sign in to comment.
You can’t perform that action at this time.