All files / dist/src/HttpProviders RxAjaxHttpProvider.js

0% Statements 0/39
0% Branches 0/14
0% Functions 0/6
0% Lines 0/38
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                                                                                                                             
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const rxjs_1 = require("@reactivex/rxjs");
const Config_1 = require("../Config");
const _1 = require("./");
class RxAjaxHttpProvider extends _1.BaseHttpProvider {
    constructor() {
        super();
        this.ForceCheckCrossDomain = true;
        this.SetGlobalHeader('content-type', 'application/json; charset=utf-8');
        this.SetGlobalHeader('Accept', 'application/json');
    }
    uploadInner(returnType, File, options) {
        const subject = new rxjs_1.Subject();
        const formData = new FormData();
        formData.append(File.name || 'File', File);
        if (options.body) {
            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);
        if (options.headers) {
            for (const header in options.headers) {
                request.setRequestHeader(header, options.headers[header]);
            }
        }
        request.onreadystatechange = () => {
            if (request.readyState === 4) {
                switch (request.status) {
                    case 200:
                        try {
                            const responseResult = 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();
    }
    isCrossDomain(path) {
        return path.indexOf(Config_1.SnConfigModel.DEFAULT_BASE_URL) === -1;
    }
    ajaxInner(tReturnType, options) {
        if (this.ForceCheckCrossDomain) {
            const crossDomain = this.isCrossDomain(options.url || '');
            options.withCredentials = crossDomain;
            options.crossDomain = crossDomain;
        }
        return rxjs_1.Observable.ajax(options).map((req) => req.response).share();
    }
}
exports.RxAjaxHttpProvider = RxAjaxHttpProvider;
//# sourceMappingURL=RxAjaxHttpProvider.js.map