kentico-cloud-delivery
Version:
Official Kentico Cloud Delivery SDK
336 lines • 13 kB
JavaScript
"use strict";
exports.__esModule = true;
var rxjs_1 = require("rxjs");
var operators_1 = require("rxjs/operators");
var mappers_1 = require("../mappers");
var delivery_retry_strategy_1 = require("./http/delivery-retry-strategy");
var QueryService = /** @class */ (function () {
function QueryService(
/**
* Delivery client configuration
*/
config,
/**
* Http service for fetching data
*/
httpService,
/**
* Used for manipulating with rich text HTML (required for Node / Browser support)
*/
richTextHtmlParser,
/**
* Information about the SDK
* This can contain information from both this & Node SDK for internal logging with 'SDKID' header
*/
sdkInfo) {
this.config = config;
this.httpService = httpService;
this.richTextHtmlParser = richTextHtmlParser;
this.sdkInfo = sdkInfo;
/**
* Default number of retry attempts when user did not set any
*/
this.defaultRetryAttempts = 3;
/**
* Excluded status code from retry functionality
*/
this.retryExcludedStatuses = [404, 401];
/**
* Header name for SDK usage
*/
this.sdkVersionHeader = 'X-KC-SDKID';
/**
* Default base Url to Kentico Delivery API
*/
this.defaultBaseDeliveryApiUrl = 'https://deliver.kenticocloud.com';
/**
* Default preview url to Kentico Delivery API
*/
this.defaultPreviewDeliveryApiUrl = 'https://preview-deliver.kenticocloud.com';
/**
* Name of the header used when 'wait for loading new content' feature is used
*/
this.waitForLoadingNewContentHeader = 'X-KC-Wait-For-Loading-New-Content';
if (!config) {
throw Error("Invalid configuration has been provided");
}
this.responseMapper = new mappers_1.ResponseMapper(config, richTextHtmlParser);
}
/**
* Gets url based on the action, query configuration and options (parameters)
* @param action Action (= url part) that will be hit
* @param queryConfig Query configuration
* @param options Query options
*/
QueryService.prototype.getUrl = function (action, queryConfig, options) {
return this.addOptionsToUrl(this.getBaseUrl(queryConfig) + action, options);
};
/**
* Gets single item from given url
* @param url Url used to get single item
* @param queryConfig Query configuration
*/
QueryService.prototype.getSingleItem = function (url, queryConfig) {
var _this = this;
return this.getResponse(url, queryConfig)
.pipe(operators_1.map(function (response) {
return _this.responseMapper.mapSingleResponse(response, queryConfig);
}), operators_1.catchError(function (err) {
return rxjs_1.throwError(_this.handleError(err));
}));
};
/**
* Gets multiple items from given url
* @param url Url used to get multiple items
* @param queryConfig Query configuration
*/
QueryService.prototype.getMultipleItems = function (url, queryConfig) {
var _this = this;
return this.getResponse(url, queryConfig)
.pipe(operators_1.map(function (response) {
return _this.responseMapper.mapMultipleResponse(response, queryConfig);
}), operators_1.catchError(function (err) {
return rxjs_1.throwError(_this.handleError(err));
}));
};
/**
* Gets single content type from given url
* @param url Url used to get single type
* @param queryConfig Query configuration
*/
QueryService.prototype.getSingleType = function (url, queryConfig) {
var _this = this;
return this.getResponse(url, queryConfig)
.pipe(operators_1.map(function (response) {
return _this.responseMapper.mapSingleTypeResponse(response);
}), operators_1.catchError(function (err) {
return rxjs_1.throwError(_this.handleError(err));
}));
};
/**
* Gets multiple content types from given url
* @param url Url used to get multiple types
* @param queryConfig Query configuration
*/
QueryService.prototype.getMultipleTypes = function (url, queryConfig) {
var _this = this;
return this.getResponse(url, queryConfig)
.pipe(operators_1.map(function (response) {
return _this.responseMapper.mapMultipleTypeResponse(response);
}), operators_1.catchError(function (err) {
return rxjs_1.throwError(_this.handleError(err));
}));
};
/**
* Gets single taxonomy from given url
* @param url Url used to get single taxonomy
* @param queryConfig Query configuration
*/
QueryService.prototype.getTaxonomy = function (url, queryConfig) {
var _this = this;
return this.getResponse(url, queryConfig)
.pipe(operators_1.map(function (response) {
return _this.responseMapper.mapTaxonomyResponse(response);
}), operators_1.catchError(function (err) {
return rxjs_1.throwError(_this.handleError(err));
}));
};
/**
* Gets multiple taxonomies from given url
* @param url Url used to get multiple taxonomies
* @param queryConfig Query configuration
*/
QueryService.prototype.getTaxonomies = function (url, queryConfig) {
var _this = this;
return this.getResponse(url, queryConfig)
.pipe(operators_1.map(function (response) {
return _this.responseMapper.mapTaxonomiesResponse(response);
}), operators_1.catchError(function (err) {
return rxjs_1.throwError(_this.handleError(err));
}));
};
/**
* Gets single content type element from given url
* @param url Url used to get single content type element
* @param queryConfig Query configuration
*/
QueryService.prototype.getElement = function (url, queryConfig) {
var _this = this;
return this.getResponse(url, queryConfig)
.pipe(operators_1.map(function (response) {
return _this.responseMapper.mapElementResponse(response);
}), operators_1.catchError(function (err) {
return rxjs_1.throwError(_this.handleError(err));
}));
};
/**
* Gets proper set of headers for given request.
* @param queryConfig Query configuration
* @param customHeaders Custom headers
*/
QueryService.prototype.getHeaders = function (queryConfig) {
var headers = [];
if (queryConfig.customHeaders) {
headers = headers.concat(queryConfig.customHeaders);
}
// add SDK Id header for monitoring SDK usage
headers.push(this.getSdkIdHeader());
if (this.isPreviewModeEnabled(queryConfig) && this.isSecuredModeEnabled(queryConfig)) {
throw Error("Preview & secured modes cannot be used at the same time.");
}
// add preview header is required
if (this.isPreviewModeEnabled(queryConfig)) {
headers.push(this.getAuthorizationHeader(this.config.previewApiKey));
}
// add secured mode header is required
if (this.isSecuredModeEnabled(queryConfig)) {
headers.push(this.getAuthorizationHeader(this.config.securedApiKey));
}
// add 'X-KC-Wait-For-Loading-New-Content' header if required
if (queryConfig.waitForLoadingNewContent) {
headers.push({
header: this.waitForLoadingNewContentHeader,
value: 'true'
});
}
return headers;
};
/**
* Http get response
* @param url Url of request
* @param queryConfig Query configuration
*/
QueryService.prototype.getResponse = function (url, queryConfig) {
var _this = this;
return this.httpService.get(url, this.getHeaders(queryConfig))
.pipe(operators_1.map(function (response) {
return response;
}), operators_1.retryWhen(delivery_retry_strategy_1.deliveryRetryStrategy.strategy({
maxRetryAttempts: this.getRetryAttempts(),
excludedStatusCodes: this.retryExcludedStatuses
})), operators_1.catchError(function (err) {
return rxjs_1.throwError(_this.handleError(err));
}));
};
/**
* Gets number of retry attempts used by queries
*/
QueryService.prototype.getRetryAttempts = function () {
// get the attempts
var attempts;
if (this.config.retryAttempts || this.config.retryAttempts === 0) {
// use custom defined number of attempts
attempts = this.config.retryAttempts;
}
else {
// use default attempts
attempts = this.defaultRetryAttempts;
}
return attempts;
};
/**
* Handles given error
* @param error Error to be handled
*/
QueryService.prototype.handleError = function (error) {
if (this.config.enableAdvancedLogging) {
console.error(error);
}
if (error.cloudError) {
return error.cloudError;
}
return error;
};
/**
* Indicates if current query should use preview mode
* @param queryConfig Query configuration
*/
QueryService.prototype.isPreviewModeEnabled = function (queryConfig) {
if (queryConfig.usePreviewMode != null) {
return queryConfig.usePreviewMode;
}
return this.config.enablePreviewMode === true;
};
/**
* Indicates if current query should use secured mode
* @param queryConfig Query configuration
*/
QueryService.prototype.isSecuredModeEnabled = function (queryConfig) {
if (queryConfig.useSecuredMode != null) {
return queryConfig.useSecuredMode;
}
return this.config.enableSecuredMode === true;
};
/**
* Gets preview or standard URL based on client and query configuration
* @param queryConfig Query configuration
*/
QueryService.prototype.getDeliveryUrl = function (queryConfig) {
if (this.isPreviewModeEnabled(queryConfig)) {
if (!this.config.previewApiKey) {
throw Error("You have to configure 'previewApiKey' to use 'preview' mode");
}
// use custom base / preview url if its configured
if (this.config.basePreviewUrl) {
return this.config.basePreviewUrl;
}
// use default preview url
return this.defaultPreviewDeliveryApiUrl;
}
// use custom base / preview url if its configured
if (this.config.baseUrl) {
return this.config.baseUrl;
}
return this.defaultBaseDeliveryApiUrl;
};
/**
* Gets base URL of the request including the project Id
* @param queryConfig Query configuration
*/
QueryService.prototype.getBaseUrl = function (queryConfig) {
return this.getDeliveryUrl(queryConfig) + '/' + this.config.projectId;
};
/**
* Adds query parameters to given url
* @param url Url to which options will be added
* @param options Query parameters to add
*/
QueryService.prototype.addOptionsToUrl = function (url, options) {
if (options) {
options.forEach(function (filter) {
if (url.indexOf('?') > -1) {
url = url + '&' + filter.getParam() + '=' + filter.getParamValue();
}
else {
url = url + '?' + filter.getParam() + '=' + filter.getParamValue();
}
});
}
return url;
};
/**
* Gets authorization header. This is used for 'preview' functionality
*/
QueryService.prototype.getAuthorizationHeader = function (key) {
if (!key) {
throw Error("Cannot get authorization header because key is undefined");
}
// authorization header required for preview mode
return {
header: 'authorization',
value: "bearer " + key
};
};
/**
* Header identifying SDK type & version for internal purposes of Kentico
*/
QueryService.prototype.getSdkIdHeader = function () {
return {
header: this.sdkVersionHeader,
value: this.sdkInfo.host + ";" + this.sdkInfo.name + ";" + this.sdkInfo.version
};
};
return QueryService;
}());
exports.QueryService = QueryService;
//# sourceMappingURL=query.service.js.map