http.js
9.99 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/**
* @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
*/
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
import { Injectable } from '@angular/core';
import { RequestOptions } from './base_request_options';
import { RequestMethod } from './enums';
import { ConnectionBackend } from './interfaces';
import { Request } from './static_request';
/**
* @param {?} backend
* @param {?} request
* @return {?}
*/
function httpRequest(backend, request) {
return backend.createConnection(request).response;
}
/**
* @param {?} defaultOpts
* @param {?} providedOpts
* @param {?} method
* @param {?} url
* @return {?}
*/
function mergeOptions(defaultOpts, providedOpts, method, url) {
var /** @type {?} */ newOptions = defaultOpts;
if (providedOpts) {
// Hack so Dart can used named parameters
return newOptions.merge(new RequestOptions({
method: providedOpts.method || method,
url: providedOpts.url || url,
search: providedOpts.search,
headers: providedOpts.headers,
body: providedOpts.body,
withCredentials: providedOpts.withCredentials,
responseType: providedOpts.responseType
}));
}
return newOptions.merge(new RequestOptions({ method: method, url: url }));
}
/**
* Performs http requests using `XMLHttpRequest` as the default backend.
*
* `Http` is available as an injectable class, with methods to perform http requests. Calling
* `request` returns an `Observable` which will emit a single {\@link Response} when a
* response is received.
*
* ### Example
*
* ```typescript
* import {Http, HTTP_PROVIDERS} from '\@angular/http';
* import 'rxjs/add/operator/map'
* \@Component({
* selector: 'http-app',
* viewProviders: [HTTP_PROVIDERS],
* templateUrl: 'people.html'
* })
* class PeopleComponent {
* constructor(http: Http) {
* http.get('people.json')
* // Call map on the response observable to get the parsed people object
* .map(res => res.json())
* // Subscribe to the observable to get the parsed people object and attach it to the
* // component
* .subscribe(people => this.people = people);
* }
* }
* ```
*
*
* ### Example
*
* ```
* http.get('people.json').subscribe((res:Response) => this.people = res.json());
* ```
*
* The default construct used to perform requests, `XMLHttpRequest`, is abstracted as a "Backend" (
* {\@link XHRBackend} in this case), which could be mocked with dependency injection by replacing
* the {\@link XHRBackend} provider, as in the following example:
*
* ### Example
*
* ```typescript
* import {BaseRequestOptions, Http} from '\@angular/http';
* import {MockBackend} from '\@angular/http/testing';
* var injector = Injector.resolveAndCreate([
* BaseRequestOptions,
* MockBackend,
* {provide: Http, useFactory:
* function(backend, defaultOptions) {
* return new Http(backend, defaultOptions);
* },
* deps: [MockBackend, BaseRequestOptions]}
* ]);
* var http = injector.get(Http);
* http.get('request-from-mock-backend.json').subscribe((res:Response) => doSomething(res));
* ```
*
* \@experimental
*/
export var Http = (function () {
/**
* @param {?} _backend
* @param {?} _defaultOptions
*/
function Http(_backend, _defaultOptions) {
this._backend = _backend;
this._defaultOptions = _defaultOptions;
}
/**
* Performs any type of http request. First argument is required, and can either be a url or
* a {\@link Request} instance. If the first argument is a url, an optional {\@link RequestOptions}
* object can be provided as the 2nd argument. The options object will be merged with the values
* of {\@link BaseRequestOptions} before performing the request.
* @param {?} url
* @param {?=} options
* @return {?}
*/
Http.prototype.request = function (url, options) {
var /** @type {?} */ responseObservable;
if (typeof url === 'string') {
responseObservable = httpRequest(this._backend, new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, /** @type {?} */ (url))));
}
else if (url instanceof Request) {
responseObservable = httpRequest(this._backend, url);
}
else {
throw new Error('First argument must be a url string or Request instance.');
}
return responseObservable;
};
/**
* Performs a request with `get` http method.
* @param {?} url
* @param {?=} options
* @return {?}
*/
Http.prototype.get = function (url, options) {
return this.request(new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, url)));
};
/**
* Performs a request with `post` http method.
* @param {?} url
* @param {?} body
* @param {?=} options
* @return {?}
*/
Http.prototype.post = function (url, body, options) {
return this.request(new Request(mergeOptions(this._defaultOptions.merge(new RequestOptions({ body: body })), options, RequestMethod.Post, url)));
};
/**
* Performs a request with `put` http method.
* @param {?} url
* @param {?} body
* @param {?=} options
* @return {?}
*/
Http.prototype.put = function (url, body, options) {
return this.request(new Request(mergeOptions(this._defaultOptions.merge(new RequestOptions({ body: body })), options, RequestMethod.Put, url)));
};
/**
* Performs a request with `delete` http method.
* @param {?} url
* @param {?=} options
* @return {?}
*/
Http.prototype.delete = function (url, options) {
return this.request(new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Delete, url)));
};
/**
* Performs a request with `patch` http method.
* @param {?} url
* @param {?} body
* @param {?=} options
* @return {?}
*/
Http.prototype.patch = function (url, body, options) {
return this.request(new Request(mergeOptions(this._defaultOptions.merge(new RequestOptions({ body: body })), options, RequestMethod.Patch, url)));
};
/**
* Performs a request with `head` http method.
* @param {?} url
* @param {?=} options
* @return {?}
*/
Http.prototype.head = function (url, options) {
return this.request(new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Head, url)));
};
/**
* Performs a request with `options` http method.
* @param {?} url
* @param {?=} options
* @return {?}
*/
Http.prototype.options = function (url, options) {
return this.request(new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Options, url)));
};
Http.decorators = [
{ type: Injectable },
];
/** @nocollapse */
Http.ctorParameters = function () { return [
{ type: ConnectionBackend, },
{ type: RequestOptions, },
]; };
return Http;
}());
function Http_tsickle_Closure_declarations() {
/** @type {?} */
Http.decorators;
/**
* @nocollapse
* @type {?}
*/
Http.ctorParameters;
/** @type {?} */
Http.prototype._backend;
/** @type {?} */
Http.prototype._defaultOptions;
}
/**
* \@experimental
*/
export var Jsonp = (function (_super) {
__extends(Jsonp, _super);
/**
* @param {?} backend
* @param {?} defaultOptions
*/
function Jsonp(backend, defaultOptions) {
_super.call(this, backend, defaultOptions);
}
/**
* Performs any type of http request. First argument is required, and can either be a url or
* a {\@link Request} instance. If the first argument is a url, an optional {\@link RequestOptions}
* object can be provided as the 2nd argument. The options object will be merged with the values
* of {\@link BaseRequestOptions} before performing the request.
*
* \@security Regular XHR is the safest alternative to JSONP for most applications, and is
* supported by all current browsers. Because JSONP creates a `<script>` element with
* contents retrieved from a remote source, attacker-controlled data introduced by an untrusted
* source could expose your application to XSS risks. Data exposed by JSONP may also be
* readable by malicious third-party websites. In addition, JSONP introduces potential risk for
* future security issues (e.g. content sniffing). For more detail, see the
* [Security Guide](http://g.co/ng/security).
* @param {?} url
* @param {?=} options
* @return {?}
*/
Jsonp.prototype.request = function (url, options) {
var /** @type {?} */ responseObservable;
if (typeof url === 'string') {
url =
new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, /** @type {?} */ (url)));
}
if (url instanceof Request) {
if (url.method !== RequestMethod.Get) {
throw new Error('JSONP requests must use GET request method.');
}
responseObservable = httpRequest(this._backend, url);
}
else {
throw new Error('First argument must be a url string or Request instance.');
}
return responseObservable;
};
Jsonp.decorators = [
{ type: Injectable },
];
/** @nocollapse */
Jsonp.ctorParameters = function () { return [
{ type: ConnectionBackend, },
{ type: RequestOptions, },
]; };
return Jsonp;
}(Http));
function Jsonp_tsickle_Closure_declarations() {
/** @type {?} */
Jsonp.decorators;
/**
* @nocollapse
* @type {?}
*/
Jsonp.ctorParameters;
}
//# sourceMappingURL=http.js.map