Skip to content

Commit 6728785

Browse files
feat($httpUrlParams): introduce new service abstracting params serialization
Closes angular#7429 Closes angular#9224
1 parent 301663e commit 6728785

File tree

2 files changed

+51
-16
lines changed

2 files changed

+51
-16
lines changed

src/AngularPublic.js

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
$IntervalProvider,
6868
$HttpProvider,
6969
$HttpBackendProvider,
70+
$HttpUrlParamsProvider,
7071
$LocationProvider,
7172
$LogProvider,
7273
$ParseProvider,
@@ -222,6 +223,7 @@ function publishExternalAPI(angular) {
222223
$interval: $IntervalProvider,
223224
$http: $HttpProvider,
224225
$httpBackend: $HttpBackendProvider,
226+
$httpUrlParams: $HttpUrlParamsProvider,
225227
$location: $LocationProvider,
226228
$log: $LogProvider,
227229
$parse: $ParseProvider,

src/ng/http.js

+49-16
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,8 @@ function $HttpProvider() {
214214
**/
215215
var interceptorFactories = this.interceptors = [];
216216

217-
this.$get = ['$httpBackend', '$browser', '$cacheFactory', '$rootScope', '$q', '$injector',
218-
function($httpBackend, $browser, $cacheFactory, $rootScope, $q, $injector) {
217+
this.$get = ['$httpBackend', '$httpUrlParams', '$browser', '$cacheFactory', '$rootScope', '$q', '$injector',
218+
function($httpBackend, $httpUrlParams, $browser, $cacheFactory, $rootScope, $q, $injector) {
219219

220220
var defaultCache = $cacheFactory('$http');
221221

@@ -1130,28 +1130,61 @@ function $HttpProvider() {
11301130

11311131

11321132
function buildUrl(url, params) {
1133-
if (!params) return url;
1133+
params = $httpUrlParams.serialize(params);
1134+
if (params) {
1135+
url += ((url.indexOf('?') == -1) ? '?' : '&') + params;
1136+
}
1137+
return url;
1138+
}
1139+
}];
1140+
}
1141+
1142+
/**
1143+
* @ngdoc provider
1144+
* @name $httpUrlParamsProvider
1145+
* @description
1146+
* Use `$httpUrlParamsProvider` to change the default behavior of the {@link ng.$httpUrlParams $httpUrlParams} service.
1147+
* */
1148+
function $HttpUrlParamsProvider() {
1149+
1150+
/**
1151+
* @ngdoc service
1152+
* @name $httpUrlParams
1153+
*
1154+
* @description
1155+
* The `$httpUrlParams` service is responsible for serializing request params
1156+
* (expressed as a JavaScript object) to a string.
1157+
* It abstracts the way in which request params are transformed, grouped, encoded etc.
1158+
*
1159+
* Normally you wouldn't use this service directly in the application's code but rather override this service
1160+
* to enable custom serialization schemas for URL parameters.
1161+
*
1162+
* The `$httpUrlParams` service comes handy while unit testing with {@link ngMock.$httpBackend $httpBackend mock},
1163+
* as one can inject `$httpUrlParams` into a test and serialize URL params as {@link ng.$http $http} service.
1164+
*/
1165+
this.$get = function() {
1166+
var HttpUrlParams = {};
1167+
1168+
HttpUrlParams.serialize = function(params) {
11341169
var parts = [];
1170+
1171+
if (!params) return '';
1172+
11351173
forEachSorted(params, function(value, key) {
11361174
if (value === null || isUndefined(value)) return;
11371175
if (!isArray(value)) value = [value];
11381176

11391177
forEach(value, function(v) {
11401178
if (isObject(v)) {
1141-
if (isDate(v)) {
1142-
v = v.toISOString();
1143-
} else {
1144-
v = toJson(v);
1145-
}
1179+
v = isDate(v) ? v.toISOString() : toJson(v);
11461180
}
1147-
parts.push(encodeUriQuery(key) + '=' +
1148-
encodeUriQuery(v));
1181+
parts.push(encodeUriQuery(key) + '=' + encodeUriQuery(v));
11491182
});
11501183
});
1151-
if (parts.length > 0) {
1152-
url += ((url.indexOf('?') == -1) ? '?' : '&') + parts.join('&');
1153-
}
1154-
return url;
1155-
}
1156-
}];
1184+
1185+
return parts.join('&');
1186+
};
1187+
1188+
return HttpUrlParams;
1189+
};
11571190
}

0 commit comments

Comments
 (0)