@sitecore/sc-contenthub-webclient-sdk
Version:
Sitecore Content Hub WebClient SDK.
145 lines • 7.09 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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.QueryingClient = void 0;
const query_load_configuration_1 = require("../contracts/querying/query-load-configuration");
const error_messages_1 = __importDefault(require("../error-messages"));
const invalid_operation_error_1 = require("../errors/invalid-operation-error");
const guard_1 = require("../guard");
const load_options_mapper_1 = require("../mappers/load-options-mapper");
const querying_mapper_1 = require("../mappers/querying-mapper");
const schema_querying_1 = require("./schema-querying");
/**
* Contains functionality to query and scroll for entities.
*/
class QueryingClientBase {
constructor(client) {
guard_1.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_1.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 invalid_operation_error_1.InvalidOperationError(error_messages_1.default.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_1.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 invalid_operation_error_1.InvalidOperationError(error_messages_1.default.QueryingClient.MultipleResults);
}
return result.items[0];
});
}
//? deprecated
resolveLoadConfiguration(query, loadConfiguration) {
let queryLoadConfiguration;
if (loadConfiguration != null) {
queryLoadConfiguration = load_options_mapper_1.LoadOptionsMapper.toQueryLoadConfiguration(loadConfiguration);
}
else {
queryLoadConfiguration = query_load_configuration_1.QueryLoadConfiguration.Default;
}
return queryLoadConfiguration;
}
}
/**
* Contains functionality to query and scroll for entities.
*/
class QueryingClient extends QueryingClientBase {
constructor(client) {
super(client);
guard_1.Guard.notNullOrUndefined(client);
this._extendedClient = client;
this._schemaQuerying = new schema_querying_1.SchemaQuerying(client);
this._mapper = new querying_mapper_1.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_1.Guard.notNullOrUndefined(query, "query");
//TODO query.validate();
const queryLoadConfiguration = this.resolveLoadConfiguration(query, loadConfiguration || undefined);
const resource = yield this._schemaQuerying.queryWithSchemaAsync(query, queryLoadConfiguration, cancelCallback);
loadConfiguration = load_options_mapper_1.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_1.Guard.notNullOrUndefined(query, "query");
//TODO query.validate();
const resource = yield this._schemaQuerying.queryWithSchemaAsync(query, query_load_configuration_1.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_1.Guard.notNullOrUndefined(query, "query");
guard_1.Guard.notNullOrUndefined(query.filter, "query.filter");
const resource = yield this._schemaQuerying.searchAfterWithSchemaAsync(query, cancelCallback);
const result = yield this._mapper.mapEntitySearchAfterResult(resource);
return result;
});
}
}
exports.QueryingClient = QueryingClient;
//# sourceMappingURL=querying-client.js.map