UNPKG

js-data-jsonapi-light

Version:

JsonApi adapter serializer/dezerializer light.

102 lines 4.58 kB
"use strict"; var __extends = (this && this.__extends) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; var js_data_1 = require("js-data"); var js_data_http_1 = require("js-data-http"); var deserializer_1 = require("./deserializer"); var serializer_1 = require("./serializer"); var strings_1 = require("./strings"); var JsonApiAdapter = (function (_super) { __extends(JsonApiAdapter, _super); function JsonApiAdapter(options) { var _this = this; options = js_data_1.utils.deepMixIn({}, options || {}); if (options.serialize || options.deserialize) { throw new Error(strings_1.ERROR.PREVENT_SERIALIZE_DESERIALIZE_OPTIONS); } _this = _super.call(this, options) || this; _this.options = options; _this.serialize = serializer_1.wrapSerialize(_this); _this.deserialize = deserializer_1.wrapDeserialize(_this); return _this; } JsonApiAdapter.prototype.warn = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } console.warn.apply(null, arguments); return; }; JsonApiAdapter.prototype.handleResponse = function (opts) { return function (response) { if (opts && opts.raw) { response.meta = response.data.meta; response.data = response.data.result; } return response; }; }; JsonApiAdapter.prototype.handleBeforeLifecycle = function (opts) { if (opts && (opts.serialize || opts.deserialize)) { return Promise.reject(new Error(strings_1.ERROR.PREVENT_SERIALIZE_DESERIALIZE_OPTIONS)); } return Promise.resolve(); }; JsonApiAdapter.prototype.find = function (mapper, id, opts) { var _this = this; return this.handleBeforeLifecycle(opts).then(function () { return js_data_http_1.HttpAdapter.prototype.find.call(_this, mapper, id, opts); }).then(this.handleResponse(opts)); }; JsonApiAdapter.prototype.findAll = function (mapper, query, opts) { var _this = this; return this.handleBeforeLifecycle(opts).then(function () { return js_data_http_1.HttpAdapter.prototype.findAll.call(_this, mapper, query, opts); }).then(this.handleResponse(opts)); }; JsonApiAdapter.prototype.create = function (mapper, props, opts) { var _this = this; return this.handleBeforeLifecycle(opts).then(function () { return js_data_http_1.HttpAdapter.prototype.create.call(_this, mapper, props, opts); }).then(this.handleResponse(opts)); }; JsonApiAdapter.prototype.update = function (mapper, id, props, opts) { var _this = this; props[mapper.idAttribute] = id; if (!opts.forceReplace) { opts.method = opts.method || 'patch'; var record = mapper.datastore.get(mapper.name, id); if (record) { opts.changes = js_data_1.utils.diffObjects(props, record._get('previous'), null); } } return this.handleBeforeLifecycle(opts).then(function () { return js_data_http_1.HttpAdapter.prototype.update.call(_this, mapper, id, props, opts); }).then(this.handleResponse(opts)); }; JsonApiAdapter.prototype.destroy = function (mapper, id, opts) { var _this = this; return this.handleBeforeLifecycle(opts).then(function () { return js_data_http_1.HttpAdapter.prototype.destroy.call(_this, mapper, id, opts); }).then(this.handleResponse(opts)); }; JsonApiAdapter.prototype.createMany = function (mapper, props, opts) { return Promise.reject(new Error(strings_1.ERROR.NO_BATCH_CREATE)); }; JsonApiAdapter.prototype.updateAll = function (mapper, props, query, opts) { return Promise.reject(new Error(strings_1.ERROR.NO_BATCH_UPDATE)); }; JsonApiAdapter.prototype.updateMany = function (mapper, records, opts) { return Promise.reject(new Error(strings_1.ERROR.NO_BATCH_UPDATE)); }; JsonApiAdapter.prototype.destroyAll = function (mapper, query, opts) { return Promise.reject(new Error(strings_1.ERROR.NO_BATCH_DESTROY)); }; return JsonApiAdapter; }(js_data_http_1.HttpAdapter)); exports.JsonApiAdapter = JsonApiAdapter; //# sourceMappingURL=adapter.js.map