@@ -6,6 +6,8 @@ import {Request} from './static_request';
66import { Response } from './static_response' ;
77import { XHRBackend } from './backends/xhr_backend' ;
88import { BaseRequestOptions } from './base_request_options' ;
9+ import { RequestMethods } from './enums' ;
10+ import { URLSearchParams } from './url_search_params' ;
911import * as Rx from 'rx' ;
1012
1113/**
@@ -43,9 +45,59 @@ import * as Rx from 'rx';
4345 *
4446 **/
4547
48+ function httpRequest ( backend : XHRBackend , request : Request ) {
49+ return < Rx . Observable < Response > > ( Observable . create ( observer => {
50+ var connection : Connection = backend . createConnection ( request ) ;
51+ var internalSubscription = connection . response . subscribe ( observer ) ;
52+ return ( ) => {
53+ internalSubscription . dispose ( ) ;
54+ connection . dispose ( ) ;
55+ }
56+ } ) )
57+ }
58+
4659// Abstract
4760@Injectable ( )
4861export class Http {
62+ constructor ( private backend : XHRBackend , private defaultOptions : BaseRequestOptions ) { }
63+
64+ request ( url : string , options ?: RequestOptions ) : Rx . Observable < Response > {
65+ return httpRequest ( this . backend , new Request ( url , this . defaultOptions . merge ( options ) ) ) ;
66+ }
67+
68+ get ( url : string , options ?: RequestOptions ) {
69+ return httpRequest ( this . backend , new Request ( url , this . defaultOptions . merge ( options )
70+ . merge ( { method : RequestMethods . GET } ) ) ) ;
71+ }
72+
73+ post ( url : string , body : URLSearchParams | FormData | Blob | string , options ?: RequestOptions ) {
74+ return httpRequest ( this . backend ,
75+ new Request ( url , this . defaultOptions . merge ( options )
76+
77+ . merge ( { body : body , method : RequestMethods . POST } ) ) ) ;
78+ }
79+
80+ put ( url : string , body : URLSearchParams | FormData | Blob | string , options ?: RequestOptions ) {
81+ return httpRequest ( this . backend ,
82+ new Request ( url , this . defaultOptions . merge ( options )
83+ . merge ( { body : body , method : RequestMethods . PUT } ) ) ) ;
84+ }
85+
86+ delete ( url : string , options ?: RequestOptions ) {
87+ return httpRequest ( this . backend , new Request ( url , this . defaultOptions . merge ( options )
88+ . merge ( { method : RequestMethods . DELETE } ) ) ) ;
89+ }
90+
91+ patch ( url : string , body : URLSearchParams | FormData | Blob | string , options ?: RequestOptions ) {
92+ return httpRequest ( this . backend ,
93+ new Request ( url , this . defaultOptions . merge ( options )
94+ . merge ( { body : body , method : RequestMethods . PATCH } ) ) ) ;
95+ }
96+
97+ head ( url : string , options ?: RequestOptions ) {
98+ return httpRequest ( this . backend , new Request ( url , this . defaultOptions . merge ( options )
99+ . merge ( { method : RequestMethods . HEAD } ) ) ) ;
100+ }
49101}
50102
51103var Observable ;
@@ -56,13 +108,6 @@ if (Rx.hasOwnProperty('default')) {
56108}
57109export function HttpFactory ( backend : XHRBackend , defaultOptions : BaseRequestOptions ) {
58110 return function ( url : string , options ?: RequestOptions ) {
59- return < Rx . Observable < Response > > ( Observable . create ( observer => {
60- var connection : Connection = backend . createConnection ( new Request ( url , options ) ) ;
61- var internalSubscription = connection . response . subscribe ( observer ) ;
62- return ( ) => {
63- internalSubscription . dispose ( ) ;
64- connection . dispose ( ) ;
65- }
66- } ) )
111+ return httpRequest ( backend , new Request ( url , defaultOptions . merge ( options ) ) ) ;
67112 }
68113}
0 commit comments