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

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions test/ng/httpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');}
Expand Down