All files / dist NetworkUtils.js

18.64% Statements 11/59
4.55% Branches 1/22
0% Functions 0/19
20.37% Lines 11/54
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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176  1x 1x 1x 1x                                                                                                                               1x                             1x                               1x                               1x                             1x                                       1x                                               1x  
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Response_1 = require("./Response");
var utils_1 = require("./utils");
exports.config = {
    /** Base URL for all API calls */
    baseUrl: '/',
    /** Default headers that will be sent to the server */
    defaultHeaders: {
        'content-type': 'application/vnd.api+json',
    },
    /** Reference of the fetch method that should be used */
    fetchReference: utils_1.isBrowser && window.fetch,
    /**
     * Base implementation of the fetch function (can be overriden)
     *
     * @param {string} method API call method
     * @param {string} url API call URL
     * @param {object} [body] API call body
     * @param {IHeaders} [requestHeaders] Headers that will be sent
     * @returns {Promise<IRawResponse>} Resolves with a raw response object
     */
    baseFetch: function (method, url, body, requestHeaders) {
        var _this = this;
        var data;
        var status;
        var headers;
        var request = Promise.resolve();
        return request
            .then(function () {
            var reqHeaders = utils_1.assign({}, exports.config.defaultHeaders, requestHeaders);
            return _this.fetchReference(url, {
                body: JSON.stringify(body),
                headers: reqHeaders,
                method: method,
            });
        })
            .then(function (response) {
            status = response.status;
            headers = response.headers;
            return response.json();
        })
            .catch(function (e) {
            if (status === 204) {
                return null;
            }
            throw e;
        })
            .then(function (responseData) {
            data = responseData;
            if (status >= 400) {
                throw {
                    message: "Invalid HTTP status: " + status,
                    status: status,
                };
            }
            return { data: data, headers: headers, requestHeaders: requestHeaders, status: status };
        })
            .catch(function (error) {
            return { data: data, error: error, headers: headers, requestHeaders: requestHeaders, status: status };
        });
    },
};
function fetch(_a) {
    var url = _a.url, options = _a.options, data = _a.data, _b = _a.method, method = _b === void 0 ? 'GET' : _b, store = _a.store;
    return exports.config.baseFetch(method, url, data, options && options.headers)
        .then(function (response) { return new Response_1.Response(response, store, options); });
}
exports.fetch = fetch;
/**
 * API call used to get data from the server
 *
 * @export
 * @param {Store} store Related Store
 * @param {string} url API call URL
 * @param {IHeaders} [headers] Headers to be sent
 * @param {IRequestOptions} [options] Server options
 * @returns {Promise<Response>} Resolves with a Response object
 */
function read(store, url, headers, options) {
    return exports.config.baseFetch('GET', url, null, headers)
        .then(function (response) { return new Response_1.Response(response, store, options); });
}
exports.read = read;
/**
 * API call used to create data on the server
 *
 * @export
 * @param {Store} store Related Store
 * @param {string} url API call URL
 * @param {object} [data] Request body
 * @param {IHeaders} [headers] Headers to be sent
 * @param {IRequestOptions} [options] Server options
 * @returns {Promise<Response>} Resolves with a Response object
 */
function create(store, url, data, headers, options) {
    return exports.config.baseFetch('POST', url, data, headers)
        .then(function (response) { return new Response_1.Response(response, store, options); });
}
exports.create = create;
/**
 * API call used to update data on the server
 *
 * @export
 * @param {Store} store Related Store
 * @param {string} url API call URL
 * @param {object} [data] Request body
 * @param {IHeaders} [headers] Headers to be sent
 * @param {IRequestOptions} [options] Server options
 * @returns {Promise<Response>} Resolves with a Response object
 */
function update(store, url, data, headers, options) {
    return exports.config.baseFetch('PATCH', url, data, headers)
        .then(function (response) { return new Response_1.Response(response, store, options); });
}
exports.update = update;
/**
 * API call used to remove data from the server
 *
 * @export
 * @param {Store} store Related Store
 * @param {string} url API call URL
 * @param {IHeaders} [headers] Headers to be sent
 * @param {IRequestOptions} [options] Server options
 * @returns {Promise<Response>} Resolves with a Response object
 */
function remove(store, url, headers, options) {
    return exports.config.baseFetch('DELETE', url, null, headers)
        .then(function (response) { return new Response_1.Response(response, store, options); });
}
exports.remove = remove;
/**
 * Fetch a link from the server
 *
 * @export
 * @param {JsonApi.ILink} link Link URL or a link object
 * @param {Store} store Store that will be used to save the response
 * @param {IDictionary<string>} [requestHeaders] Request headers
 * @param {IRequestOptions} [options] Server options
 * @returns {Promise<LibResponse>} Response promise
 */
function fetchLink(link, store, requestHeaders, options) {
    if (link) {
        var href = typeof link === 'object' ? link.href : link;
        if (href) {
            return read(store, href, requestHeaders, options);
        }
    }
    return Promise.resolve(new Response_1.Response({ data: null }, store));
}
exports.fetchLink = fetchLink;
function handleResponse(record, prop) {
    return function (response) {
        if (response.error) {
            throw response.error;
        }
        if (response.status === 204) {
            record['__persisted'] = true;
            return record;
        }
        else if (response.status === 201) {
            response.data.update({
                __prop__: prop,
                __queue__: true,
                __related__: record,
            });
            return response.data;
        }
        else {
            record['__persisted'] = true;
            return response.replaceData(record).data;
        }
    };
}
exports.handleResponse = handleResponse;