-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurlbuilder.js
More file actions
131 lines (108 loc) · 3.28 KB
/
urlbuilder.js
File metadata and controls
131 lines (108 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* angular-hu-urlbuilder v1.1.0
* https://github.com/angular-hu/angular-hu
* (c) 2015 Telefónica I+D - http://www.tid.es
* @license MIT
*/
(function(angular) {
'use strict';
angular.module('httpu.urlbuilder', [])
.factory('huURLBuilderInterceptor', huURLBuilderInterceptor)
.factory('huURLBuilderFactory', huURLBuilderFactory);
huURLBuilderInterceptor.$inject = ['$injector', '$q'];
function huURLBuilderInterceptor($injector, $q) {
//the property to add to the config
var KEY = '__huURLBuilder';
return {
request: requestInterceptor,
response: responseInterceptor,
responseError: responseErrorInterceptor
};
//////////////////////////
function requestInterceptor(config) {
if (!config.buildUrl) {
return config;
}
//Get the serialization service or func
var buildUrl = angular.isString(config.buildUrl) ?
$injector.get(config.buildUrl) :
config.buildUrl;
//Save the original config for restoring later
config[KEY] = {
url: config.url,
params: config.params
};
config.url = buildUrl(config.url, config.params);
config.params = null;
return config;
}
function responseInterceptor(response) {
return restoreOriginal(response);
}
function responseErrorInterceptor(rejection) {
return $q.reject(restoreOriginal(rejection));
}
/**
* Restores params an url from the original config
*
* @private
* @param {Object} res The response/rejection object
* @returns {Object} The response/rejection object
*/
function restoreOriginal(res) {
if (!res || !res.config || !res.config[KEY]) {
return res;
}
res.config.url = res.config[KEY].url;
res.config.params = res.config[KEY].params;
delete res.config[KEY];
return res;
}
}
huURLBuilderFactory.$inject = ['$window'];
function huURLBuilderFactory($window) {
return paramsSerializer;
//////////////////////////////
/**
* From Angular
* Please guys! export this!
*/
function encodeUriQuery(val) {
return $window.encodeURIComponent(val)
.replace(/%40/gi, '@')
.replace(/%3A/gi, ':')
.replace(/%24/g, '$')
.replace(/%2C/gi, ',')
.replace(/%20/g, '+');
}
/**
* Default serializer
* Stringifies the parameters values, with any order
*
* @param {Object} params The parameters object
* @param {Function} cb The callback(key, value) function to call when done
* serializing a parameter
*/
function toStringEncode(params, cb) {
angular.forEach(params, function iterator(value, key) {
cb(key, '' + value);
});
}
function paramsSerializer(serializer) {
serializer = serializer || toStringEncode;
return function buildUrl(url, params) {
if (!params) {
return url;
}
var parts = [];
serializer(params, function addKeyValue(key, value) {
parts.push(encodeUriQuery(key) + '=' + encodeUriQuery(value));
});
if (parts.length > 0) {
url += ((url.indexOf('?') === -1) ? '?' : '&') + parts.join('&');
}
return url;
};
}
}
})(window.angular);