UNPKG

@sitecore/sc-contenthub-webclient-sdk

Version:

Sitecore Content Hub WebClient SDK.

43 lines 1.46 kB
import { Rendition } from "../contracts/base/rendition"; import { InvalidOperationError } from "../errors/invalid-operation-error"; export class RenditionsConverter { /** * Serializes an array of renditions into a Json Value. * * @param value - An array of renditions * * @returns A Json Value. */ serialize(renditions) { const result = {}; renditions.forEach(rendition => { result[rendition.name] = rendition.items; }); return JSON.stringify(result); } /** * Deserializes a Json Value into an array of renditions. * * @param value - A Json Value * * @returns An array with Renditions. */ deserialize(value) { if (!value) { throw new InvalidOperationError("Can't deserialize falsy value."); } else if (typeof value !== "object") { throw new InvalidOperationError(`Expected a value of type 'JSON', but value was of type '${typeof value}'.`); } const renditions = typeof value === "string" ? JSON.parse(value) : JSON.parse(JSON.stringify(value)); const result = []; Object.keys(renditions).forEach(renditionName => { const items = renditions[renditionName]; result.push(Rendition.createRendition(renditionName, items)); }); return result; } } //# sourceMappingURL=renditions-converter.js.map