UNPKG

@sitecore/sc-contenthub-webclient-sdk

Version:

Sitecore Content Hub WebClient SDK.

69 lines 3.5 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import format from "string-format"; import { EntityConstructionArgs } from "../contracts/base/entity-construction-args"; import { CultureLoadOption } from "../contracts/querying/culture-load-option"; import ErrorMessages from "../error-messages"; import { ArgumentError } from "../errors/argument-error"; import Guard from "../guard"; import { LoadOptionsMapper } from "../mappers/load-options-mapper"; import { PropertyFactory } from "./property-factory"; export class EntityFactoryBase { constructor(client, relationFactory, cultureLoader, entityCreator) { Guard.notNullOrUndefined(client); Guard.notNullOrUndefined(relationFactory); Guard.notNullOrUndefined(cultureLoader); Guard.notNullOrUndefined(entityCreator); this._client = client; this._relationFactory = relationFactory; this._cultureLoader = cultureLoader; this._entityCreator = entityCreator; } createAsync(param_1) { return __awaiter(this, arguments, void 0, function* (param, cultureLoadOption = CultureLoadOption.Default) { let definition; if (typeof param === "string") { Guard.stringNotNullOrEmpty(param); definition = yield this.getEntityDefinitionAsync(param); } else { Guard.notNullOrUndefined(param); definition = param; } const cultures = yield LoadOptionsMapper.mapCultureLoadOptionsAsync(cultureLoadOption, this._cultureLoader); const properties = PropertyFactory.createProperties(definition.getPropertyDefinitions(), cultures); const relations = this._relationFactory.createRelations(definition.getRelationDefinitions()); const args = new EntityConstructionArgs(definition.name, { cultures: cultures, properties: properties, relations: relations, }); const entity = this._entityCreator.createTypedEntity(args); entity.startTracking(); return entity; }); } getEntityDefinitionAsync(definitionName) { return __awaiter(this, void 0, void 0, function* () { const definition = yield this._client.entityDefinitions.getCachedAsync(definitionName); if (definition == null) { const errorMessage = format(ErrorMessages.EntityFactory.DefinitionDoesNotExist, definitionName); throw new ArgumentError(errorMessage); } return definition; }); } } export class EntityFactory extends EntityFactoryBase { constructor(client, relationFactory, cultureLoader, entityCreator) { super(client, relationFactory, cultureLoader, entityCreator); } } //# sourceMappingURL=entity-factory.js.map