UNPKG

x-ng2-http-interceptor

Version:
94 lines (93 loc) 4.62 kB
import { ConnectionBackend, Http, Request, Response, RequestOptions, RequestOptionsArgs } from '@angular/http'; import { Observable } from 'rxjs/Observable'; import 'rxjs/add/observable/empty'; import 'rxjs/add/observable/of'; import 'rxjs/add/observable/throw'; import 'rxjs/add/operator/catch'; import 'rxjs/add/operator/mergeMap'; import { Interceptor } from './interceptor'; import { InterceptorRequestOptionsArgs } from './interceptor-request-options-args'; import { RealResponseObservableTransformer } from './real-response-observable-transformer'; /** * Wrapper around native angular `Http` service. * Allows you to add `Interceptor`s that lets you do * 1. Transform request before it reaches the actual request, such as, add headers transparently * 2. Transform response, such as stripout the top level object & return the payload (or) raise errors if `response.status` is not `ok` * 3. To short circuit the request flow based on runtime data * 4. To selective caching/log request/responses * 5. Augment the real `Observable<Response>` that native angular `http.request(..)` returns * 6. Store intermediate data that can be shared across the interceptors * 7. Generate handcrafted response incase of error/base of your runtime decision * * The service executes methods in the interceptor chain in the following manner * 1. For each of the listed interceptor, tranforms the request by invoking\ * `beforeRequest(..)` on each interceptor in the same order they are added * 2. Invokes native angular `http.request(..)` with the result of last interceptor's `beforeRequest(..)` response * 3. Invokes `onResponse(..)` on each interceptor in the reverse order they are added * 4. The response from `onResponse(..)` of the final interceptor in the chain would be sent to subscriber */ export declare class InterceptorService extends Http { private interceptors; private _realResponseObservableTransformer; constructor(backend: ConnectionBackend, defaultOptions: RequestOptions); /** Parent overrides **/ request(url: string | Request, options?: RequestOptionsArgs | InterceptorRequestOptionsArgs): Observable<Response>; /** * Performs a request with `get` http method. */ get(url: string, options?: RequestOptionsArgs | InterceptorRequestOptionsArgs): Observable<Response>; /** * Performs a request with `post` http method. */ post(url: string, body: any, options?: RequestOptionsArgs | InterceptorRequestOptionsArgs): Observable<Response>; /** * Performs a request with `put` http method. */ put(url: string, body: any, options?: RequestOptionsArgs | InterceptorRequestOptionsArgs): Observable<Response>; /** * Performs a request with `delete` http method. */ delete(url: string, options?: RequestOptionsArgs | InterceptorRequestOptionsArgs): Observable<Response>; /** * Performs a request with `patch` http method. */ patch(url: string, body: any, options?: RequestOptionsArgs | InterceptorRequestOptionsArgs): Observable<Response>; /** * Performs a request with `head` http method. */ head(url: string, options?: RequestOptionsArgs | InterceptorRequestOptionsArgs): Observable<Response>; /** * Performs a request with `options` http method. */ options(url: string, options?: RequestOptionsArgs | InterceptorRequestOptionsArgs): Observable<Response>; /** * Appends this interceptor to the list of interceptors */ addInterceptor(interceptor: Interceptor): void; /** * Sets response transformer */ realResponseObservableTransformer: RealResponseObservableTransformer; /** Private functions **/ private httpRequest(request); private runBeforeInterceptors(params); private runAfterInterceptors(responseWrapper); /** * Tests whether the passed in object represents interceptor version/native request options */ private representsInterceptorFlavor(options); /** * Returns an implementation that mirrors angular `Http` interface; * This interface allows the response transformers to make calls directly to HTTP calls * without being interceted by {@code InterceptorService}; i.e `this` */ private readonly HttpDirect; private requestNative(url, options?); private getNative(url, options?); private postNative(url, body, options?); private putNative(url, body, options?); private deleteNative(url, options?); private patchNative(url, body, options?); private headNative(url, options?); private optionsNative(url, options?); }