@sitecore/sc-contenthub-webclient-sdk
Version:
Sitecore Content Hub WebClient SDK.
35 lines • 1.6 kB
JavaScript
/* eslint-disable @typescript-eslint/no-unsafe-argument,@typescript-eslint/no-unsafe-member-access */
import { TaJson } from "ta-json";
import { InvalidOperationError } from "../errors/invalid-operation-error";
import Link from "../link";
import { RelationResource } from "../models/relation-resource";
export class RelationMapResourceConverter {
serialize(value) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return TaJson.serialize(value);
}
deserialize(value) {
if (!value) {
throw new InvalidOperationError("Can't deserialize falsy value.");
}
else if (typeof value !== "object" || value instanceof Array) {
throw new InvalidOperationError(`Expected a value of type 'JSON', but value was of type '${typeof value}'.`);
}
let obj = null;
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
typeof value === "string" ? (obj = JSON.parse(value)) : (obj = JSON.parse(JSON.stringify(value)));
try {
return Object.keys(obj).reduce((map, relationName) => {
const resource = obj[relationName];
map[relationName] = resource.href
? TaJson.deserialize(resource, Link)
: TaJson.deserialize(resource, RelationResource);
return map;
}, {});
}
catch (ex) {
throw new InvalidOperationError(`Couldn't deserialize this JsonValue - ${ex}.`);
}
}
}
//# sourceMappingURL=relation-map-resource-converter.js.map