UNPKG

@datocms/rest-client-utils

Version:
149 lines 5.7 kB
"use strict"; 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.serializeRawRequestBodyWithItems = exports.serializeRawItem = 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((entity) => { var _a; return (_a = serializeRequestBody(entity, options)) === null || _a === void 0 ? void 0 : _a.data; }), }; } const _a = body, { id, type: typeInBody, meta } = _a, otherProperties = __rest(_a, ["id", "type", "meta"]); const attributes = {}; const relationships = {}; if (options.attributes === '*') { for (const [key, value] of Object.entries(otherProperties)) { if (options.relationships.includes(key)) { if (isRel(value)) { const { id, type } = value; relationships[key] = { data: { id, type } }; } else if (isRelArray(value)) { relationships[key] = { data: value.map(({ id, type }) => ({ id, type })), }; } else { relationships[key] = { data: null, }; } } else { attributes[key] = value; } } } else if (options.relationships === '*') { for (const [key, value] of Object.entries(otherProperties)) { if (options.attributes.includes(key)) { attributes[key] = value; } else { if (isRel(value)) { const { id, type } = value; relationships[key] = { data: { id, type } }; } else if (isRelArray(value)) { relationships[key] = { data: value.map(({ id, type }) => ({ id, type })), }; } else { relationships[key] = { data: null, }; } } } } else { for (const [key, value] of Object.entries(otherProperties)) { if (options.attributes.includes(key)) { attributes[key] = value; } else if (options.relationships.includes(key)) { if (isRel(value)) { const { id, type } = value; relationships[key] = { data: { id, type } }; } else if (isRelArray(value)) { relationships[key] = { data: value.map(({ id, type }) => ({ id, 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: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, (id || options.id ? { id: id || options.id } : {})), { type: options.type }), (Object.keys(attributes).length > 0 ? { attributes } : {})), (Object.keys(relationships).length > 0 ? { relationships } : {})), (meta ? { meta } : {})), }; } exports.serializeRequestBody = serializeRequestBody; function processValueForSerialization(value) { if (Array.isArray(value)) { return value.map(processValueForSerialization); } if (value && typeof value === 'object') { const result = {}; for (const [key, val] of Object.entries(value)) { if (key !== '__itemTypeId') { result[key] = processValueForSerialization(val); } } return result; } return value; } function serializeRawItem(item) { if (!item || typeof item !== 'object') { return item; } const { __itemTypeId } = item, rest = __rest(item, ["__itemTypeId"]); const processedItem = {}; for (const [key, value] of Object.entries(rest)) { processedItem[key] = processValueForSerialization(value); } return processedItem; } exports.serializeRawItem = serializeRawItem; function serializeRawRequestBodyWithItems(body) { if (!body || !('data' in body)) { return body; } if (Array.isArray(body.data)) { return Object.assign(Object.assign({}, body), { data: body.data.map(serializeRawItem) }); } return Object.assign(Object.assign({}, body), { data: serializeRawItem(body.data) }); } exports.serializeRawRequestBodyWithItems = serializeRawRequestBodyWithItems; //# sourceMappingURL=serialize.js.map