UNPKG

@sitecore/sc-contenthub-webclient-sdk

Version:

Sitecore Content Hub WebClient SDK.

287 lines 12.9 kB
"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.EntitiesClient = void 0; const ta_json_1 = require("ta-json"); const api_1 = require("../constants/api"); const defaults_1 = require("../constants/defaults"); const entity_iterator_1 = require("../contracts/querying/entity-iterator"); const entity_load_configuration_1 = require("../contracts/querying/entity-load-configuration"); const filters_1 = require("../contracts/querying/filters"); const id_iterator_1 = require("../contracts/querying/id-iterator"); const query_1 = require("../contracts/querying/query"); const error_messages_1 = __importDefault(require("../error-messages")); const internal_error_1 = require("../errors/internal-error"); const web_api_error_1 = require("../errors/web-api-error"); const guard_1 = __importDefault(require("../guard")); const link_1 = __importDefault(require("../link")); const entity_copy_options_mapper_1 = require("../mappers/entity-copy-options-mapper"); const type_guards_1 = require("../type-guards"); const response_handler_1 = require("./response-handler"); /** * The client responsible for getting, saving and deleting entities. */ class EntitiesClient { constructor(client) { guard_1.default.notNullOrUndefined(client); this._client = client; } /** * {@inheritDoc} */ getAsync(param, loadConfiguration, cancelCallback) { return __awaiter(this, void 0, void 0, function* () { let query; if (typeof param === "string") { guard_1.default.stringNotNullOrEmpty(param); query = new query_1.Query({ filter: new filters_1.IdentifierQueryFilter({ operator: filters_1.ComparisonOperator.Equals, value: param, }), }); } else { guard_1.default.validId(param); query = new query_1.Query({ filter: new filters_1.IdQueryFilter({ operator: filters_1.ComparisonOperator.Equals, value: param, }), }); } const result = yield this._client.querying.queryAsync(query, loadConfiguration || null, cancelCallback); const entity = EntitiesClient.single(result); return entity; }); } /** * {@inheritDoc} */ getManyAsync(param, loadConfiguration, cancelCallback) { return __awaiter(this, void 0, void 0, function* () { if (param == null || param.length === 0) return []; let query; if (type_guards_1.TypeGuards.isStringArray(param)) { const identifiers = param; guard_1.default.arrayNoneNullOrEmptyString(identifiers); query = new query_1.Query({ filter: new filters_1.IdentifierQueryFilter({ operator: filters_1.ComparisonOperator.Equals, values: identifiers, }), }); } else { const ids = param; guard_1.default.arrayNoneNullOrUndefined(ids); guard_1.default.validIds(ids); query = new query_1.Query({ filter: new filters_1.IdQueryFilter({ operator: filters_1.ComparisonOperator.Equals, values: ids, }), }); } const result = yield this._client.querying.queryAsync(query, loadConfiguration || null, cancelCallback); return result.items; }); } /** * {@inheritDoc} */ getByDefinitionAsync(definitionName_1, loadConfiguration_1) { return __awaiter(this, arguments, void 0, function* (definitionName, loadConfiguration, skip = defaults_1.ENTITIES.skip, take = defaults_1.ENTITIES.take, sorting, cancelCallback) { guard_1.default.stringNotNullOrEmpty(definitionName); guard_1.default.notNegative(skip); guard_1.default.notNegative(take); const query = new query_1.Query({ filter: new filters_1.DefinitionQueryFilter({ name: definitionName, operator: filters_1.ComparisonOperator.Equals, }), skip: skip, take: take, sorting: sorting, }); return this._client.querying.queryAsync(query, loadConfiguration, cancelCallback); }); } /** * {@inheritDoc} */ getIdsByDefinitionAsync(definitionName, skip, take, cancelCallback) { return __awaiter(this, void 0, void 0, function* () { guard_1.default.stringNotNullOrEmpty(definitionName); if (skip != null) guard_1.default.notNegative(skip); if (take != null) guard_1.default.notNegative(take); const query = new query_1.Query({ filter: new filters_1.DefinitionQueryFilter({ name: definitionName, operator: filters_1.ComparisonOperator.Equals, }), skip: skip, take: take, //TODO sorting on created on ascending? }); const result = yield this._client.querying.queryIdsAsync(query, cancelCallback); return result; }); } /** * {@inheritDoc} */ getEntityIterator(definition, loadConfiguration) { guard_1.default.stringNotNullOrEmpty(definition); loadConfiguration = loadConfiguration || entity_load_configuration_1.EntityLoadConfiguration.Default; const query = new query_1.Query({ filter: new filters_1.DefinitionQueryFilter({ name: definition, operator: filters_1.ComparisonOperator.Equals, }), }); return new entity_iterator_1.EntityIterator(this._client, query, loadConfiguration); } /** * {@inheritDoc} */ getEntityIdIterator(definition) { guard_1.default.stringNotNullOrEmpty(definition); const query = new query_1.Query({ filter: new filters_1.DefinitionQueryFilter({ name: definition, operator: filters_1.ComparisonOperator.Equals, }), }); return new id_iterator_1.EntityIdIterator(this._client, query); } /** * {@inheritDoc} */ saveAsync(entity, cancelCallback, culture) { return __awaiter(this, void 0, void 0, function* () { guard_1.default.notNullOrUndefined(entity); let id; if (entity.isNew) { id = yield this.createEntityAsync(entity, cancelCallback); } else { yield this.updateEntityAsync(entity, cancelCallback, culture); id = entity.id; } entity.markClean(); return id; }); } /** * {@inheritDoc} */ deleteAsync(entityId, cancelCallback) { return __awaiter(this, void 0, void 0, function* () { guard_1.default.validId(entityId); const link = yield this._client.linkHelper.entityToLinkAsync(entityId); const response = yield this._client.raw.deleteAsync(link.href, undefined, cancelCallback); response_handler_1.ResponseHandler.handleErrors(response); }); } /** * {@inheritDoc} */ copyAsync(entityId, copyOptions, cancelCallback) { return __awaiter(this, void 0, void 0, function* () { guard_1.default.validId(entityId); guard_1.default.notNull(copyOptions); const link = yield this._client.linkHelper.copyEntityToLinkAsync(entityId); const options = entity_copy_options_mapper_1.EntityCopyOptionsMapper.map(copyOptions); const response = yield this._client.raw.postAsync(link.href, ta_json_1.TaJson.serialize(options), undefined, cancelCallback); response_handler_1.ResponseHandler.handleErrors(response); const href = response.responseHeaders[api_1.HEADERS.location]; if (!href) { throw new web_api_error_1.WebApiError(error_messages_1.default.EntitiesClient.UnableToExtractLocationHeaderFromResponse, response.statusCode, response); } const id = yield this._client.linkHelper.idFromEntityAsync(new link_1.default(href)); if (id === null || id === 0) { throw new internal_error_1.InternalError(error_messages_1.default.EntitiesClient.UnableToExtractEntityIdFromHeader); } return id; }); } /** * Creates the specified {@link IEntity} in the system. * * @param entity - The entity to create/save in the system */ createEntityAsync(entity, cancelCallback) { return __awaiter(this, void 0, void 0, function* () { const link = yield this._client.linkHelper.entitiesLinkAsync(); const resource = yield this._client.entityMapper.toResourceAsync(entity); const response = yield this._client.raw.postAsync(link.href, ta_json_1.TaJson.serialize(resource), undefined, cancelCallback); response_handler_1.ResponseHandler.handleErrors(response); const href = response.responseHeaders[api_1.HEADERS.location]; if (!href) { throw new web_api_error_1.WebApiError(error_messages_1.default.EntitiesClient.UnableToExtractLocationHeaderFromResponse, response.statusCode, response); } const id = yield this._client.linkHelper.idFromEntityAsync(new link_1.default(href)); if (id === null || id === 0) { throw new internal_error_1.InternalError(error_messages_1.default.EntitiesClient.UnableToExtractEntityIdFromHeader); } return id; }); } /** * Update the specified {@link IEntity}. * * @param entity - The entity to update * @param cancelCallback - A {@link CancelCallback} that will be placed in an Axios {@link CancelToken} if provided * @param cultureParam - The culture to update the entity in */ updateEntityAsync(entity, cancelCallback, cultureParam) { return __awaiter(this, void 0, void 0, function* () { // Check if the entity is loaded in only one culture to improve save performance. let culture = cultureParam; if (entity.cultures.length === 1) { culture = entity.cultures[0]; } const link = yield this._client.linkHelper.entityToLinkAsync(entity.id, culture !== null && culture !== void 0 ? culture : undefined); const resource = yield this._client.entityMapper.mapDirtiesToResourceAsync(entity); const response = yield this._client.raw.putAsync(link.href, ta_json_1.TaJson.serialize(resource), undefined, cancelCallback); response_handler_1.ResponseHandler.handleErrors(response); }); } /** * Get an {@link IEntity} or `null` from the specified {@link IEntityQueryResult}. * * @remarks * Throws an {@link InternalError} if multiple results where available. * * @param queryResult - The query result (should contain one or no items) * @returns An {@link IEntity} or `null`. */ static single(queryResult) { if (queryResult.totalNumberOfResults === 0) { return null; } else if (queryResult.totalNumberOfResults === 1) { return queryResult.items[0]; } else { throw new internal_error_1.InternalError(error_messages_1.default.QueryingClient.MultipleResultsWhenOneExpected); } } } exports.EntitiesClient = EntitiesClient; //# sourceMappingURL=entities-client.js.map