Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 1b74097

Browse files
feat($http): pass response status code to data transform functions
Fixes #10324 Closes #6734 Closes #10440
1 parent b9bdbe6 commit 1b74097

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

src/ng/http.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,17 @@ function headersGetter(headers) {
9292
* This function is used for both request and response transforming
9393
*
9494
* @param {*} data Data to transform.
95-
* @param {function(string=)} headers Http headers getter fn.
95+
* @param {function(string=)} headers HTTP headers getter fn.
96+
* @param {number} status HTTP status code of the response.
9697
* @param {(Function|Array.<Function>)} fns Function or an array of functions.
9798
* @returns {*} Transformed data.
9899
*/
99-
function transformData(data, headers, fns) {
100+
function transformData(data, headers, status, fns) {
100101
if (isFunction(fns))
101-
return fns(data, headers);
102+
return fns(data, headers, status);
102103

103104
forEach(fns, function(fn) {
104-
data = fn(data, headers);
105+
data = fn(data, headers, status);
105106
});
106107

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

766767
var serverRequest = function(config) {
767768
var headers = config.headers;
768-
var reqData = transformData(config.data, headersGetter(headers), config.transformRequest);
769+
var reqData = transformData(config.data, headersGetter(headers), undefined, config.transformRequest);
769770

770771
// strip content-type if data is undefined
771772
if (isUndefined(reqData)) {
@@ -826,7 +827,7 @@ function $HttpProvider() {
826827
if (!response.data) {
827828
resp.data = response.data;
828829
} else {
829-
resp.data = transformData(response.data, response.headers, config.transformResponse);
830+
resp.data = transformData(response.data, response.headers, response.status, config.transformResponse);
830831
}
831832
return (isSuccess(response.status))
832833
? resp

test/ng/httpSpec.js

+13
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,19 @@ describe('$http', function() {
12321232
expect(callback.mostRecentCall.args[0]).toBe('header1');
12331233
});
12341234

1235+
it('should have access to response status', function() {
1236+
$httpBackend.expect('GET', '/url').respond(200, 'response', {h1: 'header1'});
1237+
$http.get('/url', {
1238+
transformResponse: function(data, headers, status) {
1239+
return status;
1240+
}
1241+
}).success(callback);
1242+
$httpBackend.flush();
1243+
1244+
expect(callback).toHaveBeenCalledOnce();
1245+
expect(callback.mostRecentCall.args[0]).toBe(200);
1246+
});
1247+
12351248

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

0 commit comments

Comments
 (0)