From 3b17bc812ebcde497f86d1a5c748ec7d45f1eb75 Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Fri, 12 Dec 2014 19:04:47 +0100 Subject: [PATCH] feat($http): pass response status code to data transform functions Fixes #10324 Closes #6734 --- src/ng/http.js | 19 ++++++++++--------- test/ng/httpSpec.js | 13 +++++++++++++ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/ng/http.js b/src/ng/http.js index a8cbf7b03527..02ff5f2b6be5 100644 --- a/src/ng/http.js +++ b/src/ng/http.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.)} 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, status)|Array.}` – * 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 diff --git a/test/ng/httpSpec.js b/test/ng/httpSpec.js index 27d79684ef9a..5deddc5dec76 100644 --- a/test/ng/httpSpec.js +++ b/test/ng/httpSpec.js @@ -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');}