From 10eb23865101f9347570552c04e9d6211376e25e Mon Sep 17 00:00:00 2001 From: Matt Zabriskie Date: Thu, 23 Jun 2016 15:11:35 -0600 Subject: [PATCH] Move transform response logic from adapters to dispatcher --- lib/adapters/README.md | 10 +++------- lib/adapters/http.js | 5 +++-- lib/adapters/xhr.js | 12 +++++------- lib/core/dispatchRequest.js | 9 +++++++++ 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/lib/adapters/README.md b/lib/adapters/README.md index d3cb189035..d831a47d29 100644 --- a/lib/adapters/README.md +++ b/lib/adapters/README.md @@ -6,7 +6,6 @@ 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: @@ -14,15 +13,11 @@ module.exports myAdapter(resolve, reject, config) { // - 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 } ``` diff --git a/lib/adapters/http.js b/lib/adapters/http.js index 265b008c61..d0589db3ea 100644 --- a/lib/adapters/http.js +++ b/lib/adapters/http.js @@ -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); }); } diff --git a/lib/adapters/xhr.js b/lib/adapters/xhr.js index 0f19802228..22dd06f1a0 100644 --- a/lib/adapters/xhr.js +++ b/lib/adapters/xhr.js @@ -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, diff --git a/lib/core/dispatchRequest.js b/lib/core/dispatchRequest.js index 3c8c963b6a..c7af27b10a 100644 --- a/lib/core/dispatchRequest.js +++ b/lib/core/dispatchRequest.js @@ -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; }); };