UNPKG

@sitecore/sc-contenthub-webclient-sdk

Version:

Sitecore Content Hub WebClient SDK.

280 lines 11.7 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 { TaJson } from "ta-json"; import { HEADERS } from "../constants/api"; import { ENTITIES } from "../constants/defaults"; import { EntityIterator } from "../contracts/querying/entity-iterator"; import { EntityLoadConfiguration } from "../contracts/querying/entity-load-configuration"; import { ComparisonOperator, DefinitionQueryFilter, IdentifierQueryFilter, IdQueryFilter, } from "../contracts/querying/filters"; import { EntityIdIterator } from "../contracts/querying/id-iterator"; import { Query } from "../contracts/querying/query"; import ErrorMessages from "../error-messages"; import { InternalError } from "../errors/internal-error"; import { WebApiError } from "../errors/web-api-error"; import Guard from "../guard"; import Link from "../link"; import { EntityCopyOptionsMapper } from "../mappers/entity-copy-options-mapper"; import { TypeGuards } from "../type-guards"; import { ResponseHandler } from "./response-handler"; /** * The client responsible for getting, saving and deleting entities. */ export class EntitiesClient { constructor(client) { Guard.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.stringNotNullOrEmpty(param); query = new Query({ filter: new IdentifierQueryFilter({ operator: ComparisonOperator.Equals, value: param, }), }); } else { Guard.validId(param); query = new Query({ filter: new IdQueryFilter({ operator: 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 (TypeGuards.isStringArray(param)) { const identifiers = param; Guard.arrayNoneNullOrEmptyString(identifiers); query = new Query({ filter: new IdentifierQueryFilter({ operator: ComparisonOperator.Equals, values: identifiers, }), }); } else { const ids = param; Guard.arrayNoneNullOrUndefined(ids); Guard.validIds(ids); query = new Query({ filter: new IdQueryFilter({ operator: 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 = ENTITIES.skip, take = ENTITIES.take, sorting, cancelCallback) { Guard.stringNotNullOrEmpty(definitionName); Guard.notNegative(skip); Guard.notNegative(take); const query = new Query({ filter: new DefinitionQueryFilter({ name: definitionName, operator: 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.stringNotNullOrEmpty(definitionName); if (skip != null) Guard.notNegative(skip); if (take != null) Guard.notNegative(take); const query = new Query({ filter: new DefinitionQueryFilter({ name: definitionName, operator: 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.stringNotNullOrEmpty(definition); loadConfiguration = loadConfiguration || EntityLoadConfiguration.Default; const query = new Query({ filter: new DefinitionQueryFilter({ name: definition, operator: ComparisonOperator.Equals, }), }); return new EntityIterator(this._client, query, loadConfiguration); } /** * {@inheritDoc} */ getEntityIdIterator(definition) { Guard.stringNotNullOrEmpty(definition); const query = new Query({ filter: new DefinitionQueryFilter({ name: definition, operator: ComparisonOperator.Equals, }), }); return new EntityIdIterator(this._client, query); } /** * {@inheritDoc} */ saveAsync(entity, cancelCallback, culture) { return __awaiter(this, void 0, void 0, function* () { Guard.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.validId(entityId); const link = yield this._client.linkHelper.entityToLinkAsync(entityId); const response = yield this._client.raw.deleteAsync(link.href, undefined, cancelCallback); ResponseHandler.handleErrors(response); }); } /** * {@inheritDoc} */ copyAsync(entityId, copyOptions, cancelCallback) { return __awaiter(this, void 0, void 0, function* () { Guard.validId(entityId); Guard.notNull(copyOptions); const link = yield this._client.linkHelper.copyEntityToLinkAsync(entityId); const options = EntityCopyOptionsMapper.map(copyOptions); const response = yield this._client.raw.postAsync(link.href, TaJson.serialize(options), undefined, cancelCallback); ResponseHandler.handleErrors(response); const href = response.responseHeaders[HEADERS.location]; if (!href) { throw new WebApiError(ErrorMessages.EntitiesClient.UnableToExtractLocationHeaderFromResponse, response.statusCode, response); } const id = yield this._client.linkHelper.idFromEntityAsync(new Link(href)); if (id === null || id === 0) { throw new InternalError(ErrorMessages.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, TaJson.serialize(resource), undefined, cancelCallback); ResponseHandler.handleErrors(response); const href = response.responseHeaders[HEADERS.location]; if (!href) { throw new WebApiError(ErrorMessages.EntitiesClient.UnableToExtractLocationHeaderFromResponse, response.statusCode, response); } const id = yield this._client.linkHelper.idFromEntityAsync(new Link(href)); if (id === null || id === 0) { throw new InternalError(ErrorMessages.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, TaJson.serialize(resource), undefined, cancelCallback); 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 InternalError(ErrorMessages.QueryingClient.MultipleResultsWhenOneExpected); } } } //# sourceMappingURL=entities-client.js.map