@sitecore/sc-contenthub-webclient-sdk
Version:
Sitecore Content Hub WebClient SDK.
361 lines • 20 kB
JavaScript
"use strict";
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EntityDefinitionsClient = void 0;
const http_status_codes_1 = require("http-status-codes");
const ta_json_1 = require("ta-json");
const urijs_1 = __importDefault(require("urijs"));
const entity_definition_cache_1 = require("../caches/entity-definition-cache");
const api_1 = require("../constants/api");
const entity_definition_id_iterator_1 = require("../contracts/querying/entity-definition-id-iterator");
const entity_definition_id_query_result_1 = require("../contracts/querying/entity-definition-id-query-result");
const entity_definition_iterator_1 = require("../contracts/querying/entity-definition-iterator");
const entity_definition_query_result_1 = require("../contracts/querying/entity-definition-query-result");
const internal_error_1 = require("../errors/internal-error");
const guard_1 = __importDefault(require("../guard"));
const entity_definition_collection_resource_1 = require("../models/entity-definition-collection-resource");
const entity_definition_resource_1 = require("../models/entity-definition-resource");
const type_guards_1 = require("../type-guards");
const array_utilities_1 = require("../utilities/array-utilities");
const response_handler_1 = require("./response-handler");
class EntityDefinitionsClient {
constructor(client) {
this._client = client;
this._cache = new entity_definition_cache_1.EntityDefinitionCache();
}
getAllCachedAsync() {
return __awaiter(this, arguments, void 0, function* (includeConditionalMembers = true, loadPermissions, viewMode, excludeTaxonomyDefinitions, excludeSystemOwnedDefinitions, filter, definitionsToLoadByName, definitionsToLoadById, excludeLightDefinitions) {
let definitions = this._cache.getAll(undefined, includeConditionalMembers, loadPermissions, viewMode, excludeTaxonomyDefinitions, excludeSystemOwnedDefinitions, filter, definitionsToLoadByName, definitionsToLoadById, excludeLightDefinitions);
if (!definitions) {
definitions = yield this.getAllAsync(includeConditionalMembers, loadPermissions, viewMode, excludeTaxonomyDefinitions, excludeSystemOwnedDefinitions, filter, definitionsToLoadByName, definitionsToLoadById, excludeLightDefinitions);
}
return definitions;
});
}
getAllAsync() {
return __awaiter(this, arguments, void 0, function* (includeConditionalMembers = true, loadPermissions, viewMode, excludeTaxonomyDefinitions, excludeSystemOwnedDefinitions, filter, definitionsToLoadByName, definitionsToLoadById, excludeLightDefinitions) {
const allEntityDefinitions = yield this.getAllBatchedAsync(includeConditionalMembers, loadPermissions, viewMode, excludeTaxonomyDefinitions, excludeSystemOwnedDefinitions, filter, definitionsToLoadByName, definitionsToLoadById, excludeLightDefinitions);
this._cache.loadAll(allEntityDefinitions, undefined, undefined, includeConditionalMembers, loadPermissions, viewMode, excludeTaxonomyDefinitions, excludeSystemOwnedDefinitions, filter, definitionsToLoadByName, definitionsToLoadById, excludeLightDefinitions);
return allEntityDefinitions;
});
}
getAsync(param_1) {
return __awaiter(this, arguments, void 0, function* (param, includeConditionalMembers = true, loadPermissions, cacheSignature) {
guard_1.default.notNullOrUndefined(param);
const link = yield this._client.linkHelper.definitionToLinkAsync(param, includeConditionalMembers, loadPermissions);
const uri = new urijs_1.default(link.href);
if (cacheSignature && !uri.hasQuery("after")) {
uri.addQuery("after", cacheSignature);
link.href = uri.toString();
}
return this.getDefinitionAsync(link);
});
}
getCachedAsync(param_1) {
return __awaiter(this, arguments, void 0, function* (param, includeConditionalMembers = true, loadPermissions, cacheSignature) {
guard_1.default.notNullOrUndefined(param);
let definitionPromise = this._cache.getAsync(param, includeConditionalMembers, loadPermissions);
if (!definitionPromise) {
definitionPromise = this.getAsync(param, includeConditionalMembers, loadPermissions, cacheSignature);
void this._cache.loadAsync(param, definitionPromise, includeConditionalMembers, loadPermissions);
}
return definitionPromise;
});
}
getNameAsync(id, cacheSignature) {
return __awaiter(this, void 0, void 0, function* () {
guard_1.default.validId(id);
const definition = yield this.getAsync(id, undefined, undefined, cacheSignature);
return definition && definition.name;
});
}
getNameCachedAsync(id, cacheSignature) {
return __awaiter(this, void 0, void 0, function* () {
guard_1.default.validId(id);
let definition = this._cache.get(id);
if (!definition) {
definition = yield this.getAsync(id, undefined, undefined, cacheSignature);
}
return definition && definition.name;
});
}
getIdAsync(name, cacheSignature) {
return __awaiter(this, void 0, void 0, function* () {
guard_1.default.stringNotNullOrEmpty(name);
const definition = yield this.getAsync(name, undefined, undefined, cacheSignature);
return definition && definition.id;
});
}
getIdCachedAsync(name, cacheSignature) {
return __awaiter(this, void 0, void 0, function* () {
guard_1.default.stringNotNullOrEmpty(name);
let definition = this._cache.get(name);
if (!definition) {
definition = yield this.getAsync(name, undefined, undefined, cacheSignature);
}
return definition && definition.id;
});
}
getManyAsync(param_1) {
return __awaiter(this, arguments, void 0, function* (param, includeConditionalMembers = true, loadPermissions) {
guard_1.default.notNullOrUndefined(param);
if (param.length === 0) {
return [];
}
else if (type_guards_1.TypeGuards.isStringArray(param)) {
guard_1.default.arrayNoneNullOrEmptyString(param);
const linkFactory = (batch, batchSize) => this._client.linkHelper.definitionsToLinkAsync(batch, undefined, batchSize, includeConditionalMembers, undefined, loadPermissions);
return this.getDefinitionsBatchedAsync(param, linkFactory);
}
else {
guard_1.default.validIds(param);
const linkFactory = (batch, batchSize) => this._client.linkHelper.definitionsToLinkAsync(batch, undefined, batchSize, includeConditionalMembers);
return this.getDefinitionsBatchedAsync(param, linkFactory);
}
});
}
getManyCachedAsync(param_1) {
return __awaiter(this, arguments, void 0, function* (param, includeConditionalMembers = true, loadPermissions) {
guard_1.default.notNullOrUndefined(param);
if (param.length === 0)
return [];
const shouldGetByName = typeof param[0] === "string";
const stillToFetch = [];
const result = [];
for (const nameOrId of param) {
const definition = this._cache.getAsync(nameOrId, includeConditionalMembers, loadPermissions);
if (definition != null) {
result.push(definition);
}
else {
stillToFetch.push(nameOrId);
}
}
if (stillToFetch != null && stillToFetch.length > 0) {
const set = new Set(stillToFetch);
const fetchedDefinitions = this.getManyAsync(shouldGetByName ? [...set] : [...set], includeConditionalMembers, loadPermissions);
result.push(fetchedDefinitions);
}
const loadedResults = yield Promise.all(result);
return loadedResults.flat().filter(definition => definition != null);
});
}
getNamesAsync(ids) {
return __awaiter(this, void 0, void 0, function* () {
guard_1.default.notNullOrUndefined(ids);
if (ids.length === 0)
return {};
guard_1.default.validIds(ids);
const definitions = yield this.getManyAsync(ids);
const result = {};
for (const definition of definitions) {
result[definition.id] = definition.name;
}
return result;
});
}
getNamesCachedAsync(ids) {
return __awaiter(this, void 0, void 0, function* () {
guard_1.default.notNullOrUndefined(ids);
if (ids.length === 0)
return {};
guard_1.default.validIds(ids);
const definitions = yield this.getManyCachedAsync(ids);
const result = {};
for (const definition of definitions) {
result[definition.id] = definition.name;
}
return result;
});
}
getIdsAsync(names) {
return __awaiter(this, void 0, void 0, function* () {
guard_1.default.notNullOrUndefined(names);
if (names.length === 0)
return {};
const definitions = yield this.getManyAsync(names);
const result = {};
for (const definition of definitions) {
result[definition.name] = definition.id;
}
return result;
});
}
getIdsCachedAsync(names) {
return __awaiter(this, void 0, void 0, function* () {
guard_1.default.notNullOrUndefined(names);
if (names.length === 0)
return {};
const definitions = yield this.getManyCachedAsync(names);
const result = {};
for (const definition of definitions) {
result[definition.name] = definition.id;
}
return result;
});
}
saveAsync(definition) {
return __awaiter(this, void 0, void 0, function* () {
guard_1.default.notNullOrUndefined(definition);
const resource = yield this._client.entityDefinitionMapper.mapEntityDefinitionResourceAsync(definition);
let response;
if (definition.isNew) {
const link = yield this._client.linkHelper.definitionsLinkAsync();
response = yield this._client.raw.postAsync(link.href, ta_json_1.TaJson.serialize(resource));
}
else {
const link = yield this._client.linkHelper.definitionToLinkAsync(definition.name);
response = yield this._client.raw.putAsync(link.href, ta_json_1.TaJson.serialize(resource));
}
response_handler_1.ResponseHandler.handleErrors(response);
let definitionId = 0;
if (definition.isNew) {
definitionId = (yield this.getIdAsync(definition.name)) || 0;
}
else {
definitionId = definition.id;
this._cache.invalidate(definitionId);
}
if (definitionId === 0) {
throw new internal_error_1.InternalError("Did not receive a valid id after saving.");
}
return definitionId;
});
}
deleteAsync(param) {
return __awaiter(this, void 0, void 0, function* () {
guard_1.default.notNullOrUndefined(param);
if (typeof param === "string") {
guard_1.default.stringNotNullOrEmpty(param);
}
else {
guard_1.default.validId(param);
}
const link = yield this._client.linkHelper.definitionToLinkAsync(param);
const response = yield this._client.raw.deleteAsync(link.href);
response_handler_1.ResponseHandler.handleErrors(response);
this._cache.invalidate(param);
});
}
getRangeAsync(skip, take) {
return __awaiter(this, void 0, void 0, function* () {
guard_1.default.notNegative(skip);
guard_1.default.notNegative(take);
const resource = yield this.getDefinitionsSkipTake(skip, take);
const definitions = yield this._client.entityDefinitionMapper.mapEntityDefinitionsAsync(resource);
for (const definition of definitions) {
this._cache.load(definition);
}
return new entity_definition_query_result_1.EntityDefinitionQueryResult(this._client, definitions, resource.totalItems || 0, skip);
});
}
getRangeIdsAsync(skip, take) {
return __awaiter(this, void 0, void 0, function* () {
guard_1.default.notNegative(skip);
guard_1.default.notNegative(take);
const result = yield this.getRangeAsync(skip, take);
return new entity_definition_id_query_result_1.EntityDefinitionIdQueryResult(this._client, result.items.map(item => item.id), result.totalNumberOfResults, skip);
});
}
createEntityDefinitionIterator(pageSize) {
return new entity_definition_iterator_1.EntityDefinitionIterator(this._client, pageSize);
}
createEntityDefinitionIdIterator(pageSize) {
return new entity_definition_id_iterator_1.EntityDefinitionIdIterator(this._client, pageSize);
}
getDefinitionsSkipTake(skip_1, take_1) {
return __awaiter(this, arguments, void 0, function* (skip, take, includeConditionalMembers = true, loadPermissions) {
const link = yield this._client.linkHelper.definitionsToLinkAsync(null, skip, take, includeConditionalMembers, undefined, loadPermissions);
const response = yield this._client.raw.getAsync(link.href);
const collectionResource = ta_json_1.TaJson.deserialize(response.content, entity_definition_collection_resource_1.EntityDefinitionCollectionResource);
return collectionResource;
});
}
getDefinitionAsync(link) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this._client.raw.getAsync(link.href);
if (response.statusCode === http_status_codes_1.StatusCodes.NOT_FOUND) {
return null;
}
const resource = ta_json_1.TaJson.deserialize(response.content, entity_definition_resource_1.EntityDefinitionResource);
const definition = yield this._client.entityDefinitionMapper.mapEntityDefinitionAsync(resource);
if (definition) {
const uri = new urijs_1.default(link.href);
this._cache.load(definition, uri.hasQuery(api_1.ENTITY_DEFINITIONS.includeConditionalMembers, true), uri.hasQuery(api_1.ENTITY_DEFINITIONS.loadPermissions, true));
}
return definition;
});
}
getDefinitionsBatchedAsync(definitionsToFetch, linkFactory) {
return __awaiter(this, void 0, void 0, function* () {
if (!definitionsToFetch || definitionsToFetch.length === 0) {
return [];
}
// We need to batch this or the backend will refuse to respond.
const batches = (0, array_utilities_1.chunk)(definitionsToFetch, EntityDefinitionsClient._batchSize);
const includeConditionalMembers = true;
const definitions = [];
for (const definitionBatch of batches) {
const batchResult = linkFactory(definitionBatch, EntityDefinitionsClient._batchSize, includeConditionalMembers)
.then(link => {
return this._client.raw.getAsync(link.href);
})
.then(response => {
response_handler_1.ResponseHandler.handleErrors(response);
const resource = ta_json_1.TaJson.deserialize(response.content, entity_definition_collection_resource_1.EntityDefinitionCollectionResource);
return resource.items;
});
// Map each definition key to its result and load into cache individually
for (const definitionKey of definitionBatch) {
const definition = batchResult
.then(result => {
var _a;
return (_a = result.find(definition => typeof definitionKey === "string"
? definition.name === definitionKey
: definition.id === definitionKey)) !== null && _a !== void 0 ? _a : null;
})
.then(resource => resource ? this._client.entityDefinitionMapper.mapEntityDefinitionAsync(resource) : null);
definitions.push(definition);
// Important this caching is done before any promise is resolved, to cache the load ASAP.
void this._cache.loadAsync(definitionKey, definition, includeConditionalMembers);
}
}
return Promise.all(definitions).then(results => results.filter((result) => result != null));
});
}
getAllBatchedAsync() {
return __awaiter(this, arguments, void 0, function* (includeConditionalMembers = true, loadPermissions, viewMode, excludeTaxonomyDefinitions, excludeSystemOwnedDefinitions, filter, definitionsToLoadByName, definitionsToLoadById, excludeLightDefinitions) {
var _a, _b;
const resources = [];
let resource = null;
let skip = 0;
while (resource == null || resource.next) {
const link = ((_a = resource === null || resource === void 0 ? void 0 : resource.next) !== null && _a !== void 0 ? _a : (yield this._client.linkHelper.definitionsLinkAsync(skip, EntityDefinitionsClient._batchSize, includeConditionalMembers, viewMode, loadPermissions, excludeTaxonomyDefinitions, excludeSystemOwnedDefinitions, filter, definitionsToLoadByName, definitionsToLoadById, excludeLightDefinitions)));
const response = yield this._client.raw.getAsync(link.href);
response_handler_1.ResponseHandler.handleErrors(response);
resource = ta_json_1.TaJson.deserialize(response.content, entity_definition_collection_resource_1.EntityDefinitionCollectionResource);
resources.push(...resource.items);
skip += (_b = resource.returnedItems) !== null && _b !== void 0 ? _b : resource.items.length;
}
const definitions = yield this._client.entityDefinitionMapper.mapEntityDefinitionsAsync(resources);
for (const definition of definitions) {
this._cache.load(definition);
}
return definitions;
});
}
}
exports.EntityDefinitionsClient = EntityDefinitionsClient;
EntityDefinitionsClient._batchSize = 25;
//# sourceMappingURL=entity-definitions-client.js.map