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

Commit aecb954

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 5081982 commit aecb954

File tree

2 files changed

+84
-43
lines changed

2 files changed

+84
-43
lines changed

src/ng/http.js

+65-43
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,29 @@ function $HttpProvider() {
330330
return useApplyAsync;
331331
};
332332

333+
var useHttpPromise = true;
334+
/**
335+
* @ngdoc method
336+
* @name $httpProvider#useLegacyMethods
337+
*
338+
* Configure $http service to return promises without the shorthand methods `success` and `error`. It should
339+
* be used to make sure that applications work without these methods.
340+
*
341+
* Defaults to false. If no value is specified, returns the current configured value.
342+
*
343+
* @param {boolean=} value If true, $http will return a promise without the `success` and `error methods.
344+
*
345+
* @returns {boolean|Object} If a value is specified, returns the $httpProvider for chaining.
346+
* otherwise, returns the current configured value.
347+
**/
348+
this.useLegacyMethods = function(value) {
349+
if (isDefined(value)) {
350+
useHttpPromise = !!value;
351+
return this;
352+
}
353+
return useHttpPromise;
354+
};
355+
333356
/**
334357
* @ngdoc property
335358
* @name $httpProvider#interceptors
@@ -396,17 +419,15 @@ function $HttpProvider() {
396419
*
397420
* ## General usage
398421
* 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`.
422+
* that is used to generate an HTTP request and returns a {@link ng.$q promise}.
401423
*
402424
* ```js
403425
* // Simple GET request example :
404426
* $http.get('/someUrl').
405-
* success(function(data, status, headers, config) {
427+
* then(function(response) {
406428
* // this callback will be called asynchronously
407429
* // when the response is available
408-
* }).
409-
* error(function(data, status, headers, config) {
430+
* }, function(response) {
410431
* // called asynchronously if an error occurs
411432
* // or server returns response with an error status.
412433
* });
@@ -415,21 +436,23 @@ function $HttpProvider() {
415436
* ```js
416437
* // Simple POST request example (passing data) :
417438
* $http.post('/someUrl', {msg:'hello word!'}).
418-
* success(function(data, status, headers, config) {
439+
* then(function(response) {
419440
* // this callback will be called asynchronously
420441
* // when the response is available
421-
* }).
422-
* error(function(data, status, headers, config) {
442+
* }, function(response) {
423443
* // called asynchronously if an error occurs
424444
* // or server returns response with an error status.
425445
* });
426446
* ```
427447
*
448+
* The response object has these properties:
428449
*
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.
450+
* - **data** – `{string|Object}` – The response body transformed with the transform
451+
* functions.
452+
* - **status** – `{number}` – HTTP status code of the response.
453+
* - **headers** – `{function([headerName])}` – Header getter function.
454+
* - **config** – `{Object}` – The configuration object that was used to generate the request.
455+
* - **statusText** – `{string}` – HTTP status text of the response.
433456
*
434457
* A response status code between 200 and 299 is considered a success status and
435458
* will result in the success callback being called. Note that if the response is a redirect,
@@ -453,8 +476,8 @@ function $HttpProvider() {
453476
* request data must be passed in for POST/PUT requests.
454477
*
455478
* ```js
456-
* $http.get('/someUrl').success(successCallback);
457-
* $http.post('/someUrl', data).success(successCallback);
479+
* $http.get('/someUrl').then(successCallback);
480+
* $http.post('/someUrl', data).then(successCallback);
458481
* ```
459482
*
460483
* Complete list of shortcut methods:
@@ -511,7 +534,7 @@ function $HttpProvider() {
511534
* data: { test: 'test' }
512535
* }
513536
*
514-
* $http(req).success(function(){...}).error(function(){...});
537+
* $http(req).then(function(){...}, function(){...});
515538
* ```
516539
*
517540
* ## Transforming Requests and Responses
@@ -794,14 +817,12 @@ function $HttpProvider() {
794817
* response object. The `success` and `error` methods take a single argument - a function that
795818
* will be called when the request succeeds or fails respectively. The arguments passed into
796819
* these functions are destructured representation of the response object passed into the
797-
* `then` method. The response object has these properties:
820+
* `then` method.
798821
*
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.
822+
* <div class="alert alert-error">
823+
* **Note:** the short hand methods `success` and `error` are deprecated.
824+
* Use the standard `then` method instead.
825+
* </div>
805826
*
806827
* @property {Array.<Object>} pendingRequests Array of config objects for currently pending
807828
* requests. This is primarily meant to be used for debugging purposes.
@@ -843,13 +864,12 @@ function $HttpProvider() {
843864
$scope.response = null;
844865
845866
$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;
867+
then(function(response) {
868+
$scope.status = response.status;
869+
$scope.data = response.data;
870+
}, function(response) {
871+
$scope.data = response.data || "Request failed";
872+
$scope.status = response.status;
853873
});
854874
};
855875
@@ -954,23 +974,25 @@ function $HttpProvider() {
954974
promise = promise.then(thenFn, rejectFn);
955975
}
956976

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

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

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

969-
promise.then(null, function(response) {
970-
fn(response.data, response.status, response.headers, config);
971-
});
972-
return promise;
973-
};
990+
promise.then(null, function(response) {
991+
fn(response.data, response.status, response.headers, config);
992+
});
993+
return promise;
994+
};
995+
}
974996

975997
return promise;
976998

test/ng/httpSpec.js

+19
Original file line numberDiff line numberDiff line change
@@ -1975,6 +1975,25 @@ 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 not have the success and error methods if useLegacyMethods is false', function() {
1990+
$httpBackend.expect('GET', '/url').respond('');
1991+
var promise = $http({url: '/url'});
1992+
expect('success' in promise).toBe(false);
1993+
expect('error' in promise).toBe(false);
1994+
});
1995+
});
1996+
19781997
describe('$http param serializers', function() {
19791998

19801999
var defSer, jqrSer;

0 commit comments

Comments
 (0)