UNPKG

@sitecore/sc-contenthub-webclient-sdk

Version:

Sitecore Content Hub WebClient SDK.

138 lines 6.49 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 { QueryLoadConfiguration } from "../contracts/querying/query-load-configuration"; import ErrorMessages from "../error-messages"; import { InvalidOperationError } from "../errors/invalid-operation-error"; import { Guard } from "../guard"; import { LoadOptionsMapper } from "../mappers/load-options-mapper"; import { QueryingMapper } from "../mappers/querying-mapper"; import { SchemaQuerying } from "./schema-querying"; /** * Contains functionality to query and scroll for entities. */ class QueryingClientBase { constructor(client) { Guard.notNullOrUndefined(client); this._client = client; } /** * Gets a single entity that matches the query. * * @param query - The query to execute * @param loadConfiguration - The load configuration. If it is null, then {@link EntityLoadConfiguration#Default} will be used * @param cancelCallback - A {@link CancelCallback} that will be placed in an Axios {@link CancelToken} if provided * @returns The entity that matches the query or null if there are no results. */ singleAsync(query, loadConfiguration, cancelCallback) { return __awaiter(this, void 0, void 0, function* () { Guard.notNullOrUndefined(query); const result = yield this.queryAsync(query, loadConfiguration, cancelCallback); if (result.items.length === 0) { return null; } else if (result.items.length > 1) { throw new InvalidOperationError(ErrorMessages.QueryingClient.MultipleResults); } return result.items[0]; }); } /** * Gets a the id of the entity that matches the query. * * @param query - The query to execute * @param cancelCallback - A {@link CancelCallback} that will be placed in an Axios {@link CancelToken} if provided * @returns The id of the entity that matches the query or null if there are no results. */ singleIdAsync(query, cancelCallback) { return __awaiter(this, void 0, void 0, function* () { Guard.notNullOrUndefined(query); const result = yield this.queryIdsAsync(query, cancelCallback); if (result.items.length === 0) { return null; } else if (result.items.length > 1) { throw new InvalidOperationError(ErrorMessages.QueryingClient.MultipleResults); } return result.items[0]; }); } //? deprecated resolveLoadConfiguration(query, loadConfiguration) { let queryLoadConfiguration; if (loadConfiguration != null) { queryLoadConfiguration = LoadOptionsMapper.toQueryLoadConfiguration(loadConfiguration); } else { queryLoadConfiguration = QueryLoadConfiguration.Default; } return queryLoadConfiguration; } } /** * Contains functionality to query and scroll for entities. */ export class QueryingClient extends QueryingClientBase { constructor(client) { super(client); Guard.notNullOrUndefined(client); this._extendedClient = client; this._schemaQuerying = new SchemaQuerying(client); this._mapper = new QueryingMapper(client); } /** * Retrieves entities matching the query. * * @param query - The query to execute * @param loadConfiguration - The load configuration. If it is null, then {@link EntityLoadConfiguration#Default} will be used * @param cancelCallback - A {@link CancelCallback} that will be placed in an Axios {@link CancelToken} if provided */ queryAsync(query, loadConfiguration, cancelCallback) { return __awaiter(this, void 0, void 0, function* () { Guard.notNullOrUndefined(query, "query"); //TODO query.validate(); const queryLoadConfiguration = this.resolveLoadConfiguration(query, loadConfiguration || undefined); const resource = yield this._schemaQuerying.queryWithSchemaAsync(query, queryLoadConfiguration, cancelCallback); loadConfiguration = LoadOptionsMapper.toEntityLoadConfiguration(queryLoadConfiguration || undefined); const result = yield this._mapper.mapEntityQueryResultAsync(resource, query, loadConfiguration); return result; }); } /** * Retrieves the ids of entities matching the query. * * @param query - The query to execute * @param cancelCallback - A {@link CancelCallback} that will be placed in an Axios {@link CancelToken} if provided */ queryIdsAsync(query, cancelCallback) { return __awaiter(this, void 0, void 0, function* () { Guard.notNullOrUndefined(query, "query"); //TODO query.validate(); const resource = yield this._schemaQuerying.queryWithSchemaAsync(query, QueryLoadConfiguration.Ids, cancelCallback); const result = yield this._mapper.mapEntityIdQueryResultAsync(resource, query); return result; }); } /** * Retrieves entities matching the query. * * @param query - The query to execute * @param cancelCallback - A {@link CancelCallback} that will be placed in an Axios {@link CancelToken} if provided */ searchAfterAsync(query, cancelCallback) { return __awaiter(this, void 0, void 0, function* () { Guard.notNullOrUndefined(query, "query"); Guard.notNullOrUndefined(query.filter, "query.filter"); const resource = yield this._schemaQuerying.searchAfterWithSchemaAsync(query, cancelCallback); const result = yield this._mapper.mapEntitySearchAfterResult(resource); return result; }); } } //# sourceMappingURL=querying-client.js.map