@sitecore/sc-contenthub-webclient-sdk
Version:
Sitecore Content Hub WebClient SDK.
47 lines • 2.12 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApiResourceConverter = void 0;
const api_resource_1 = require("../api/api-resource");
const invalid_operation_error_1 = require("../errors/invalid-operation-error");
const not_implemented_error_1 = require("../errors/not-implemented-error");
const link_1 = __importDefault(require("../link"));
class ApiResourceConverter {
/**
* Not implemented.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
serialize(value) {
throw new not_implemented_error_1.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 invalid_operation_error_1.InvalidOperationError("Can't deserialize falsy value.");
}
else if (typeof value !== "object" || value instanceof Array) {
throw new invalid_operation_error_1.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_1.default(route.href, route.title, route.templated);
return apiResource;
}, new api_resource_1.ApiResource());
}
}
exports.ApiResourceConverter = ApiResourceConverter;
//# sourceMappingURL=api-resource-converter.js.map