UNPKG

@sitecore/sc-contenthub-webclient-sdk

Version:

Sitecore Content Hub WebClient SDK.

102 lines 4.99 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 URI from "urijs"; import URITemplate from "urijs/src/URITemplate"; import { HEADERS, QUERY, SEARCH_AFTER } from "../constants/api"; import { QueryRequest } from "../contracts/querying/query-request"; import { QuerySortOrder } from "../contracts/querying/query-sort-order"; import { SortFieldType } from "../contracts/querying/sort-field-type"; import { Sorting } from "../contracts/querying/sorting"; import Guard from "../guard"; import { EntityCollectionResource } from "../models/entity-collection-resource"; import { QueryResultResource } from "../models/query-result-resource"; import { ResponseHandler } from "./response-handler"; export class SchemaQuerying { constructor(client) { Guard.notNullOrUndefined(client); this._client = client; } queryWithSchemaAsync(query, loadConfiguration, cancelCallback) { return __awaiter(this, void 0, void 0, function* () { Guard.notNullOrUndefined(query, "query"); Guard.notNullOrUndefined(loadConfiguration, "loadConfiguration"); //TODO query.validate(); const routes = yield this._client.api.getApiRoutesAsync(); const queryEndpoint = routes[QUERY.templateName]; let uri; /* istanbul ignore next */ if (queryEndpoint.templated) { const uriTemplate = new URITemplate(queryEndpoint.href); uri = uriTemplate.expand({}); } else { uri = new URI(queryEndpoint.href); } const queryRequest = new QueryRequest({ query: query, loadConfiguration: loadConfiguration, }); const content = TaJson.serialize(queryRequest); const resource = yield this.queryEndpointAsync(uri, content, cancelCallback); return resource; }); } // eslint-disable-next-line @typescript-eslint/no-unused-vars,@typescript-eslint/require-await scrollWithSchemaAsync(scrollRequest) { return __awaiter(this, void 0, void 0, function* () { throw new Error("Method not implemented."); //TODO }); } searchAfterWithSchemaAsync(query, cancelCallback) { return __awaiter(this, void 0, void 0, function* () { Guard.notNullOrUndefined(query, "query"); Guard.notNullOrUndefined(query.filter, "query.filter"); const routes = yield this._client.api.getApiRoutesAsync(); const queryEndpoint = routes[SEARCH_AFTER.templateName]; let uri; /* istanbul ignore next */ if (queryEndpoint.templated) { const uriTemplate = new URITemplate(queryEndpoint.href); uri = uriTemplate.expand({}); } else { uri = new URI(queryEndpoint.href); } if (query.sorting == null || query.sorting.length == 0) { query.sorting = [ new Sorting({ field: "created_on", fieldType: SortFieldType.Property, order: QuerySortOrder.Desc }), ]; } const headers = {}; headers[HEADERS.apiVersion] = "2"; headers[HEADERS.minimalSchema] = "true"; headers["Content-Type"] = "application/json"; const content = TaJson.serialize(query); const response = yield this._client.raw.postAsync(uri, content, headers, cancelCallback); ResponseHandler.handleErrors(response); const resource = TaJson.deserialize(response.content, EntityCollectionResource); return resource; }); } queryEndpointAsync(endpoint, content, cancelCallback) { return __awaiter(this, void 0, void 0, function* () { const response = yield this._client.raw.postAsync(endpoint, content, { [HEADERS.apiVersion]: "3", [HEADERS.minimalSchema]: "true", "Content-Type": "application/json", }, cancelCallback); ResponseHandler.handleErrors(response); const resource = TaJson.deserialize(response.content, QueryResultResource); return resource; }); } } //# sourceMappingURL=schema-querying.js.map