UNPKG

@datocms/rest-client-utils

Version:
135 lines 5.43 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.serializeRequestBody = void 0; function isRel(object) { return (typeof object === 'object' && !!object && 'id' in object && 'type' in object); } function isRelArray(object) { return Array.isArray(object) && object.every(isRel); } function serializeRequestBody(body, options) { if (typeof body !== 'object' || !body) { throw new Error('Invalid body!'); } if (Array.isArray(body)) { return { data: body.map(function (entity) { var _a; return (_a = serializeRequestBody(entity, options)) === null || _a === void 0 ? void 0 : _a.data; }), }; } var _a = body, id = _a.id, typeInBody = _a.type, meta = _a.meta, otherProperties = __rest(_a, ["id", "type", "meta"]); var attributes = {}; var relationships = {}; if (options.attributes === '*') { for (var _i = 0, _b = Object.entries(otherProperties); _i < _b.length; _i++) { var _c = _b[_i], key = _c[0], value = _c[1]; if (options.relationships.includes(key)) { if (isRel(value)) { var id_1 = value.id, type = value.type; relationships[key] = { data: { id: id_1, type: type } }; } else if (isRelArray(value)) { relationships[key] = { data: value.map(function (_a) { var id = _a.id, type = _a.type; return ({ id: id, type: type }); }), }; } else { relationships[key] = { data: null, }; } } else { attributes[key] = value; } } } else if (options.relationships === '*') { for (var _d = 0, _e = Object.entries(otherProperties); _d < _e.length; _d++) { var _f = _e[_d], key = _f[0], value = _f[1]; if (options.attributes.includes(key)) { attributes[key] = value; } else { if (isRel(value)) { var id_2 = value.id, type = value.type; relationships[key] = { data: { id: id_2, type: type } }; } else if (isRelArray(value)) { relationships[key] = { data: value.map(function (_a) { var id = _a.id, type = _a.type; return ({ id: id, type: type }); }), }; } else { relationships[key] = { data: null, }; } } } } else { for (var _g = 0, _h = Object.entries(otherProperties); _g < _h.length; _g++) { var _j = _h[_g], key = _j[0], value = _j[1]; if (options.attributes.includes(key)) { attributes[key] = value; } else if (options.relationships.includes(key)) { if (isRel(value)) { var id_3 = value.id, type = value.type; relationships[key] = { data: { id: id_3, type: type } }; } else if (isRelArray(value)) { relationships[key] = { data: value.map(function (_a) { var id = _a.id, type = _a.type; return ({ id: id, type: type }); }), }; } else { relationships[key] = { data: null, }; } } } // 'type' key present in data is always ignored.. // UNLESS the entity has a `type` attribute if (options.attributes.includes('type')) { attributes.type = typeInBody; } } return { data: __assign(__assign(__assign(__assign(__assign({}, (id || options.id ? { id: id || options.id } : {})), { type: options.type }), (Object.keys(attributes).length > 0 ? { attributes: attributes } : {})), (Object.keys(relationships).length > 0 ? { relationships: relationships } : {})), (meta ? { meta: meta } : {})), }; } exports.serializeRequestBody = serializeRequestBody; //# sourceMappingURL=serialize.js.map