All files / sn-client-js/src/HttpProviders BaseHttpProvider.ts

100% Statements 9/9
100% Branches 4/4
100% Functions 4/4
100% Lines 9/9
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                    1x 381x               22x                 62x 2x                   84x 84x 3x   84x                  
/**
 * @module HttpProviders
 * @preferred
 * @description Library module for storing HttpProvider abstracts and implementations.
 *//** */
 
import { Observable, AjaxRequest } from '@reactivex/rxjs';
/**
 * 
 */
export abstract class BaseHttpProvider {
    protected headers: string[] = [];
    
    /**
     * 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, headerValue) {
        this.headers[headerName] = headerValue;
    }
 
    /**
     * Removes a specified HTTP header from the global header settings
     * @param headerName The name of the header
     * 
     */
    public UnsetGlobalHeader(headerName) {
        if (this.headers[headerName]){
            delete this.headers[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): T }, options: AjaxRequest): Observable<T> {
        options.headers = options.headers || [];
        for (let key in this.headers){
            options.headers[key] = this.headers[key];
        }
        return this.AjaxInner(tReturnType, 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): T }, options?: AjaxRequest): Observable<T>;
}