All files index.js

100% Statements 56/56
100% Branches 24/24
100% Functions 18/18
100% Lines 53/53
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103  1x 1x 1x   33x 33x   1x 4x         1x 4x         1x 4x         1x 4x   4x 3x   1x         1x 4x         1x 4x         1x 18x   1x 6x   1x 20x 15x   5x             1x 33x 33x 33x 1x     32x 32x 32x 32x 7x   32x 4x 4x 2x     32x   1x 12x   1x 4x   1x 4x   1x 4x   1x   1x  
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
require("whatwg-fetch");
var FetchBase = /** @class */ (function () {
    function FetchBase(config) {
        this.config = config;
        this.endpoint = "";
    }
    FetchBase.prototype.findAll = function (suffix) {
        return fetch("" + this.getUrl() + suffix, this.getOptions())
            .then(this.handleFetchResponse)
            .then(this.jsonResponse)
            .catch(this.rejectErrorPromise);
    };
    FetchBase.prototype.single = function (id) {
        return fetch(this.getUrl(id), this.getOptions())
            .then(this.handleFetchResponse)
            .then(this.jsonResponse)
            .catch(this.rejectErrorPromise);
    };
    FetchBase.prototype.get = function () {
        return fetch(this.getUrl(), this.getOptions())
            .then(this.handleFetchResponse)
            .then(this.jsonResponse)
            .catch(this.rejectErrorPromise);
    };
    FetchBase.prototype.put = function (item) {
        return fetch(this.getUrl(item.id), this.putOptions(item))
            .then(function (response) {
            if (!response.ok) {
                throw new Error(response.statusText);
            }
            return response.json();
        })
            .then(this.jsonResponse)
            .catch(this.rejectErrorPromise);
    };
    FetchBase.prototype.post = function (item) {
        return fetch(this.getUrl(item.id), this.postOptions(item))
            .then(this.handleFetchResponse)
            .then(this.jsonResponse)
            .catch(this.rejectErrorPromise);
    };
    FetchBase.prototype.delete = function (item) {
        return fetch(this.getUrl(item.id), this.deleteOptions())
            .then(this.handleFetchResponse)
            .then(this.jsonResponse)
            .catch(this.rejectErrorPromise);
    };
    FetchBase.prototype.rejectErrorPromise = function (reason) {
        return Promise.reject(reason);
    };
    FetchBase.prototype.jsonResponse = function (json) {
        return Promise.resolve(json);
    };
    FetchBase.prototype.handleFetchResponse = function (response) {
        if (!response.ok) {
            throw new Error(response.statusText);
        }
        return response.json();
    };
    /**
     *
     * @param params A list of name=value strings comma separated as parameters. The query params
     *               will be joined with the correct ? and & symbols.
     */
    FetchBase.prototype.getUrl = function (resourceId, queryParams) {
        if (resourceId === void 0) { resourceId = 0; }
        if (queryParams === void 0) { queryParams = []; }
        if (!this.config.protocol || !this.config.ip) {
            throw new Error("'protocol' and 'ip' props are required for fetch-base");
        }
        // https://some.com/some/api/v1/resource?param1=value1&param2=value2
        var url = this.config.protocol + "://" + this.config.ip;
        url += this.config.api ? "/" + this.config.api : "";
        url += this.endpoint ? "/" + this.endpoint : "";
        if (resourceId > 0) {
            url += "/" + resourceId;
        }
        if (queryParams && queryParams.length > 0) {
            url = url + "?" + queryParams.shift();
            if (queryParams.length > 0) {
                url += queryParams.map(function (p) { return "&" + p; }).join("");
            }
        }
        return url;
    };
    FetchBase.prototype.getOptions = function () {
        return { method: "GET" };
    };
    FetchBase.prototype.putOptions = function (item) {
        return { method: "PUT", body: JSON.stringify(item) };
    };
    FetchBase.prototype.postOptions = function (item) {
        return { method: "POST", body: JSON.stringify(item) };
    };
    FetchBase.prototype.deleteOptions = function () {
        return { method: "DELETE" };
    };
    return FetchBase;
}());
exports.FetchBase = FetchBase;
//# sourceMappingURL=index.js.map