All files / src/HttpProviders RxAjaxHttpProvider.ts

100% Statements 36/36
100% Branches 6/6
100% Functions 6/6
100% Lines 35/35
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        1x   1x   1x 1x   1x         1x   3x 3x 3x   3x 3x     3x 3x 3x   3x 3x     3x 4x   3x   2x 2x 1x   1x   2x   1x       3x 3x       4x       9x 9x 9x         1x 1x 1x 1x      
/**
 * @module HttpProviders
 *//** */
 
import { Observable } from 'rxjs/Observable';
import { AjaxRequest } from 'rxjs/observable/dom/AjaxObservable';
import { Subject } from 'rxjs/Subject';
 
import { SnConfigModel } from '../Config';
import { BaseHttpProvider } from './';
 
import 'rxjs/add/observable/dom/ajax';
 
/**
 * This is the default RxJs-Ajax based Http calls.
 */
export class RxAjaxHttpProvider extends BaseHttpProvider {
    protected uploadInner<T>(returnType: new (...args: any[]) => T, File: File, options: AjaxRequest & { url: string, headers: string[], body: any}): Observable<T> {
        const subject = new Subject<T>();
        const formData = new FormData();
        formData.append(File.name, File);
 
        for (const index in options.body) {
            formData.append(index, options.body[index]);
        }
 
        const request = new XMLHttpRequest();
        request.withCredentials = this.isCrossDomain(options.url);
        request.open('POST', options.url);
 
        for (const header in options.headers) {
            request.setRequestHeader(header, (options.headers as any)[header]);
        }
 
        request.onreadystatechange = () => {
            if (request.readyState === 4) {
 
                switch (request.status) {
                    case 200:
                        try {
                            const responseResult: T = JSON.parse(request.response);
                            subject.next(responseResult);
                        } catch (error) {
                            subject.next(request.response);
                        }
                        break;
                    default:
                        subject.error({ message: 'Invalid Request status', request });
                }
            }
        };
        request.send(formData);
        return subject.asObservable();
    }
 
    private isCrossDomain(path: string): boolean {
        return path.indexOf(SnConfigModel.DEFAULT_BASE_URL) === -1;
    }
 
    constructor() {
        super();
        this.SetGlobalHeader('content-type', 'application/json; charset=utf-8');
        this.SetGlobalHeader('Accept', 'application/json');
    }
 
    protected ajaxInner<T>(tReturnType: {new(...args: any[]): T}, options: AjaxRequest): Observable<T> {
 
        const crossDomain = this.isCrossDomain(options.url || '');
        options.withCredentials = crossDomain;
        options.crossDomain = crossDomain;
        return Observable.ajax(options).map((req) => req.response as T).share();
    }
}