All files / dist Response.js

21.05% Statements 8/38
0% Branches 0/20
16.67% Functions 1/6
21.62% Lines 8/37
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  1x 1x 1x 1x                                                                         1x                                       1x             1x   1x  
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var mobx_1 = require("mobx");
var NetworkUtils_1 = require("./NetworkUtils");
var Response = (function () {
    function Response(response, store, options, overrideData) {
        var _this = this;
        /**
         * Cache used for the link requests
         *
         * @private
         * @type {IDictionary<Promise<Response>>}
         * @memberOf Response
         */
        this.__cache = {};
        this.__store = store;
        this.__options = options;
        this.__response = response;
        this.status = response.status;
        this.data = overrideData ? store.add(overrideData) : store.sync(response.data);
        this.meta = (response.data && response.data.meta) || {};
        this.links = (response.data && response.data.links) || {};
        this.jsonapi = (response.data && response.data.jsonapi) || {};
        this.headers = response.headers;
        this.requestHeaders = response.requestHeaders;
        this.error = (response.data && response.data.errors) || response.error;
        var linkGetter = {};
        Object.keys(this.links).forEach(function (link) {
            linkGetter[link] = mobx_1.computed(function () { return _this.__fetchLink(link); });
        });
        mobx_1.extendObservable(this, linkGetter);
        Object.freeze(this);
    }
    /**
     * Replace the response record with a different record. Used to replace a record while keeping the same reference
     *
     * @param {IModel} data New data
     * @returns {Response}
     *
     * @memberOf Response
     */
    Response.prototype.replaceData = function (data) {
        var record = this.data;
        if (record === data) {
            return this;
        }
        this.__store.remove(record.type, record.id);
        data.update(record.toJS());
        // tslint:disable-next-line:no-string-literal
        data['__data'].id = record.id;
        return new Response(this.__response, this.__store, this.__options, data);
    };
    /**
     * Function called when a link is beeing fetched. The returned value is cached
     *
     * @private
     * @param {any} name Link name
     * @returns Promise that resolves with a Response object
     *
     * @memberOf Response
     */
    Response.prototype.__fetchLink = function (name) {
        if (!this.__cache[name]) {
            var link = name in this.links ? this.links[name] : null;
            this.__cache[name] = NetworkUtils_1.fetchLink(link, this.__store, this.requestHeaders, this.__options);
        }
        return this.__cache[name];
    };
    return Response;
}());
exports.Response = Response;