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

feat($http): pass success flag to transformResponse #6734

Closed
wants to merge 1 commit into from
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
20 changes: 10 additions & 10 deletions src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,15 @@ function headersGetter(headers) {
* @param {*} data Data to transform.
* @param {function(string=)} headers Http headers getter fn.
* @param {(Function|Array.<Function>)} fns Function or an array of functions.
* @param {boolean} success True when this is response and it is success
* @returns {*} Transformed data.
*/
function transformData(data, headers, fns) {
function transformData(data, headers, fns, success) {
if (isFunction(fns))
return fns(data, headers);
return fns(data, headers, success);

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

return data;
Expand Down Expand Up @@ -739,13 +740,12 @@ function $HttpProvider() {
return promise;

function transformResponse(response) {
// make a copy since the response must be cacheable
var resp = extend({}, response, {
data: transformData(response.data, response.headers, config.transformResponse)
});
return (isSuccess(response.status))
? resp
: $q.reject(resp);
var success = isSuccess(response.status),
// make a copy since the response must be cacheable
resp = extend({}, response, {
data: transformData(response.data, response.headers, config.transformResponse, success)
});
return success ? resp : $q.reject(resp);
}

function mergeHeaders(config) {
Expand Down
13 changes: 13 additions & 0 deletions test/ng/httpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,19 @@ describe('$http', function() {
expect(callback.mostRecentCall.args[0]).toBe('header1');
});

it('should know when response is failed', function() {
$httpBackend.expect('GET', '/url').respond(404, 'Not found');
$http.get('/url', {
transformResponse: function(data, headers, success) {
return success;
}
}).error(callback);
$httpBackend.flush();

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


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