@sitecore/sc-contenthub-webclient-sdk
Version:
Sitecore Content Hub WebClient SDK.
40 lines • 1.68 kB
JavaScript
import { ApiResource } from "../api/api-resource";
import { InvalidOperationError } from "../errors/invalid-operation-error";
import { NotImplementedError } from "../errors/not-implemented-error";
import Link from "../link";
export class ApiResourceConverter {
/**
* Not implemented.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
serialize(value) {
throw new NotImplementedError("Serialization of an API resource is not implemented.");
}
/**
* Deserializes a JSON object into an ApiResource instance.
*
* @param value - A JSON object
*
* @returns An ApiResource instance.
*/
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 'object', but value was of type '${typeof value}'.`);
}
return Object.keys(value).reduce((apiResource, routeName) => {
// We will use the Link type purely to be able to access the correct properties and avoid using any.
const route = value[routeName];
// We skip empty entries or entries that don't have an href property.
if (route == null || !route.href) {
return apiResource;
}
// Create proper instances for the route link.
apiResource[routeName] = new Link(route.href, route.title, route.templated);
return apiResource;
}, new ApiResource());
}
}
//# sourceMappingURL=api-resource-converter.js.map