kitsu-core
Version:
Core serialisation and deserialsation functions for the kitsu JSON:API client
627 lines (494 loc) • 19 kB
JavaScript
import _regeneratorRuntime from '@babel/runtime/regenerator';
import _asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator';
function deattribute() {
return _deattribute.apply(this, arguments);
}
function _deattribute() {
return _deattribute = _asyncToGenerator(_regeneratorRuntime.mark(function _callee2(data) {
return _regeneratorRuntime.wrap(function (_context2) {
for (;;) switch (_context2.prev = _context2.next) {
case 0:
if (_context2.t0 = 'object' === typeof data && null !== data, !_context2.t0) {
_context2.next = 8;
break;
}
if (!Array.isArray(data)) {
_context2.next = 7;
break;
}
return _context2.next = 5, data.map(function () {
var _ref = _asyncToGenerator(_regeneratorRuntime.mark(function _callee(el) {
return _regeneratorRuntime.wrap(function (_context) {
for (;;) switch (_context.prev = _context.next) {
case 0:
return _context.abrupt("return", deattribute(el));
case 1:
case "end":
return _context.stop();
}
}, _callee, this);
}));
return function () {
return _ref.apply(this, arguments);
};
}());
case 5:
_context2.next = 8;
break;
case 7:
data.attributes && data.attributes.constructor === Object && (Object.keys(data.attributes).forEach(key => {
data[key] = data.attributes[key];
}), delete data.attributes);
case 8:
return _context2.abrupt("return", data);
case 9:
case "end":
return _context2.stop();
}
}, _callee2, this);
})), _deattribute.apply(this, arguments);
}
function error(E) {
if (E.response) {
const e = E.response.data;
e && e.errors && (E.errors = e.errors);
}
throw E;
}
function filterIncludes() {
return _filterIncludes.apply(this, arguments);
}
function _filterIncludes() {
return _filterIncludes = _asyncToGenerator(_regeneratorRuntime.mark(function _callee(included, _ref) {
var id, type, filtered;
return _regeneratorRuntime.wrap(function (_context) {
for (;;) switch (_context.prev = _context.next) {
case 0:
return id = _ref.id, type = _ref.type, _context.prev = 1, filtered = included.filter(el => el.id === id && el.type === type)[0] || {
id,
type
}, _context.abrupt("return", Object.assign({}, filtered));
case 6:
_context.prev = 6, _context.t0 = _context["catch"](1), error(_context.t0);
case 9:
case "end":
return _context.stop();
}
}, _callee, this, [[1, 6]]);
})), _filterIncludes.apply(this, arguments);
}
function link() {
return _link.apply(this, arguments);
}
function _link() {
return _link = _asyncToGenerator(_regeneratorRuntime.mark(function _callee(_ref, included) {
var id, type, meta, filtered;
return _regeneratorRuntime.wrap(function (_context) {
for (;;) switch (_context.prev = _context.next) {
case 0:
return id = _ref.id, type = _ref.type, meta = _ref.meta, _context.next = 3, filterIncludes(included, {
id,
type
});
case 3:
if (filtered = _context.sent, _context.t0 = filtered.relationships, !_context.t0) {
_context.next = 8;
break;
}
return _context.next = 8, linkRelationships(filtered, included);
case 8:
return meta && (filtered.meta = meta), _context.abrupt("return", deattribute(filtered));
case 10:
case "end":
return _context.stop();
}
}, _callee, this);
})), _link.apply(this, arguments);
}
function linkArray() {
return _linkArray.apply(this, arguments);
}
function _linkArray() {
return _linkArray = _asyncToGenerator(_regeneratorRuntime.mark(function _callee2(data, included, key) {
var _iteratorNormalCompletion, _didIteratorError, _iteratorError, _step, _iterator, resource;
return _regeneratorRuntime.wrap(function (_context2) {
for (;;) switch (_context2.prev = _context2.next) {
case 0:
return data[key] = [], _iteratorNormalCompletion = !0, _didIteratorError = !1, _iteratorError = void 0, _context2.prev = 2, _context2.next = 5, data.relationships[key].data;
case 5:
_context2.t0 = Symbol.iterator, _iterator = _context2.sent[_context2.t0]();
case 7:
if (_iteratorNormalCompletion = (_step = _iterator.next()).done) {
_context2.next = 17;
break;
}
return resource = _step.value, _context2.t1 = data[key], _context2.next = 12, link(resource, included);
case 12:
_context2.t2 = _context2.sent, _context2.t1.push.call(_context2.t1, _context2.t2);
case 14:
_iteratorNormalCompletion = !0, _context2.next = 7;
break;
case 17:
_context2.next = 22;
break;
case 19:
_context2.prev = 19, _context2.t3 = _context2["catch"](2), _didIteratorError = !0, _iteratorError = _context2.t3;
case 22:
_context2.prev = 22, _context2.prev = 23, _iteratorNormalCompletion || null == _iterator.return || _iterator.return();
case 25:
if (_context2.prev = 25, !_didIteratorError) {
_context2.next = 28;
break;
}
throw _iteratorError;
case 28:
return _context2.finish(25);
case 29:
return _context2.finish(22);
case 30:
case "end":
return _context2.stop();
}
}, _callee2, this, [[2, 19, 22, 30], [23,, 25, 29]]);
})), _linkArray.apply(this, arguments);
}
function linkObject() {
return _linkObject.apply(this, arguments);
}
function _linkObject() {
return _linkObject = _asyncToGenerator(_regeneratorRuntime.mark(function _callee3(data, included, key) {
return _regeneratorRuntime.wrap(function (_context3) {
for (;;) switch (_context3.prev = _context3.next) {
case 0:
return _context3.next = 2, link(data.relationships[key].data, included);
case 2:
data[key] = _context3.sent, delete data[key].relationships;
case 4:
case "end":
return _context3.stop();
}
}, _callee3, this);
})), _linkObject.apply(this, arguments);
}
function linkRelationships() {
return _linkRelationships.apply(this, arguments);
}
function _linkRelationships() {
return _linkRelationships = _asyncToGenerator(_regeneratorRuntime.mark(function _callee4(data, included) {
var relationships, removeRelationships, key;
return _regeneratorRuntime.wrap(function (_context4) {
for (;;) switch (_context4.prev = _context4.next) {
case 0:
return relationships = data.relationships, removeRelationships = !1, _context4.next = 4, relationships;
case 4:
_context4.t0 = _regeneratorRuntime.keys(_context4.sent);
case 5:
if ((_context4.t1 = _context4.t0()).done) {
_context4.next = 20;
break;
}
if (key = _context4.t1.value, !(relationships[key].data && Array.isArray(relationships[key].data))) {
_context4.next = 13;
break;
}
return _context4.next = 10, linkArray(data, included, key);
case 10:
removeRelationships = !0, _context4.next = 18;
break;
case 13:
if (_context4.t2 = relationships[key].data, !_context4.t2) {
_context4.next = 18;
break;
}
return _context4.next = 17, linkObject(data, included, key);
case 17:
removeRelationships = !0;
case 18:
_context4.next = 5;
break;
case 20:
return _context4.abrupt("return", (removeRelationships && delete data.relationships, data));
case 21:
case "end":
return _context4.stop();
}
}, _callee4, this);
})), _linkRelationships.apply(this, arguments);
}
function deserialiseArray() {
return _deserialiseArray.apply(this, arguments);
}
function _deserialiseArray() {
return _deserialiseArray = _asyncToGenerator(_regeneratorRuntime.mark(function _callee(obj) {
var _iteratorNormalCompletion, _didIteratorError, _iteratorError, _step, _iterator, value;
return _regeneratorRuntime.wrap(function (_context) {
for (;;) switch (_context.prev = _context.next) {
case 0:
return _iteratorNormalCompletion = !0, _didIteratorError = !1, _iteratorError = void 0, _context.prev = 1, _context.next = 4, obj.data;
case 4:
_context.t0 = Symbol.iterator, _iterator = _context.sent[_context.t0]();
case 6:
if (_iteratorNormalCompletion = (_step = _iterator.next()).done) {
_context.next = 22;
break;
}
if (value = _step.value, _context.t1 = obj.included, !_context.t1) {
_context.next = 13;
break;
}
return _context.next = 12, linkRelationships(value, obj.included);
case 12:
value = _context.sent;
case 13:
if (_context.t2 = value.attributes, !_context.t2) {
_context.next = 18;
break;
}
return _context.next = 17, deattribute(value);
case 17:
value = _context.sent;
case 18:
obj.data[obj.data.indexOf(value)] = value;
case 19:
_iteratorNormalCompletion = !0, _context.next = 6;
break;
case 22:
_context.next = 27;
break;
case 24:
_context.prev = 24, _context.t3 = _context["catch"](1), _didIteratorError = !0, _iteratorError = _context.t3;
case 27:
_context.prev = 27, _context.prev = 28, _iteratorNormalCompletion || null == _iterator.return || _iterator.return();
case 30:
if (_context.prev = 30, !_didIteratorError) {
_context.next = 33;
break;
}
throw _iteratorError;
case 33:
return _context.finish(30);
case 34:
return _context.finish(27);
case 35:
return _context.abrupt("return", obj);
case 36:
case "end":
return _context.stop();
}
}, _callee, this, [[1, 24, 27, 35], [28,, 30, 34]]);
})), _deserialiseArray.apply(this, arguments);
}
function deserialise() {
return _deserialise.apply(this, arguments);
}
function _deserialise() {
return _deserialise = _asyncToGenerator(_regeneratorRuntime.mark(function _callee2(obj) {
return _regeneratorRuntime.wrap(function (_context2) {
for (;;) switch (_context2.prev = _context2.next) {
case 0:
if (!(obj.data && obj.data.constructor === Array)) {
_context2.next = 6;
break;
}
return _context2.next = 3, deserialiseArray(obj);
case 3:
obj = _context2.sent, _context2.next = 11;
break;
case 6:
if (_context2.t0 = obj.included, !_context2.t0) {
_context2.next = 11;
break;
}
return _context2.next = 10, linkRelationships(obj.data, obj.included);
case 10:
obj.data = _context2.sent;
case 11:
if (delete obj.included, _context2.t1 = obj.data && obj.data.attributes, !_context2.t1) {
_context2.next = 17;
break;
}
return _context2.next = 16, deattribute(obj.data);
case 16:
obj.data = _context2.sent;
case 17:
return _context2.abrupt("return", obj);
case 18:
case "end":
return _context2.stop();
}
}, _callee2, this);
})), _deserialise.apply(this, arguments);
}
function queryFormat(value, key) {
return null !== value && 'object' === typeof value ? query(value, key) : encodeURIComponent(key) + '=' + encodeURIComponent(value);
}
function query(params) {
let prefix = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : null;
const str = [];
for (const param in params) params.hasOwnProperty(param) && str.push(queryFormat(params[param], prefix ? `${prefix}[${param}]` : param));
return str.join('&');
}
const requiresID = (method, key) => `${method} requires an ID for the ${key} relationships`;
function isValid() {
return _isValid.apply(this, arguments);
}
function _isValid() {
return _isValid = _asyncToGenerator(_regeneratorRuntime.mark(function _callee(obj, method, type) {
return _regeneratorRuntime.wrap(function (_context) {
for (;;) switch (_context.prev = _context.next) {
case 0:
if (obj.constructor === Object && 0 !== Object.keys(obj).length) {
_context.next = 2;
break;
}
throw new Error(`${method} requires a JSON object body`);
case 2:
if ('POST' === method || obj.id) {
_context.next = 4;
break;
}
throw new Error(`${method} requires an ID for the ${type} type`);
case 4:
case "end":
return _context.stop();
}
}, _callee, this);
})), _isValid.apply(this, arguments);
}
function serialiseObject() {
return _serialiseObject.apply(this, arguments);
}
function _serialiseObject() {
return _serialiseObject = _asyncToGenerator(_regeneratorRuntime.mark(function _callee2(node, nodeType, key, data, method) {
return _regeneratorRuntime.wrap(function (_context2) {
for (;;) switch (_context2.prev = _context2.next) {
case 0:
if ('string' === typeof node.id) {
_context2.next = 2;
break;
}
throw new Error(requiresID(method, key));
case 2:
return _context2.abrupt("return", (data.relationships || (data.relationships = {}), node.type || (node.type = nodeType), data.relationships[key] = {
data: Object.assign(node)
}, data));
case 3:
case "end":
return _context2.stop();
}
}, _callee2, this);
})), _serialiseObject.apply(this, arguments);
}
function serialiseArray() {
return _serialiseArray.apply(this, arguments);
}
function _serialiseArray() {
return _serialiseArray = _asyncToGenerator(_regeneratorRuntime.mark(function _callee3(node, nodeType, key, data, method) {
return _regeneratorRuntime.wrap(function (_context3) {
for (;;) switch (_context3.prev = _context3.next) {
case 0:
return _context3.abrupt("return", (data.relationships || (data.relationships = {}), data.relationships[key] = {
data: node.map((_ref) => {
let id = _ref.id,
type = _ref.type;
if (!id) throw new Error(requiresID(method, key));
return {
id,
type: type || nodeType
};
})
}, data));
case 1:
case "end":
return _context3.stop();
}
}, _callee3, this);
})), _serialiseArray.apply(this, arguments);
}
function serialiseAttr() {
return _serialiseAttr.apply(this, arguments);
}
function _serialiseAttr() {
return _serialiseAttr = _asyncToGenerator(_regeneratorRuntime.mark(function _callee4(node, key, data) {
return _regeneratorRuntime.wrap(function (_context4) {
for (;;) switch (_context4.prev = _context4.next) {
case 0:
return _context4.abrupt("return", (data.attributes || (data.attributes = {}), data.attributes[key] = node, data));
case 1:
case "end":
return _context4.stop();
}
}, _callee4, this);
})), _serialiseAttr.apply(this, arguments);
}
function serialise() {
return _serialise.apply(this, arguments);
}
function _serialise() {
return _serialise = _asyncToGenerator(_regeneratorRuntime.mark(function _callee5(model) {
var obj,
method,
type,
data,
key,
node,
nodeType,
_args5 = arguments;
return _regeneratorRuntime.wrap(function (_context5) {
for (;;) switch (_context5.prev = _context5.next) {
case 0:
return obj = 1 < _args5.length && void 0 !== _args5[1] ? _args5[1] : {}, method = 2 < _args5.length && void 0 !== _args5[2] ? _args5[2] : 'POST', _context5.prev = 1, type = this.plural(this.camel(model)), data = {
type
}, _context5.next = 6, isValid(obj, method, type);
case 6:
'POST' !== method && (data.id = obj.id + ""), _context5.t0 = _regeneratorRuntime.keys(obj);
case 8:
if ((_context5.t1 = _context5.t0()).done) {
_context5.next = 30;
break;
}
if (key = _context5.t1.value, node = obj[key], nodeType = this.plural(this.camel(key)), !(null !== node & node.constructor === Object)) {
_context5.next = 17;
break;
}
return _context5.next = 14, serialiseObject(node, nodeType, key, data, method);
case 14:
data = _context5.sent, _context5.next = 28;
break;
case 17:
if (!(null !== node && Array.isArray(node))) {
_context5.next = 23;
break;
}
return _context5.next = 20, serialiseArray(node, nodeType, key, data, method);
case 20:
data = _context5.sent, _context5.next = 28;
break;
case 23:
if (_context5.t2 = 'id' !== key && 'type' !== key, !_context5.t2) {
_context5.next = 28;
break;
}
return _context5.next = 27, serialiseAttr(node, key, data);
case 27:
data = _context5.sent;
case 28:
_context5.next = 8;
break;
case 30:
return _context5.abrupt("return", {
data
});
case 33:
throw _context5.prev = 33, _context5.t3 = _context5["catch"](1), error(_context5.t3);
case 36:
case "end":
return _context5.stop();
}
}, _callee5, this, [[1, 33]]);
})), _serialise.apply(this, arguments);
}
var index = (s => s.replace(/[-_][a-z\u00E0-\u00F6\u00F8-\u00FE]/g, match => match.slice(1).toUpperCase()));
var index$1 = (s => s.charAt(0).toLowerCase() + s.slice(1).replace(/[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g, match => '-' + match.toLowerCase()));
var index$2 = (s => s.charAt(0).toLowerCase() + s.slice(1).replace(/[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g, match => '_' + match.toLowerCase()));
export { index as camel, index$1 as kebab, index$2 as snake, deattribute, deserialise, error, filterIncludes, linkRelationships, query, serialise };