UNPKG

@sitecore/sc-contenthub-webclient-sdk

Version:

Sitecore Content Hub WebClient SDK.

99 lines 4.32 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 format from "string-format"; import { TaJson } from "ta-json"; import { ApiInfoResource } from "../api/api-info-resource"; import { API_ROOT, HEADERS } from "../constants/api"; import ErrorMessages from "../error-messages"; import { NotSupportedError } from "../errors/not-supported-error"; import { FileVersion } from "../fileversion"; import Guard from "../guard"; import { ApiResourceMapper } from "../mappers/api-resource-mapper"; import { Compatibility } from "../models/versionining/compatibility"; import { ResponseHandler } from "./response-handler"; export class ApiClient { get disableCompatibilityChecks() { return ApiClient._disableCompatibilityChecks; } set disableCompatibilityChecks(value) { ApiClient._disableCompatibilityChecks = value; } constructor(client) { Guard.notNullOrUndefined(client); this._client = client; } getApiInfoAsync() { return __awaiter(this, void 0, void 0, function* () { yield this.loadAsync(); return this._api; }); } getApiRoutesAsync() { return __awaiter(this, void 0, void 0, function* () { yield this.loadAsync(); return this._api.routes; }); } refreshAsync() { return __awaiter(this, void 0, void 0, function* () { this._api = yield this.fetchApiInfoAsync(); }); } /** * Load the REST API information if absent. */ loadAsync() { return __awaiter(this, void 0, void 0, function* () { if (!this._api) { yield this.refreshAsync(); } }); } /** * Fetches the API information and checks if the server is running a compatible Content Hub version. * * @returns The API information. */ fetchApiInfoAsync() { return __awaiter(this, void 0, void 0, function* () { const headers = {}; headers[HEADERS.apiVersion] = "2"; const response = yield this._client.raw.getAsync(API_ROOT, headers); ResponseHandler.handleErrors(response); let resource; try { resource = TaJson.deserialize(response && response.content, ApiInfoResource); if (resource === null || resource.fileVersion === null || resource.minimumSdkVersion === null || resource.routes === null) { throw 0; } } catch (ex) { throw new NotSupportedError(ErrorMessages.ContentHubClient.IncompatibleVersionsPre3_0); } const sdkVersion = new FileVersion(3, 0, 0); //TODO Load this from somewhere const serverVersion = resource.fileVersion; if (!this.disableCompatibilityChecks) { const compatibilityResult = this._client.versionChecker.getCompatibility(serverVersion, sdkVersion, resource.minimumSdkVersion); if (compatibilityResult == Compatibility.SdkOld) { throw new NotSupportedError(format(ErrorMessages.ContentHubClient.IncompatibleMinimumVersion, sdkVersion, serverVersion, resource.minimumSdkVersion)); } if (compatibilityResult == Compatibility.SdkNew) { throw new NotSupportedError(format(ErrorMessages.ContentHubClient.IncompatibleVersion, sdkVersion, serverVersion)); } } const apiInfo = ApiResourceMapper.map(resource); return apiInfo; }); } } //# sourceMappingURL=api-client.js.map