angular2
Version:
Angular 2 - a web framework for modern web apps
194 lines (193 loc) • 7.89 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { isString, isPresent } from 'angular2/src/facade/lang';
import { makeTypeError } from 'angular2/src/facade/exceptions';
import { Injectable } from 'angular2/core';
import { ConnectionBackend } from './interfaces';
import { Request } from './static_request';
import { RequestOptions } from './base_request_options';
import { RequestMethod } from './enums';
function httpRequest(backend, request) {
return backend.createConnection(request).response;
}
function mergeOptions(defaultOpts, providedOpts, method, url) {
var newOptions = defaultOpts;
if (isPresent(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
}));
}
if (isPresent(method)) {
return newOptions.merge(new RequestOptions({ method: method, url: url }));
}
else {
return newOptions.merge(new RequestOptions({ 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 'angular2/http';
* @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').observer({next: (value) => this.people = value});
* ```
*
* 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 'angular2/http';
* import {MockBackend} from 'angular2/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));
* ```
*
**/
export let Http = class Http {
constructor(_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.
*/
request(url, options) {
var responseObservable;
if (isString(url)) {
responseObservable = httpRequest(this._backend, new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, url)));
}
else if (url instanceof Request) {
responseObservable = httpRequest(this._backend, url);
}
else {
throw makeTypeError('First argument must be a url string or Request instance.');
}
return responseObservable;
}
/**
* Performs a request with `get` http method.
*/
get(url, options) {
return httpRequest(this._backend, new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, url)));
}
/**
* Performs a request with `post` http method.
*/
post(url, body, options) {
return httpRequest(this._backend, new Request(mergeOptions(this._defaultOptions.merge(new RequestOptions({ body: body })), options, RequestMethod.Post, url)));
}
/**
* Performs a request with `put` http method.
*/
put(url, body, options) {
return httpRequest(this._backend, new Request(mergeOptions(this._defaultOptions.merge(new RequestOptions({ body: body })), options, RequestMethod.Put, url)));
}
/**
* Performs a request with `delete` http method.
*/
delete(url, options) {
return httpRequest(this._backend, new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Delete, url)));
}
/**
* Performs a request with `patch` http method.
*/
patch(url, body, options) {
return httpRequest(this._backend, new Request(mergeOptions(this._defaultOptions.merge(new RequestOptions({ body: body })), options, RequestMethod.Patch, url)));
}
/**
* Performs a request with `head` http method.
*/
head(url, options) {
return httpRequest(this._backend, new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Head, url)));
}
};
Http = __decorate([
Injectable(),
__metadata('design:paramtypes', [ConnectionBackend, RequestOptions])
], Http);
export let Jsonp = class Jsonp extends Http {
constructor(backend, defaultOptions) {
super(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.
*/
request(url, options) {
var responseObservable;
if (isString(url)) {
url =
new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, url));
}
if (url instanceof Request) {
if (url.method !== RequestMethod.Get) {
makeTypeError('JSONP requests must use GET request method.');
}
responseObservable = httpRequest(this._backend, url);
}
else {
throw makeTypeError('First argument must be a url string or Request instance.');
}
return responseObservable;
}
};
Jsonp = __decorate([
Injectable(),
__metadata('design:paramtypes', [ConnectionBackend, RequestOptions])
], Jsonp);