UNPKG

angular2

Version:

Angular 2 - a web framework for modern web apps

204 lines 9.06 kB
'use strict';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 __()); }; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc); switch (arguments.length) { case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target); case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0); case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc); } }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var lang_1 = require('angular2/src/facade/lang'); var exceptions_1 = require('angular2/src/facade/exceptions'); var core_1 = require('angular2/core'); var interfaces_1 = require('./interfaces'); var static_request_1 = require('./static_request'); var base_request_options_1 = require('./base_request_options'); var enums_1 = require('./enums'); function httpRequest(backend, request) { return backend.createConnection(request).response; } function mergeOptions(defaultOpts, providedOpts, method, url) { var newOptions = defaultOpts; if (lang_1.isPresent(providedOpts)) { // Hack so Dart can used named parameters return newOptions.merge(new base_request_options_1.RequestOptions({ method: providedOpts.method || method, url: providedOpts.url || url, search: providedOpts.search, headers: providedOpts.headers, body: providedOpts.body })); } if (lang_1.isPresent(method)) { return newOptions.merge(new base_request_options_1.RequestOptions({ method: method, url: url })); } else { return newOptions.merge(new base_request_options_1.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 {@link 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 {MockBackend, BaseRequestOptions, Http} from 'angular2/http'; * 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)); * ``` * **/ var Http = (function () { 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. */ Http.prototype.request = function (url, options) { var responseObservable; if (lang_1.isString(url)) { responseObservable = httpRequest(this._backend, new static_request_1.Request(mergeOptions(this._defaultOptions, options, enums_1.RequestMethods.Get, url))); } else if (url instanceof static_request_1.Request) { responseObservable = httpRequest(this._backend, url); } else { throw exceptions_1.makeTypeError('First argument must be a url string or Request instance.'); } return responseObservable; }; /** * Performs a request with `get` http method. */ Http.prototype.get = function (url, options) { return httpRequest(this._backend, new static_request_1.Request(mergeOptions(this._defaultOptions, options, enums_1.RequestMethods.Get, url))); }; /** * Performs a request with `post` http method. */ Http.prototype.post = function (url, body, options) { return httpRequest(this._backend, new static_request_1.Request(mergeOptions(this._defaultOptions.merge(new base_request_options_1.RequestOptions({ body: body })), options, enums_1.RequestMethods.Post, url))); }; /** * Performs a request with `put` http method. */ Http.prototype.put = function (url, body, options) { return httpRequest(this._backend, new static_request_1.Request(mergeOptions(this._defaultOptions.merge(new base_request_options_1.RequestOptions({ body: body })), options, enums_1.RequestMethods.Put, url))); }; /** * Performs a request with `delete` http method. */ Http.prototype.delete = function (url, options) { return httpRequest(this._backend, new static_request_1.Request(mergeOptions(this._defaultOptions, options, enums_1.RequestMethods.Delete, url))); }; /** * Performs a request with `patch` http method. */ Http.prototype.patch = function (url, body, options) { return httpRequest(this._backend, new static_request_1.Request(mergeOptions(this._defaultOptions.merge(new base_request_options_1.RequestOptions({ body: body })), options, enums_1.RequestMethods.Patch, url))); }; /** * Performs a request with `head` http method. */ Http.prototype.head = function (url, options) { return httpRequest(this._backend, new static_request_1.Request(mergeOptions(this._defaultOptions, options, enums_1.RequestMethods.Head, url))); }; Http = __decorate([ core_1.Injectable(), __metadata('design:paramtypes', [interfaces_1.ConnectionBackend, base_request_options_1.RequestOptions]) ], Http); return Http; })(); exports.Http = Http; var Jsonp = (function (_super) { __extends(Jsonp, _super); 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. */ Jsonp.prototype.request = function (url, options) { var responseObservable; if (lang_1.isString(url)) { url = new static_request_1.Request(mergeOptions(this._defaultOptions, options, enums_1.RequestMethods.Get, url)); } if (url instanceof static_request_1.Request) { if (url.method !== enums_1.RequestMethods.Get) { exceptions_1.makeTypeError('JSONP requests must use GET request method.'); } responseObservable = httpRequest(this._backend, url); } else { throw exceptions_1.makeTypeError('First argument must be a url string or Request instance.'); } return responseObservable; }; Jsonp = __decorate([ core_1.Injectable(), __metadata('design:paramtypes', [interfaces_1.ConnectionBackend, base_request_options_1.RequestOptions]) ], Jsonp); return Jsonp; })(Http); exports.Jsonp = Jsonp; //# sourceMappingURL=http.js.map