feat(common): two missing features in HttpClient (#17996)

- Add params to HttpRequest API
- Add optional description to testing APIs
This commit is contained in:
Alex Rickabaugh
2017-07-07 14:56:36 -07:00
committed by Jason Aden
parent 37797e2b4e
commit c81ad9d19d
14 changed files with 487 additions and 198 deletions

View File

@ -15,6 +15,7 @@ import {map} from 'rxjs/operator/map';
import {HttpHandler} from './backend';
import {HttpHeaders} from './headers';
import {HttpParams} from './params';
import {HttpRequest} from './request';
import {HttpEvent, HttpEventType, HttpResponse} from './response';
@ -27,6 +28,7 @@ function addBody<T>(
options: {
headers?: HttpHeaders,
observe?: HttpObserve,
params?: HttpParams,
responseType?: 'arraybuffer' | 'blob' | 'json' | 'text',
withCredentials?: boolean,
},
@ -35,6 +37,7 @@ function addBody<T>(
body,
headers: options.headers,
observe: options.observe,
params: options.params,
responseType: options.responseType,
withCredentials: options.withCredentials,
};
@ -59,70 +62,75 @@ export class HttpClient {
body?: any,
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType: 'arraybuffer', withCredentials?: boolean,
}): Observable<ArrayBuffer>;
request(method: string, url: string, options: {
body?: any,
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType: 'blob', withCredentials?: boolean,
}): Observable<Blob>;
request(method: string, url: string, options: {
body?: any,
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType: 'text', withCredentials?: boolean,
}): Observable<string>;
request(method: string, url: string, options: {
body?: any,
headers?: HttpHeaders,
params?: HttpParams,
observe: 'events',
responseType: 'arraybuffer', withCredentials?: boolean,
}): Observable<HttpEvent<ArrayBuffer>>;
request(method: string, url: string, options: {
body?: any,
headers?: HttpHeaders,
observe: 'events',
observe: 'events', params?: HttpParams,
responseType: 'blob', withCredentials?: boolean,
}): Observable<HttpEvent<Blob>>;
request(method: string, url: string, options: {
body?: any,
headers?: HttpHeaders,
observe: 'events',
observe: 'events', params?: HttpParams,
responseType: 'text', withCredentials?: boolean,
}): Observable<HttpEvent<string>>;
request<R>(method: string, url: string, options: {
body?: any,
headers?: HttpHeaders,
observe: 'events', responseType?: 'json', withCredentials?: boolean,
observe: 'events', params?: HttpParams, responseType?: 'json', withCredentials?: boolean,
}): Observable<HttpEvent<R>>;
request(method: string, url: string, options: {
body?: any,
headers?: HttpHeaders,
observe: 'response',
observe: 'response', params?: HttpParams,
responseType: 'arraybuffer', withCredentials?: boolean,
}): Observable<HttpResponse<ArrayBuffer>>;
request(method: string, url: string, options: {
body?: any,
headers?: HttpHeaders,
observe: 'response',
observe: 'response', params?: HttpParams,
responseType: 'blob', withCredentials?: boolean,
}): Observable<HttpResponse<Blob>>;
request(method: string, url: string, options: {
body?: any,
headers?: HttpHeaders,
observe: 'response',
observe: 'response', params?: HttpParams,
responseType: 'text', withCredentials?: boolean,
}): Observable<HttpResponse<string>>;
request<R>(method: string, url: string, options: {
body?: any,
headers?: HttpHeaders,
observe: 'response', responseType?: 'json', withCredentials?: boolean,
observe: 'response', params?: HttpParams, responseType?: 'json', withCredentials?: boolean,
}): Observable<HttpResponse<R>>;
request(method: string, url: string, options?: {
body?: any,
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType?: 'json',
withCredentials?: boolean,
}): Observable<Object>;
@ -130,12 +138,14 @@ export class HttpClient {
body?: any,
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType?: 'json',
withCredentials?: boolean,
}): Observable<R>;
request(method: string, url: string, options?: {
body?: any,
headers?: HttpHeaders,
params?: HttpParams,
observe?: HttpObserve,
responseType?: 'arraybuffer'|'blob'|'json'|'text',
withCredentials?: boolean,
@ -175,6 +185,7 @@ export class HttpClient {
body?: any,
headers?: HttpHeaders,
observe?: HttpObserve,
params?: HttpParams,
responseType?: 'arraybuffer'|'blob'|'json'|'text',
withCredentials?: boolean,
} = {}): Observable<any> {
@ -190,6 +201,7 @@ export class HttpClient {
// provided.
req = new HttpRequest(first, url !, options.body || null, {
headers: options.headers,
params: options.params,
// By default, JSON is assumed to be returned for all calls.
responseType: options.responseType || 'json',
withCredentials: options.withCredentials,
@ -266,73 +278,78 @@ export class HttpClient {
delete (url: string, options: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType: 'arraybuffer', withCredentials?: boolean,
}): Observable<ArrayBuffer>;
delete (url: string, options: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType: 'blob', withCredentials?: boolean,
}): Observable<Blob>;
delete (url: string, options: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType: 'text', withCredentials?: boolean,
}): Observable<string>;
delete (url: string, options: {
headers?: HttpHeaders,
observe: 'events',
observe: 'events', params?: HttpParams,
responseType: 'arraybuffer', withCredentials?: boolean,
}): Observable<HttpEvent<ArrayBuffer>>;
delete (url: string, options: {
headers?: HttpHeaders,
observe: 'events',
observe: 'events', params?: HttpParams,
responseType: 'blob', withCredentials?: boolean,
}): Observable<HttpEvent<Blob>>;
delete (url: string, options: {
headers?: HttpHeaders,
observe: 'events',
observe: 'events', params?: HttpParams,
responseType: 'text', withCredentials?: boolean,
}): Observable<HttpEvent<string>>;
delete (url: string, options: {
headers?: HttpHeaders,
observe: 'events', responseType?: 'json', withCredentials?: boolean,
observe: 'events', params?: HttpParams, responseType?: 'json', withCredentials?: boolean,
}): Observable<HttpEvent<Object>>;
delete<T>(url: string, options: {
headers?: HttpHeaders,
observe: 'events', responseType?: 'json', withCredentials?: boolean,
observe: 'events', params?: HttpParams, responseType?: 'json', withCredentials?: boolean,
}): Observable<HttpEvent<T>>;
delete (url: string, options: {
headers?: HttpHeaders,
observe: 'response',
observe: 'response', params?: HttpParams,
responseType: 'arraybuffer', withCredentials?: boolean,
}): Observable<HttpResponse<ArrayBuffer>>;
delete (url: string, options: {
headers?: HttpHeaders,
observe: 'response',
observe: 'response', params?: HttpParams,
responseType: 'blob', withCredentials?: boolean,
}): Observable<HttpResponse<Blob>>;
delete (url: string, options: {
headers?: HttpHeaders,
observe: 'response',
observe: 'response', params?: HttpParams,
responseType: 'text', withCredentials?: boolean,
}): Observable<HttpResponse<string>>;
delete (url: string, options: {
headers?: HttpHeaders,
observe: 'response', responseType?: 'json', withCredentials?: boolean,
observe: 'response', params?: HttpParams, responseType?: 'json', withCredentials?: boolean,
}): Observable<HttpResponse<Object>>;
delete<T>(url: string, options: {
headers?: HttpHeaders,
observe: 'response', responseType?: 'json', withCredentials?: boolean,
observe: 'response', params?: HttpParams, responseType?: 'json', withCredentials?: boolean,
}): Observable<HttpResponse<T>>;
delete (url: string, options?: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType?: 'json',
withCredentials?: boolean,
}): Observable<Object>;
delete<T>(url: string, options?: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType?: 'json',
withCredentials?: boolean,
}): Observable<T>;
@ -344,6 +361,7 @@ export class HttpClient {
delete (url: string, options: {
headers?: HttpHeaders,
observe?: HttpObserve,
params?: HttpParams,
responseType?: 'arraybuffer'|'blob'|'json'|'text',
withCredentials?: boolean,
} = {}): Observable<any> {
@ -353,73 +371,78 @@ export class HttpClient {
get(url: string, options: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType: 'arraybuffer', withCredentials?: boolean,
}): Observable<ArrayBuffer>;
get(url: string, options: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType: 'blob', withCredentials?: boolean,
}): Observable<Blob>;
get(url: string, options: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType: 'text', withCredentials?: boolean,
}): Observable<string>;
get(url: string, options: {
headers?: HttpHeaders,
observe: 'events',
observe: 'events', params?: HttpParams,
responseType: 'arraybuffer', withCredentials?: boolean,
}): Observable<HttpEvent<ArrayBuffer>>;
get(url: string, options: {
headers?: HttpHeaders,
observe: 'events',
observe: 'events', params?: HttpParams,
responseType: 'blob', withCredentials?: boolean,
}): Observable<HttpEvent<Blob>>;
get(url: string, options: {
headers?: HttpHeaders,
observe: 'events',
observe: 'events', params?: HttpParams,
responseType: 'text', withCredentials?: boolean,
}): Observable<HttpEvent<string>>;
get(url: string, options: {
headers?: HttpHeaders,
observe: 'events', responseType?: 'json', withCredentials?: boolean,
observe: 'events', params?: HttpParams, responseType?: 'json', withCredentials?: boolean,
}): Observable<HttpEvent<Object>>;
get<T>(url: string, options: {
headers?: HttpHeaders,
observe: 'events', responseType?: 'json', withCredentials?: boolean,
observe: 'events', params?: HttpParams, responseType?: 'json', withCredentials?: boolean,
}): Observable<HttpEvent<T>>;
get(url: string, options: {
headers?: HttpHeaders,
observe: 'response',
observe: 'response', params?: HttpParams,
responseType: 'arraybuffer', withCredentials?: boolean,
}): Observable<HttpResponse<ArrayBuffer>>;
get(url: string, options: {
headers?: HttpHeaders,
observe: 'response',
observe: 'response', params?: HttpParams,
responseType: 'blob', withCredentials?: boolean,
}): Observable<HttpResponse<Blob>>;
get(url: string, options: {
headers?: HttpHeaders,
observe: 'response',
observe: 'response', params?: HttpParams,
responseType: 'text', withCredentials?: boolean,
}): Observable<HttpResponse<string>>;
get(url: string, options: {
headers?: HttpHeaders,
observe: 'response', responseType?: 'json', withCredentials?: boolean,
observe: 'response', params?: HttpParams, responseType?: 'json', withCredentials?: boolean,
}): Observable<HttpResponse<Object>>;
get<T>(url: string, options: {
headers?: HttpHeaders,
observe: 'response', responseType?: 'json', withCredentials?: boolean,
observe: 'response', params?: HttpParams, responseType?: 'json', withCredentials?: boolean,
}): Observable<HttpResponse<T>>;
get(url: string, options?: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType?: 'json',
withCredentials?: boolean,
}): Observable<Object>;
get<T>(url: string, options?: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType?: 'json',
withCredentials?: boolean,
}): Observable<T>;
@ -431,6 +454,7 @@ export class HttpClient {
get(url: string, options: {
headers?: HttpHeaders,
observe?: HttpObserve,
params?: HttpParams,
responseType?: 'arraybuffer'|'blob'|'json'|'text',
withCredentials?: boolean,
} = {}): Observable<any> {
@ -440,73 +464,78 @@ export class HttpClient {
head(url: string, options: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType: 'arraybuffer', withCredentials?: boolean,
}): Observable<ArrayBuffer>;
head(url: string, options: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType: 'blob', withCredentials?: boolean,
}): Observable<Blob>;
head(url: string, options: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType: 'text', withCredentials?: boolean,
}): Observable<string>;
head(url: string, options: {
headers?: HttpHeaders,
observe: 'events',
observe: 'events', params?: HttpParams,
responseType: 'arraybuffer', withCredentials?: boolean,
}): Observable<HttpEvent<ArrayBuffer>>;
head(url: string, options: {
headers?: HttpHeaders,
observe: 'events',
observe: 'events', params?: HttpParams,
responseType: 'blob', withCredentials?: boolean,
}): Observable<HttpEvent<Blob>>;
head(url: string, options: {
headers?: HttpHeaders,
observe: 'events',
observe: 'events', params?: HttpParams,
responseType: 'text', withCredentials?: boolean,
}): Observable<HttpEvent<string>>;
head(url: string, options: {
headers?: HttpHeaders,
observe: 'events', responseType?: 'json', withCredentials?: boolean,
observe: 'events', params?: HttpParams, responseType?: 'json', withCredentials?: boolean,
}): Observable<HttpEvent<Object>>;
head<T>(url: string, options: {
headers?: HttpHeaders,
observe: 'events', responseType?: 'json', withCredentials?: boolean,
observe: 'events', params?: HttpParams, responseType?: 'json', withCredentials?: boolean,
}): Observable<HttpEvent<T>>;
head(url: string, options: {
headers?: HttpHeaders,
observe: 'response',
observe: 'response', params?: HttpParams,
responseType: 'arraybuffer', withCredentials?: boolean,
}): Observable<HttpResponse<ArrayBuffer>>;
head(url: string, options: {
headers?: HttpHeaders,
observe: 'response',
observe: 'response', params?: HttpParams,
responseType: 'blob', withCredentials?: boolean,
}): Observable<HttpResponse<Blob>>;
head(url: string, options: {
headers?: HttpHeaders,
observe: 'response',
observe: 'response', params?: HttpParams,
responseType: 'text', withCredentials?: boolean,
}): Observable<HttpResponse<string>>;
head(url: string, options: {
headers?: HttpHeaders,
observe: 'response', responseType?: 'json', withCredentials?: boolean,
observe: 'response', params?: HttpParams, responseType?: 'json', withCredentials?: boolean,
}): Observable<HttpResponse<Object>>;
head<T>(url: string, options: {
headers?: HttpHeaders,
observe: 'response', responseType?: 'json', withCredentials?: boolean,
observe: 'response', params?: HttpParams, responseType?: 'json', withCredentials?: boolean,
}): Observable<HttpResponse<T>>;
head(url: string, options?: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType?: 'json',
withCredentials?: boolean,
}): Observable<Object>;
head<T>(url: string, options?: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType?: 'json',
withCredentials?: boolean,
}): Observable<T>;
@ -518,14 +547,15 @@ export class HttpClient {
head(url: string, options: {
headers?: HttpHeaders,
observe?: HttpObserve,
params?: HttpParams,
responseType?: 'arraybuffer'|'blob'|'json'|'text',
withCredentials?: boolean,
} = {}): Observable<any> {
return this.request<any>('HEAD', url, options as any);
}
jsonp(url: string): Observable<any>;
jsonp<T>(url: string): Observable<T>;
jsonp(url: string, callbackParam: string): Observable<any>;
jsonp<T>(url: string, callbackParam: string): Observable<T>;
/**
* Constructs an `Observable` which, when subscribed, will cause a request
* with the special method `JSONP` to be dispatched via the interceptor pipeline.
@ -534,8 +564,9 @@ export class HttpClient {
* If no such interceptor is reached, then the `JSONP` request will likely be
* rejected by the configured backend.
*/
jsonp<T>(url: string): Observable<T> {
jsonp<T>(url: string, callbackParam: string): Observable<T> {
return this.request<any>('JSONP', url, {
params: new HttpParams().append(callbackParam, 'JSONP_CALLBACK'),
observe: 'body',
responseType: 'json',
});
@ -544,73 +575,78 @@ export class HttpClient {
options(url: string, options: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType: 'arraybuffer', withCredentials?: boolean,
}): Observable<ArrayBuffer>;
options(url: string, options: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType: 'blob', withCredentials?: boolean,
}): Observable<Blob>;
options(url: string, options: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType: 'text', withCredentials?: boolean,
}): Observable<string>;
options(url: string, options: {
headers?: HttpHeaders,
observe: 'events',
observe: 'events', params?: HttpParams,
responseType: 'arraybuffer', withCredentials?: boolean,
}): Observable<HttpEvent<ArrayBuffer>>;
options(url: string, options: {
headers?: HttpHeaders,
observe: 'events',
observe: 'events', params?: HttpParams,
responseType: 'blob', withCredentials?: boolean,
}): Observable<HttpEvent<Blob>>;
options(url: string, options: {
headers?: HttpHeaders,
observe: 'events',
observe: 'events', params?: HttpParams,
responseType: 'text', withCredentials?: boolean,
}): Observable<HttpEvent<string>>;
options(url: string, options: {
headers?: HttpHeaders,
observe: 'events', responseType?: 'json', withCredentials?: boolean,
observe: 'events', params?: HttpParams, responseType?: 'json', withCredentials?: boolean,
}): Observable<HttpEvent<Object>>;
options<T>(url: string, options: {
headers?: HttpHeaders,
observe: 'events', responseType?: 'json', withCredentials?: boolean,
observe: 'events', params?: HttpParams, responseType?: 'json', withCredentials?: boolean,
}): Observable<HttpEvent<T>>;
options(url: string, options: {
headers?: HttpHeaders,
observe: 'response',
observe: 'response', params?: HttpParams,
responseType: 'arraybuffer', withCredentials?: boolean,
}): Observable<HttpResponse<ArrayBuffer>>;
options(url: string, options: {
headers?: HttpHeaders,
observe: 'response',
observe: 'response', params?: HttpParams,
responseType: 'blob', withCredentials?: boolean,
}): Observable<HttpResponse<Blob>>;
options(url: string, options: {
headers?: HttpHeaders,
observe: 'response',
observe: 'response', params?: HttpParams,
responseType: 'text', withCredentials?: boolean,
}): Observable<HttpResponse<string>>;
options(url: string, options: {
headers?: HttpHeaders,
observe: 'response', responseType?: 'json', withCredentials?: boolean,
observe: 'response', params?: HttpParams, responseType?: 'json', withCredentials?: boolean,
}): Observable<HttpResponse<Object>>;
options<T>(url: string, options: {
headers?: HttpHeaders,
observe: 'response', responseType?: 'json', withCredentials?: boolean,
observe: 'response', params?: HttpParams, responseType?: 'json', withCredentials?: boolean,
}): Observable<HttpResponse<T>>;
options(url: string, options?: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType?: 'json',
withCredentials?: boolean,
}): Observable<Object>;
options<T>(url: string, options?: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType?: 'json',
withCredentials?: boolean,
}): Observable<T>;
@ -622,6 +658,7 @@ export class HttpClient {
options(url: string, options: {
headers?: HttpHeaders,
observe?: HttpObserve,
params?: HttpParams,
responseType?: 'arraybuffer'|'blob'|'json'|'text',
withCredentials?: boolean,
} = {}): Observable<any> {
@ -631,73 +668,78 @@ export class HttpClient {
patch(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType: 'arraybuffer', withCredentials?: boolean,
}): Observable<ArrayBuffer>;
patch(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType: 'blob', withCredentials?: boolean,
}): Observable<Blob>;
patch(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType: 'text', withCredentials?: boolean,
}): Observable<string>;
patch(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe: 'events',
observe: 'events', params?: HttpParams,
responseType: 'arraybuffer', withCredentials?: boolean,
}): Observable<HttpEvent<ArrayBuffer>>;
patch(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe: 'events',
observe: 'events', params?: HttpParams,
responseType: 'blob', withCredentials?: boolean,
}): Observable<HttpEvent<Blob>>;
patch(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe: 'events',
observe: 'events', params?: HttpParams,
responseType: 'text', withCredentials?: boolean,
}): Observable<HttpEvent<string>>;
patch(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe: 'events', responseType?: 'json', withCredentials?: boolean,
observe: 'events', params?: HttpParams, responseType?: 'json', withCredentials?: boolean,
}): Observable<HttpEvent<Object>>;
patch<T>(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe: 'events', responseType?: 'json', withCredentials?: boolean,
observe: 'events', params?: HttpParams, responseType?: 'json', withCredentials?: boolean,
}): Observable<HttpEvent<T>>;
patch(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe: 'response',
observe: 'response', params?: HttpParams,
responseType: 'arraybuffer', withCredentials?: boolean,
}): Observable<HttpResponse<ArrayBuffer>>;
patch(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe: 'response',
observe: 'response', params?: HttpParams,
responseType: 'blob', withCredentials?: boolean,
}): Observable<HttpResponse<Blob>>;
patch(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe: 'response',
observe: 'response', params?: HttpParams,
responseType: 'text', withCredentials?: boolean,
}): Observable<HttpResponse<string>>;
patch(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe: 'response', responseType?: 'json', withCredentials?: boolean,
observe: 'response', params?: HttpParams, responseType?: 'json', withCredentials?: boolean,
}): Observable<HttpResponse<Object>>;
patch<T>(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe: 'response', responseType?: 'json', withCredentials?: boolean,
observe: 'response', params?: HttpParams, responseType?: 'json', withCredentials?: boolean,
}): Observable<HttpResponse<T>>;
patch(url: string, body: any|null, options?: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType?: 'json',
withCredentials?: boolean,
}): Observable<Object>;
patch<T>(url: string, body: any|null, options?: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType?: 'json',
withCredentials?: boolean,
}): Observable<T>;
@ -709,6 +751,7 @@ export class HttpClient {
patch(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe?: HttpObserve,
params?: HttpParams,
responseType?: 'arraybuffer'|'blob'|'json'|'text',
withCredentials?: boolean,
} = {}): Observable<any> {
@ -718,73 +761,78 @@ export class HttpClient {
post(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType: 'arraybuffer', withCredentials?: boolean,
}): Observable<ArrayBuffer>;
post(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType: 'blob', withCredentials?: boolean,
}): Observable<Blob>;
post(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType: 'text', withCredentials?: boolean,
}): Observable<string>;
post(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe: 'events',
observe: 'events', params?: HttpParams,
responseType: 'arraybuffer', withCredentials?: boolean,
}): Observable<HttpEvent<ArrayBuffer>>;
post(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe: 'events',
observe: 'events', params?: HttpParams,
responseType: 'blob', withCredentials?: boolean,
}): Observable<HttpEvent<Blob>>;
post(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe: 'events',
observe: 'events', params?: HttpParams,
responseType: 'text', withCredentials?: boolean,
}): Observable<HttpEvent<string>>;
post(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe: 'events', responseType?: 'json', withCredentials?: boolean,
observe: 'events', params?: HttpParams, responseType?: 'json', withCredentials?: boolean,
}): Observable<HttpEvent<Object>>;
post<T>(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe: 'events', responseType?: 'json', withCredentials?: boolean,
observe: 'events', params?: HttpParams, responseType?: 'json', withCredentials?: boolean,
}): Observable<HttpEvent<T>>;
post(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe: 'response',
observe: 'response', params?: HttpParams,
responseType: 'arraybuffer', withCredentials?: boolean,
}): Observable<HttpResponse<ArrayBuffer>>;
post(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe: 'response',
observe: 'response', params?: HttpParams,
responseType: 'blob', withCredentials?: boolean,
}): Observable<HttpResponse<Blob>>;
post(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe: 'response',
observe: 'response', params?: HttpParams,
responseType: 'text', withCredentials?: boolean,
}): Observable<HttpResponse<string>>;
post(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe: 'response', responseType?: 'json', withCredentials?: boolean,
observe: 'response', params?: HttpParams, responseType?: 'json', withCredentials?: boolean,
}): Observable<HttpResponse<Object>>;
post<T>(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe: 'response', responseType?: 'json', withCredentials?: boolean,
observe: 'response', params?: HttpParams, responseType?: 'json', withCredentials?: boolean,
}): Observable<HttpResponse<T>>;
post(url: string, body: any|null, options?: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType?: 'json',
withCredentials?: boolean,
}): Observable<Object>;
post<T>(url: string, body: any|null, options?: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType?: 'json',
withCredentials?: boolean,
}): Observable<T>;
@ -796,6 +844,7 @@ export class HttpClient {
post(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe?: HttpObserve,
params?: HttpParams,
responseType?: 'arraybuffer'|'blob'|'json'|'text',
withCredentials?: boolean,
} = {}): Observable<any> {
@ -805,36 +854,39 @@ export class HttpClient {
put(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType: 'arraybuffer', withCredentials?: boolean,
}): Observable<ArrayBuffer>;
put(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType: 'blob', withCredentials?: boolean,
}): Observable<Blob>;
put(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType: 'text', withCredentials?: boolean,
}): Observable<string>;
put(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe: 'events',
observe: 'events', params?: HttpParams,
responseType: 'arraybuffer', withCredentials?: boolean,
}): Observable<HttpEvent<ArrayBuffer>>;
put(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe: 'events',
observe: 'events', params?: HttpParams,
responseType: 'blob', withCredentials?: boolean,
}): Observable<HttpEvent<Blob>>;
put(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe: 'events',
observe: 'events', params?: HttpParams,
responseType: 'text', withCredentials?: boolean,
}): Observable<HttpEvent<string>>;
put(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe: 'events', responseType?: 'json', withCredentials?: boolean,
observe: 'events', params?: HttpParams, responseType?: 'json', withCredentials?: boolean,
}): Observable<HttpEvent<Object>>;
put<T>(url: string, body: any|null, options: {
headers?: HttpHeaders,
@ -842,36 +894,38 @@ export class HttpClient {
}): Observable<HttpEvent<T>>;
put(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe: 'response',
observe: 'response', params?: HttpParams,
responseType: 'arraybuffer', withCredentials?: boolean,
}): Observable<HttpResponse<ArrayBuffer>>;
put(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe: 'response',
observe: 'response', params?: HttpParams,
responseType: 'blob', withCredentials?: boolean,
}): Observable<HttpResponse<Blob>>;
put(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe: 'response',
observe: 'response', params?: HttpParams,
responseType: 'text', withCredentials?: boolean,
}): Observable<HttpResponse<string>>;
put(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe: 'response', responseType?: 'json', withCredentials?: boolean,
observe: 'response', params?: HttpParams, responseType?: 'json', withCredentials?: boolean,
}): Observable<HttpResponse<Object>>;
put<T>(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe: 'response', responseType?: 'json', withCredentials?: boolean,
observe: 'response', params?: HttpParams, responseType?: 'json', withCredentials?: boolean,
}): Observable<HttpResponse<T>>;
put(url: string, body: any|null, options?: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType?: 'json',
withCredentials?: boolean,
}): Observable<Object>;
put<T>(url: string, body: any|null, options?: {
headers?: HttpHeaders,
observe?: 'body',
params?: HttpParams,
responseType?: 'json',
withCredentials?: boolean,
}): Observable<T>;
@ -883,6 +937,7 @@ export class HttpClient {
put(url: string, body: any|null, options: {
headers?: HttpHeaders,
observe?: HttpObserve,
params?: HttpParams,
responseType?: 'arraybuffer'|'blob'|'json'|'text',
withCredentials?: boolean,
} = {}): Observable<any> {

View File

@ -73,7 +73,7 @@ export class JsonpClientBackend implements HttpBackend {
// callback placeholder in the URL with the name. Care has to be taken here to ensure
// a trailing &, if matched, gets inserted back into the URL in the correct place.
const callback = this.nextCallback();
const url = req.url.replace(/=JSONP_CALLBACK(&|$)/, `=${callback}$1`);
const url = req.urlWithParams.replace(/=JSONP_CALLBACK(&|$)/, `=${callback}$1`);
// Construct the <script> tag and point it at the URL.
const node = this.document.createElement('script');

View File

@ -9,11 +9,11 @@
/**
* A codec for encoding and decoding parameters in URLs.
*
* Used by `HttpUrlEncodedBody`.
* Used by `HttpParams`.
*
* @experimental
**/
export interface HttpUrlParameterCodec {
export interface HttpParameterCodec {
encodeKey(key: string): string;
encodeValue(value: string): string;
@ -22,12 +22,12 @@ export interface HttpUrlParameterCodec {
}
/**
* A `HttpUrlParameterCodec` that uses `encodeURIComponent` and `decodeURIComponent` to
* A `HttpParameterCodec` that uses `encodeURIComponent` and `decodeURIComponent` to
* serialize and parse URL parameter keys and values.
*
* @experimental
*/
export class HttpStandardUrlParameterCodec implements HttpUrlParameterCodec {
export class HttpUrlEncodingCodec implements HttpParameterCodec {
encodeKey(k: string): string { return standardEncoding(k); }
encodeValue(v: string): string { return standardEncoding(v); }
@ -38,7 +38,7 @@ export class HttpStandardUrlParameterCodec implements HttpUrlParameterCodec {
}
function paramParser(rawParams: string, codec: HttpUrlParameterCodec): Map<string, string[]> {
function paramParser(rawParams: string, codec: HttpParameterCodec): Map<string, string[]> {
const map = new Map<string, string[]>();
if (rawParams.length > 0) {
const params: string[] = rawParams.split('&');
@ -74,25 +74,24 @@ interface Update {
}
/**
* An HTTP request/response body that represents serialized parameters in urlencoded form,
* An HTTP request/response body that represents serialized parameters,
* per the MIME type `application/x-www-form-urlencoded`.
*
* This class is immuatable - all mutation operations return a new instance.
*
* @experimental
*/
export class HttpUrlEncodedBody {
export class HttpParams {
private map: Map<string, string[]>|null;
private encoder: HttpUrlParameterCodec;
private encoder: HttpParameterCodec;
private updates: Update[]|null = null;
private cloneFrom: HttpUrlEncodedBody|null = null;
private cloneFrom: HttpParams|null = null;
constructor(options: {
fromString?: string,
encoder?: HttpUrlParameterCodec,
encoder?: HttpParameterCodec,
} = {}) {
(this as any)['__HttpUrlEncodedBody'] = true;
this.encoder = options.encoder || new HttpStandardUrlParameterCodec();
this.encoder = options.encoder || new HttpUrlEncodingCodec();
this.map = !!options.fromString ? paramParser(options.fromString, this.encoder) : null;
}
@ -124,7 +123,7 @@ export class HttpUrlEncodedBody {
/**
* Get all the parameter names for this body.
*/
params(): string[] {
keys(): string[] {
this.init();
return Array.from(this.map !.keys());
}
@ -132,25 +131,19 @@ export class HttpUrlEncodedBody {
/**
* Construct a new body with an appended value for the given parameter name.
*/
append(param: string, value: string): HttpUrlEncodedBody {
return this.clone({param, value, op: 'a'});
}
append(param: string, value: string): HttpParams { return this.clone({param, value, op: 'a'}); }
/**
* Construct a new body with a new value for the given parameter name.
*/
set(param: string, value: string): HttpUrlEncodedBody {
return this.clone({param, value, op: 's'});
}
set(param: string, value: string): HttpParams { return this.clone({param, value, op: 's'}); }
/**
* Construct a new body with either the given value for the given parameter
* removed, if a value is given, or all values for the given parameter removed
* if not.
*/
delete (param: string, value?: string): HttpUrlEncodedBody {
return this.clone({param, value, op: 'd'});
}
delete (param: string, value?: string): HttpParams { return this.clone({param, value, op: 'd'}); }
/**
* Serialize the body to an encoded string, where key-value pairs (separated by `=`) are
@ -158,7 +151,7 @@ export class HttpUrlEncodedBody {
*/
toString(): string {
this.init();
return this.params()
return this.keys()
.map(key => {
const eKey = this.encoder.encodeKey(key);
return this.map !.get(key) !.map(value => eKey + '=' + this.encoder.encodeValue(value))
@ -167,8 +160,8 @@ export class HttpUrlEncodedBody {
.join('&');
}
private clone(update: Update): HttpUrlEncodedBody {
const clone = new HttpUrlEncodedBody({encoder: this.encoder});
private clone(update: Update): HttpParams {
const clone = new HttpParams({encoder: this.encoder});
clone.cloneFrom = this.cloneFrom || this;
clone.updates = (this.updates || []).concat([update]);
return clone;
@ -180,8 +173,7 @@ export class HttpUrlEncodedBody {
}
if (this.cloneFrom !== null) {
this.cloneFrom.init();
this.cloneFrom.params().forEach(
key => this.map !.set(key, this.cloneFrom !.map !.get(key) !));
this.cloneFrom.keys().forEach(key => this.map !.set(key, this.cloneFrom !.map !.get(key) !));
this.updates !.forEach(update => {
switch (update.op) {
case 'a':

View File

@ -7,6 +7,7 @@
*/
import {HttpHeaders} from './headers';
import {HttpParams} from './params';
/**
* Construction interface for `HttpRequest`s.
@ -14,7 +15,7 @@ import {HttpHeaders} from './headers';
* All values are optional and will override default values if provided.
*/
interface HttpRequestInit {
headers?: HttpHeaders, reportProgress?: boolean,
headers?: HttpHeaders, reportProgress?: boolean, params?: HttpParams,
responseType?: 'arraybuffer'|'blob'|'json'|'text', withCredentials?: boolean,
}
@ -61,10 +62,6 @@ function isFormData(value: any): value is FormData {
return typeof FormData !== 'undefined' && value instanceof FormData;
}
function isUrlEncodedBody(value: any): value is Object {
return typeof value === 'object' && value['__HttpUrlEncodedBody'];
}
/**
* An outgoing HTTP request with an optional typed body.
*
@ -116,34 +113,49 @@ export class HttpRequest<T> {
*/
readonly method: string;
/**
* Outgoing URL parameters.
*/
readonly params: HttpParams;
/**
* The outgoing URL with all URL parameters set.
*/
readonly urlWithParams: string;
constructor(method: 'DELETE'|'GET'|'HEAD'|'JSONP'|'OPTIONS', url: string, init?: {
headers?: HttpHeaders,
reportProgress?: boolean,
params?: HttpParams,
responseType?: 'arraybuffer'|'blob'|'json'|'text',
withCredentials?: boolean,
});
constructor(method: 'POST'|'PUT'|'PATCH', url: string, body: T|null, init?: {
headers?: HttpHeaders,
reportProgress?: boolean,
params?: HttpParams,
responseType?: 'arraybuffer'|'blob'|'json'|'text',
withCredentials?: boolean,
});
constructor(method: string, url: string, body: T|null, init?: {
headers?: HttpHeaders,
reportProgress?: boolean,
params?: HttpParams,
responseType?: 'arraybuffer'|'blob'|'json'|'text',
withCredentials?: boolean,
});
constructor(
method: string, public url: string, third?: T|{
method: string, readonly url: string, third?: T|{
headers?: HttpHeaders,
reportProgress?: boolean,
params?: HttpParams,
responseType?: 'arraybuffer'|'blob'|'json'|'text',
withCredentials?: boolean,
}|null,
fourth?: {
headers?: HttpHeaders,
reportProgress?: boolean,
params?: HttpParams,
responseType?: 'arraybuffer'|'blob'|'json'|'text',
withCredentials?: boolean,
}) {
@ -178,12 +190,41 @@ export class HttpRequest<T> {
if (!!options.headers) {
this.headers = options.headers;
}
if (!!options.params) {
this.params = options.params;
}
}
// If no headers have been passed in, construct a new HttpHeaders instance.
if (!this.headers) {
this.headers = new HttpHeaders();
}
// If no parameters have been passed in, construct a new HttpUrlEncodedParams instance.
if (!this.params) {
this.params = new HttpParams();
this.urlWithParams = url;
} else {
// Encode the parameters to a string in preparation for inclusion in the URL.
const params = this.params.toString();
if (params.length === 0) {
// No parameters, the visible URL is just the URL given at creation time.
this.urlWithParams = url;
} else {
// Does the URL already have query parameters? Look for '?'.
const qIdx = url.indexOf('?');
// There are 3 cases to handle:
// 1) No existing parameters -> append '?' followed by params.
// 2) '?' exists and is followed by existing query string ->
// append '&' followed by params.
// 3) '?' exists at the end of the url -> append params directly.
// This basically amounts to determining the character, if any, with
// which to join the URL and parameters.
const sep: string = qIdx === -1 ? '?' : (qIdx < url.length - 1 ? '&' : '');
this.urlWithParams = url + sep + params;
}
}
}
/**
@ -201,9 +242,8 @@ export class HttpRequest<T> {
typeof this.body === 'string') {
return this.body;
}
// Check whether the body is an instance of HttpUrlEncodedBody, avoiding any direct
// references to the class in order to permit it being tree-shaken.
if (isUrlEncodedBody(this.body)) {
// Check whether the body is an instance of HttpUrlEncodedParams.
if (this.body instanceof HttpParams) {
return this.body.toString();
}
// Check whether the body is an object or array, and serialize with JSON if so.
@ -244,9 +284,8 @@ export class HttpRequest<T> {
if (typeof this.body === 'string') {
return 'text/plain';
}
// `HttpUrlEncodedBody` is detected specially so as to allow it to be
// tree-shaken.
if (isUrlEncodedBody(this.body)) {
// `HttpUrlEncodedParams` has its own content-type.
if (this.body instanceof HttpParams) {
return 'application/x-www-form-urlencoded;charset=UTF-8';
}
// Arrays, objects, and numbers will be encoded as JSON.
@ -262,28 +301,38 @@ export class HttpRequest<T> {
clone(update: {
headers?: HttpHeaders,
reportProgress?: boolean,
params?: HttpParams,
responseType?: 'arraybuffer'|'blob'|'json'|'text',
withCredentials?: boolean,
body?: T|null,
method?: string,
url?: string,
setHeaders?: {[name: string]: string | string[]},
setParams?: {[param: string]: string},
}): HttpRequest<T>;
clone<V>(update: {
headers?: HttpHeaders,
reportProgress?: boolean,
params?: HttpParams,
responseType?: 'arraybuffer'|'blob'|'json'|'text',
withCredentials?: boolean,
body?: V|null,
method?: string,
url?: string,
setHeaders?: {[name: string]: string | string[]},
setParams?: {[param: string]: string},
}): HttpRequest<V>;
clone(update: {
headers?: HttpHeaders,
reportProgress?: boolean,
params?: HttpParams,
responseType?: 'arraybuffer'|'blob'|'json'|'text',
withCredentials?: boolean,
body?: any|null,
method?: string,
url?: string,
setHeaders?: {[name: string]: string | string[]},
setParams?: {[param: string]: string};
} = {}): HttpRequest<any> {
// For method, url, and responseType, take the current value unless
// it is overridden in the update hash.
@ -304,9 +353,10 @@ export class HttpRequest<T> {
const reportProgress =
(update.reportProgress !== undefined) ? update.reportProgress : this.reportProgress;
// Headers may need to be cloned later if they're sealed, but being
// appended to.
// Headers and params may be appended to if `setHeaders` or
// `setParams` are used.
let headers = update.headers || this.headers;
let params = update.params || this.params;
// Check whether the caller has asked to add headers.
if (update.setHeaders !== undefined) {
@ -316,10 +366,17 @@ export class HttpRequest<T> {
.reduce((headers, name) => headers.set(name, update.setHeaders ![name]), headers);
}
// Check whether the caller has asked to set params.
if (update.setParams) {
// Set every requested param.
params = Object.keys(update.setParams)
.reduce((params, param) => params.set(param, update.setParams ![param]), params);
}
// Finally, construct the new HttpRequest using the pieces from above.
return new HttpRequest(
method, url, body, {
headers, reportProgress, responseType, withCredentials,
params, headers, reportProgress, responseType, withCredentials,
});
}
}

View File

@ -83,7 +83,7 @@ export class HttpXhrBackend implements HttpBackend {
return new Observable((observer: Observer<HttpEvent<any>>) => {
// Start by setting up the XHR object with request method, URL, and withCredentials flag.
const xhr = this.xhrFactory.build();
xhr.open(req.method, req.url);
xhr.open(req.method, req.urlWithParams);
if (!!req.withCredentials) {
xhr.withCredentials = true;
}