2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
|
2016-05-26 18:47:20 -04:00
|
|
|
import {RequestMethod, ResponseContentType} from './enums';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {Headers} from './headers';
|
|
|
|
import {normalizeMethodName} from './http_utils';
|
2015-08-11 18:01:29 -04:00
|
|
|
import {RequestOptionsArgs} from './interfaces';
|
2015-07-13 14:47:10 -04:00
|
|
|
import {URLSearchParams} from './url_search_params';
|
2016-06-08 19:38:52 -04:00
|
|
|
|
2015-04-29 02:07:55 -04:00
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
2015-09-21 18:54:13 -04:00
|
|
|
* Creates a request options object to be optionally provided when instantiating a
|
2015-06-24 03:27:07 -04:00
|
|
|
* {@link Request}.
|
|
|
|
*
|
2015-09-21 18:54:13 -04:00
|
|
|
* 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`.
|
|
|
|
*
|
|
|
|
* ```typescript
|
2016-04-28 20:50:03 -04:00
|
|
|
* import {RequestOptions, Request, RequestMethod} from '@angular/http';
|
2015-09-21 18:54:13 -04:00
|
|
|
*
|
2017-03-29 18:11:59 -04:00
|
|
|
* const options = new RequestOptions({
|
2015-12-03 16:44:14 -05:00
|
|
|
* method: RequestMethod.Post,
|
2015-09-21 18:54:13 -04:00
|
|
|
* url: 'https://google.com'
|
|
|
|
* });
|
2017-03-29 18:11:59 -04:00
|
|
|
* const req = new Request(options);
|
2015-12-03 16:44:14 -05:00
|
|
|
* console.log('req.method:', RequestMethod[req.method]); // Post
|
2015-09-21 18:54:13 -04:00
|
|
|
* console.log('options.url:', options.url); // https://google.com
|
|
|
|
* ```
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
2018-06-01 16:09:01 -04:00
|
|
|
* @deprecated see https://angular.io/guide/http
|
2018-10-19 12:47:13 -04:00
|
|
|
* @publicApi
|
2015-06-09 18:18:57 -04:00
|
|
|
*/
|
2015-08-11 18:01:29 -04:00
|
|
|
export class RequestOptions {
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
2015-09-21 18:54:13 -04:00
|
|
|
* Http method with which to execute a {@link Request}.
|
2015-12-03 16:44:14 -05:00
|
|
|
* Acceptable methods are defined in the {@link RequestMethod} enum.
|
2015-06-09 18:18:57 -04:00
|
|
|
*/
|
2017-04-17 14:12:53 -04:00
|
|
|
method: RequestMethod|string|null;
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
2015-09-21 18:54:13 -04:00
|
|
|
* {@link Headers} to be attached to a {@link Request}.
|
2015-06-09 18:18:57 -04:00
|
|
|
*/
|
2017-04-17 14:12:53 -04:00
|
|
|
headers: Headers|null;
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
2015-09-21 18:54:13 -04:00
|
|
|
* Body to be used when creating a {@link Request}.
|
2015-06-09 18:18:57 -04:00
|
|
|
*/
|
2016-02-26 06:25:55 -05:00
|
|
|
body: any;
|
2015-09-21 18:54:13 -04:00
|
|
|
/**
|
|
|
|
* Url with which to perform a {@link Request}.
|
|
|
|
*/
|
2017-04-17 14:12:53 -04:00
|
|
|
url: string|null;
|
2015-09-21 18:54:13 -04:00
|
|
|
/**
|
|
|
|
* Search parameters to be included in a {@link Request}.
|
|
|
|
*/
|
2016-12-09 18:38:29 -05:00
|
|
|
params: URLSearchParams;
|
|
|
|
/**
|
|
|
|
* @deprecated from 4.0.0. Use params instead.
|
|
|
|
*/
|
|
|
|
get search(): URLSearchParams { return this.params; }
|
|
|
|
/**
|
|
|
|
* @deprecated from 4.0.0. Use params instead.
|
|
|
|
*/
|
|
|
|
set search(params: URLSearchParams) { this.params = params; }
|
2016-02-24 16:57:35 -05:00
|
|
|
/**
|
|
|
|
* Enable use credentials for a {@link Request}.
|
|
|
|
*/
|
2017-04-17 14:12:53 -04:00
|
|
|
withCredentials: boolean|null;
|
2016-02-24 10:37:18 -05:00
|
|
|
/*
|
|
|
|
* Select a buffer to store the response, such as ArrayBuffer, Blob, Json (or Document)
|
|
|
|
*/
|
2017-04-17 14:12:53 -04:00
|
|
|
responseType: ResponseContentType|null;
|
2016-02-24 10:37:18 -05:00
|
|
|
|
2016-12-09 18:38:29 -05:00
|
|
|
// TODO(Dzmitry): remove search when this.search is removed
|
2017-06-06 18:58:38 -04:00
|
|
|
constructor(opts: RequestOptionsArgs = {}) {
|
|
|
|
const {method, headers, body, url, search, params, withCredentials, responseType} = opts;
|
2016-11-15 12:19:14 -05:00
|
|
|
this.method = method != null ? normalizeMethodName(method) : null;
|
|
|
|
this.headers = headers != null ? headers : null;
|
|
|
|
this.body = body != null ? body : null;
|
|
|
|
this.url = url != null ? url : null;
|
2016-12-09 18:38:29 -05:00
|
|
|
this.params = this._mergeSearchParams(params || search);
|
2016-11-15 12:19:14 -05:00
|
|
|
this.withCredentials = withCredentials != null ? withCredentials : null;
|
|
|
|
this.responseType = responseType != null ? responseType : null;
|
2015-06-13 00:50:19 -04:00
|
|
|
}
|
2015-04-29 02:07:55 -04:00
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* Creates a copy of the `RequestOptions` instance, using the optional input as values to override
|
2015-09-21 18:54:13 -04:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* ```typescript
|
2016-04-28 20:50:03 -04:00
|
|
|
* import {RequestOptions, Request, RequestMethod} from '@angular/http';
|
2015-09-21 18:54:13 -04:00
|
|
|
*
|
2017-03-29 18:11:59 -04:00
|
|
|
* const options = new RequestOptions({
|
2015-12-03 16:44:14 -05:00
|
|
|
* method: RequestMethod.Post
|
2015-09-21 18:54:13 -04:00
|
|
|
* });
|
2017-03-29 18:11:59 -04:00
|
|
|
* const req = new Request(options.merge({
|
2015-09-21 18:54:13 -04:00
|
|
|
* url: 'https://google.com'
|
|
|
|
* }));
|
2015-12-03 16:44:14 -05:00
|
|
|
* console.log('req.method:', RequestMethod[req.method]); // Post
|
2015-09-21 18:54:13 -04:00
|
|
|
* console.log('options.url:', options.url); // null
|
|
|
|
* console.log('req.url:', req.url); // https://google.com
|
|
|
|
* ```
|
2015-06-09 18:18:57 -04:00
|
|
|
*/
|
2015-08-11 18:01:29 -04:00
|
|
|
merge(options?: RequestOptionsArgs): RequestOptions {
|
2015-06-19 15:14:12 -04:00
|
|
|
return new RequestOptions({
|
2016-11-15 12:19:14 -05:00
|
|
|
method: options && options.method != null ? options.method : this.method,
|
2016-12-12 14:16:34 -05:00
|
|
|
headers: options && options.headers != null ? options.headers : new Headers(this.headers),
|
2016-11-15 12:19:14 -05:00
|
|
|
body: options && options.body != null ? options.body : this.body,
|
|
|
|
url: options && options.url != null ? options.url : this.url,
|
2016-12-09 18:38:29 -05:00
|
|
|
params: options && this._mergeSearchParams(options.params || options.search),
|
2016-11-15 12:19:14 -05:00
|
|
|
withCredentials: options && options.withCredentials != null ? options.withCredentials :
|
|
|
|
this.withCredentials,
|
|
|
|
responseType: options && options.responseType != null ? options.responseType :
|
|
|
|
this.responseType
|
2015-06-19 15:14:12 -04:00
|
|
|
});
|
|
|
|
}
|
2015-06-13 00:50:19 -04:00
|
|
|
|
2017-04-17 14:12:53 -04:00
|
|
|
private _mergeSearchParams(params?: string|URLSearchParams|{[key: string]: any | any[]}|
|
|
|
|
null): URLSearchParams {
|
2016-12-09 18:38:29 -05:00
|
|
|
if (!params) return this.params;
|
|
|
|
|
|
|
|
if (params instanceof URLSearchParams) {
|
|
|
|
return params.clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof params === 'string') {
|
|
|
|
return new URLSearchParams(params);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._parseParams(params);
|
|
|
|
}
|
|
|
|
|
|
|
|
private _parseParams(objParams: {[key: string]: any | any[]} = {}): URLSearchParams {
|
|
|
|
const params = new URLSearchParams();
|
|
|
|
Object.keys(objParams).forEach((key: string) => {
|
|
|
|
const value: any|any[] = objParams[key];
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
value.forEach((item: any) => this._appendParam(key, item, params));
|
|
|
|
} else {
|
|
|
|
this._appendParam(key, value, params);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
private _appendParam(key: string, value: any, params: URLSearchParams): void {
|
|
|
|
if (typeof value !== 'string') {
|
|
|
|
value = JSON.stringify(value);
|
|
|
|
}
|
|
|
|
params.append(key, value);
|
|
|
|
}
|
|
|
|
}
|
2015-09-22 17:05:36 -04:00
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
2015-09-22 17:05:36 -04:00
|
|
|
* Subclass of {@link RequestOptions}, with default values.
|
|
|
|
*
|
|
|
|
* Default values:
|
2015-12-03 16:44:14 -05:00
|
|
|
* * method: {@link RequestMethod RequestMethod.Get}
|
2015-09-22 17:05:36 -04:00
|
|
|
* * headers: empty {@link Headers} object
|
2015-06-09 18:18:57 -04:00
|
|
|
*
|
2015-09-22 17:05:36 -04:00
|
|
|
* 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}.
|
|
|
|
*
|
2015-09-24 04:32:43 -04:00
|
|
|
* ```typescript
|
2017-03-29 18:11:59 -04:00
|
|
|
* import {BaseRequestOptions, RequestOptions} from '@angular/http';
|
2015-09-22 17:05:36 -04:00
|
|
|
*
|
|
|
|
* class MyOptions extends BaseRequestOptions {
|
|
|
|
* search: string = 'coreTeam=true';
|
2015-06-09 18:18:57 -04:00
|
|
|
* }
|
|
|
|
*
|
2017-03-29 18:11:59 -04:00
|
|
|
* {provide: RequestOptions, useClass: MyOptions};
|
2015-09-22 17:05:36 -04:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* The options could also be extended when manually creating a {@link Request}
|
|
|
|
* object.
|
|
|
|
*
|
|
|
|
* ```
|
2016-04-28 20:50:03 -04:00
|
|
|
* import {BaseRequestOptions, Request, RequestMethod} from '@angular/http';
|
2015-09-22 17:05:36 -04:00
|
|
|
*
|
2017-03-29 18:11:59 -04:00
|
|
|
* const options = new BaseRequestOptions();
|
|
|
|
* const req = new Request(options.merge({
|
2015-12-03 16:44:14 -05:00
|
|
|
* method: RequestMethod.Post,
|
2015-09-22 17:05:36 -04:00
|
|
|
* url: 'https://google.com'
|
|
|
|
* }));
|
2015-12-03 16:44:14 -05:00
|
|
|
* console.log('req.method:', RequestMethod[req.method]); // Post
|
2015-09-22 17:05:36 -04:00
|
|
|
* console.log('options.url:', options.url); // null
|
|
|
|
* console.log('req.url:', req.url); // https://google.com
|
2015-06-09 18:18:57 -04:00
|
|
|
* ```
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
2018-06-01 16:09:01 -04:00
|
|
|
* @deprecated see https://angular.io/guide/http
|
2018-10-19 12:47:13 -04:00
|
|
|
* @publicApi
|
2015-06-09 18:18:57 -04:00
|
|
|
*/
|
2015-06-13 00:50:19 -04:00
|
|
|
@Injectable()
|
2015-06-13 22:48:40 -04:00
|
|
|
export class BaseRequestOptions extends RequestOptions {
|
2015-12-03 16:44:14 -05:00
|
|
|
constructor() { super({method: RequestMethod.Get, headers: new Headers()}); }
|
2015-06-13 00:50:19 -04:00
|
|
|
}
|