@datocms/cma-client
Version:
JS client for DatoCMS REST Content Management API
24 lines • 1.5 kB
JavaScript
import * as Utils from '@datocms/rest-client-utils';
import { Item } from '../generated/resources/index.js';
export function buildBlockRecord(body) {
var _a, _b, _c, _d, _e, _f;
const data = Utils.serializeRequestBody(body, {
type: Item.TYPE,
attributes: '*',
relationships: ['item_type'],
}).data;
// Mirror the deserializer: expose `item_type.data.id` as a top-level
// `__itemTypeId` so locally-built blocks support the same TS narrowing
// pattern as blocks read from API responses. The field is TS-only and is
// stripped again by the serializer when the outer request is sent.
//
// `item_type` is optional on `UpdateBlockRecordSchema` (id-only updates), so
// the ID may be absent here. Accept any source the caller provided and
// leave `__itemTypeId` unset when none is available — the property is
// declared optional, so consumers already handle the `undefined` case.
const itemTypeId = (_c = (_a = body.__itemTypeId) !== null && _a !== void 0 ? _a : (_b = body.item_type) === null || _b === void 0 ? void 0 : _b.id) !== null && _c !== void 0 ? _c : (_f = (_e = (_d = data.relationships) === null || _d === void 0 ? void 0 : _d.item_type) === null || _e === void 0 ? void 0 : _e.data) === null || _f === void 0 ? void 0 : _f.id;
if (itemTypeId === undefined)
return data;
return Object.assign(Object.assign({}, data), { __itemTypeId: itemTypeId });
}
//# sourceMappingURL=buildBlockRecord.js.map