1
|
/**
|
2
|
* @license Angular v4.4.6
|
3
|
* (c) 2010-2017 Google, Inc. https://angular.io/
|
4
|
* License: MIT
|
5
|
*/
|
6
|
import { Injectable, NgModule, Version } from '@angular/core';
|
7
|
import { Observable } from 'rxjs/Observable';
|
8
|
import { ɵgetDOM } from '@angular/platform-browser';
|
9
|
|
10
|
/**
|
11
|
* @license
|
12
|
* Copyright Google Inc. All Rights Reserved.
|
13
|
*
|
14
|
* Use of this source code is governed by an MIT-style license that can be
|
15
|
* found in the LICENSE file at https://angular.io/license
|
16
|
*/
|
17
|
/**
|
18
|
* A backend for http that uses the `XMLHttpRequest` browser API.
|
19
|
*
|
20
|
* Take care not to evaluate this in non-browser contexts.
|
21
|
*
|
22
|
* \@experimental
|
23
|
*/
|
24
|
class BrowserXhr {
|
25
|
constructor() { }
|
26
|
/**
|
27
|
* @return {?}
|
28
|
*/
|
29
|
build() { return ((new XMLHttpRequest())); }
|
30
|
}
|
31
|
BrowserXhr.decorators = [
|
32
|
{ type: Injectable },
|
33
|
];
|
34
|
/**
|
35
|
* @nocollapse
|
36
|
*/
|
37
|
BrowserXhr.ctorParameters = () => [];
|
38
|
|
39
|
let RequestMethod = {};
|
40
|
RequestMethod.Get = 0;
|
41
|
RequestMethod.Post = 1;
|
42
|
RequestMethod.Put = 2;
|
43
|
RequestMethod.Delete = 3;
|
44
|
RequestMethod.Options = 4;
|
45
|
RequestMethod.Head = 5;
|
46
|
RequestMethod.Patch = 6;
|
47
|
RequestMethod[RequestMethod.Get] = "Get";
|
48
|
RequestMethod[RequestMethod.Post] = "Post";
|
49
|
RequestMethod[RequestMethod.Put] = "Put";
|
50
|
RequestMethod[RequestMethod.Delete] = "Delete";
|
51
|
RequestMethod[RequestMethod.Options] = "Options";
|
52
|
RequestMethod[RequestMethod.Head] = "Head";
|
53
|
RequestMethod[RequestMethod.Patch] = "Patch";
|
54
|
let ReadyState = {};
|
55
|
ReadyState.Unsent = 0;
|
56
|
ReadyState.Open = 1;
|
57
|
ReadyState.HeadersReceived = 2;
|
58
|
ReadyState.Loading = 3;
|
59
|
ReadyState.Done = 4;
|
60
|
ReadyState.Cancelled = 5;
|
61
|
ReadyState[ReadyState.Unsent] = "Unsent";
|
62
|
ReadyState[ReadyState.Open] = "Open";
|
63
|
ReadyState[ReadyState.HeadersReceived] = "HeadersReceived";
|
64
|
ReadyState[ReadyState.Loading] = "Loading";
|
65
|
ReadyState[ReadyState.Done] = "Done";
|
66
|
ReadyState[ReadyState.Cancelled] = "Cancelled";
|
67
|
let ResponseType = {};
|
68
|
ResponseType.Basic = 0;
|
69
|
ResponseType.Cors = 1;
|
70
|
ResponseType.Default = 2;
|
71
|
ResponseType.Error = 3;
|
72
|
ResponseType.Opaque = 4;
|
73
|
ResponseType[ResponseType.Basic] = "Basic";
|
74
|
ResponseType[ResponseType.Cors] = "Cors";
|
75
|
ResponseType[ResponseType.Default] = "Default";
|
76
|
ResponseType[ResponseType.Error] = "Error";
|
77
|
ResponseType[ResponseType.Opaque] = "Opaque";
|
78
|
let ContentType = {};
|
79
|
ContentType.NONE = 0;
|
80
|
ContentType.JSON = 1;
|
81
|
ContentType.FORM = 2;
|
82
|
ContentType.FORM_DATA = 3;
|
83
|
ContentType.TEXT = 4;
|
84
|
ContentType.BLOB = 5;
|
85
|
ContentType.ARRAY_BUFFER = 6;
|
86
|
ContentType[ContentType.NONE] = "NONE";
|
87
|
ContentType[ContentType.JSON] = "JSON";
|
88
|
ContentType[ContentType.FORM] = "FORM";
|
89
|
ContentType[ContentType.FORM_DATA] = "FORM_DATA";
|
90
|
ContentType[ContentType.TEXT] = "TEXT";
|
91
|
ContentType[ContentType.BLOB] = "BLOB";
|
92
|
ContentType[ContentType.ARRAY_BUFFER] = "ARRAY_BUFFER";
|
93
|
let ResponseContentType = {};
|
94
|
ResponseContentType.Text = 0;
|
95
|
ResponseContentType.Json = 1;
|
96
|
ResponseContentType.ArrayBuffer = 2;
|
97
|
ResponseContentType.Blob = 3;
|
98
|
ResponseContentType[ResponseContentType.Text] = "Text";
|
99
|
ResponseContentType[ResponseContentType.Json] = "Json";
|
100
|
ResponseContentType[ResponseContentType.ArrayBuffer] = "ArrayBuffer";
|
101
|
ResponseContentType[ResponseContentType.Blob] = "Blob";
|
102
|
|
103
|
/**
|
104
|
* Polyfill for [Headers](https://developer.mozilla.org/en-US/docs/Web/API/Headers/Headers), as
|
105
|
* specified in the [Fetch Spec](https://fetch.spec.whatwg.org/#headers-class).
|
106
|
*
|
107
|
* The only known difference between this `Headers` implementation and the spec is the
|
108
|
* lack of an `entries` method.
|
109
|
*
|
110
|
* ### Example
|
111
|
*
|
112
|
* ```
|
113
|
* import {Headers} from '\@angular/http';
|
114
|
*
|
115
|
* var firstHeaders = new Headers();
|
116
|
* firstHeaders.append('Content-Type', 'image/jpeg');
|
117
|
* console.log(firstHeaders.get('Content-Type')) //'image/jpeg'
|
118
|
*
|
119
|
* // Create headers from Plain Old JavaScript Object
|
120
|
* var secondHeaders = new Headers({
|
121
|
* 'X-My-Custom-Header': 'Angular'
|
122
|
* });
|
123
|
* console.log(secondHeaders.get('X-My-Custom-Header')); //'Angular'
|
124
|
*
|
125
|
* var thirdHeaders = new Headers(secondHeaders);
|
126
|
* console.log(thirdHeaders.get('X-My-Custom-Header')); //'Angular'
|
127
|
* ```
|
128
|
*
|
129
|
* \@experimental
|
130
|
*/
|
131
|
class Headers {
|
132
|
/**
|
133
|
* @param {?=} headers
|
134
|
*/
|
135
|
constructor(headers) {
|
136
|
/**
|
137
|
* \@internal header names are lower case
|
138
|
*/
|
139
|
this._headers = new Map();
|
140
|
/**
|
141
|
* \@internal map lower case names to actual names
|
142
|
*/
|
143
|
this._normalizedNames = new Map();
|
144
|
if (!headers) {
|
145
|
return;
|
146
|
}
|
147
|
if (headers instanceof Headers) {
|
148
|
headers.forEach((values, name) => {
|
149
|
values.forEach(value => this.append(name, value));
|
150
|
});
|
151
|
return;
|
152
|
}
|
153
|
Object.keys(headers).forEach((name) => {
|
154
|
const values = Array.isArray(headers[name]) ? headers[name] : [headers[name]];
|
155
|
this.delete(name);
|
156
|
values.forEach(value => this.append(name, value));
|
157
|
});
|
158
|
}
|
159
|
/**
|
160
|
* Returns a new Headers instance from the given DOMString of Response Headers
|
161
|
* @param {?} headersString
|
162
|
* @return {?}
|
163
|
*/
|
164
|
static fromResponseHeaderString(headersString) {
|
165
|
const /** @type {?} */ headers = new Headers();
|
166
|
headersString.split('\n').forEach(line => {
|
167
|
const /** @type {?} */ index = line.indexOf(':');
|
168
|
if (index > 0) {
|
169
|
const /** @type {?} */ name = line.slice(0, index);
|
170
|
const /** @type {?} */ value = line.slice(index + 1).trim();
|
171
|
headers.set(name, value);
|
172
|
}
|
173
|
});
|
174
|
return headers;
|
175
|
}
|
176
|
/**
|
177
|
* Appends a header to existing list of header values for a given header name.
|
178
|
* @param {?} name
|
179
|
* @param {?} value
|
180
|
* @return {?}
|
181
|
*/
|
182
|
append(name, value) {
|
183
|
const /** @type {?} */ values = this.getAll(name);
|
184
|
if (values === null) {
|
185
|
this.set(name, value);
|
186
|
}
|
187
|
else {
|
188
|
values.push(value);
|
189
|
}
|
190
|
}
|
191
|
/**
|
192
|
* Deletes all header values for the given name.
|
193
|
* @param {?} name
|
194
|
* @return {?}
|
195
|
*/
|
196
|
delete(name) {
|
197
|
const /** @type {?} */ lcName = name.toLowerCase();
|
198
|
this._normalizedNames.delete(lcName);
|
199
|
this._headers.delete(lcName);
|
200
|
}
|
201
|
/**
|
202
|
* @param {?} fn
|
203
|
* @return {?}
|
204
|
*/
|
205
|
forEach(fn) {
|
206
|
this._headers.forEach((values, lcName) => fn(values, this._normalizedNames.get(lcName), this._headers));
|
207
|
}
|
208
|
/**
|
209
|
* Returns first header that matches given name.
|
210
|
* @param {?} name
|
211
|
* @return {?}
|
212
|
*/
|
213
|
get(name) {
|
214
|
const /** @type {?} */ values = this.getAll(name);
|
215
|
if (values === null) {
|
216
|
return null;
|
217
|
}
|
218
|
return values.length > 0 ? values[0] : null;
|
219
|
}
|
220
|
/**
|
221
|
* Checks for existence of header by given name.
|
222
|
* @param {?} name
|
223
|
* @return {?}
|
224
|
*/
|
225
|
has(name) { return this._headers.has(name.toLowerCase()); }
|
226
|
/**
|
227
|
* Returns the names of the headers
|
228
|
* @return {?}
|
229
|
*/
|
230
|
keys() { return Array.from(this._normalizedNames.values()); }
|
231
|
/**
|
232
|
* Sets or overrides header value for given name.
|
233
|
* @param {?} name
|
234
|
* @param {?} value
|
235
|
* @return {?}
|
236
|
*/
|
237
|
set(name, value) {
|
238
|
if (Array.isArray(value)) {
|
239
|
if (value.length) {
|
240
|
this._headers.set(name.toLowerCase(), [value.join(',')]);
|
241
|
}
|
242
|
}
|
243
|
else {
|
244
|
this._headers.set(name.toLowerCase(), [value]);
|
245
|
}
|
246
|
this.mayBeSetNormalizedName(name);
|
247
|
}
|
248
|
/**
|
249
|
* Returns values of all headers.
|
250
|
* @return {?}
|
251
|
*/
|
252
|
values() { return Array.from(this._headers.values()); }
|
253
|
/**
|
254
|
* @return {?}
|
255
|
*/
|
256
|
toJSON() {
|
257
|
const /** @type {?} */ serialized = {};
|
258
|
this._headers.forEach((values, name) => {
|
259
|
const /** @type {?} */ split = [];
|
260
|
values.forEach(v => split.push(...v.split(',')));
|
261
|
serialized[((this._normalizedNames.get(name)))] = split;
|
262
|
});
|
263
|
return serialized;
|
264
|
}
|
265
|
/**
|
266
|
* Returns list of header values for a given name.
|
267
|
* @param {?} name
|
268
|
* @return {?}
|
269
|
*/
|
270
|
getAll(name) {
|
271
|
return this.has(name) ? this._headers.get(name.toLowerCase()) || null : null;
|
272
|
}
|
273
|
/**
|
274
|
* This method is not implemented.
|
275
|
* @return {?}
|
276
|
*/
|
277
|
entries() { throw new Error('"entries" method is not implemented on Headers class'); }
|
278
|
/**
|
279
|
* @param {?} name
|
280
|
* @return {?}
|
281
|
*/
|
282
|
mayBeSetNormalizedName(name) {
|
283
|
const /** @type {?} */ lcName = name.toLowerCase();
|
284
|
if (!this._normalizedNames.has(lcName)) {
|
285
|
this._normalizedNames.set(lcName, name);
|
286
|
}
|
287
|
}
|
288
|
}
|
289
|
|
290
|
/**
|
291
|
* @license
|
292
|
* Copyright Google Inc. All Rights Reserved.
|
293
|
*
|
294
|
* Use of this source code is governed by an MIT-style license that can be
|
295
|
* found in the LICENSE file at https://angular.io/license
|
296
|
*/
|
297
|
/**
|
298
|
* Creates a response options object to be optionally provided when instantiating a
|
299
|
* {\@link Response}.
|
300
|
*
|
301
|
* This class is based on the `ResponseInit` description in the [Fetch
|
302
|
* Spec](https://fetch.spec.whatwg.org/#responseinit).
|
303
|
*
|
304
|
* All values are null by default. Typical defaults can be found in the
|
305
|
* {\@link BaseResponseOptions} class, which sub-classes `ResponseOptions`.
|
306
|
*
|
307
|
* This class may be used in tests to build {\@link Response Responses} for
|
308
|
* mock responses (see {\@link MockBackend}).
|
309
|
*
|
310
|
* ### Example ([live demo](http://plnkr.co/edit/P9Jkk8e8cz6NVzbcxEsD?p=preview))
|
311
|
*
|
312
|
* ```typescript
|
313
|
* import {ResponseOptions, Response} from '\@angular/http';
|
314
|
*
|
315
|
* var options = new ResponseOptions({
|
316
|
* body: '{"name":"Jeff"}'
|
317
|
* });
|
318
|
* var res = new Response(options);
|
319
|
*
|
320
|
* console.log('res.json():', res.json()); // Object {name: "Jeff"}
|
321
|
* ```
|
322
|
*
|
323
|
* \@experimental
|
324
|
*/
|
325
|
class ResponseOptions {
|
326
|
/**
|
327
|
* @param {?=} opts
|
328
|
*/
|
329
|
constructor(opts = {}) {
|
330
|
const { body, status, headers, statusText, type, url } = opts;
|
331
|
this.body = body != null ? body : null;
|
332
|
this.status = status != null ? status : null;
|
333
|
this.headers = headers != null ? headers : null;
|
334
|
this.statusText = statusText != null ? statusText : null;
|
335
|
this.type = type != null ? type : null;
|
336
|
this.url = url != null ? url : null;
|
337
|
}
|
338
|
/**
|
339
|
* Creates a copy of the `ResponseOptions` instance, using the optional input as values to
|
340
|
* override
|
341
|
* existing values. This method will not change the values of the instance on which it is being
|
342
|
* called.
|
343
|
*
|
344
|
* This may be useful when sharing a base `ResponseOptions` object inside tests,
|
345
|
* where certain properties may change from test to test.
|
346
|
*
|
347
|
* ### Example ([live demo](http://plnkr.co/edit/1lXquqFfgduTFBWjNoRE?p=preview))
|
348
|
*
|
349
|
* ```typescript
|
350
|
* import {ResponseOptions, Response} from '\@angular/http';
|
351
|
*
|
352
|
* var options = new ResponseOptions({
|
353
|
* body: {name: 'Jeff'}
|
354
|
* });
|
355
|
* var res = new Response(options.merge({
|
356
|
* url: 'https://google.com'
|
357
|
* }));
|
358
|
* console.log('options.url:', options.url); // null
|
359
|
* console.log('res.json():', res.json()); // Object {name: "Jeff"}
|
360
|
* console.log('res.url:', res.url); // https://google.com
|
361
|
* ```
|
362
|
* @param {?=} options
|
363
|
* @return {?}
|
364
|
*/
|
365
|
merge(options) {
|
366
|
return new ResponseOptions({
|
367
|
body: options && options.body != null ? options.body : this.body,
|
368
|
status: options && options.status != null ? options.status : this.status,
|
369
|
headers: options && options.headers != null ? options.headers : this.headers,
|
370
|
statusText: options && options.statusText != null ? options.statusText : this.statusText,
|
371
|
type: options && options.type != null ? options.type : this.type,
|
372
|
url: options && options.url != null ? options.url : this.url,
|
373
|
});
|
374
|
}
|
375
|
}
|
376
|
/**
|
377
|
* Subclass of {\@link ResponseOptions}, with default values.
|
378
|
*
|
379
|
* Default values:
|
380
|
* * status: 200
|
381
|
* * headers: empty {\@link Headers} object
|
382
|
*
|
383
|
* This class could be extended and bound to the {\@link ResponseOptions} class
|
384
|
* when configuring an {\@link Injector}, in order to override the default options
|
385
|
* used by {\@link Http} to create {\@link Response Responses}.
|
386
|
*
|
387
|
* ### Example ([live demo](http://plnkr.co/edit/qv8DLT?p=preview))
|
388
|
*
|
389
|
* ```typescript
|
390
|
* import {provide} from '\@angular/core';
|
391
|
* import {bootstrap} from '\@angular/platform-browser/browser';
|
392
|
* import {HTTP_PROVIDERS, Headers, Http, BaseResponseOptions, ResponseOptions} from
|
393
|
* '\@angular/http';
|
394
|
* import {App} from './myapp';
|
395
|
*
|
396
|
* class MyOptions extends BaseResponseOptions {
|
397
|
* headers:Headers = new Headers({network: 'github'});
|
398
|
* }
|
399
|
*
|
400
|
* bootstrap(App, [HTTP_PROVIDERS, {provide: ResponseOptions, useClass: MyOptions}]);
|
401
|
* ```
|
402
|
*
|
403
|
* The options could also be extended when manually creating a {\@link Response}
|
404
|
* object.
|
405
|
*
|
406
|
* ### Example ([live demo](http://plnkr.co/edit/VngosOWiaExEtbstDoix?p=preview))
|
407
|
*
|
408
|
* ```
|
409
|
* import {BaseResponseOptions, Response} from '\@angular/http';
|
410
|
*
|
411
|
* var options = new BaseResponseOptions();
|
412
|
* var res = new Response(options.merge({
|
413
|
* body: 'Angular',
|
414
|
* headers: new Headers({framework: 'angular'})
|
415
|
* }));
|
416
|
* console.log('res.headers.get("framework"):', res.headers.get('framework')); // angular
|
417
|
* console.log('res.text():', res.text()); // Angular;
|
418
|
* ```
|
419
|
*
|
420
|
* \@experimental
|
421
|
*/
|
422
|
class BaseResponseOptions extends ResponseOptions {
|
423
|
constructor() {
|
424
|
super({ status: 200, statusText: 'Ok', type: ResponseType.Default, headers: new Headers() });
|
425
|
}
|
426
|
}
|
427
|
BaseResponseOptions.decorators = [
|
428
|
{ type: Injectable },
|
429
|
];
|
430
|
/**
|
431
|
* @nocollapse
|
432
|
*/
|
433
|
BaseResponseOptions.ctorParameters = () => [];
|
434
|
|
435
|
/**
|
436
|
* @license
|
437
|
* Copyright Google Inc. All Rights Reserved.
|
438
|
*
|
439
|
* Use of this source code is governed by an MIT-style license that can be
|
440
|
* found in the LICENSE file at https://angular.io/license
|
441
|
*/
|
442
|
/**
|
443
|
* Abstract class from which real backends are derived.
|
444
|
*
|
445
|
* The primary purpose of a `ConnectionBackend` is to create new connections to fulfill a given
|
446
|
* {\@link Request}.
|
447
|
*
|
448
|
* \@experimental
|
449
|
* @abstract
|
450
|
*/
|
451
|
class ConnectionBackend {
|
452
|
/**
|
453
|
* @abstract
|
454
|
* @param {?} request
|
455
|
* @return {?}
|
456
|
*/
|
457
|
createConnection(request) { }
|
458
|
}
|
459
|
/**
|
460
|
* Abstract class from which real connections are derived.
|
461
|
*
|
462
|
* \@experimental
|
463
|
* @abstract
|
464
|
*/
|
465
|
class Connection {
|
466
|
}
|
467
|
/**
|
468
|
* An XSRFStrategy configures XSRF protection (e.g. via headers) on an HTTP request.
|
469
|
*
|
470
|
* \@experimental
|
471
|
* @abstract
|
472
|
*/
|
473
|
class XSRFStrategy {
|
474
|
/**
|
475
|
* @abstract
|
476
|
* @param {?} req
|
477
|
* @return {?}
|
478
|
*/
|
479
|
configureRequest(req) { }
|
480
|
}
|
481
|
|
482
|
/**
|
483
|
* @license
|
484
|
* Copyright Google Inc. All Rights Reserved.
|
485
|
*
|
486
|
* Use of this source code is governed by an MIT-style license that can be
|
487
|
* found in the LICENSE file at https://angular.io/license
|
488
|
*/
|
489
|
/**
|
490
|
* @param {?} method
|
491
|
* @return {?}
|
492
|
*/
|
493
|
function normalizeMethodName(method) {
|
494
|
if (typeof method !== 'string')
|
495
|
return method;
|
496
|
switch (method.toUpperCase()) {
|
497
|
case 'GET':
|
498
|
return RequestMethod.Get;
|
499
|
case 'POST':
|
500
|
return RequestMethod.Post;
|
501
|
case 'PUT':
|
502
|
return RequestMethod.Put;
|
503
|
case 'DELETE':
|
504
|
return RequestMethod.Delete;
|
505
|
case 'OPTIONS':
|
506
|
return RequestMethod.Options;
|
507
|
case 'HEAD':
|
508
|
return RequestMethod.Head;
|
509
|
case 'PATCH':
|
510
|
return RequestMethod.Patch;
|
511
|
}
|
512
|
throw new Error(`Invalid request method. The method "${method}" is not supported.`);
|
513
|
}
|
514
|
const isSuccess = (status) => (status >= 200 && status < 300);
|
515
|
/**
|
516
|
* @param {?} xhr
|
517
|
* @return {?}
|
518
|
*/
|
519
|
function getResponseURL(xhr) {
|
520
|
if ('responseURL' in xhr) {
|
521
|
return xhr.responseURL;
|
522
|
}
|
523
|
if (/^X-Request-URL:/m.test(xhr.getAllResponseHeaders())) {
|
524
|
return xhr.getResponseHeader('X-Request-URL');
|
525
|
}
|
526
|
return null;
|
527
|
}
|
528
|
/**
|
529
|
* @param {?} input
|
530
|
* @return {?}
|
531
|
*/
|
532
|
|
533
|
/**
|
534
|
* @param {?} input
|
535
|
* @return {?}
|
536
|
*/
|
537
|
function stringToArrayBuffer(input) {
|
538
|
const /** @type {?} */ view = new Uint16Array(input.length);
|
539
|
for (let /** @type {?} */ i = 0, /** @type {?} */ strLen = input.length; i < strLen; i++) {
|
540
|
view[i] = input.charCodeAt(i);
|
541
|
}
|
542
|
return view.buffer;
|
543
|
}
|
544
|
|
545
|
/**
|
546
|
* @license
|
547
|
* Copyright Google Inc. All Rights Reserved.
|
548
|
*
|
549
|
* Use of this source code is governed by an MIT-style license that can be
|
550
|
* found in the LICENSE file at https://angular.io/license
|
551
|
* @param {?=} rawParams
|
552
|
* @return {?}
|
553
|
*/
|
554
|
function paramParser(rawParams = '') {
|
555
|
const /** @type {?} */ map = new Map();
|
556
|
if (rawParams.length > 0) {
|
557
|
const /** @type {?} */ params = rawParams.split('&');
|
558
|
params.forEach((param) => {
|
559
|
const /** @type {?} */ eqIdx = param.indexOf('=');
|
560
|
const [key, val] = eqIdx == -1 ? [param, ''] : [param.slice(0, eqIdx), param.slice(eqIdx + 1)];
|
561
|
const /** @type {?} */ list = map.get(key) || [];
|
562
|
list.push(val);
|
563
|
map.set(key, list);
|
564
|
});
|
565
|
}
|
566
|
return map;
|
567
|
}
|
568
|
/**
|
569
|
* \@experimental
|
570
|
*
|
571
|
*/
|
572
|
class QueryEncoder {
|
573
|
/**
|
574
|
* @param {?} k
|
575
|
* @return {?}
|
576
|
*/
|
577
|
encodeKey(k) { return standardEncoding(k); }
|
578
|
/**
|
579
|
* @param {?} v
|
580
|
* @return {?}
|
581
|
*/
|
582
|
encodeValue(v) { return standardEncoding(v); }
|
583
|
}
|
584
|
/**
|
585
|
* @param {?} v
|
586
|
* @return {?}
|
587
|
*/
|
588
|
function standardEncoding(v) {
|
589
|
return encodeURIComponent(v)
|
590
|
.replace(/%40/gi, '@')
|
591
|
.replace(/%3A/gi, ':')
|
592
|
.replace(/%24/gi, '$')
|
593
|
.replace(/%2C/gi, ',')
|
594
|
.replace(/%3B/gi, ';')
|
595
|
.replace(/%2B/gi, '+')
|
596
|
.replace(/%3D/gi, '=')
|
597
|
.replace(/%3F/gi, '?')
|
598
|
.replace(/%2F/gi, '/');
|
599
|
}
|
600
|
/**
|
601
|
* Map-like representation of url search parameters, based on
|
602
|
* [URLSearchParams](https://url.spec.whatwg.org/#urlsearchparams) in the url living standard,
|
603
|
* with several extensions for merging URLSearchParams objects:
|
604
|
* - setAll()
|
605
|
* - appendAll()
|
606
|
* - replaceAll()
|
607
|
*
|
608
|
* This class accepts an optional second parameter of ${\@link QueryEncoder},
|
609
|
* which is used to serialize parameters before making a request. By default,
|
610
|
* `QueryEncoder` encodes keys and values of parameters using `encodeURIComponent`,
|
611
|
* and then un-encodes certain characters that are allowed to be part of the query
|
612
|
* according to IETF RFC 3986: https://tools.ietf.org/html/rfc3986.
|
613
|
*
|
614
|
* These are the characters that are not encoded: `! $ \' ( ) * + , ; A 9 - . _ ~ ? /`
|
615
|
*
|
616
|
* If the set of allowed query characters is not acceptable for a particular backend,
|
617
|
* `QueryEncoder` can be subclassed and provided as the 2nd argument to URLSearchParams.
|
618
|
*
|
619
|
* ```
|
620
|
* import {URLSearchParams, QueryEncoder} from '\@angular/http';
|
621
|
* class MyQueryEncoder extends QueryEncoder {
|
622
|
* encodeKey(k: string): string {
|
623
|
* return myEncodingFunction(k);
|
624
|
* }
|
625
|
*
|
626
|
* encodeValue(v: string): string {
|
627
|
* return myEncodingFunction(v);
|
628
|
* }
|
629
|
* }
|
630
|
*
|
631
|
* let params = new URLSearchParams('', new MyQueryEncoder());
|
632
|
* ```
|
633
|
* \@experimental
|
634
|
*/
|
635
|
class URLSearchParams {
|
636
|
/**
|
637
|
* @param {?=} rawParams
|
638
|
* @param {?=} queryEncoder
|
639
|
*/
|
640
|
constructor(rawParams = '', queryEncoder = new QueryEncoder()) {
|
641
|
this.rawParams = rawParams;
|
642
|
this.queryEncoder = queryEncoder;
|
643
|
this.paramsMap = paramParser(rawParams);
|
644
|
}
|
645
|
/**
|
646
|
* @return {?}
|
647
|
*/
|
648
|
clone() {
|
649
|
const /** @type {?} */ clone = new URLSearchParams('', this.queryEncoder);
|
650
|
clone.appendAll(this);
|
651
|
return clone;
|
652
|
}
|
653
|
/**
|
654
|
* @param {?} param
|
655
|
* @return {?}
|
656
|
*/
|
657
|
has(param) { return this.paramsMap.has(param); }
|
658
|
/**
|
659
|
* @param {?} param
|
660
|
* @return {?}
|
661
|
*/
|
662
|
get(param) {
|
663
|
const /** @type {?} */ storedParam = this.paramsMap.get(param);
|
664
|
return Array.isArray(storedParam) ? storedParam[0] : null;
|
665
|
}
|
666
|
/**
|
667
|
* @param {?} param
|
668
|
* @return {?}
|
669
|
*/
|
670
|
getAll(param) { return this.paramsMap.get(param) || []; }
|
671
|
/**
|
672
|
* @param {?} param
|
673
|
* @param {?} val
|
674
|
* @return {?}
|
675
|
*/
|
676
|
set(param, val) {
|
677
|
if (val === void 0 || val === null) {
|
678
|
this.delete(param);
|
679
|
return;
|
680
|
}
|
681
|
const /** @type {?} */ list = this.paramsMap.get(param) || [];
|
682
|
list.length = 0;
|
683
|
list.push(val);
|
684
|
this.paramsMap.set(param, list);
|
685
|
}
|
686
|
/**
|
687
|
* @param {?} searchParams
|
688
|
* @return {?}
|
689
|
*/
|
690
|
setAll(searchParams) {
|
691
|
searchParams.paramsMap.forEach((value, param) => {
|
692
|
const /** @type {?} */ list = this.paramsMap.get(param) || [];
|
693
|
list.length = 0;
|
694
|
list.push(value[0]);
|
695
|
this.paramsMap.set(param, list);
|
696
|
});
|
697
|
}
|
698
|
/**
|
699
|
* @param {?} param
|
700
|
* @param {?} val
|
701
|
* @return {?}
|
702
|
*/
|
703
|
append(param, val) {
|
704
|
if (val === void 0 || val === null)
|
705
|
return;
|
706
|
const /** @type {?} */ list = this.paramsMap.get(param) || [];
|
707
|
list.push(val);
|
708
|
this.paramsMap.set(param, list);
|
709
|
}
|
710
|
/**
|
711
|
* @param {?} searchParams
|
712
|
* @return {?}
|
713
|
*/
|
714
|
appendAll(searchParams) {
|
715
|
searchParams.paramsMap.forEach((value, param) => {
|
716
|
const /** @type {?} */ list = this.paramsMap.get(param) || [];
|
717
|
for (let /** @type {?} */ i = 0; i < value.length; ++i) {
|
718
|
list.push(value[i]);
|
719
|
}
|
720
|
this.paramsMap.set(param, list);
|
721
|
});
|
722
|
}
|
723
|
/**
|
724
|
* @param {?} searchParams
|
725
|
* @return {?}
|
726
|
*/
|
727
|
replaceAll(searchParams) {
|
728
|
searchParams.paramsMap.forEach((value, param) => {
|
729
|
const /** @type {?} */ list = this.paramsMap.get(param) || [];
|
730
|
list.length = 0;
|
731
|
for (let /** @type {?} */ i = 0; i < value.length; ++i) {
|
732
|
list.push(value[i]);
|
733
|
}
|
734
|
this.paramsMap.set(param, list);
|
735
|
});
|
736
|
}
|
737
|
/**
|
738
|
* @return {?}
|
739
|
*/
|
740
|
toString() {
|
741
|
const /** @type {?} */ paramsList = [];
|
742
|
this.paramsMap.forEach((values, k) => {
|
743
|
values.forEach(v => paramsList.push(this.queryEncoder.encodeKey(k) + '=' + this.queryEncoder.encodeValue(v)));
|
744
|
});
|
745
|
return paramsList.join('&');
|
746
|
}
|
747
|
/**
|
748
|
* @param {?} param
|
749
|
* @return {?}
|
750
|
*/
|
751
|
delete(param) { this.paramsMap.delete(param); }
|
752
|
}
|
753
|
|
754
|
/**
|
755
|
* @license
|
756
|
* Copyright Google Inc. All Rights Reserved.
|
757
|
*
|
758
|
* Use of this source code is governed by an MIT-style license that can be
|
759
|
* found in the LICENSE file at https://angular.io/license
|
760
|
*/
|
761
|
/**
|
762
|
* HTTP request body used by both {\@link Request} and {\@link Response}
|
763
|
* https://fetch.spec.whatwg.org/#body
|
764
|
* @abstract
|
765
|
*/
|
766
|
class Body {
|
767
|
/**
|
768
|
* Attempts to return body as parsed `JSON` object, or raises an exception.
|
769
|
* @return {?}
|
770
|
*/
|
771
|
json() {
|
772
|
if (typeof this._body === 'string') {
|
773
|
return JSON.parse(/** @type {?} */ (this._body));
|
774
|
}
|
775
|
if (this._body instanceof ArrayBuffer) {
|
776
|
return JSON.parse(this.text());
|
777
|
}
|
778
|
return this._body;
|
779
|
}
|
780
|
/**
|
781
|
* Returns the body as a string, presuming `toString()` can be called on the response body.
|
782
|
*
|
783
|
* When decoding an `ArrayBuffer`, the optional `encodingHint` parameter determines how the
|
784
|
* bytes in the buffer will be interpreted. Valid values are:
|
785
|
*
|
786
|
* - `legacy` - incorrectly interpret the bytes as UTF-16 (technically, UCS-2). Only characters
|
787
|
* in the Basic Multilingual Plane are supported, surrogate pairs are not handled correctly.
|
788
|
* In addition, the endianness of the 16-bit octet pairs in the `ArrayBuffer` is not taken
|
789
|
* into consideration. This is the default behavior to avoid breaking apps, but should be
|
790
|
* considered deprecated.
|
791
|
*
|
792
|
* - `iso-8859` - interpret the bytes as ISO-8859 (which can be used for ASCII encoded text).
|
793
|
* @param {?=} encodingHint
|
794
|
* @return {?}
|
795
|
*/
|
796
|
text(encodingHint = 'legacy') {
|
797
|
if (this._body instanceof URLSearchParams) {
|
798
|
return this._body.toString();
|
799
|
}
|
800
|
if (this._body instanceof ArrayBuffer) {
|
801
|
switch (encodingHint) {
|
802
|
case 'legacy':
|
803
|
return String.fromCharCode.apply(null, new Uint16Array(/** @type {?} */ (this._body)));
|
804
|
case 'iso-8859':
|
805
|
return String.fromCharCode.apply(null, new Uint8Array(/** @type {?} */ (this._body)));
|
806
|
default:
|
807
|
throw new Error(`Invalid value for encodingHint: ${encodingHint}`);
|
808
|
}
|
809
|
}
|
810
|
if (this._body == null) {
|
811
|
return '';
|
812
|
}
|
813
|
if (typeof this._body === 'object') {
|
814
|
return JSON.stringify(this._body, null, 2);
|
815
|
}
|
816
|
return this._body.toString();
|
817
|
}
|
818
|
/**
|
819
|
* Return the body as an ArrayBuffer
|
820
|
* @return {?}
|
821
|
*/
|
822
|
arrayBuffer() {
|
823
|
if (this._body instanceof ArrayBuffer) {
|
824
|
return (this._body);
|
825
|
}
|
826
|
return stringToArrayBuffer(this.text());
|
827
|
}
|
828
|
/**
|
829
|
* Returns the request's body as a Blob, assuming that body exists.
|
830
|
* @return {?}
|
831
|
*/
|
832
|
blob() {
|
833
|
if (this._body instanceof Blob) {
|
834
|
return (this._body);
|
835
|
}
|
836
|
if (this._body instanceof ArrayBuffer) {
|
837
|
return new Blob([this._body]);
|
838
|
}
|
839
|
throw new Error('The request body isn\'t either a blob or an array buffer');
|
840
|
}
|
841
|
}
|
842
|
|
843
|
/**
|
844
|
* @license
|
845
|
* Copyright Google Inc. All Rights Reserved.
|
846
|
*
|
847
|
* Use of this source code is governed by an MIT-style license that can be
|
848
|
* found in the LICENSE file at https://angular.io/license
|
849
|
*/
|
850
|
/**
|
851
|
* Creates `Response` instances from provided values.
|
852
|
*
|
853
|
* Though this object isn't
|
854
|
* usually instantiated by end-users, it is the primary object interacted with when it comes time to
|
855
|
* add data to a view.
|
856
|
*
|
857
|
* ### Example
|
858
|
*
|
859
|
* ```
|
860
|
* http.request('my-friends.txt').subscribe(response => this.friends = response.text());
|
861
|
* ```
|
862
|
*
|
863
|
* The Response's interface is inspired by the Response constructor defined in the [Fetch
|
864
|
* Spec](https://fetch.spec.whatwg.org/#response-class), but is considered a static value whose body
|
865
|
* can be accessed many times. There are other differences in the implementation, but this is the
|
866
|
* most significant.
|
867
|
*
|
868
|
* \@experimental
|
869
|
*/
|
870
|
class Response extends Body {
|
871
|
/**
|
872
|
* @param {?} responseOptions
|
873
|
*/
|
874
|
constructor(responseOptions) {
|
875
|
super();
|
876
|
this._body = responseOptions.body;
|
877
|
this.status = responseOptions.status;
|
878
|
this.ok = (this.status >= 200 && this.status <= 299);
|
879
|
this.statusText = responseOptions.statusText;
|
880
|
this.headers = responseOptions.headers;
|
881
|
this.type = responseOptions.type;
|
882
|
this.url = responseOptions.url;
|
883
|
}
|
884
|
/**
|
885
|
* @return {?}
|
886
|
*/
|
887
|
toString() {
|
888
|
return `Response with status: ${this.status} ${this.statusText} for URL: ${this.url}`;
|
889
|
}
|
890
|
}
|
891
|
|
892
|
/**
|
893
|
* @license
|
894
|
* Copyright Google Inc. All Rights Reserved.
|
895
|
*
|
896
|
* Use of this source code is governed by an MIT-style license that can be
|
897
|
* found in the LICENSE file at https://angular.io/license
|
898
|
*/
|
899
|
let _nextRequestId = 0;
|
900
|
const JSONP_HOME = '__ng_jsonp__';
|
901
|
let _jsonpConnections = null;
|
902
|
/**
|
903
|
* @return {?}
|
904
|
*/
|
905
|
function _getJsonpConnections() {
|
906
|
const /** @type {?} */ w = typeof window == 'object' ? window : {};
|
907
|
if (_jsonpConnections === null) {
|
908
|
_jsonpConnections = w[JSONP_HOME] = {};
|
909
|
}
|
910
|
return _jsonpConnections;
|
911
|
}
|
912
|
class BrowserJsonp {
|
913
|
/**
|
914
|
* @param {?} url
|
915
|
* @return {?}
|
916
|
*/
|
917
|
build(url) {
|
918
|
const /** @type {?} */ node = document.createElement('script');
|
919
|
node.src = url;
|
920
|
return node;
|
921
|
}
|
922
|
/**
|
923
|
* @return {?}
|
924
|
*/
|
925
|
nextRequestID() { return `__req${_nextRequestId++}`; }
|
926
|
/**
|
927
|
* @param {?} id
|
928
|
* @return {?}
|
929
|
*/
|
930
|
requestCallback(id) { return `${JSONP_HOME}.${id}.finished`; }
|
931
|
/**
|
932
|
* @param {?} id
|
933
|
* @param {?} connection
|
934
|
* @return {?}
|
935
|
*/
|
936
|
exposeConnection(id, connection) {
|
937
|
const /** @type {?} */ connections = _getJsonpConnections();
|
938
|
connections[id] = connection;
|
939
|
}
|
940
|
/**
|
941
|
* @param {?} id
|
942
|
* @return {?}
|
943
|
*/
|
944
|
removeConnection(id) {
|
945
|
const /** @type {?} */ connections = _getJsonpConnections();
|
946
|
connections[id] = null;
|
947
|
}
|
948
|
/**
|
949
|
* @param {?} node
|
950
|
* @return {?}
|
951
|
*/
|
952
|
send(node) { document.body.appendChild(/** @type {?} */ ((node))); }
|
953
|
/**
|
954
|
* @param {?} node
|
955
|
* @return {?}
|
956
|
*/
|
957
|
cleanup(node) {
|
958
|
if (node.parentNode) {
|
959
|
node.parentNode.removeChild(/** @type {?} */ ((node)));
|
960
|
}
|
961
|
}
|
962
|
}
|
963
|
BrowserJsonp.decorators = [
|
964
|
{ type: Injectable },
|
965
|
];
|
966
|
/**
|
967
|
* @nocollapse
|
968
|
*/
|
969
|
BrowserJsonp.ctorParameters = () => [];
|
970
|
|
971
|
/**
|
972
|
* @license
|
973
|
* Copyright Google Inc. All Rights Reserved.
|
974
|
*
|
975
|
* Use of this source code is governed by an MIT-style license that can be
|
976
|
* found in the LICENSE file at https://angular.io/license
|
977
|
*/
|
978
|
const JSONP_ERR_NO_CALLBACK = 'JSONP injected script did not invoke callback.';
|
979
|
const JSONP_ERR_WRONG_METHOD = 'JSONP requests must use GET request method.';
|
980
|
/**
|
981
|
* Abstract base class for an in-flight JSONP request.
|
982
|
*
|
983
|
* \@experimental
|
984
|
* @abstract
|
985
|
*/
|
986
|
class JSONPConnection {
|
987
|
/**
|
988
|
* Callback called when the JSONP request completes, to notify the application
|
989
|
* of the new data.
|
990
|
* @abstract
|
991
|
* @param {?=} data
|
992
|
* @return {?}
|
993
|
*/
|
994
|
finished(data) { }
|
995
|
}
|
996
|
class JSONPConnection_ extends JSONPConnection {
|
997
|
/**
|
998
|
* @param {?} req
|
999
|
* @param {?} _dom
|
1000
|
* @param {?=} baseResponseOptions
|
1001
|
*/
|
1002
|
constructor(req, _dom, baseResponseOptions) {
|
1003
|
super();
|
1004
|
this._dom = _dom;
|
1005
|
this.baseResponseOptions = baseResponseOptions;
|
1006
|
this._finished = false;
|
1007
|
if (req.method !== RequestMethod.Get) {
|
1008
|
throw new TypeError(JSONP_ERR_WRONG_METHOD);
|
1009
|
}
|
1010
|
this.request = req;
|
1011
|
this.response = new Observable((responseObserver) => {
|
1012
|
this.readyState = ReadyState.Loading;
|
1013
|
const id = this._id = _dom.nextRequestID();
|
1014
|
_dom.exposeConnection(id, this);
|
1015
|
// Workaround Dart
|
1016
|
// url = url.replace(/=JSONP_CALLBACK(&|$)/, `generated method`);
|
1017
|
const callback = _dom.requestCallback(this._id);
|
1018
|
let url = req.url;
|
1019
|
if (url.indexOf('=JSONP_CALLBACK&') > -1) {
|
1020
|
url = url.replace('=JSONP_CALLBACK&', `=${callback}&`);
|
1021
|
}
|
1022
|
else if (url.lastIndexOf('=JSONP_CALLBACK') === url.length - '=JSONP_CALLBACK'.length) {
|
1023
|
url = url.substring(0, url.length - '=JSONP_CALLBACK'.length) + `=${callback}`;
|
1024
|
}
|
1025
|
const script = this._script = _dom.build(url);
|
1026
|
const onLoad = (event) => {
|
1027
|
if (this.readyState === ReadyState.Cancelled)
|
1028
|
return;
|
1029
|
this.readyState = ReadyState.Done;
|
1030
|
_dom.cleanup(script);
|
1031
|
if (!this._finished) {
|
1032
|
let responseOptions = new ResponseOptions({ body: JSONP_ERR_NO_CALLBACK, type: ResponseType.Error, url });
|
1033
|
if (baseResponseOptions) {
|
1034
|
responseOptions = baseResponseOptions.merge(responseOptions);
|
1035
|
}
|
1036
|
responseObserver.error(new Response(responseOptions));
|
1037
|
return;
|
1038
|
}
|
1039
|
let responseOptions = new ResponseOptions({ body: this._responseData, url });
|
1040
|
if (this.baseResponseOptions) {
|
1041
|
responseOptions = this.baseResponseOptions.merge(responseOptions);
|
1042
|
}
|
1043
|
responseObserver.next(new Response(responseOptions));
|
1044
|
responseObserver.complete();
|
1045
|
};
|
1046
|
const onError = (error) => {
|
1047
|
if (this.readyState === ReadyState.Cancelled)
|
1048
|
return;
|
1049
|
this.readyState = ReadyState.Done;
|
1050
|
_dom.cleanup(script);
|
1051
|
let responseOptions = new ResponseOptions({ body: error.message, type: ResponseType.Error });
|
1052
|
if (baseResponseOptions) {
|
1053
|
responseOptions = baseResponseOptions.merge(responseOptions);
|
1054
|
}
|
1055
|
responseObserver.error(new Response(responseOptions));
|
1056
|
};
|
1057
|
script.addEventListener('load', onLoad);
|
1058
|
script.addEventListener('error', onError);
|
1059
|
_dom.send(script);
|
1060
|
return () => {
|
1061
|
this.readyState = ReadyState.Cancelled;
|
1062
|
script.removeEventListener('load', onLoad);
|
1063
|
script.removeEventListener('error', onError);
|
1064
|
this._dom.cleanup(script);
|
1065
|
};
|
1066
|
});
|
1067
|
}
|
1068
|
/**
|
1069
|
* @param {?=} data
|
1070
|
* @return {?}
|
1071
|
*/
|
1072
|
finished(data) {
|
1073
|
// Don't leak connections
|
1074
|
this._finished = true;
|
1075
|
this._dom.removeConnection(this._id);
|
1076
|
if (this.readyState === ReadyState.Cancelled)
|
1077
|
return;
|
1078
|
this._responseData = data;
|
1079
|
}
|
1080
|
}
|
1081
|
/**
|
1082
|
* A {\@link ConnectionBackend} that uses the JSONP strategy of making requests.
|
1083
|
*
|
1084
|
* \@experimental
|
1085
|
* @abstract
|
1086
|
*/
|
1087
|
class JSONPBackend extends ConnectionBackend {
|
1088
|
}
|
1089
|
class JSONPBackend_ extends JSONPBackend {
|
1090
|
/**
|
1091
|
* @param {?} _browserJSONP
|
1092
|
* @param {?} _baseResponseOptions
|
1093
|
*/
|
1094
|
constructor(_browserJSONP, _baseResponseOptions) {
|
1095
|
super();
|
1096
|
this._browserJSONP = _browserJSONP;
|
1097
|
this._baseResponseOptions = _baseResponseOptions;
|
1098
|
}
|
1099
|
/**
|
1100
|
* @param {?} request
|
1101
|
* @return {?}
|
1102
|
*/
|
1103
|
createConnection(request) {
|
1104
|
return new JSONPConnection_(request, this._browserJSONP, this._baseResponseOptions);
|
1105
|
}
|
1106
|
}
|
1107
|
JSONPBackend_.decorators = [
|
1108
|
{ type: Injectable },
|
1109
|
];
|
1110
|
/**
|
1111
|
* @nocollapse
|
1112
|
*/
|
1113
|
JSONPBackend_.ctorParameters = () => [
|
1114
|
{ type: BrowserJsonp, },
|
1115
|
{ type: ResponseOptions, },
|
1116
|
];
|
1117
|
|
1118
|
/**
|
1119
|
* @license
|
1120
|
* Copyright Google Inc. All Rights Reserved.
|
1121
|
*
|
1122
|
* Use of this source code is governed by an MIT-style license that can be
|
1123
|
* found in the LICENSE file at https://angular.io/license
|
1124
|
*/
|
1125
|
const XSSI_PREFIX = /^\)\]\}',?\n/;
|
1126
|
/**
|
1127
|
* Creates connections using `XMLHttpRequest`. Given a fully-qualified
|
1128
|
* request, an `XHRConnection` will immediately create an `XMLHttpRequest` object and send the
|
1129
|
* request.
|
1130
|
*
|
1131
|
* This class would typically not be created or interacted with directly inside applications, though
|
1132
|
* the {\@link MockConnection} may be interacted with in tests.
|
1133
|
*
|
1134
|
* \@experimental
|
1135
|
*/
|
1136
|
class XHRConnection {
|
1137
|
/**
|
1138
|
* @param {?} req
|
1139
|
* @param {?} browserXHR
|
1140
|
* @param {?=} baseResponseOptions
|
1141
|
*/
|
1142
|
constructor(req, browserXHR, baseResponseOptions) {
|
1143
|
this.request = req;
|
1144
|
this.response = new Observable((responseObserver) => {
|
1145
|
const _xhr = browserXHR.build();
|
1146
|
_xhr.open(RequestMethod[req.method].toUpperCase(), req.url);
|
1147
|
if (req.withCredentials != null) {
|
1148
|
_xhr.withCredentials = req.withCredentials;
|
1149
|
}
|
1150
|
// load event handler
|
1151
|
const onLoad = () => {
|
1152
|
// normalize IE9 bug (http://bugs.jquery.com/ticket/1450)
|
1153
|
let status = _xhr.status === 1223 ? 204 : _xhr.status;
|
1154
|
let body = null;
|
1155
|
// HTTP 204 means no content
|
1156
|
if (status !== 204) {
|
1157
|
// responseText is the old-school way of retrieving response (supported by IE8 & 9)
|
1158
|
// response/responseType properties were introduced in ResourceLoader Level2 spec
|
1159
|
// (supported by IE10)
|
1160
|
body = (typeof _xhr.response === 'undefined') ? _xhr.responseText : _xhr.response;
|
1161
|
// Implicitly strip a potential XSSI prefix.
|
1162
|
if (typeof body === 'string') {
|
1163
|
body = body.replace(XSSI_PREFIX, '');
|
1164
|
}
|
1165
|
}
|
1166
|
// fix status code when it is 0 (0 status is undocumented).
|
1167
|
// Occurs when accessing file resources or on Android 4.1 stock browser
|
1168
|
// while retrieving files from application cache.
|
1169
|
if (status === 0) {
|
1170
|
status = body ? 200 : 0;
|
1171
|
}
|
1172
|
const headers = Headers.fromResponseHeaderString(_xhr.getAllResponseHeaders());
|
1173
|
// IE 9 does not provide the way to get URL of response
|
1174
|
const url = getResponseURL(_xhr) || req.url;
|
1175
|
const statusText = _xhr.statusText || 'OK';
|
1176
|
let responseOptions = new ResponseOptions({ body, status, headers, statusText, url });
|
1177
|
if (baseResponseOptions != null) {
|
1178
|
responseOptions = baseResponseOptions.merge(responseOptions);
|
1179
|
}
|
1180
|
const response = new Response(responseOptions);
|
1181
|
response.ok = isSuccess(status);
|
1182
|
if (response.ok) {
|
1183
|
responseObserver.next(response);
|
1184
|
// TODO(gdi2290): defer complete if array buffer until done
|
1185
|
responseObserver.complete();
|
1186
|
return;
|
1187
|
}
|
1188
|
responseObserver.error(response);
|
1189
|
};
|
1190
|
// error event handler
|
1191
|
const onError = (err) => {
|
1192
|
let responseOptions = new ResponseOptions({
|
1193
|
body: err,
|
1194
|
type: ResponseType.Error,
|
1195
|
status: _xhr.status,
|
1196
|
statusText: _xhr.statusText,
|
1197
|
});
|
1198
|
if (baseResponseOptions != null) {
|
1199
|
responseOptions = baseResponseOptions.merge(responseOptions);
|
1200
|
}
|
1201
|
responseObserver.error(new Response(responseOptions));
|
1202
|
};
|
1203
|
this.setDetectedContentType(req, _xhr);
|
1204
|
if (req.headers == null) {
|
1205
|
req.headers = new Headers();
|
1206
|
}
|
1207
|
if (!req.headers.has('Accept')) {
|
1208
|
req.headers.append('Accept', 'application/json, text/plain, */*');
|
1209
|
}
|
1210
|
req.headers.forEach((values, name) => _xhr.setRequestHeader(name, values.join(',')));
|
1211
|
// Select the correct buffer type to store the response
|
1212
|
if (req.responseType != null && _xhr.responseType != null) {
|
1213
|
switch (req.responseType) {
|
1214
|
case ResponseContentType.ArrayBuffer:
|
1215
|
_xhr.responseType = 'arraybuffer';
|
1216
|
break;
|
1217
|
case ResponseContentType.Json:
|
1218
|
_xhr.responseType = 'json';
|
1219
|
break;
|
1220
|
case ResponseContentType.Text:
|
1221
|
_xhr.responseType = 'text';
|
1222
|
break;
|
1223
|
case ResponseContentType.Blob:
|
1224
|
_xhr.responseType = 'blob';
|
1225
|
break;
|
1226
|
default:
|
1227
|
throw new Error('The selected responseType is not supported');
|
1228
|
}
|
1229
|
}
|
1230
|
_xhr.addEventListener('load', onLoad);
|
1231
|
_xhr.addEventListener('error', onError);
|
1232
|
_xhr.send(this.request.getBody());
|
1233
|
return () => {
|
1234
|
_xhr.removeEventListener('load', onLoad);
|
1235
|
_xhr.removeEventListener('error', onError);
|
1236
|
_xhr.abort();
|
1237
|
};
|
1238
|
});
|
1239
|
}
|
1240
|
/**
|
1241
|
* @param {?} req
|
1242
|
* @param {?} _xhr
|
1243
|
* @return {?}
|
1244
|
*/
|
1245
|
setDetectedContentType(req /** TODO Request */, _xhr /** XMLHttpRequest */) {
|
1246
|
// Skip if a custom Content-Type header is provided
|
1247
|
if (req.headers != null && req.headers.get('Content-Type') != null) {
|
1248
|
return;
|
1249
|
}
|
1250
|
// Set the detected content type
|
1251
|
switch (req.contentType) {
|
1252
|
case ContentType.NONE:
|
1253
|
break;
|
1254
|
case ContentType.JSON:
|
1255
|
_xhr.setRequestHeader('content-type', 'application/json');
|
1256
|
break;
|
1257
|
case ContentType.FORM:
|
1258
|
_xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded;charset=UTF-8');
|
1259
|
break;
|
1260
|
case ContentType.TEXT:
|
1261
|
_xhr.setRequestHeader('content-type', 'text/plain');
|
1262
|
break;
|
1263
|
case ContentType.BLOB:
|
1264
|
const /** @type {?} */ blob = req.blob();
|
1265
|
if (blob.type) {
|
1266
|
_xhr.setRequestHeader('content-type', blob.type);
|
1267
|
}
|
1268
|
break;
|
1269
|
}
|
1270
|
}
|
1271
|
}
|
1272
|
/**
|
1273
|
* `XSRFConfiguration` sets up Cross Site Request Forgery (XSRF) protection for the application
|
1274
|
* using a cookie. See https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)
|
1275
|
* for more information on XSRF.
|
1276
|
*
|
1277
|
* Applications can configure custom cookie and header names by binding an instance of this class
|
1278
|
* with different `cookieName` and `headerName` values. See the main HTTP documentation for more
|
1279
|
* details.
|
1280
|
*
|
1281
|
* \@experimental
|
1282
|
*/
|
1283
|
class CookieXSRFStrategy {
|
1284
|
/**
|
1285
|
* @param {?=} _cookieName
|
1286
|
* @param {?=} _headerName
|
1287
|
*/
|
1288
|
constructor(_cookieName = 'XSRF-TOKEN', _headerName = 'X-XSRF-TOKEN') {
|
1289
|
this._cookieName = _cookieName;
|
1290
|
this._headerName = _headerName;
|
1291
|
}
|
1292
|
/**
|
1293
|
* @param {?} req
|
1294
|
* @return {?}
|
1295
|
*/
|
1296
|
configureRequest(req) {
|
1297
|
const /** @type {?} */ xsrfToken = ɵgetDOM().getCookie(this._cookieName);
|
1298
|
if (xsrfToken) {
|
1299
|
req.headers.set(this._headerName, xsrfToken);
|
1300
|
}
|
1301
|
}
|
1302
|
}
|
1303
|
/**
|
1304
|
* Creates {\@link XHRConnection} instances.
|
1305
|
*
|
1306
|
* This class would typically not be used by end users, but could be
|
1307
|
* overridden if a different backend implementation should be used,
|
1308
|
* such as in a node backend.
|
1309
|
*
|
1310
|
* ### Example
|
1311
|
*
|
1312
|
* ```
|
1313
|
* import {Http, MyNodeBackend, HTTP_PROVIDERS, BaseRequestOptions} from '\@angular/http';
|
1314
|
* \@Component({
|
1315
|
* viewProviders: [
|
1316
|
* HTTP_PROVIDERS,
|
1317
|
* {provide: Http, useFactory: (backend, options) => {
|
1318
|
* return new Http(backend, options);
|
1319
|
* }, deps: [MyNodeBackend, BaseRequestOptions]}]
|
1320
|
* })
|
1321
|
* class MyComponent {
|
1322
|
* constructor(http:Http) {
|
1323
|
* http.request('people.json').subscribe(res => this.people = res.json());
|
1324
|
* }
|
1325
|
* }
|
1326
|
* ```
|
1327
|
* \@experimental
|
1328
|
*/
|
1329
|
class XHRBackend {
|
1330
|
/**
|
1331
|
* @param {?} _browserXHR
|
1332
|
* @param {?} _baseResponseOptions
|
1333
|
* @param {?} _xsrfStrategy
|
1334
|
*/
|
1335
|
constructor(_browserXHR, _baseResponseOptions, _xsrfStrategy) {
|
1336
|
this._browserXHR = _browserXHR;
|
1337
|
this._baseResponseOptions = _baseResponseOptions;
|
1338
|
this._xsrfStrategy = _xsrfStrategy;
|
1339
|
}
|
1340
|
/**
|
1341
|
* @param {?} request
|
1342
|
* @return {?}
|
1343
|
*/
|
1344
|
createConnection(request) {
|
1345
|
this._xsrfStrategy.configureRequest(request);
|
1346
|
return new XHRConnection(request, this._browserXHR, this._baseResponseOptions);
|
1347
|
}
|
1348
|
}
|
1349
|
XHRBackend.decorators = [
|
1350
|
{ type: Injectable },
|
1351
|
];
|
1352
|
/**
|
1353
|
* @nocollapse
|
1354
|
*/
|
1355
|
XHRBackend.ctorParameters = () => [
|
1356
|
{ type: BrowserXhr, },
|
1357
|
{ type: ResponseOptions, },
|
1358
|
{ type: XSRFStrategy, },
|
1359
|
];
|
1360
|
|
1361
|
/**
|
1362
|
* @license
|
1363
|
* Copyright Google Inc. All Rights Reserved.
|
1364
|
*
|
1365
|
* Use of this source code is governed by an MIT-style license that can be
|
1366
|
* found in the LICENSE file at https://angular.io/license
|
1367
|
*/
|
1368
|
/**
|
1369
|
* Creates a request options object to be optionally provided when instantiating a
|
1370
|
* {\@link Request}.
|
1371
|
*
|
1372
|
* This class is based on the `RequestInit` description in the [Fetch
|
1373
|
* Spec](https://fetch.spec.whatwg.org/#requestinit).
|
1374
|
*
|
1375
|
* All values are null by default. Typical defaults can be found in the {\@link BaseRequestOptions}
|
1376
|
* class, which sub-classes `RequestOptions`.
|
1377
|
*
|
1378
|
* ```typescript
|
1379
|
* import {RequestOptions, Request, RequestMethod} from '\@angular/http';
|
1380
|
*
|
1381
|
* const options = new RequestOptions({
|
1382
|
* method: RequestMethod.Post,
|
1383
|
* url: 'https://google.com'
|
1384
|
* });
|
1385
|
* const req = new Request(options);
|
1386
|
* console.log('req.method:', RequestMethod[req.method]); // Post
|
1387
|
* console.log('options.url:', options.url); // https://google.com
|
1388
|
* ```
|
1389
|
*
|
1390
|
* \@experimental
|
1391
|
*/
|
1392
|
class RequestOptions {
|
1393
|
/**
|
1394
|
* @deprecated from 4.0.0. Use params instead.
|
1395
|
* @return {?}
|
1396
|
*/
|
1397
|
get search() { return this.params; }
|
1398
|
/**
|
1399
|
* @deprecated from 4.0.0. Use params instead.
|
1400
|
* @param {?} params
|
1401
|
* @return {?}
|
1402
|
*/
|
1403
|
set search(params) { this.params = params; }
|
1404
|
/**
|
1405
|
* @param {?=} opts
|
1406
|
*/
|
1407
|
constructor(opts = {}) {
|
1408
|
const { method, headers, body, url, search, params, withCredentials, responseType } = opts;
|
1409
|
this.method = method != null ? normalizeMethodName(method) : null;
|
1410
|
this.headers = headers != null ? headers : null;
|
1411
|
this.body = body != null ? body : null;
|
1412
|
this.url = url != null ? url : null;
|
1413
|
this.params = this._mergeSearchParams(params || search);
|
1414
|
this.withCredentials = withCredentials != null ? withCredentials : null;
|
1415
|
this.responseType = responseType != null ? responseType : null;
|
1416
|
}
|
1417
|
/**
|
1418
|
* Creates a copy of the `RequestOptions` instance, using the optional input as values to override
|
1419
|
* existing values. This method will not change the values of the instance on which it is being
|
1420
|
* called.
|
1421
|
*
|
1422
|
* Note that `headers` and `search` will override existing values completely if present in
|
1423
|
* the `options` object. If these values should be merged, it should be done prior to calling
|
1424
|
* `merge` on the `RequestOptions` instance.
|
1425
|
*
|
1426
|
* ```typescript
|
1427
|
* import {RequestOptions, Request, RequestMethod} from '\@angular/http';
|
1428
|
*
|
1429
|
* const options = new RequestOptions({
|
1430
|
* method: RequestMethod.Post
|
1431
|
* });
|
1432
|
* const req = new Request(options.merge({
|
1433
|
* url: 'https://google.com'
|
1434
|
* }));
|
1435
|
* console.log('req.method:', RequestMethod[req.method]); // Post
|
1436
|
* console.log('options.url:', options.url); // null
|
1437
|
* console.log('req.url:', req.url); // https://google.com
|
1438
|
* ```
|
1439
|
* @param {?=} options
|
1440
|
* @return {?}
|
1441
|
*/
|
1442
|
merge(options) {
|
1443
|
return new RequestOptions({
|
1444
|
method: options && options.method != null ? options.method : this.method,
|
1445
|
headers: options && options.headers != null ? options.headers : new Headers(this.headers),
|
1446
|
body: options && options.body != null ? options.body : this.body,
|
1447
|
url: options && options.url != null ? options.url : this.url,
|
1448
|
params: options && this._mergeSearchParams(options.params || options.search),
|
1449
|
withCredentials: options && options.withCredentials != null ? options.withCredentials :
|
1450
|
this.withCredentials,
|
1451
|
responseType: options && options.responseType != null ? options.responseType :
|
1452
|
this.responseType
|
1453
|
});
|
1454
|
}
|
1455
|
/**
|
1456
|
* @param {?=} params
|
1457
|
* @return {?}
|
1458
|
*/
|
1459
|
_mergeSearchParams(params) {
|
1460
|
if (!params)
|
1461
|
return this.params;
|
1462
|
if (params instanceof URLSearchParams) {
|
1463
|
return params.clone();
|
1464
|
}
|
1465
|
if (typeof params === 'string') {
|
1466
|
return new URLSearchParams(params);
|
1467
|
}
|
1468
|
return this._parseParams(params);
|
1469
|
}
|
1470
|
/**
|
1471
|
* @param {?=} objParams
|
1472
|
* @return {?}
|
1473
|
*/
|
1474
|
_parseParams(objParams = {}) {
|
1475
|
const /** @type {?} */ params = new URLSearchParams();
|
1476
|
Object.keys(objParams).forEach((key) => {
|
1477
|
const /** @type {?} */ value = objParams[key];
|
1478
|
if (Array.isArray(value)) {
|
1479
|
value.forEach((item) => this._appendParam(key, item, params));
|
1480
|
}
|
1481
|
else {
|
1482
|
this._appendParam(key, value, params);
|
1483
|
}
|
1484
|
});
|
1485
|
return params;
|
1486
|
}
|
1487
|
/**
|
1488
|
* @param {?} key
|
1489
|
* @param {?} value
|
1490
|
* @param {?} params
|
1491
|
* @return {?}
|
1492
|
*/
|
1493
|
_appendParam(key, value, params) {
|
1494
|
if (typeof value !== 'string') {
|
1495
|
value = JSON.stringify(value);
|
1496
|
}
|
1497
|
params.append(key, value);
|
1498
|
}
|
1499
|
}
|
1500
|
/**
|
1501
|
* Subclass of {\@link RequestOptions}, with default values.
|
1502
|
*
|
1503
|
* Default values:
|
1504
|
* * method: {\@link RequestMethod RequestMethod.Get}
|
1505
|
* * headers: empty {\@link Headers} object
|
1506
|
*
|
1507
|
* This class could be extended and bound to the {\@link RequestOptions} class
|
1508
|
* when configuring an {\@link Injector}, in order to override the default options
|
1509
|
* used by {\@link Http} to create and send {\@link Request Requests}.
|
1510
|
*
|
1511
|
* ```typescript
|
1512
|
* import {BaseRequestOptions, RequestOptions} from '\@angular/http';
|
1513
|
*
|
1514
|
* class MyOptions extends BaseRequestOptions {
|
1515
|
* search: string = 'coreTeam=true';
|
1516
|
* }
|
1517
|
*
|
1518
|
* {provide: RequestOptions, useClass: MyOptions};
|
1519
|
* ```
|
1520
|
*
|
1521
|
* The options could also be extended when manually creating a {\@link Request}
|
1522
|
* object.
|
1523
|
*
|
1524
|
* ```
|
1525
|
* import {BaseRequestOptions, Request, RequestMethod} from '\@angular/http';
|
1526
|
*
|
1527
|
* const options = new BaseRequestOptions();
|
1528
|
* const req = new Request(options.merge({
|
1529
|
* method: RequestMethod.Post,
|
1530
|
* url: 'https://google.com'
|
1531
|
* }));
|
1532
|
* console.log('req.method:', RequestMethod[req.method]); // Post
|
1533
|
* console.log('options.url:', options.url); // null
|
1534
|
* console.log('req.url:', req.url); // https://google.com
|
1535
|
* ```
|
1536
|
*
|
1537
|
* \@experimental
|
1538
|
*/
|
1539
|
class BaseRequestOptions extends RequestOptions {
|
1540
|
constructor() { super({ method: RequestMethod.Get, headers: new Headers() }); }
|
1541
|
}
|
1542
|
BaseRequestOptions.decorators = [
|
1543
|
{ type: Injectable },
|
1544
|
];
|
1545
|
/**
|
1546
|
* @nocollapse
|
1547
|
*/
|
1548
|
BaseRequestOptions.ctorParameters = () => [];
|
1549
|
|
1550
|
/**
|
1551
|
* @license
|
1552
|
* Copyright Google Inc. All Rights Reserved.
|
1553
|
*
|
1554
|
* Use of this source code is governed by an MIT-style license that can be
|
1555
|
* found in the LICENSE file at https://angular.io/license
|
1556
|
*/
|
1557
|
/**
|
1558
|
* Creates `Request` instances from provided values.
|
1559
|
*
|
1560
|
* The Request's interface is inspired by the Request constructor defined in the [Fetch
|
1561
|
* Spec](https://fetch.spec.whatwg.org/#request-class),
|
1562
|
* but is considered a static value whose body can be accessed many times. There are other
|
1563
|
* differences in the implementation, but this is the most significant.
|
1564
|
*
|
1565
|
* `Request` instances are typically created by higher-level classes, like {\@link Http} and
|
1566
|
* {\@link Jsonp}, but it may occasionally be useful to explicitly create `Request` instances.
|
1567
|
* One such example is when creating services that wrap higher-level services, like {\@link Http},
|
1568
|
* where it may be useful to generate a `Request` with arbitrary headers and search params.
|
1569
|
*
|
1570
|
* ```typescript
|
1571
|
* import {Injectable, Injector} from '\@angular/core';
|
1572
|
* import {HTTP_PROVIDERS, Http, Request, RequestMethod} from '\@angular/http';
|
1573
|
*
|
1574
|
* \@Injectable()
|
1575
|
* class AutoAuthenticator {
|
1576
|
* constructor(public http:Http) {}
|
1577
|
* request(url:string) {
|
1578
|
* return this.http.request(new Request({
|
1579
|
* method: RequestMethod.Get,
|
1580
|
* url: url,
|
1581
|
* search: 'password=123'
|
1582
|
* }));
|
1583
|
* }
|
1584
|
* }
|
1585
|
*
|
1586
|
* var injector = Injector.resolveAndCreate([HTTP_PROVIDERS, AutoAuthenticator]);
|
1587
|
* var authenticator = injector.get(AutoAuthenticator);
|
1588
|
* authenticator.request('people.json').subscribe(res => {
|
1589
|
* //URL should have included '?password=123'
|
1590
|
* console.log('people', res.json());
|
1591
|
* });
|
1592
|
* ```
|
1593
|
*
|
1594
|
* \@experimental
|
1595
|
*/
|
1596
|
class Request extends Body {
|
1597
|
/**
|
1598
|
* @param {?} requestOptions
|
1599
|
*/
|
1600
|
constructor(requestOptions) {
|
1601
|
super();
|
1602
|
// TODO: assert that url is present
|
1603
|
const url = requestOptions.url;
|
1604
|
this.url = requestOptions.url;
|
1605
|
const paramsArg = requestOptions.params || requestOptions.search;
|
1606
|
if (paramsArg) {
|
1607
|
let params;
|
1608
|
if (typeof paramsArg === 'object' && !(paramsArg instanceof URLSearchParams)) {
|
1609
|
params = urlEncodeParams(paramsArg).toString();
|
1610
|
}
|
1611
|
else {
|
1612
|
params = paramsArg.toString();
|
1613
|
}
|
1614
|
if (params.length > 0) {
|
1615
|
let prefix = '?';
|
1616
|
if (this.url.indexOf('?') != -1) {
|
1617
|
prefix = (this.url[this.url.length - 1] == '&') ? '' : '&';
|
1618
|
}
|
1619
|
// TODO: just delete search-query-looking string in url?
|
1620
|
this.url = url + prefix + params;
|
1621
|
}
|
1622
|
}
|
1623
|
this._body = requestOptions.body;
|
1624
|
this.method = normalizeMethodName(requestOptions.method);
|
1625
|
// TODO(jeffbcross): implement behavior
|
1626
|
// Defaults to 'omit', consistent with browser
|
1627
|
this.headers = new Headers(requestOptions.headers);
|
1628
|
this.contentType = this.detectContentType();
|
1629
|
this.withCredentials = requestOptions.withCredentials;
|
1630
|
this.responseType = requestOptions.responseType;
|
1631
|
}
|
1632
|
/**
|
1633
|
* Returns the content type enum based on header options.
|
1634
|
* @return {?}
|
1635
|
*/
|
1636
|
detectContentType() {
|
1637
|
switch (this.headers.get('content-type')) {
|
1638
|
case 'application/json':
|
1639
|
return ContentType.JSON;
|
1640
|
case 'application/x-www-form-urlencoded':
|
1641
|
return ContentType.FORM;
|
1642
|
case 'multipart/form-data':
|
1643
|
return ContentType.FORM_DATA;
|
1644
|
case 'text/plain':
|
1645
|
case 'text/html':
|
1646
|
return ContentType.TEXT;
|
1647
|
case 'application/octet-stream':
|
1648
|
return this._body instanceof ArrayBuffer$1 ? ContentType.ARRAY_BUFFER : ContentType.BLOB;
|
1649
|
default:
|
1650
|
return this.detectContentTypeFromBody();
|
1651
|
}
|
1652
|
}
|
1653
|
/**
|
1654
|
* Returns the content type of request's body based on its type.
|
1655
|
* @return {?}
|
1656
|
*/
|
1657
|
detectContentTypeFromBody() {
|
1658
|
if (this._body == null) {
|
1659
|
return ContentType.NONE;
|
1660
|
}
|
1661
|
else if (this._body instanceof URLSearchParams) {
|
1662
|
return ContentType.FORM;
|
1663
|
}
|
1664
|
else if (this._body instanceof FormData) {
|
1665
|
return ContentType.FORM_DATA;
|
1666
|
}
|
1667
|
else if (this._body instanceof Blob$1) {
|
1668
|
return ContentType.BLOB;
|
1669
|
}
|
1670
|
else if (this._body instanceof ArrayBuffer$1) {
|
1671
|
return ContentType.ARRAY_BUFFER;
|
1672
|
}
|
1673
|
else if (this._body && typeof this._body === 'object') {
|
1674
|
return ContentType.JSON;
|
1675
|
}
|
1676
|
else {
|
1677
|
return ContentType.TEXT;
|
1678
|
}
|
1679
|
}
|
1680
|
/**
|
1681
|
* Returns the request's body according to its type. If body is undefined, return
|
1682
|
* null.
|
1683
|
* @return {?}
|
1684
|
*/
|
1685
|
getBody() {
|
1686
|
switch (this.contentType) {
|
1687
|
case ContentType.JSON:
|
1688
|
return this.text();
|
1689
|
case ContentType.FORM:
|
1690
|
return this.text();
|
1691
|
case ContentType.FORM_DATA:
|
1692
|
return this._body;
|
1693
|
case ContentType.TEXT:
|
1694
|
return this.text();
|
1695
|
case ContentType.BLOB:
|
1696
|
return this.blob();
|
1697
|
case ContentType.ARRAY_BUFFER:
|
1698
|
return this.arrayBuffer();
|
1699
|
default:
|
1700
|
return null;
|
1701
|
}
|
1702
|
}
|
1703
|
}
|
1704
|
/**
|
1705
|
* @param {?} params
|
1706
|
* @return {?}
|
1707
|
*/
|
1708
|
function urlEncodeParams(params) {
|
1709
|
const /** @type {?} */ searchParams = new URLSearchParams();
|
1710
|
Object.keys(params).forEach(key => {
|
1711
|
const /** @type {?} */ value = params[key];
|
1712
|
if (value && Array.isArray(value)) {
|
1713
|
value.forEach(element => searchParams.append(key, element.toString()));
|
1714
|
}
|
1715
|
else {
|
1716
|
searchParams.append(key, value.toString());
|
1717
|
}
|
1718
|
});
|
1719
|
return searchParams;
|
1720
|
}
|
1721
|
const noop = function () { };
|
1722
|
const w = typeof window == 'object' ? window : noop;
|
1723
|
const FormData = ((w) /** TODO #9100 */)['FormData'] || noop;
|
1724
|
const Blob$1 = ((w) /** TODO #9100 */)['Blob'] || noop;
|
1725
|
const ArrayBuffer$1 = ((w) /** TODO #9100 */)['ArrayBuffer'] || noop;
|
1726
|
|
1727
|
/**
|
1728
|
* @license
|
1729
|
* Copyright Google Inc. All Rights Reserved.
|
1730
|
*
|
1731
|
* Use of this source code is governed by an MIT-style license that can be
|
1732
|
* found in the LICENSE file at https://angular.io/license
|
1733
|
*/
|
1734
|
/**
|
1735
|
* @param {?} backend
|
1736
|
* @param {?} request
|
1737
|
* @return {?}
|
1738
|
*/
|
1739
|
function httpRequest(backend, request) {
|
1740
|
return backend.createConnection(request).response;
|
1741
|
}
|
1742
|
/**
|
1743
|
* @param {?} defaultOpts
|
1744
|
* @param {?} providedOpts
|
1745
|
* @param {?} method
|
1746
|
* @param {?} url
|
1747
|
* @return {?}
|
1748
|
*/
|
1749
|
function mergeOptions(defaultOpts, providedOpts, method, url) {
|
1750
|
const /** @type {?} */ newOptions = defaultOpts;
|
1751
|
if (providedOpts) {
|
1752
|
// Hack so Dart can used named parameters
|
1753
|
return (newOptions.merge(new RequestOptions({
|
1754
|
method: providedOpts.method || method,
|
1755
|
url: providedOpts.url || url,
|
1756
|
search: providedOpts.search,
|
1757
|
params: providedOpts.params,
|
1758
|
headers: providedOpts.headers,
|
1759
|
body: providedOpts.body,
|
1760
|
withCredentials: providedOpts.withCredentials,
|
1761
|
responseType: providedOpts.responseType
|
1762
|
})));
|
1763
|
}
|
1764
|
return (newOptions.merge(new RequestOptions({ method, url })));
|
1765
|
}
|
1766
|
/**
|
1767
|
* Performs http requests using `XMLHttpRequest` as the default backend.
|
1768
|
*
|
1769
|
* `Http` is available as an injectable class, with methods to perform http requests. Calling
|
1770
|
* `request` returns an `Observable` which will emit a single {\@link Response} when a
|
1771
|
* response is received.
|
1772
|
*
|
1773
|
* ### Example
|
1774
|
*
|
1775
|
* ```typescript
|
1776
|
* import {Http, HTTP_PROVIDERS} from '\@angular/http';
|
1777
|
* import 'rxjs/add/operator/map'
|
1778
|
* \@Component({
|
1779
|
* selector: 'http-app',
|
1780
|
* viewProviders: [HTTP_PROVIDERS],
|
1781
|
* templateUrl: 'people.html'
|
1782
|
* })
|
1783
|
* class PeopleComponent {
|
1784
|
* constructor(http: Http) {
|
1785
|
* http.get('people.json')
|
1786
|
* // Call map on the response observable to get the parsed people object
|
1787
|
* .map(res => res.json())
|
1788
|
* // Subscribe to the observable to get the parsed people object and attach it to the
|
1789
|
* // component
|
1790
|
* .subscribe(people => this.people = people);
|
1791
|
* }
|
1792
|
* }
|
1793
|
* ```
|
1794
|
*
|
1795
|
*
|
1796
|
* ### Example
|
1797
|
*
|
1798
|
* ```
|
1799
|
* http.get('people.json').subscribe((res:Response) => this.people = res.json());
|
1800
|
* ```
|
1801
|
*
|
1802
|
* The default construct used to perform requests, `XMLHttpRequest`, is abstracted as a "Backend" (
|
1803
|
* {\@link XHRBackend} in this case), which could be mocked with dependency injection by replacing
|
1804
|
* the {\@link XHRBackend} provider, as in the following example:
|
1805
|
*
|
1806
|
* ### Example
|
1807
|
*
|
1808
|
* ```typescript
|
1809
|
* import {BaseRequestOptions, Http} from '\@angular/http';
|
1810
|
* import {MockBackend} from '\@angular/http/testing';
|
1811
|
* var injector = Injector.resolveAndCreate([
|
1812
|
* BaseRequestOptions,
|
1813
|
* MockBackend,
|
1814
|
* {provide: Http, useFactory:
|
1815
|
* function(backend, defaultOptions) {
|
1816
|
* return new Http(backend, defaultOptions);
|
1817
|
* },
|
1818
|
* deps: [MockBackend, BaseRequestOptions]}
|
1819
|
* ]);
|
1820
|
* var http = injector.get(Http);
|
1821
|
* http.get('request-from-mock-backend.json').subscribe((res:Response) => doSomething(res));
|
1822
|
* ```
|
1823
|
*
|
1824
|
* \@experimental
|
1825
|
*/
|
1826
|
class Http {
|
1827
|
/**
|
1828
|
* @param {?} _backend
|
1829
|
* @param {?} _defaultOptions
|
1830
|
*/
|
1831
|
constructor(_backend, _defaultOptions) {
|
1832
|
this._backend = _backend;
|
1833
|
this._defaultOptions = _defaultOptions;
|
1834
|
}
|
1835
|
/**
|
1836
|
* Performs any type of http request. First argument is required, and can either be a url or
|
1837
|
* a {\@link Request} instance. If the first argument is a url, an optional {\@link RequestOptions}
|
1838
|
* object can be provided as the 2nd argument. The options object will be merged with the values
|
1839
|
* of {\@link BaseRequestOptions} before performing the request.
|
1840
|
* @param {?} url
|
1841
|
* @param {?=} options
|
1842
|
* @return {?}
|
1843
|
*/
|
1844
|
request(url, options) {
|
1845
|
let /** @type {?} */ responseObservable;
|
1846
|
if (typeof url === 'string') {
|
1847
|
responseObservable = httpRequest(this._backend, new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, /** @type {?} */ (url))));
|
1848
|
}
|
1849
|
else if (url instanceof Request) {
|
1850
|
responseObservable = httpRequest(this._backend, url);
|
1851
|
}
|
1852
|
else {
|
1853
|
throw new Error('First argument must be a url string or Request instance.');
|
1854
|
}
|
1855
|
return responseObservable;
|
1856
|
}
|
1857
|
/**
|
1858
|
* Performs a request with `get` http method.
|
1859
|
* @param {?} url
|
1860
|
* @param {?=} options
|
1861
|
* @return {?}
|
1862
|
*/
|
1863
|
get(url, options) {
|
1864
|
return this.request(new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, url)));
|
1865
|
}
|
1866
|
/**
|
1867
|
* Performs a request with `post` http method.
|
1868
|
* @param {?} url
|
1869
|
* @param {?} body
|
1870
|
* @param {?=} options
|
1871
|
* @return {?}
|
1872
|
*/
|
1873
|
post(url, body, options) {
|
1874
|
return this.request(new Request(mergeOptions(this._defaultOptions.merge(new RequestOptions({ body: body })), options, RequestMethod.Post, url)));
|
1875
|
}
|
1876
|
/**
|
1877
|
* Performs a request with `put` http method.
|
1878
|
* @param {?} url
|
1879
|
* @param {?} body
|
1880
|
* @param {?=} options
|
1881
|
* @return {?}
|
1882
|
*/
|
1883
|
put(url, body, options) {
|
1884
|
return this.request(new Request(mergeOptions(this._defaultOptions.merge(new RequestOptions({ body: body })), options, RequestMethod.Put, url)));
|
1885
|
}
|
1886
|
/**
|
1887
|
* Performs a request with `delete` http method.
|
1888
|
* @param {?} url
|
1889
|
* @param {?=} options
|
1890
|
* @return {?}
|
1891
|
*/
|
1892
|
delete(url, options) {
|
1893
|
return this.request(new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Delete, url)));
|
1894
|
}
|
1895
|
/**
|
1896
|
* Performs a request with `patch` http method.
|
1897
|
* @param {?} url
|
1898
|
* @param {?} body
|
1899
|
* @param {?=} options
|
1900
|
* @return {?}
|
1901
|
*/
|
1902
|
patch(url, body, options) {
|
1903
|
return this.request(new Request(mergeOptions(this._defaultOptions.merge(new RequestOptions({ body: body })), options, RequestMethod.Patch, url)));
|
1904
|
}
|
1905
|
/**
|
1906
|
* Performs a request with `head` http method.
|
1907
|
* @param {?} url
|
1908
|
* @param {?=} options
|
1909
|
* @return {?}
|
1910
|
*/
|
1911
|
head(url, options) {
|
1912
|
return this.request(new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Head, url)));
|
1913
|
}
|
1914
|
/**
|
1915
|
* Performs a request with `options` http method.
|
1916
|
* @param {?} url
|
1917
|
* @param {?=} options
|
1918
|
* @return {?}
|
1919
|
*/
|
1920
|
options(url, options) {
|
1921
|
return this.request(new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Options, url)));
|
1922
|
}
|
1923
|
}
|
1924
|
Http.decorators = [
|
1925
|
{ type: Injectable },
|
1926
|
];
|
1927
|
/**
|
1928
|
* @nocollapse
|
1929
|
*/
|
1930
|
Http.ctorParameters = () => [
|
1931
|
{ type: ConnectionBackend, },
|
1932
|
{ type: RequestOptions, },
|
1933
|
];
|
1934
|
/**
|
1935
|
* \@experimental
|
1936
|
*/
|
1937
|
class Jsonp extends Http {
|
1938
|
/**
|
1939
|
* @param {?} backend
|
1940
|
* @param {?} defaultOptions
|
1941
|
*/
|
1942
|
constructor(backend, defaultOptions) {
|
1943
|
super(backend, defaultOptions);
|
1944
|
}
|
1945
|
/**
|
1946
|
* Performs any type of http request. First argument is required, and can either be a url or
|
1947
|
* a {\@link Request} instance. If the first argument is a url, an optional {\@link RequestOptions}
|
1948
|
* object can be provided as the 2nd argument. The options object will be merged with the values
|
1949
|
* of {\@link BaseRequestOptions} before performing the request.
|
1950
|
*
|
1951
|
* \@security Regular XHR is the safest alternative to JSONP for most applications, and is
|
1952
|
* supported by all current browsers. Because JSONP creates a `<script>` element with
|
1953
|
* contents retrieved from a remote source, attacker-controlled data introduced by an untrusted
|
1954
|
* source could expose your application to XSS risks. Data exposed by JSONP may also be
|
1955
|
* readable by malicious third-party websites. In addition, JSONP introduces potential risk for
|
1956
|
* future security issues (e.g. content sniffing). For more detail, see the
|
1957
|
* [Security Guide](http://g.co/ng/security).
|
1958
|
* @param {?} url
|
1959
|
* @param {?=} options
|
1960
|
* @return {?}
|
1961
|
*/
|
1962
|
request(url, options) {
|
1963
|
let /** @type {?} */ responseObservable;
|
1964
|
if (typeof url === 'string') {
|
1965
|
url =
|
1966
|
new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, /** @type {?} */ (url)));
|
1967
|
}
|
1968
|
if (url instanceof Request) {
|
1969
|
if (url.method !== RequestMethod.Get) {
|
1970
|
throw new Error('JSONP requests must use GET request method.');
|
1971
|
}
|
1972
|
responseObservable = httpRequest(this._backend, url);
|
1973
|
}
|
1974
|
else {
|
1975
|
throw new Error('First argument must be a url string or Request instance.');
|
1976
|
}
|
1977
|
return responseObservable;
|
1978
|
}
|
1979
|
}
|
1980
|
Jsonp.decorators = [
|
1981
|
{ type: Injectable },
|
1982
|
];
|
1983
|
/**
|
1984
|
* @nocollapse
|
1985
|
*/
|
1986
|
Jsonp.ctorParameters = () => [
|
1987
|
{ type: ConnectionBackend, },
|
1988
|
{ type: RequestOptions, },
|
1989
|
];
|
1990
|
|
1991
|
/**
|
1992
|
* @license
|
1993
|
* Copyright Google Inc. All Rights Reserved.
|
1994
|
*
|
1995
|
* Use of this source code is governed by an MIT-style license that can be
|
1996
|
* found in the LICENSE file at https://angular.io/license
|
1997
|
*/
|
1998
|
/**
|
1999
|
* @module
|
2000
|
* @description
|
2001
|
* The http module provides services to perform http requests. To get started, see the {@link Http}
|
2002
|
* class.
|
2003
|
*/
|
2004
|
/**
|
2005
|
* @return {?}
|
2006
|
*/
|
2007
|
function _createDefaultCookieXSRFStrategy() {
|
2008
|
return new CookieXSRFStrategy();
|
2009
|
}
|
2010
|
/**
|
2011
|
* @param {?} xhrBackend
|
2012
|
* @param {?} requestOptions
|
2013
|
* @return {?}
|
2014
|
*/
|
2015
|
function httpFactory(xhrBackend, requestOptions) {
|
2016
|
return new Http(xhrBackend, requestOptions);
|
2017
|
}
|
2018
|
/**
|
2019
|
* @param {?} jsonpBackend
|
2020
|
* @param {?} requestOptions
|
2021
|
* @return {?}
|
2022
|
*/
|
2023
|
function jsonpFactory(jsonpBackend, requestOptions) {
|
2024
|
return new Jsonp(jsonpBackend, requestOptions);
|
2025
|
}
|
2026
|
/**
|
2027
|
* The module that includes http's providers
|
2028
|
*
|
2029
|
* \@experimental
|
2030
|
*/
|
2031
|
class HttpModule {
|
2032
|
}
|
2033
|
HttpModule.decorators = [
|
2034
|
{ type: NgModule, args: [{
|
2035
|
providers: [
|
2036
|
// TODO(pascal): use factory type annotations once supported in DI
|
2037
|
// issue: https://github.com/angular/angular/issues/3183
|
2038
|
{ provide: Http, useFactory: httpFactory, deps: [XHRBackend, RequestOptions] },
|
2039
|
BrowserXhr,
|
2040
|
{ provide: RequestOptions, useClass: BaseRequestOptions },
|
2041
|
{ provide: ResponseOptions, useClass: BaseResponseOptions },
|
2042
|
XHRBackend,
|
2043
|
{ provide: XSRFStrategy, useFactory: _createDefaultCookieXSRFStrategy },
|
2044
|
],
|
2045
|
},] },
|
2046
|
];
|
2047
|
/**
|
2048
|
* @nocollapse
|
2049
|
*/
|
2050
|
HttpModule.ctorParameters = () => [];
|
2051
|
/**
|
2052
|
* The module that includes jsonp's providers
|
2053
|
*
|
2054
|
* \@experimental
|
2055
|
*/
|
2056
|
class JsonpModule {
|
2057
|
}
|
2058
|
JsonpModule.decorators = [
|
2059
|
{ type: NgModule, args: [{
|
2060
|
providers: [
|
2061
|
// TODO(pascal): use factory type annotations once supported in DI
|
2062
|
// issue: https://github.com/angular/angular/issues/3183
|
2063
|
{ provide: Jsonp, useFactory: jsonpFactory, deps: [JSONPBackend, RequestOptions] },
|
2064
|
BrowserJsonp,
|
2065
|
{ provide: RequestOptions, useClass: BaseRequestOptions },
|
2066
|
{ provide: ResponseOptions, useClass: BaseResponseOptions },
|
2067
|
{ provide: JSONPBackend, useClass: JSONPBackend_ },
|
2068
|
],
|
2069
|
},] },
|
2070
|
];
|
2071
|
/**
|
2072
|
* @nocollapse
|
2073
|
*/
|
2074
|
JsonpModule.ctorParameters = () => [];
|
2075
|
|
2076
|
/**
|
2077
|
* @license
|
2078
|
* Copyright Google Inc. All Rights Reserved.
|
2079
|
*
|
2080
|
* Use of this source code is governed by an MIT-style license that can be
|
2081
|
* found in the LICENSE file at https://angular.io/license
|
2082
|
*/
|
2083
|
/**
|
2084
|
* @module
|
2085
|
* @description
|
2086
|
* Entry point for all public APIs of the common package.
|
2087
|
*/
|
2088
|
/**
|
2089
|
* \@stable
|
2090
|
*/
|
2091
|
const VERSION = new Version('4.4.6');
|
2092
|
|
2093
|
/**
|
2094
|
* @license
|
2095
|
* Copyright Google Inc. All Rights Reserved.
|
2096
|
*
|
2097
|
* Use of this source code is governed by an MIT-style license that can be
|
2098
|
* found in the LICENSE file at https://angular.io/license
|
2099
|
*/
|
2100
|
|
2101
|
/**
|
2102
|
* @license
|
2103
|
* Copyright Google Inc. All Rights Reserved.
|
2104
|
*
|
2105
|
* Use of this source code is governed by an MIT-style license that can be
|
2106
|
* found in the LICENSE file at https://angular.io/license
|
2107
|
*/
|
2108
|
/**
|
2109
|
* @module
|
2110
|
* @description
|
2111
|
* Entry point for all public APIs of the http package.
|
2112
|
*/
|
2113
|
|
2114
|
// This file only reexports content of the `src` folder. Keep it that way.
|
2115
|
|
2116
|
/**
|
2117
|
* Generated bundle index. Do not edit.
|
2118
|
*/
|
2119
|
|
2120
|
export { BrowserXhr, JSONPBackend, JSONPConnection, CookieXSRFStrategy, XHRBackend, XHRConnection, BaseRequestOptions, RequestOptions, BaseResponseOptions, ResponseOptions, ReadyState, RequestMethod, ResponseContentType, ResponseType, Headers, Http, Jsonp, HttpModule, JsonpModule, Connection, ConnectionBackend, XSRFStrategy, Request, Response, QueryEncoder, URLSearchParams, VERSION, BrowserJsonp as ɵg, JSONPBackend_ as ɵa, Body as ɵf, _createDefaultCookieXSRFStrategy as ɵb, httpFactory as ɵc, jsonpFactory as ɵd };
|
2121
|
//# sourceMappingURL=http.js.map
|