UNPKG

5.42 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright Google Inc. All Rights Reserved.
4 *
5 * Use of this source code is governed by an MIT-style license that can be
6 * found in the LICENSE file at https://angular.io/license
7 */
8import { Observable } from 'rxjs';
9import { RequestOptions } from './base_request_options';
10import { ConnectionBackend, RequestOptionsArgs } from './interfaces';
11import { Request } from './static_request';
12import { Response } from './static_response';
13/**
14 * Performs http requests using `XMLHttpRequest` as the default backend.
15 *
16 * `Http` is available as an injectable class, with methods to perform http requests. Calling
17 * `request` returns an `Observable` which will emit a single {@link Response} when a
18 * response is received.
19 *
20 * @usageNotes
21 * ### Example
22 *
23 * ```typescript
24 * import {Http, HTTP_PROVIDERS} from '@angular/http';
25 * import {map} from 'rxjs/operators';
26 *
27 * @Component({
28 * selector: 'http-app',
29 * viewProviders: [HTTP_PROVIDERS],
30 * templateUrl: 'people.html'
31 * })
32 * class PeopleComponent {
33 * constructor(http: Http) {
34 * http.get('people.json')
35 * // Call map on the response observable to get the parsed people object
36 * .pipe(map(res => res.json()))
37 * // Subscribe to the observable to get the parsed people object and attach it to the
38 * // component
39 * .subscribe(people => this.people = people);
40 * }
41 * }
42 * ```
43 *
44 *
45 * ### Example
46 *
47 * ```
48 * http.get('people.json').subscribe((res:Response) => this.people = res.json());
49 * ```
50 *
51 * The default construct used to perform requests, `XMLHttpRequest`, is abstracted as a "Backend" (
52 * {@link XHRBackend} in this case), which could be mocked with dependency injection by replacing
53 * the {@link XHRBackend} provider, as in the following example:
54 *
55 * ### Example
56 *
57 * ```typescript
58 * import {BaseRequestOptions, Http} from '@angular/http';
59 * import {MockBackend} from '@angular/http/testing';
60 * var injector = Injector.resolveAndCreate([
61 * BaseRequestOptions,
62 * MockBackend,
63 * {provide: Http, useFactory:
64 * function(backend, defaultOptions) {
65 * return new Http(backend, defaultOptions);
66 * },
67 * deps: [MockBackend, BaseRequestOptions]}
68 * ]);
69 * var http = injector.get(Http);
70 * http.get('request-from-mock-backend.json').subscribe((res:Response) => doSomething(res));
71 * ```
72 *
73 * @deprecated see https://angular.io/guide/http
74 * @publicApi
75 */
76export declare class Http {
77 protected _backend: ConnectionBackend;
78 protected _defaultOptions: RequestOptions;
79 constructor(_backend: ConnectionBackend, _defaultOptions: RequestOptions);
80 /**
81 * Performs any type of http request. First argument is required, and can either be a url or
82 * a {@link Request} instance. If the first argument is a url, an optional {@link RequestOptions}
83 * object can be provided as the 2nd argument. The options object will be merged with the values
84 * of {@link BaseRequestOptions} before performing the request.
85 */
86 request(url: string | Request, options?: RequestOptionsArgs): Observable<Response>;
87 /**
88 * Performs a request with `get` http method.
89 */
90 get(url: string, options?: RequestOptionsArgs): Observable<Response>;
91 /**
92 * Performs a request with `post` http method.
93 */
94 post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>;
95 /**
96 * Performs a request with `put` http method.
97 */
98 put(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>;
99 /**
100 * Performs a request with `delete` http method.
101 */
102 delete(url: string, options?: RequestOptionsArgs): Observable<Response>;
103 /**
104 * Performs a request with `patch` http method.
105 */
106 patch(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>;
107 /**
108 * Performs a request with `head` http method.
109 */
110 head(url: string, options?: RequestOptionsArgs): Observable<Response>;
111 /**
112 * Performs a request with `options` http method.
113 */
114 options(url: string, options?: RequestOptionsArgs): Observable<Response>;
115}
116/**
117 * @deprecated see https://angular.io/guide/http
118 * @publicApi
119 */
120export declare class Jsonp extends Http {
121 constructor(backend: ConnectionBackend, defaultOptions: RequestOptions);
122 /**
123 * Performs any type of http request. First argument is required, and can either be a url or
124 * a {@link Request} instance. If the first argument is a url, an optional {@link RequestOptions}
125 * object can be provided as the 2nd argument. The options object will be merged with the values
126 * of {@link BaseRequestOptions} before performing the request.
127 *
128 * @security Regular XHR is the safest alternative to JSONP for most applications, and is
129 * supported by all current browsers. Because JSONP creates a `<script>` element with
130 * contents retrieved from a remote source, attacker-controlled data introduced by an untrusted
131 * source could expose your application to XSS risks. Data exposed by JSONP may also be
132 * readable by malicious third-party websites. In addition, JSONP introduces potential risk for
133 * future security issues (e.g. content sniffing). For more detail, see the
134 * [Security Guide](http://g.co/ng/security).
135 */
136 request(url: string | Request, options?: RequestOptionsArgs): Observable<Response>;
137}