1 | /**
|
2 | * @module
|
3 | * @description
|
4 | * The http module provides services to perform http requests. To get started, see the {@link Http}
|
5 | * class.
|
6 | */
|
7 | import {provide, Provider} from 'angular2/core';
|
8 | import {Http, Jsonp} from './src/http/http';
|
9 | import {XHRBackend, XHRConnection} from './src/http/backends/xhr_backend';
|
10 | import {JSONPBackend, JSONPBackend_, JSONPConnection} from './src/http/backends/jsonp_backend';
|
11 | import {BrowserXhr} from './src/http/backends/browser_xhr';
|
12 | import {BrowserJsonp} from './src/http/backends/browser_jsonp';
|
13 | import {BaseRequestOptions, RequestOptions} from './src/http/base_request_options';
|
14 | import {ConnectionBackend} from './src/http/interfaces';
|
15 | import {BaseResponseOptions, ResponseOptions} from './src/http/base_response_options';
|
16 | export {Request} from './src/http/static_request';
|
17 | export {Response} from './src/http/static_response';
|
18 |
|
19 | export {
|
20 | RequestOptionsArgs,
|
21 | ResponseOptionsArgs,
|
22 | Connection,
|
23 | ConnectionBackend
|
24 | } from './src/http/interfaces';
|
25 |
|
26 | export {BrowserXhr} from './src/http/backends/browser_xhr';
|
27 | export {BaseRequestOptions, RequestOptions} from './src/http/base_request_options';
|
28 | export {BaseResponseOptions, ResponseOptions} from './src/http/base_response_options';
|
29 | export {XHRBackend, XHRConnection} from './src/http/backends/xhr_backend';
|
30 | export {JSONPBackend, JSONPConnection} from './src/http/backends/jsonp_backend';
|
31 | export {Http, Jsonp} from './src/http/http';
|
32 |
|
33 | export {Headers} from './src/http/headers';
|
34 |
|
35 | export {ResponseType, ReadyState, RequestMethod} from './src/http/enums';
|
36 | export {URLSearchParams} from './src/http/url_search_params';
|
37 |
|
38 | /**
|
39 | * Provides a basic set of injectables to use the {@link Http} service in any application.
|
40 | *
|
41 | * The `HTTP_PROVIDERS` should be included either in a component's injector,
|
42 | * or in the root injector when bootstrapping an application.
|
43 | *
|
44 | * ### Example ([live demo](http://plnkr.co/edit/snj7Nv?p=preview))
|
45 | *
|
46 | * ```
|
47 | * import {Component} from 'angular2/core';
|
48 | * import {bootstrap} from 'angular2/platform/browser';
|
49 | * import {NgFor} from 'angular2/common';
|
50 | * import {HTTP_PROVIDERS, Http} from 'angular2/http';
|
51 | *
|
52 | * @Component({
|
53 | * selector: 'app',
|
54 | * providers: [HTTP_PROVIDERS],
|
55 | * template: `
|
56 | * <div>
|
57 | * <h1>People</h1>
|
58 | * <ul>
|
59 | * <li *ngFor="let person of people">
|
60 | * {{person.name}}
|
61 | * </li>
|
62 | * </ul>
|
63 | * </div>
|
64 | * `,
|
65 | * directives: [NgFor]
|
66 | * })
|
67 | * export class App {
|
68 | * people: Object[];
|
69 | * constructor(http:Http) {
|
70 | * http.get('people.json').subscribe(res => {
|
71 | * this.people = res.json();
|
72 | * });
|
73 | * }
|
74 | * active:boolean = false;
|
75 | * toggleActiveState() {
|
76 | * this.active = !this.active;
|
77 | * }
|
78 | * }
|
79 | *
|
80 | * bootstrap(App)
|
81 | * .catch(err => console.error(err));
|
82 | * ```
|
83 | *
|
84 | * The primary public API included in `HTTP_PROVIDERS` is the {@link Http} class.
|
85 | * However, other providers required by `Http` are included,
|
86 | * which may be beneficial to override in certain cases.
|
87 | *
|
88 | * The providers included in `HTTP_PROVIDERS` include:
|
89 | * * {@link Http}
|
90 | * * {@link XHRBackend}
|
91 | * * `BrowserXHR` - Private factory to create `XMLHttpRequest` instances
|
92 | * * {@link RequestOptions} - Bound to {@link BaseRequestOptions} class
|
93 | * * {@link ResponseOptions} - Bound to {@link BaseResponseOptions} class
|
94 | *
|
95 | * There may be cases where it makes sense to extend the base request options,
|
96 | * such as to add a search string to be appended to all URLs.
|
97 | * To accomplish this, a new provider for {@link RequestOptions} should
|
98 | * be added in the same injector as `HTTP_PROVIDERS`.
|
99 | *
|
100 | * ### Example ([live demo](http://plnkr.co/edit/aCMEXi?p=preview))
|
101 | *
|
102 | * ```
|
103 | * import {provide} from 'angular2/core';
|
104 | * import {bootstrap} from 'angular2/platform/browser';
|
105 | * import {HTTP_PROVIDERS, BaseRequestOptions, RequestOptions} from 'angular2/http';
|
106 | *
|
107 | * class MyOptions extends BaseRequestOptions {
|
108 | * search: string = 'coreTeam=true';
|
109 | * }
|
110 | *
|
111 | * bootstrap(App, [HTTP_PROVIDERS, provide(RequestOptions, {useClass: MyOptions})])
|
112 | * .catch(err => console.error(err));
|
113 | * ```
|
114 | *
|
115 | * Likewise, to use a mock backend for unit tests, the {@link XHRBackend}
|
116 | * provider should be bound to {@link MockBackend}.
|
117 | *
|
118 | * ### Example ([live demo](http://plnkr.co/edit/7LWALD?p=preview))
|
119 | *
|
120 | * ```
|
121 | * import {provide} from 'angular2/core';
|
122 | * import {bootstrap} from 'angular2/platform/browser';
|
123 | * import {HTTP_PROVIDERS, Http, Response, XHRBackend} from 'angular2/http';
|
124 | * import {MockBackend} from 'angular2/http/testing';
|
125 | *
|
126 | * var people = [{name: 'Jeff'}, {name: 'Tobias'}];
|
127 | *
|
128 | * var injector = Injector.resolveAndCreate([
|
129 | * HTTP_PROVIDERS,
|
130 | * MockBackend,
|
131 | * provide(XHRBackend, {useExisting: MockBackend})
|
132 | * ]);
|
133 | * var http = injector.get(Http);
|
134 | * var backend = injector.get(MockBackend);
|
135 | *
|
136 | * // Listen for any new requests
|
137 | * backend.connections.observer({
|
138 | * next: connection => {
|
139 | * var response = new Response({body: people});
|
140 | * setTimeout(() => {
|
141 | * // Send a response to the request
|
142 | * connection.mockRespond(response);
|
143 | * });
|
144 | * }
|
145 | * });
|
146 | *
|
147 | * http.get('people.json').observer({
|
148 | * next: res => {
|
149 | * // Response came from mock backend
|
150 | * console.log('first person', res.json()[0].name);
|
151 | * }
|
152 | * });
|
153 | * ```
|
154 | */
|
155 | export const HTTP_PROVIDERS: any[] = [
|
156 | // TODO(pascal): use factory type annotations once supported in DI
|
157 | // issue: https://github.com/angular/angular/issues/3183
|
158 | provide(Http,
|
159 | {
|
160 | useFactory: (xhrBackend: XHRBackend, requestOptions: RequestOptions) =>
|
161 | new Http(xhrBackend, requestOptions),
|
162 | deps: [XHRBackend, RequestOptions]
|
163 | }),
|
164 | BrowserXhr,
|
165 | provide(RequestOptions, {useClass: BaseRequestOptions}),
|
166 | provide(ResponseOptions, {useClass: BaseResponseOptions}),
|
167 | XHRBackend
|
168 | ];
|
169 |
|
170 | /**
|
171 | * See {@link HTTP_PROVIDERS} instead.
|
172 | *
|
173 | * @deprecated
|
174 | */
|
175 | export const HTTP_BINDINGS = HTTP_PROVIDERS;
|
176 |
|
177 | /**
|
178 | * Provides a basic set of providers to use the {@link Jsonp} service in any application.
|
179 | *
|
180 | * The `JSONP_PROVIDERS` should be included either in a component's injector,
|
181 | * or in the root injector when bootstrapping an application.
|
182 | *
|
183 | * ### Example ([live demo](http://plnkr.co/edit/vmeN4F?p=preview))
|
184 | *
|
185 | * ```
|
186 | * import {Component} from 'angular2/core';
|
187 | * import {NgFor} from 'angular2/common';
|
188 | * import {JSONP_PROVIDERS, Jsonp} from 'angular2/http';
|
189 | *
|
190 | * @Component({
|
191 | * selector: 'app',
|
192 | * providers: [JSONP_PROVIDERS],
|
193 | * template: `
|
194 | * <div>
|
195 | * <h1>People</h1>
|
196 | * <ul>
|
197 | * <li *ngFor="let person of people">
|
198 | * {{person.name}}
|
199 | * </li>
|
200 | * </ul>
|
201 | * </div>
|
202 | * `,
|
203 | * directives: [NgFor]
|
204 | * })
|
205 | * export class App {
|
206 | * people: Array<Object>;
|
207 | * constructor(jsonp:Jsonp) {
|
208 | * jsonp.request('people.json').subscribe(res => {
|
209 | * this.people = res.json();
|
210 | * })
|
211 | * }
|
212 | * }
|
213 | * ```
|
214 | *
|
215 | * The primary public API included in `JSONP_PROVIDERS` is the {@link Jsonp} class.
|
216 | * However, other providers required by `Jsonp` are included,
|
217 | * which may be beneficial to override in certain cases.
|
218 | *
|
219 | * The providers included in `JSONP_PROVIDERS` include:
|
220 | * * {@link Jsonp}
|
221 | * * {@link JSONPBackend}
|
222 | * * `BrowserJsonp` - Private factory
|
223 | * * {@link RequestOptions} - Bound to {@link BaseRequestOptions} class
|
224 | * * {@link ResponseOptions} - Bound to {@link BaseResponseOptions} class
|
225 | *
|
226 | * There may be cases where it makes sense to extend the base request options,
|
227 | * such as to add a search string to be appended to all URLs.
|
228 | * To accomplish this, a new provider for {@link RequestOptions} should
|
229 | * be added in the same injector as `JSONP_PROVIDERS`.
|
230 | *
|
231 | * ### Example ([live demo](http://plnkr.co/edit/TFug7x?p=preview))
|
232 | *
|
233 | * ```
|
234 | * import {provide} from 'angular2/core';
|
235 | * import {bootstrap} from 'angular2/platform/browser';
|
236 | * import {JSONP_PROVIDERS, BaseRequestOptions, RequestOptions} from 'angular2/http';
|
237 | *
|
238 | * class MyOptions extends BaseRequestOptions {
|
239 | * search: string = 'coreTeam=true';
|
240 | * }
|
241 | *
|
242 | * bootstrap(App, [JSONP_PROVIDERS, provide(RequestOptions, {useClass: MyOptions})])
|
243 | * .catch(err => console.error(err));
|
244 | * ```
|
245 | *
|
246 | * Likewise, to use a mock backend for unit tests, the {@link JSONPBackend}
|
247 | * provider should be bound to {@link MockBackend}.
|
248 | *
|
249 | * ### Example ([live demo](http://plnkr.co/edit/HDqZWL?p=preview))
|
250 | *
|
251 | * ```
|
252 | * import {provide, Injector} from 'angular2/core';
|
253 | * import {JSONP_PROVIDERS, Jsonp, Response, JSONPBackend} from 'angular2/http';
|
254 | * import {MockBackend} from 'angular2/http/testing';
|
255 | *
|
256 | * var people = [{name: 'Jeff'}, {name: 'Tobias'}];
|
257 | * var injector = Injector.resolveAndCreate([
|
258 | * JSONP_PROVIDERS,
|
259 | * MockBackend,
|
260 | * provide(JSONPBackend, {useExisting: MockBackend})
|
261 | * ]);
|
262 | * var jsonp = injector.get(Jsonp);
|
263 | * var backend = injector.get(MockBackend);
|
264 | *
|
265 | * // Listen for any new requests
|
266 | * backend.connections.observer({
|
267 | * next: connection => {
|
268 | * var response = new Response({body: people});
|
269 | * setTimeout(() => {
|
270 | * // Send a response to the request
|
271 | * connection.mockRespond(response);
|
272 | * });
|
273 | * }
|
274 | * });
|
275 |
|
276 | * jsonp.get('people.json').observer({
|
277 | * next: res => {
|
278 | * // Response came from mock backend
|
279 | * console.log('first person', res.json()[0].name);
|
280 | * }
|
281 | * });
|
282 | * ```
|
283 | */
|
284 | export const JSONP_PROVIDERS: any[] = [
|
285 | // TODO(pascal): use factory type annotations once supported in DI
|
286 | // issue: https://github.com/angular/angular/issues/3183
|
287 | provide(Jsonp,
|
288 | {
|
289 | useFactory: (jsonpBackend: JSONPBackend, requestOptions: RequestOptions) =>
|
290 | new Jsonp(jsonpBackend, requestOptions),
|
291 | deps: [JSONPBackend, RequestOptions]
|
292 | }),
|
293 | BrowserJsonp,
|
294 | provide(RequestOptions, {useClass: BaseRequestOptions}),
|
295 | provide(ResponseOptions, {useClass: BaseResponseOptions}),
|
296 | provide(JSONPBackend, {useClass: JSONPBackend_})
|
297 | ];
|
298 |
|
299 | /**
|
300 | * See {@link JSONP_PROVIDERS} instead.
|
301 | *
|
302 | * @deprecated
|
303 | */
|
304 | export const JSON_BINDINGS = JSONP_PROVIDERS;
|