Skip to content
Permalink
Browse files

feat($http): pass response status code to data transform functions

Fixes #10324
Closes #6734

Closes #10440
  • Loading branch information
pkozlowski-opensource committed Dec 12, 2014
1 parent b9bdbe6 commit 1b740974f5eb373bed04071d51f908ced7c5a8e5
Showing with 23 additions and 9 deletions.
  1. +10 −9 src/ng/http.js
  2. +13 −0 test/ng/httpSpec.js
@@ -92,16 +92,17 @@ function headersGetter(headers) {
* This function is used for both request and response transforming
*
* @param {*} data Data to transform.
* @param {function(string=)} headers Http headers getter fn.
* @param {function(string=)} headers HTTP headers getter fn.
* @param {number} status HTTP status code of the response.
* @param {(Function|Array.<Function>)} fns Function or an array of functions.
* @returns {*} Transformed data.
*/
function transformData(data, headers, fns) {
function transformData(data, headers, status, fns) {
if (isFunction(fns))
return fns(data, headers);
return fns(data, headers, status);

forEach(fns, function(fn) {
data = fn(data, headers);
data = fn(data, headers, status);
});

return data;
@@ -380,7 +381,7 @@ function $HttpProvider() {
*
* Both requests and responses can be transformed using transformation functions: `transformRequest`
* and `transformResponse`. These properties can be a single function that returns
* the transformed value (`{function(data, headersGetter)`) or an array of such transformation functions,
* the transformed value (`{function(data, headersGetter, status)`) or an array of such transformation functions,
* which allows you to `push` or `unshift` a new transformation function into the transformation chain.
*
* ### Default Transformations
@@ -624,9 +625,9 @@ function $HttpProvider() {
* See {@link ng.$http#overriding-the-default-transformations-per-request
* Overriding the Default Transformations}
* - **transformResponse** –
* `{function(data, headersGetter)|Array.<function(data, headersGetter)>}` –
* `{function(data, headersGetter, status)|Array.<function(data, headersGetter, status)>}` –
* transform function or an array of such functions. The transform function takes the http
* response body and headers and returns its transformed (typically deserialized) version.
* response body, headers and status and returns its transformed (typically deserialized) version.
* See {@link ng.$http#overriding-the-default-transformations-per-request
* Overriding the Default Transformations}
* - **cache** – `{boolean|Cache}` – If true, a default $http cache will be used to cache the
@@ -765,7 +766,7 @@ function $HttpProvider() {

var serverRequest = function(config) {
var headers = config.headers;
var reqData = transformData(config.data, headersGetter(headers), config.transformRequest);
var reqData = transformData(config.data, headersGetter(headers), undefined, config.transformRequest);

// strip content-type if data is undefined
if (isUndefined(reqData)) {
@@ -826,7 +827,7 @@ function $HttpProvider() {
if (!response.data) {
resp.data = response.data;
} else {
resp.data = transformData(response.data, response.headers, config.transformResponse);
resp.data = transformData(response.data, response.headers, response.status, config.transformResponse);
}
return (isSuccess(response.status))
? resp
@@ -1232,6 +1232,19 @@ describe('$http', function() {
expect(callback.mostRecentCall.args[0]).toBe('header1');
});

it('should have access to response status', function() {
$httpBackend.expect('GET', '/url').respond(200, 'response', {h1: 'header1'});
$http.get('/url', {
transformResponse: function(data, headers, status) {
return status;
}
}).success(callback);
$httpBackend.flush();

expect(callback).toHaveBeenCalledOnce();
expect(callback.mostRecentCall.args[0]).toBe(200);
});


it('should pipeline more functions', function() {
function first(d, h) {return d + '-first' + ':' + h('h1');}

0 comments on commit 1b74097

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