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

Commit e0477ac

Browse files
committed
feat(http): Add 'useLegacyHttpMethods' to $httpProvider
For now it defaults to true. This will open up a path to remove them. DEPRECATION NOTICE: The methods 'success' and 'error' on promises returned by $http are now deprecated.
1 parent 9e492c3 commit e0477ac

File tree

2 files changed

+104
-43
lines changed

2 files changed

+104
-43
lines changed

src/ng/http.js

+74-43
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var JSON_ENDS = {
88
'{': /}$/
99
};
1010
var JSON_PROTECTION_PREFIX = /^\)\]\}',?\n/;
11+
var $httpMinErr = minErr('$http');
1112

1213
function serializeValue(v) {
1314
if (isObject(v)) {
@@ -330,6 +331,29 @@ function $HttpProvider() {
330331
return useApplyAsync;
331332
};
332333

334+
var useHttpPromise = true;
335+
/**
336+
* @ngdoc method
337+
* @name $httpProvider#useLegacyMethods
338+
*
339+
* Configure $http service to return promises without the shorthand methods `success` and `error`. It should
340+
* be used to make sure that applications work without these methods.
341+
*
342+
* Defaults to false. If no value is specified, returns the current configured value.
343+
*
344+
* @param {boolean=} value If true, $http will return a promise without the `success` and `error methods.
345+
*
346+
* @returns {boolean|Object} If a value is specified, returns the $httpProvider for chaining.
347+
* otherwise, returns the current configured value.
348+
**/
349+
this.useLegacyMethods = function(value) {
350+
if (isDefined(value)) {
351+
useHttpPromise = !!value;
352+
return this;
353+
}
354+
return useHttpPromise;
355+
};
356+
333357
/**
334358
* @ngdoc property
335359
* @name $httpProvider#interceptors
@@ -396,17 +420,15 @@ function $HttpProvider() {
396420
*
397421
* ## General usage
398422
* The `$http` service is a function which takes a single argument — a configuration object —
399-
* that is used to generate an HTTP request and returns a {@link ng.$q promise}
400-
* with two $http specific methods: `success` and `error`.
423+
* that is used to generate an HTTP request and returns a {@link ng.$q promise}.
401424
*
402425
* ```js
403426
* // Simple GET request example :
404427
* $http.get('/someUrl').
405-
* success(function(data, status, headers, config) {
428+
* then(function(response) {
406429
* // this callback will be called asynchronously
407430
* // when the response is available
408-
* }).
409-
* error(function(data, status, headers, config) {
431+
* }, function(response) {
410432
* // called asynchronously if an error occurs
411433
* // or server returns response with an error status.
412434
* });
@@ -415,21 +437,23 @@ function $HttpProvider() {
415437
* ```js
416438
* // Simple POST request example (passing data) :
417439
* $http.post('/someUrl', {msg:'hello word!'}).
418-
* success(function(data, status, headers, config) {
440+
* then(function(response) {
419441
* // this callback will be called asynchronously
420442
* // when the response is available
421-
* }).
422-
* error(function(data, status, headers, config) {
443+
* }, function(response) {
423444
* // called asynchronously if an error occurs
424445
* // or server returns response with an error status.
425446
* });
426447
* ```
427448
*
449+
* The response object has these properties:
428450
*
429-
* Since the returned value of calling the $http function is a `promise`, you can also use
430-
* the `then` method to register callbacks, and these callbacks will receive a single argument –
431-
* an object representing the response. See the API signature and type info below for more
432-
* details.
451+
* - **data** – `{string|Object}` – The response body transformed with the transform
452+
* functions.
453+
* - **status** – `{number}` – HTTP status code of the response.
454+
* - **headers** – `{function([headerName])}` – Header getter function.
455+
* - **config** – `{Object}` – The configuration object that was used to generate the request.
456+
* - **statusText** – `{string}` – HTTP status text of the response.
433457
*
434458
* A response status code between 200 and 299 is considered a success status and
435459
* will result in the success callback being called. Note that if the response is a redirect,
@@ -453,8 +477,8 @@ function $HttpProvider() {
453477
* request data must be passed in for POST/PUT requests.
454478
*
455479
* ```js
456-
* $http.get('/someUrl').success(successCallback);
457-
* $http.post('/someUrl', data).success(successCallback);
480+
* $http.get('/someUrl').then(successCallback);
481+
* $http.post('/someUrl', data).then(successCallback);
458482
* ```
459483
*
460484
* Complete list of shortcut methods:
@@ -511,7 +535,7 @@ function $HttpProvider() {
511535
* data: { test: 'test' }
512536
* }
513537
*
514-
* $http(req).success(function(){...}).error(function(){...});
538+
* $http(req).then(function(){...}, function(){...});
515539
* ```
516540
*
517541
* ## Transforming Requests and Responses
@@ -794,14 +818,12 @@ function $HttpProvider() {
794818
* response object. The `success` and `error` methods take a single argument - a function that
795819
* will be called when the request succeeds or fails respectively. The arguments passed into
796820
* these functions are destructured representation of the response object passed into the
797-
* `then` method. The response object has these properties:
821+
* `then` method.
798822
*
799-
* - **data** – `{string|Object}` – The response body transformed with the transform
800-
* functions.
801-
* - **status** – `{number}` – HTTP status code of the response.
802-
* - **headers** – `{function([headerName])}` – Header getter function.
803-
* - **config** – `{Object}` – The configuration object that was used to generate the request.
804-
* - **statusText** – `{string}` – HTTP status text of the response.
823+
* <div class="alert alert-error">
824+
* **Note:** the short hand methods `success` and `error` are deprecated.
825+
* Use the standard `then` method instead.
826+
* </div>
805827
*
806828
* @property {Array.<Object>} pendingRequests Array of config objects for currently pending
807829
* requests. This is primarily meant to be used for debugging purposes.
@@ -843,13 +865,12 @@ function $HttpProvider() {
843865
$scope.response = null;
844866
845867
$http({method: $scope.method, url: $scope.url, cache: $templateCache}).
846-
success(function(data, status) {
847-
$scope.status = status;
848-
$scope.data = data;
849-
}).
850-
error(function(data, status) {
851-
$scope.data = data || "Request failed";
852-
$scope.status = status;
868+
then(function(response) {
869+
$scope.status = response.status;
870+
$scope.data = response.data;
871+
}, function(response) {
872+
$scope.data = response.data || "Request failed";
873+
$scope.status = response.status;
853874
});
854875
};
855876
@@ -954,23 +975,33 @@ function $HttpProvider() {
954975
promise = promise.then(thenFn, rejectFn);
955976
}
956977

957-
promise.success = function(fn) {
958-
assertArgFn(fn, 'fn');
978+
if (useHttpPromise) {
979+
promise.success = function(fn) {
980+
assertArgFn(fn, 'fn');
959981

960-
promise.then(function(response) {
961-
fn(response.data, response.status, response.headers, config);
962-
});
963-
return promise;
964-
};
982+
promise.then(function(response) {
983+
fn(response.data, response.status, response.headers, config);
984+
});
985+
return promise;
986+
};
965987

966-
promise.error = function(fn) {
967-
assertArgFn(fn, 'fn');
988+
promise.error = function(fn) {
989+
assertArgFn(fn, 'fn');
968990

969-
promise.then(null, function(response) {
970-
fn(response.data, response.status, response.headers, config);
971-
});
972-
return promise;
973-
};
991+
promise.then(null, function(response) {
992+
fn(response.data, response.status, response.headers, config);
993+
});
994+
return promise;
995+
};
996+
} else {
997+
promise.success = function() {
998+
throw $httpMinErr('nosuccess', 'The method `success` on the $http result has been disabled.');
999+
};
1000+
1001+
promise.error = function() {
1002+
throw $httpMinErr('noerror', 'The method `error` on the $http result has been disabled.');
1003+
};
1004+
}
9741005

9751006
return promise;
9761007

test/ng/httpSpec.js

+30
Original file line numberDiff line numberDiff line change
@@ -1975,6 +1975,36 @@ describe('$http with $applyAsync', function() {
19751975
});
19761976
});
19771977

1978+
describe('$http without useLegacyMethods', function() {
1979+
var $httpBackend, $http;
1980+
beforeEach(module(function($httpProvider) {
1981+
$httpProvider.useLegacyMethods(false);
1982+
}, provideLog));
1983+
1984+
beforeEach(inject(['$httpBackend', '$http', '$rootScope', function($hb, $h, $rs) {
1985+
$httpBackend = $hb;
1986+
$http = $h;
1987+
}]));
1988+
1989+
it('should throw when the success or error methods are called if useLegacyMethods is false', function() {
1990+
$httpBackend.expect('GET', '/url').respond('');
1991+
var promise = $http({url: '/url'});
1992+
1993+
function callSucess() {
1994+
promise.success();
1995+
}
1996+
1997+
function callError() {
1998+
promise.error();
1999+
}
2000+
2001+
expect(callSucess).toThrowMinErr(
2002+
'$http', 'nosuccess', 'The method `success` on the $http result has been disabled.');
2003+
expect(callError).toThrowMinErr(
2004+
'$http', 'noerror', 'The method `error` on the $http result has been disabled.');
2005+
});
2006+
});
2007+
19782008
describe('$http param serializers', function() {
19792009

19802010
var defSer, jqrSer;

0 commit comments

Comments
 (0)