all files / src/ httpFetcher.js

96.67% Statements 29/30
90.91% Branches 10/11
100% Functions 5/5
96.55% Lines 28/29
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  14× 14× 12× 12× 12×   14× 14×   13× 12× 12× 12× 12×                                            
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var graphql_1 = require("graphql");
var httpObservable_1 = require("./httpObservable");
var fetcherUtils_1 = require("./fetcherUtils");
var httpUtils_1 = require("./httpUtils");
require("isomorphic-fetch");
var HttpFetcher = (function () {
    function HttpFetcher(fetchParams) {
        var _this = this;
        this.createHttpObservable = function (fetchParams) {
            var uri = fetchParams.uri, body = fetchParams.body, method = fetchParams.method, headers = fetchParams.headers;
            var fetchResult = _this._fetch ? _this._fetch(uri, { headers: headers, body: body, method: method }) : fetch(uri, { headers: headers, body: body, method: method });
            return new httpObservable_1.default(fetchResult.then(function (res) { return res.json(); }));
        };
        this._fetch = fetchParams && fetchParams.fetch;
        this._uri = fetchParams && fetchParams.uri ? fetchParams.uri : '';
    }
    HttpFetcher.prototype.request = function (operation) {
        fetcherUtils_1.validateOperation(operation);
        var query = operation.query, variables = operation.variables, operationName = operation.operationName, context = operation.context;
        var method = httpUtils_1.default.getRequestType(query);
        var headers = { 'Accept': '*/*' };
        switch (method) {
            case 'GET':
                var uri = httpUtils_1.default.buildURI(this._uri, operation);
                return this.createHttpObservable({
                    uri: uri,
                    headers: headers,
                    method: method,
                });
            case 'POST':
                headers['Content-Type'] = 'application/json';
                var body = JSON.stringify({
                    query: graphql_1.print(query),
                    variables: variables,
                    operationName: operationName,
                    context: context,
                });
                return this.createHttpObservable({
                    uri: this._uri,
                    headers: headers,
                    method: method,
                    body: body,
                });
            default:
                throw new Error('');
        }
    };
    return HttpFetcher;
}());
exports.default = HttpFetcher;
//# sourceMappingURL=httpFetcher.js.map