kentico-cloud-delivery
Version:
Official Kentico Cloud Delivery SDK
269 lines • 10.3 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var kentico_cloud_core_1 = require("kentico-cloud-core");
var rxjs_1 = require("rxjs");
var operators_1 = require("rxjs/operators");
var BaseDeliveryQueryService = /** @class */ (function () {
function BaseDeliveryQueryService(
/**
* Delivery client configuration
*/
config,
/**
* Http service for fetching data
*/
httpService,
/**
* Information about the SDK
*/
sdkInfo,
/**
* Mapping service
*/
mappingService) {
this.config = config;
this.httpService = httpService;
this.sdkInfo = sdkInfo;
this.mappingService = mappingService;
/**
* Default number of retry attempts when user did not set any
*/
this.defaultRetryAttempts = 3;
/**
* List of response codes that can be retried
*/
this.defaultRetryStatusCodes = [500];
/**
* 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';
}
BaseDeliveryQueryService.prototype.retryPromise = function (promise) {
return this.httpService.retryPromise(promise, {
maxRetryAttempts: this.getRetryAttempts(),
useRetryForResponseCodes: this.getRetryStatusCodes()
}, 1);
};
/**
* 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
*/
BaseDeliveryQueryService.prototype.getUrl = function (action, queryConfig, options) {
if (!this.config.proxy || !this.config.proxy.advancedProxyUrlResolver) {
return kentico_cloud_core_1.urlHelper.addOptionsToUrl(this.getBaseUrl(queryConfig) + action, options);
}
return this.config.proxy.advancedProxyUrlResolver({
queryParameters: options ? options : [],
queryString: kentico_cloud_core_1.urlHelper.addOptionsToUrl('', options),
action: action,
domain: this.getDomain(queryConfig),
queryConfig: queryConfig,
projectId: this.config.projectId
});
};
/**
* Gets proper set of headers for given request.
* @param queryConfig Query configuration
* @param customHeaders Custom headers
*/
BaseDeliveryQueryService.prototype.getHeaders = function (queryConfig) {
var headers = [];
// add SDK Id header for monitoring SDK usage
headers.push(this.getSdkIdHeader());
// add headers from global config
if (this.config.globalHeaders) {
headers.push.apply(headers, this.config.globalHeaders(queryConfig));
}
// add query / global headers from query config
headers.push.apply(headers, this.getQueryHeaders(queryConfig));
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) && this.config.previewApiKey) {
headers.push(this.getAuthorizationHeader(this.config.previewApiKey));
}
// add secured mode header is required
if (this.isSecuredModeEnabled(queryConfig) && this.config.secureApiKey) {
headers.push(this.getAuthorizationHeader(this.config.secureApiKey));
}
// add 'X-KC-Wait-For-Loading-New-Content' header if required
if (this.shouldAddWaitForLoadingNewContentHeader(queryConfig)) {
headers.push({
header: this.waitForLoadingNewContentHeader,
value: 'true'
});
}
return headers;
};
/**
* Http GET response
* @param url Url of request
* @param queryConfig Query config configuration
*/
BaseDeliveryQueryService.prototype.getResponse = function (url, queryConfig) {
if (!queryConfig) {
queryConfig = {};
}
return this.httpService
.get({
url: url,
mapError: function (error) { return kentico_cloud_core_1.mapCloudError(error); }
}, {
headers: this.getHeaders(queryConfig),
maxRetryAttempts: this.getRetryAttempts(),
useRetryForResponseCodes: this.getRetryStatusCodes(),
logErrorToConsole: this.config.isDeveloperMode
})
.pipe(operators_1.catchError(function (error) {
return rxjs_1.throwError(error.mappedError);
}));
};
/**
* Gets base URL of the request including the project Id
* @param queryConfig Query configuration
*/
BaseDeliveryQueryService.prototype.getBaseUrl = function (queryConfig) {
return this.getDomain(queryConfig) + '/' + this.config.projectId;
};
/**
* Gets retry status code array
*/
BaseDeliveryQueryService.prototype.getRetryStatusCodes = function () {
if (this.config.retryStatusCodes) {
return this.config.retryStatusCodes;
}
return this.defaultRetryStatusCodes;
};
/**
* Gets number of retry attempts used by queries
*/
BaseDeliveryQueryService.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;
};
/**
* Indicates if current query should use preview mode
* @param queryConfig Query configuration
*/
BaseDeliveryQueryService.prototype.isPreviewModeEnabled = function (queryConfig) {
if (queryConfig.usePreviewMode !== undefined) {
return queryConfig.usePreviewMode;
}
if (!this.config.globalQueryConfig) {
return false;
}
if (this.config.globalQueryConfig.usePreviewMode === true) {
return true;
}
return false;
};
BaseDeliveryQueryService.prototype.getQueryHeaders = function (queryConfig) {
if (queryConfig.customHeaders) {
return queryConfig.customHeaders;
}
if (!this.config.globalQueryConfig || !this.config.globalQueryConfig.customHeaders) {
return [];
}
return this.config.globalQueryConfig.customHeaders;
};
BaseDeliveryQueryService.prototype.shouldAddWaitForLoadingNewContentHeader = function (queryConfig) {
if (queryConfig.waitForLoadingNewContent !== undefined) {
return queryConfig.waitForLoadingNewContent;
}
if (!this.config.globalQueryConfig) {
return false;
}
if (this.config.globalQueryConfig.waitForLoadingNewContent === true) {
return true;
}
return false;
};
/**
* Indicates if current query should use secured mode
* @param queryConfig Query configuration
*/
BaseDeliveryQueryService.prototype.isSecuredModeEnabled = function (queryConfig) {
if (queryConfig.useSecuredMode !== undefined) {
return queryConfig.useSecuredMode;
}
if (!this.config.globalQueryConfig) {
return false;
}
if (this.config.globalQueryConfig.useSecuredMode === true) {
return true;
}
return false;
};
/**
* Gets preview or standard URL based on client and query configuration
* @param queryConfig Query configuration
*/
BaseDeliveryQueryService.prototype.getDomain = function (queryConfig) {
if (this.isPreviewModeEnabled(queryConfig)) {
if (!this.config.previewApiKey) {
throw Error("Preview API key is not configured.");
}
// check custom preview url
if (this.config.proxy && this.config.proxy.basePreviewUrl) {
return this.config.proxy.basePreviewUrl;
}
// use default preview url
return this.defaultPreviewDeliveryApiUrl;
}
// check custom base url
if (this.config.proxy && this.config.proxy.baseUrl) {
return this.config.proxy.baseUrl;
}
return this.defaultBaseDeliveryApiUrl;
};
/**
* Gets authorization header. This is used for 'preview' functionality
*/
BaseDeliveryQueryService.prototype.getAuthorizationHeader = function (key) {
if (!key) {
throw Error("Cannot get authorization header because key is invalid");
}
// authorization header required for preview mode
return {
header: 'authorization',
value: "bearer " + key
};
};
/**
* Header identifying SDK type & version for internal purposes of Kentico
*/
BaseDeliveryQueryService.prototype.getSdkIdHeader = function () {
return {
header: this.sdkVersionHeader,
value: this.sdkInfo.host + ";" + this.sdkInfo.name + ";" + this.sdkInfo.version
};
};
return BaseDeliveryQueryService;
}());
exports.BaseDeliveryQueryService = BaseDeliveryQueryService;
//# sourceMappingURL=base-delivery-query.service.js.map