UNPKG

angular2

Version:

Angular 2 - a web framework for modern web apps

153 lines 6.96 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 headers_1 = require('./headers'); var enums_1 = require('./enums'); var angular2_1 = require('angular2/angular2'); var url_search_params_1 = require('./url_search_params'); var http_utils_1 = require('./http_utils'); /** * Creates a request options object to be optionally provided when instantiating a * {@link Request}. * * This class is based on the `RequestInit` description in the [Fetch * Spec](https://fetch.spec.whatwg.org/#requestinit). * * All values are null by default. Typical defaults can be found in the {@link BaseRequestOptions} * class, which sub-classes `RequestOptions`. * * ### Example ([live demo](http://plnkr.co/edit/7Wvi3lfLq41aQPKlxB4O?p=preview)) * * ```typescript * import {RequestOptions, Request, RequestMethods} from 'angular2/http'; * * var options = new RequestOptions({ * method: RequestMethods.Post, * url: 'https://google.com' * }); * var req = new Request(options); * console.log('req.method:', RequestMethods[req.method]); // Post * console.log('options.url:', options.url); // https://google.com * ``` */ var RequestOptions = (function () { function RequestOptions(_a) { var _b = _a === void 0 ? {} : _a, method = _b.method, headers = _b.headers, body = _b.body, url = _b.url, search = _b.search; this.method = lang_1.isPresent(method) ? http_utils_1.normalizeMethodName(method) : null; this.headers = lang_1.isPresent(headers) ? headers : null; this.body = lang_1.isPresent(body) ? body : null; this.url = lang_1.isPresent(url) ? url : null; this.search = lang_1.isPresent(search) ? (lang_1.isString(search) ? new url_search_params_1.URLSearchParams((search)) : (search)) : null; } /** * Creates a copy of the `RequestOptions` instance, using the optional input as values to override * existing values. This method will not change the values of the instance on which it is being * called. * * Note that `headers` and `search` will override existing values completely if present in * the `options` object. If these values should be merged, it should be done prior to calling * `merge` on the `RequestOptions` instance. * * ### Example ([live demo](http://plnkr.co/edit/6w8XA8YTkDRcPYpdB9dk?p=preview)) * * ```typescript * import {RequestOptions, Request, RequestMethods} from 'angular2/http'; * * var options = new RequestOptions({ * method: RequestMethods.Post * }); * var req = new Request(options.merge({ * url: 'https://google.com' * })); * console.log('req.method:', RequestMethods[req.method]); // Post * console.log('options.url:', options.url); // null * console.log('req.url:', req.url); // https://google.com * ``` */ RequestOptions.prototype.merge = function (options) { return new RequestOptions({ method: lang_1.isPresent(options) && lang_1.isPresent(options.method) ? options.method : this.method, headers: lang_1.isPresent(options) && lang_1.isPresent(options.headers) ? options.headers : this.headers, body: lang_1.isPresent(options) && lang_1.isPresent(options.body) ? options.body : this.body, url: lang_1.isPresent(options) && lang_1.isPresent(options.url) ? options.url : this.url, search: lang_1.isPresent(options) && lang_1.isPresent(options.search) ? (lang_1.isString(options.search) ? new url_search_params_1.URLSearchParams((options.search)) : (options.search).clone()) : this.search }); }; return RequestOptions; })(); exports.RequestOptions = RequestOptions; /** * Subclass of {@link RequestOptions}, with default values. * * Default values: * * method: {@link RequestMethods RequestMethods.Get} * * headers: empty {@link Headers} object * * This class could be extended and bound to the {@link RequestOptions} class * when configuring an {@link Injector}, in order to override the default options * used by {@link Http} to create and send {@link Request Requests}. * * ### Example ([live demo](http://plnkr.co/edit/LEKVSx?p=preview)) * * ```typescript * import {provide, bootstrap} from 'angular2/angular2'; * import {HTTP_PROVIDERS, Http, BaseRequestOptions, RequestOptions} from 'angular2/http'; * import {App} from './myapp'; * * class MyOptions extends BaseRequestOptions { * search: string = 'coreTeam=true'; * } * * bootstrap(App, [HTTP_PROVIDERS, provide(RequestOptions, {useClass: MyOptions})]); * ``` * * The options could also be extended when manually creating a {@link Request} * object. * * ### Example ([live demo](http://plnkr.co/edit/oyBoEvNtDhOSfi9YxaVb?p=preview)) * * ``` * import {BaseRequestOptions, Request, RequestMethods} from 'angular2/http'; * * var options = new BaseRequestOptions(); * var req = new Request(options.merge({ * method: RequestMethods.Post, * url: 'https://google.com' * })); * console.log('req.method:', RequestMethods[req.method]); // Post * console.log('options.url:', options.url); // null * console.log('req.url:', req.url); // https://google.com * ``` */ var BaseRequestOptions = (function (_super) { __extends(BaseRequestOptions, _super); function BaseRequestOptions() { _super.call(this, { method: enums_1.RequestMethods.Get, headers: new headers_1.Headers() }); } BaseRequestOptions = __decorate([ angular2_1.Injectable(), __metadata('design:paramtypes', []) ], BaseRequestOptions); return BaseRequestOptions; })(RequestOptions); exports.BaseRequestOptions = BaseRequestOptions; //# sourceMappingURL=base_request_options.js.map