@sitecore/sc-contenthub-webclient-sdk
Version:
Sitecore Content Hub WebClient SDK.
106 lines • 4.97 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.ApiClient = void 0;
const string_format_1 = __importDefault(require("string-format"));
const ta_json_1 = require("ta-json");
const api_info_resource_1 = require("../api/api-info-resource");
const api_1 = require("../constants/api");
const error_messages_1 = __importDefault(require("../error-messages"));
const not_supported_error_1 = require("../errors/not-supported-error");
const fileversion_1 = require("../fileversion");
const guard_1 = __importDefault(require("../guard"));
const api_resource_mapper_1 = require("../mappers/api-resource-mapper");
const compatibility_1 = require("../models/versionining/compatibility");
const response_handler_1 = require("./response-handler");
class ApiClient {
get disableCompatibilityChecks() {
return ApiClient._disableCompatibilityChecks;
}
set disableCompatibilityChecks(value) {
ApiClient._disableCompatibilityChecks = value;
}
constructor(client) {
guard_1.default.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[api_1.HEADERS.apiVersion] = "2";
const response = yield this._client.raw.getAsync(api_1.API_ROOT, headers);
response_handler_1.ResponseHandler.handleErrors(response);
let resource;
try {
resource = ta_json_1.TaJson.deserialize(response && response.content, api_info_resource_1.ApiInfoResource);
if (resource === null ||
resource.fileVersion === null ||
resource.minimumSdkVersion === null ||
resource.routes === null) {
throw 0;
}
}
catch (ex) {
throw new not_supported_error_1.NotSupportedError(error_messages_1.default.ContentHubClient.IncompatibleVersionsPre3_0);
}
const sdkVersion = new fileversion_1.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_1.Compatibility.SdkOld) {
throw new not_supported_error_1.NotSupportedError((0, string_format_1.default)(error_messages_1.default.ContentHubClient.IncompatibleMinimumVersion, sdkVersion, serverVersion, resource.minimumSdkVersion));
}
if (compatibilityResult == compatibility_1.Compatibility.SdkNew) {
throw new not_supported_error_1.NotSupportedError((0, string_format_1.default)(error_messages_1.default.ContentHubClient.IncompatibleVersion, sdkVersion, serverVersion));
}
}
const apiInfo = api_resource_mapper_1.ApiResourceMapper.map(resource);
return apiInfo;
});
}
}
exports.ApiClient = ApiClient;
//# sourceMappingURL=api-client.js.map