@kontent-ai/management-sdk
Version:
Official Kontent.ai management SDK
84 lines • 2.38 kB
JavaScript
import { Parameters } from '@kontent-ai/core-sdk';
import { ContentManagementApiEndpoints } from '../models';
export class BaseQuery {
constructor(config, queryService) {
this.config = config;
this.queryService = queryService;
this.parameters = [];
this.apiEndpoints = new ContentManagementApiEndpoints({
environmentId: this.config.environmentId,
subscriptionId: this.config.subscriptionId
});
this._addSlashToUrl = true;
this.queryConfig = {
headers: config.headers ? [...config.headers] : [],
cancelTokenRequest: undefined
};
}
/**
* Gets url for this query
*/
getUrl() {
// use custom URL if user specified it
if (this._customUrl) {
return this._customUrl;
}
return this.getUrlForAction(this.getAction());
}
/**
* Adds header to request
* @param header Header to add
*/
withHeader(header) {
this.queryConfig.headers.push(header);
return this;
}
/**
* Adds headers to request
* @param headers Headers to add
*/
withHeaders(headers) {
this.queryConfig.headers.push(...headers);
return this;
}
/**
* Adds cancel token to request
*/
withCancelToken(tokenRequest) {
this.queryConfig.cancelTokenRequest = tokenRequest;
return this;
}
/**
* Gets array of currently set headers
*/
getHeaders() {
return this.queryConfig.headers;
}
/**
* Sets custom query parmeter that will be added to URL
* @param name Parameter name
* @param value Parameter value
*/
withCustomParameter(name, value) {
this.parameters.push(new Parameters.CustomParameter(name, value));
return this;
}
/**
* Overrides default url resolver and resolves this query with a custom one
* @param url Custom url to resolve query
*/
withUrl(url) {
this._customUrl = url;
return this;
}
/**
* Gets parameters assigned to this query
*/
getParameters() {
return this.parameters;
}
getUrlForAction(action) {
return encodeURI(this.queryService.getFullUrl(action, this.getParameters(), this._addSlashToUrl));
}
}
//# sourceMappingURL=base-query.js.map