refactor(http): use Http.request for all http shorthand methods (#12319)
This commit is contained in:

committed by
Igor Minar

parent
d55f747858
commit
8603d9c269
@ -8,9 +8,7 @@
|
||||
|
||||
import {Injectable} from '@angular/core';
|
||||
import {Observable} from 'rxjs/Observable';
|
||||
|
||||
import {isPresent, isString} from '../src/facade/lang';
|
||||
|
||||
import {BaseRequestOptions, RequestOptions} from './base_request_options';
|
||||
import {RequestMethod} from './enums';
|
||||
import {ConnectionBackend, RequestOptionsArgs} from './interfaces';
|
||||
@ -24,7 +22,7 @@ function httpRequest(backend: ConnectionBackend, request: Request): Observable<R
|
||||
function mergeOptions(
|
||||
defaultOpts: BaseRequestOptions, providedOpts: RequestOptionsArgs, method: RequestMethod,
|
||||
url: string): RequestOptions {
|
||||
var newOptions = defaultOpts;
|
||||
const newOptions = defaultOpts;
|
||||
if (isPresent(providedOpts)) {
|
||||
// Hack so Dart can used named parameters
|
||||
return newOptions.merge(new RequestOptions({
|
||||
@ -115,7 +113,7 @@ export class Http {
|
||||
* of {@link BaseRequestOptions} before performing the request.
|
||||
*/
|
||||
request(url: string|Request, options?: RequestOptionsArgs): Observable<Response> {
|
||||
var responseObservable: any;
|
||||
let responseObservable: any;
|
||||
if (isString(url)) {
|
||||
responseObservable = httpRequest(
|
||||
this._backend,
|
||||
@ -132,8 +130,7 @@ export class Http {
|
||||
* Performs a request with `get` http method.
|
||||
*/
|
||||
get(url: string, options?: RequestOptionsArgs): Observable<Response> {
|
||||
return httpRequest(
|
||||
this._backend,
|
||||
return this.request(
|
||||
new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, url)));
|
||||
}
|
||||
|
||||
@ -141,28 +138,25 @@ export class Http {
|
||||
* Performs a request with `post` http method.
|
||||
*/
|
||||
post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
|
||||
return httpRequest(
|
||||
this._backend, new Request(mergeOptions(
|
||||
this._defaultOptions.merge(new RequestOptions({body: body})), options,
|
||||
RequestMethod.Post, url)));
|
||||
return this.request(new Request(mergeOptions(
|
||||
this._defaultOptions.merge(new RequestOptions({body: body})), options, RequestMethod.Post,
|
||||
url)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a request with `put` http method.
|
||||
*/
|
||||
put(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
|
||||
return httpRequest(
|
||||
this._backend, new Request(mergeOptions(
|
||||
this._defaultOptions.merge(new RequestOptions({body: body})), options,
|
||||
RequestMethod.Put, url)));
|
||||
return this.request(new Request(mergeOptions(
|
||||
this._defaultOptions.merge(new RequestOptions({body: body})), options, RequestMethod.Put,
|
||||
url)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a request with `delete` http method.
|
||||
*/
|
||||
delete (url: string, options?: RequestOptionsArgs): Observable<Response> {
|
||||
return httpRequest(
|
||||
this._backend,
|
||||
return this.request(
|
||||
new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Delete, url)));
|
||||
}
|
||||
|
||||
@ -170,18 +164,16 @@ export class Http {
|
||||
* Performs a request with `patch` http method.
|
||||
*/
|
||||
patch(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
|
||||
return httpRequest(
|
||||
this._backend, new Request(mergeOptions(
|
||||
this._defaultOptions.merge(new RequestOptions({body: body})), options,
|
||||
RequestMethod.Patch, url)));
|
||||
return this.request(new Request(mergeOptions(
|
||||
this._defaultOptions.merge(new RequestOptions({body: body})), options, RequestMethod.Patch,
|
||||
url)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a request with `head` http method.
|
||||
*/
|
||||
head(url: string, options?: RequestOptionsArgs): Observable<Response> {
|
||||
return httpRequest(
|
||||
this._backend,
|
||||
return this.request(
|
||||
new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Head, url)));
|
||||
}
|
||||
|
||||
@ -189,8 +181,7 @@ export class Http {
|
||||
* Performs a request with `options` http method.
|
||||
*/
|
||||
options(url: string, options?: RequestOptionsArgs): Observable<Response> {
|
||||
return httpRequest(
|
||||
this._backend,
|
||||
return this.request(
|
||||
new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Options, url)));
|
||||
}
|
||||
}
|
||||
@ -220,7 +211,7 @@ export class Jsonp extends Http {
|
||||
* [Security Guide](http://g.co/ng/security).
|
||||
*/
|
||||
request(url: string|Request, options?: RequestOptionsArgs): Observable<Response> {
|
||||
var responseObservable: any;
|
||||
let responseObservable: any;
|
||||
if (isString(url)) {
|
||||
url =
|
||||
new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, <string>url));
|
||||
|
Reference in New Issue
Block a user