All files / src/HttpProviders BaseHttpProvider.ts

100% Statements 13/13
100% Branches 5/5
100% Functions 6/6
100% Lines 13/13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76                  1x 396x               27x                 88x                 144x 144x 4x     144x 1x     144x   144x                 14x   14x                                  
/**
 * @module HttpProviders
 *//** */
 
import { Observable } from 'rxjs/Observable';
import { AjaxRequest } from 'rxjs/observable/dom/AjaxObservable';
/**
 *
 */
export abstract class BaseHttpProvider {
    protected _headers: Map<string, string> = new Map();
 
    /**
     * Sets a specified HTTP header to a specified value. The header will be the same on each request.
     * @param headerName The name of the header
     * @param headerValue The value of the header
     */
    public SetGlobalHeader(headerName: string, headerValue: string) {
        this._headers.set(headerName, headerValue);
    }
 
    /**
     * Removes a specified HTTP header from the global header settings
     * @param headerName The name of the header
     *
     */
    public UnsetGlobalHeader(headerName: string) {
        this._headers.delete(headerName);
    }
 
    /**
     * Public entry point for executing Ajax calls using a specific provider
     * @param tReturnType The return type
     * @param options Additional RxJs AjaxRequest options (the global headers will be overridden)
     */
    public Ajax<T>(tReturnType: { new (...args: any[]): T }, options: AjaxRequest, additionalHeaders: {name: string, value: string}[] = []): Observable<T> {
        const headers = options.headers || [];
        for (const key of this._headers.keys()) {
            (headers as any)[key] = this._headers.get(key);
        }
 
        additionalHeaders.forEach((h) => {
            (headers as any)[h.name] = h.value;
        });
 
        options.headers = headers;
 
        return this.ajaxInner(tReturnType, options);
    }
 
    /**
     * Public entry point for uploading files using a specific provider
     * @param tReturnType The return type
     * @param options Additional RxJs AjaxRequest options (the global headers will be overridden)
     */
    public Upload<T>(tReturnType: { new (...args: any[]): T }, File: File, options: AjaxRequest & {url: string}): Observable<T> {
        options.headers = options.headers || [];
 
        return this.uploadInner(tReturnType, File, options);
    }
 
    /**
     * The inner implementation of the Ajax call
     * @param tReturnType The return type
     * @param options Additional RxJs AjaxRequest options (the global headers will be overridden)
     */
    protected abstract ajaxInner<T>(tReturnType: { new (...args: any[]): T }, options?: AjaxRequest): Observable<T>;
 
    /**
     * The implementation of the Upload call
     * @param tReturnType The return type
     * @param options Additional RxJs AjaxRequest options (the global headers will be overridden)
     */
    protected abstract uploadInner<T>(returnType: {new(...args: any[]): T}, File: File, options?: AjaxRequest & {url: string}): Observable<T>;
}