UNPKG

@sitecore/sc-contenthub-webclient-sdk

Version:

Sitecore Content Hub WebClient SDK.

101 lines 4.66 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 { CultureCache } from "../caches/culture-cache"; import { DEFINITIONS } from "../constants"; import { CultureLoadOption } from "../contracts/querying/culture-load-option"; import { EntityLoadConfiguration } from "../contracts/querying/entity-load-configuration"; import { ComparisonOperator, CompositeFilterOperator, CompositeQueryFilter, DefinitionQueryFilter, FilterDataType, PropertyQueryFilter, } from "../contracts/querying/filters"; import { MemberLoadOption } from "../contracts/querying/member-load-option"; import { PropertyLoadOption } from "../contracts/querying/property-load-option"; import { Query } from "../contracts/querying/query"; import { RelationLoadOption } from "../contracts/querying/relation-load-option"; import ErrorMessages from "../error-messages"; import { InternalError } from "../errors/internal-error"; import Guard from "../guard"; export class CultureLoader { constructor(client) { Guard.notNull(client); this._client = client; this._cultureCache = new CultureCache(); } getDefaultCultureAsync() { return __awaiter(this, void 0, void 0, function* () { const cultures = yield this.getAllCulturesAsync(); return CultureLoader.extractDefaultCulture(cultures); }); } getDefaultCultureCachedAsync() { return __awaiter(this, void 0, void 0, function* () { const cultures = yield this.getAllCulturesCachedAsync(); return CultureLoader.extractDefaultCulture(cultures); }); } getAllCulturesAsync() { return __awaiter(this, void 0, void 0, function* () { const cultures = yield this.queryCultureAsync(); this._cultureCache.loadCache(cultures); return cultures; }); } getAllCulturesCachedAsync() { return __awaiter(this, void 0, void 0, function* () { let cultures = this._cultureCache.getCachedCultures(); if (cultures == null) { cultures = yield this.getAllCulturesAsync(); } return cultures; }); } //#region Private methods queryCultureAsync() { return __awaiter(this, void 0, void 0, function* () { const result = yield this._client.querying.singleAsync(CultureLoader._query, CultureLoader._loadConfig); if (result == null) { throw new InternalError(ErrorMessages.CultureLoader.CouldNotFindCultures); } const token = yield result.getPropertyValueAsync(DEFINITIONS["Setting"].value, MemberLoadOption.LazyLoading); if (token == null) { throw new InternalError(ErrorMessages.CultureLoader.CouldNotFindCultures); } const cultures = token instanceof Array ? token : typeof token === "string" ? JSON.parse(token) : []; return cultures; }); } static extractDefaultCulture(cultures) { if (cultures == null || cultures.length === 0) { return null; } return cultures[0]; } } CultureLoader._loadConfig = new EntityLoadConfiguration(CultureLoadOption.None, new PropertyLoadOption(DEFINITIONS["Setting"].value), RelationLoadOption.None); CultureLoader._query = new Query({ filter: new CompositeQueryFilter({ combineMethod: CompositeFilterOperator.And, children: [ new DefinitionQueryFilter({ name: DEFINITIONS["Setting"].definitionName, operator: ComparisonOperator.Equals, }), new PropertyQueryFilter({ operator: ComparisonOperator.Equals, dataType: FilterDataType.String, property: DEFINITIONS["Setting"].name, value: DEFINITIONS["Setting"].CultureSetting.name, }), ], }), take: 1, }); //# sourceMappingURL=culture-loader.js.map