@sitecore/sc-contenthub-webclient-sdk
Version:
Sitecore Content Hub WebClient SDK.
354 lines • 18.8 kB
JavaScript
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 { StatusCodes } from "http-status-codes";
import { TaJson } from "ta-json";
import URI from "urijs";
import { EntityDefinitionCache } from "../caches/entity-definition-cache";
import { ENTITY_DEFINITIONS } from "../constants/api";
import { EntityDefinitionIdIterator } from "../contracts/querying/entity-definition-id-iterator";
import { EntityDefinitionIdQueryResult } from "../contracts/querying/entity-definition-id-query-result";
import { EntityDefinitionIterator } from "../contracts/querying/entity-definition-iterator";
import { EntityDefinitionQueryResult, } from "../contracts/querying/entity-definition-query-result";
import { InternalError } from "../errors/internal-error";
import Guard from "../guard";
import { EntityDefinitionCollectionResource } from "../models/entity-definition-collection-resource";
import { EntityDefinitionResource } from "../models/entity-definition-resource";
import { TypeGuards } from "../type-guards";
import { chunk } from "../utilities/array-utilities";
import { ResponseHandler } from "./response-handler";
export class EntityDefinitionsClient {
constructor(client) {
this._client = client;
this._cache = new 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.notNullOrUndefined(param);
const link = yield this._client.linkHelper.definitionToLinkAsync(param, includeConditionalMembers, loadPermissions);
const uri = new URI(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.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.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.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.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.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.notNullOrUndefined(param);
if (param.length === 0) {
return [];
}
else if (TypeGuards.isStringArray(param)) {
Guard.arrayNoneNullOrEmptyString(param);
const linkFactory = (batch, batchSize) => this._client.linkHelper.definitionsToLinkAsync(batch, undefined, batchSize, includeConditionalMembers, undefined, loadPermissions);
return this.getDefinitionsBatchedAsync(param, linkFactory);
}
else {
Guard.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.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 = yield this.getManyAsync(shouldGetByName ? [...set] : [...set], includeConditionalMembers, loadPermissions);
result.push(...fetchedDefinitions);
}
const loadedResults = yield Promise.all(result);
return loadedResults.filter((definition) => definition != null);
});
}
getNamesAsync(ids) {
return __awaiter(this, void 0, void 0, function* () {
Guard.notNullOrUndefined(ids);
if (ids.length === 0)
return {};
Guard.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.notNullOrUndefined(ids);
if (ids.length === 0)
return {};
Guard.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.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.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.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, TaJson.serialize(resource));
}
else {
const link = yield this._client.linkHelper.definitionToLinkAsync(definition.name);
response = yield this._client.raw.putAsync(link.href, TaJson.serialize(resource));
}
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 InternalError("Did not receive a valid id after saving.");
}
return definitionId;
});
}
deleteAsync(param) {
return __awaiter(this, void 0, void 0, function* () {
Guard.notNullOrUndefined(param);
if (typeof param === "string") {
Guard.stringNotNullOrEmpty(param);
}
else {
Guard.validId(param);
}
const link = yield this._client.linkHelper.definitionToLinkAsync(param);
const response = yield this._client.raw.deleteAsync(link.href);
ResponseHandler.handleErrors(response);
this._cache.invalidate(param);
});
}
getRangeAsync(skip, take) {
return __awaiter(this, void 0, void 0, function* () {
Guard.notNegative(skip);
Guard.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 EntityDefinitionQueryResult(this._client, definitions, resource.totalItems || 0, skip);
});
}
getRangeIdsAsync(skip, take) {
return __awaiter(this, void 0, void 0, function* () {
Guard.notNegative(skip);
Guard.notNegative(take);
const result = yield this.getRangeAsync(skip, take);
return new EntityDefinitionIdQueryResult(this._client, result.items.map(item => item.id), result.totalNumberOfResults, skip);
});
}
createEntityDefinitionIterator(pageSize) {
return new EntityDefinitionIterator(this._client, pageSize);
}
createEntityDefinitionIdIterator(pageSize) {
return new 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 = TaJson.deserialize(response.content, 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 === StatusCodes.NOT_FOUND) {
return null;
}
const resource = TaJson.deserialize(response.content, EntityDefinitionResource);
const definition = yield this._client.entityDefinitionMapper.mapEntityDefinitionAsync(resource);
if (definition) {
const uri = new URI(link.href);
this._cache.load(definition, uri.hasQuery(ENTITY_DEFINITIONS.includeConditionalMembers, true), uri.hasQuery(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 = 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 => {
ResponseHandler.handleErrors(response);
const resource = TaJson.deserialize(response.content, 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);
ResponseHandler.handleErrors(response);
resource = TaJson.deserialize(response.content, 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;
});
}
}
EntityDefinitionsClient._batchSize = 25;
//# sourceMappingURL=entity-definitions-client.js.map