UNPKG

@prismicio/custom-types-client

Version:

JavaScript client to interact with the Prismic Custom Types API

98 lines (97 loc) 3.07 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => { __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); return value; }; const BulkUpdateOperationType = { CustomTypeInsert: "CUSTOM_TYPE_INSERT", CustomTypeUpdate: "CUSTOM_TYPE_UPDATE", CustomTypeDelete: "CUSTOM_TYPE_DELETE", SliceInsert: "SLICE_INSERT", SliceUpdate: "SLICE_UPDATE", SliceDelete: "SLICE_DELETE" }; const processDiff = (before, after, callbacks) => { for (const afterModel of after) { const beforeModel = before.find((model) => model.id === afterModel.id); if (beforeModel) { if (JSON.stringify(beforeModel) !== JSON.stringify(afterModel)) { callbacks.onUpdate(afterModel); } before = before.filter((model) => model !== beforeModel); } else { callbacks.onInsert(afterModel); } } for (const beforeModel of before) { callbacks.onDelete(beforeModel); } }; const createBulkUpdateTransaction = (...args) => new BulkUpdateTransaction(...args); class BulkUpdateTransaction { constructor(initialOperations = []) { __publicField(this, "operations"); this.operations = initialOperations instanceof BulkUpdateTransaction ? initialOperations.operations : initialOperations; } fromDiff(before, after) { processDiff(before.customTypes ?? [], after.customTypes ?? [], { onInsert: (model) => this.insertCustomType(model), onUpdate: (model) => this.updateCustomType(model), onDelete: (model) => this.deleteCustomType(model) }); processDiff(before.slices ?? [], after.slices ?? [], { onInsert: (model) => this.insertSlice(model), onUpdate: (model) => this.updateSlice(model), onDelete: (model) => this.deleteSlice(model) }); } insertCustomType(customType) { this.operations.push({ type: BulkUpdateOperationType.CustomTypeInsert, id: customType.id, payload: customType }); } updateCustomType(customType) { this.operations.push({ type: BulkUpdateOperationType.CustomTypeUpdate, id: customType.id, payload: customType }); } deleteCustomType(customType) { this.operations.push({ type: BulkUpdateOperationType.CustomTypeDelete, id: customType.id, payload: { id: customType.id } }); } insertSlice(slice) { this.operations.push({ type: BulkUpdateOperationType.SliceInsert, id: slice.id, payload: slice }); } updateSlice(slice) { this.operations.push({ type: BulkUpdateOperationType.SliceUpdate, id: slice.id, payload: slice }); } deleteSlice(slice) { this.operations.push({ type: BulkUpdateOperationType.SliceDelete, id: slice.id, payload: { id: slice.id } }); } } export { BulkUpdateOperationType, BulkUpdateTransaction, createBulkUpdateTransaction }; //# sourceMappingURL=bulkUpdate.js.map