url_search_params.js
7.13 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
* @param {?=} rawParams
* @return {?}
*/
function paramParser(rawParams) {
if (rawParams === void 0) { rawParams = ''; }
var /** @type {?} */ map = new Map();
if (rawParams.length > 0) {
var /** @type {?} */ params = rawParams.split('&');
params.forEach(function (param) {
var /** @type {?} */ eqIdx = param.indexOf('=');
var _a = eqIdx == -1 ? [param, ''] : [param.slice(0, eqIdx), param.slice(eqIdx + 1)], key = _a[0], val = _a[1];
var /** @type {?} */ list = map.get(key) || [];
list.push(val);
map.set(key, list);
});
}
return map;
}
/**
* \@experimental
*
*/
export var QueryEncoder = (function () {
function QueryEncoder() {
}
/**
* @param {?} k
* @return {?}
*/
QueryEncoder.prototype.encodeKey = function (k) { return standardEncoding(k); };
/**
* @param {?} v
* @return {?}
*/
QueryEncoder.prototype.encodeValue = function (v) { return standardEncoding(v); };
return QueryEncoder;
}());
/**
* @param {?} v
* @return {?}
*/
function standardEncoding(v) {
return encodeURIComponent(v)
.replace(/%40/gi, '@')
.replace(/%3A/gi, ':')
.replace(/%24/gi, '$')
.replace(/%2C/gi, ',')
.replace(/%3B/gi, ';')
.replace(/%2B/gi, '+')
.replace(/%3D/gi, '=')
.replace(/%3F/gi, '?')
.replace(/%2F/gi, '/');
}
/**
* Map-like representation of url search parameters, based on
* [URLSearchParams](https://url.spec.whatwg.org/#urlsearchparams) in the url living standard,
* with several extensions for merging URLSearchParams objects:
* - setAll()
* - appendAll()
* - replaceAll()
*
* This class accepts an optional second parameter of ${\@link QueryEncoder},
* which is used to serialize parameters before making a request. By default,
* `QueryEncoder` encodes keys and values of parameters using `encodeURIComponent`,
* and then un-encodes certain characters that are allowed to be part of the query
* according to IETF RFC 3986: https://tools.ietf.org/html/rfc3986.
*
* These are the characters that are not encoded: `! $ \' ( ) * + , ; A 9 - . _ ~ ? /`
*
* If the set of allowed query characters is not acceptable for a particular backend,
* `QueryEncoder` can be subclassed and provided as the 2nd argument to URLSearchParams.
*
* ```
* import {URLSearchParams, QueryEncoder} from '\@angular/http';
* class MyQueryEncoder extends QueryEncoder {
* encodeKey(k: string): string {
* return myEncodingFunction(k);
* }
*
* encodeValue(v: string): string {
* return myEncodingFunction(v);
* }
* }
*
* let params = new URLSearchParams('', new MyQueryEncoder());
* ```
* \@experimental
*/
export var URLSearchParams = (function () {
/**
* @param {?=} rawParams
* @param {?=} queryEncoder
*/
function URLSearchParams(rawParams, queryEncoder) {
if (rawParams === void 0) { rawParams = ''; }
if (queryEncoder === void 0) { queryEncoder = new QueryEncoder(); }
this.rawParams = rawParams;
this.queryEncoder = queryEncoder;
this.paramsMap = paramParser(rawParams);
}
/**
* @return {?}
*/
URLSearchParams.prototype.clone = function () {
var /** @type {?} */ clone = new URLSearchParams('', this.queryEncoder);
clone.appendAll(this);
return clone;
};
/**
* @param {?} param
* @return {?}
*/
URLSearchParams.prototype.has = function (param) { return this.paramsMap.has(param); };
/**
* @param {?} param
* @return {?}
*/
URLSearchParams.prototype.get = function (param) {
var /** @type {?} */ storedParam = this.paramsMap.get(param);
return Array.isArray(storedParam) ? storedParam[0] : null;
};
/**
* @param {?} param
* @return {?}
*/
URLSearchParams.prototype.getAll = function (param) { return this.paramsMap.get(param) || []; };
/**
* @param {?} param
* @param {?} val
* @return {?}
*/
URLSearchParams.prototype.set = function (param, val) {
if (val === void 0 || val === null) {
this.delete(param);
return;
}
var /** @type {?} */ list = this.paramsMap.get(param) || [];
list.length = 0;
list.push(val);
this.paramsMap.set(param, list);
};
/**
* @param {?} searchParams
* @return {?}
*/
URLSearchParams.prototype.setAll = function (searchParams) {
var _this = this;
searchParams.paramsMap.forEach(function (value, param) {
var /** @type {?} */ list = _this.paramsMap.get(param) || [];
list.length = 0;
list.push(value[0]);
_this.paramsMap.set(param, list);
});
};
/**
* @param {?} param
* @param {?} val
* @return {?}
*/
URLSearchParams.prototype.append = function (param, val) {
if (val === void 0 || val === null)
return;
var /** @type {?} */ list = this.paramsMap.get(param) || [];
list.push(val);
this.paramsMap.set(param, list);
};
/**
* @param {?} searchParams
* @return {?}
*/
URLSearchParams.prototype.appendAll = function (searchParams) {
var _this = this;
searchParams.paramsMap.forEach(function (value, param) {
var /** @type {?} */ list = _this.paramsMap.get(param) || [];
for (var /** @type {?} */ i = 0; i < value.length; ++i) {
list.push(value[i]);
}
_this.paramsMap.set(param, list);
});
};
/**
* @param {?} searchParams
* @return {?}
*/
URLSearchParams.prototype.replaceAll = function (searchParams) {
var _this = this;
searchParams.paramsMap.forEach(function (value, param) {
var /** @type {?} */ list = _this.paramsMap.get(param) || [];
list.length = 0;
for (var /** @type {?} */ i = 0; i < value.length; ++i) {
list.push(value[i]);
}
_this.paramsMap.set(param, list);
});
};
/**
* @return {?}
*/
URLSearchParams.prototype.toString = function () {
var _this = this;
var /** @type {?} */ paramsList = [];
this.paramsMap.forEach(function (values, k) {
values.forEach(function (v) { return paramsList.push(_this.queryEncoder.encodeKey(k) + '=' + _this.queryEncoder.encodeValue(v)); });
});
return paramsList.join('&');
};
/**
* @param {?} param
* @return {?}
*/
URLSearchParams.prototype.delete = function (param) { this.paramsMap.delete(param); };
return URLSearchParams;
}());
function URLSearchParams_tsickle_Closure_declarations() {
/** @type {?} */
URLSearchParams.prototype.paramsMap;
/** @type {?} */
URLSearchParams.prototype.rawParams;
/** @type {?} */
URLSearchParams.prototype.queryEncoder;
}
//# sourceMappingURL=url_search_params.js.map