/**
* Constructs an `Observable` for a particular HTTP request that, when subscribed,
* fires the request through the chain of registered interceptors and on to the
* server.
*
* This method can be called in one of two ways. Either a {@link HttpRequest}
* instance can be passed directly as the only parameter, or a string URL can be
* passed as the first parameter, a method optionally as the second, and an
* options hash as the third.
*
* If a {@link HttpRequest} object is passed directly, an `Observable` of the
* raw {@link HttpEvent} stream will be returned.
*
* If a request is instead built by providing a URL, the options object
* determines the return type of `request()`. In addition to configuring
* request parameters such as the outgoing headers and/or the body, the options
* hash specifies two key pieces of information about the request: the
* `responseType` and what to `observe`.
*
* The `responseType` value determines how a successful response body will be
* parsed. If `responseType` is the default `json`, a type interface for the
* resulting object may be passed as a type parameter to `request()`.
*
* The `observe` value determines the return type of `request()`, based on what
* the consumer is interested in observing. A value of `events` will return an
* `Observable<HttpEvent>` representing the raw {@link HttpEvent} stream,
* including progress events by default. A value of `response` will return an
* `Observable<HttpResponse<T>>` where the `T` parameter of `{@link HttpResponse}
* depends on the `responseType` and any optionally provided type parameter.
* A value of `body` will return an `Observable<T>` with the same `T` body type.
*/
request(
first: string|HttpRequest<any>, method?: HttpMethod|string,
options: HttpRequestOptions<any> = {}): Observable<any> {
let req: HttpRequest<any>;
// Firstly, check whether the primary argument is an instance of `HttpRequest`.
if (first instanceof HttpRequest) {
// It is. The other arguments must be undefined (per the signatures) and can be
// ignored.
req = first as HttpRequest<any>;
} else {
// It's a string, so it represents a URL. Construct a request based on it,
// and incorporate the remaining arguments (assuming GET unless a method is
// provided.
req = new HttpRequest(first, method !, options.body || null, {
headers: options.headers,
// By default, JSON is assumed to be returned for all calls.
responseType: options.responseType || 'json',
withCredentials: options.withCredentials,
});
}
// Start with an Observable.of() the initial request, and run the handler (which
// includes all interceptors) inside a concatMap(). This way, the handler runs
// inside an Observable chain, which causes interceptors to be re-run on every
// subscription (this also makes retries re-run the handler, including interceptors).
const events$: Observable<HttpEvent<any>> =
concatMap.call(of (req), (req: HttpRequest<any>) => this.handler.handle(req));
// If coming via the API signature which accepts a previously constructed HttpRequest,
// the only option is to get the event stream. Otherwise, return the event stream if
// that is what was requested.
if (first instanceof HttpRequest || options.observe === 'events') {
return events$;
}
// The requested stream contains either the full response or the body. In either
// case, the first step is to filter the event stream to extract a stream of
// responses(s).
const res$: Observable<HttpResponse<any>> =
filter.call(events$, (event: HttpEvent<any>) => event instanceof HttpResponse);
// Decide which stream to return.
switch (options.observe || 'body') {
case 'body':
// The requested stream is the body. Map the response stream to the response
// body. This could be done more simply, but a misbehaving interceptor might
// transform the response body into a different format and ignore the requested
// responseType. Guard against this by validating that the response is of the
// requested type.
switch (req.responseType) {
case 'arraybuffer':
return map.call(res$, (res: HttpResponse<any>) => {
// Validate that the body is an ArrayBuffer.
if (res.body !== null && !(res.body instanceof ArrayBuffer)) {
throw new Error('Response is not an ArrayBuffer.');
}
return res.body;
});
case 'blob':
return map.call(res$, (res: HttpResponse<any>) => {
// Validate that the body is a Blob.
if (res.body !== null && !(res.body instanceof Blob)) {
throw new Error('Response is not a Blob.');
}
return res.body;
});
case 'text':
return map.call(res$, (res: HttpResponse<any>) => {
// Validate that the body is a string.
//.........这里部分代码省略.........
请发表评论