@bothive/helpers
Version:
Collection of helper functions mainly used inside bothive-core project
144 lines • 5.68 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const axios_1 = (0, tslib_1.__importDefault)(require("axios"));
function formatAliasName(index) {
var _a;
const environment = ((_a = process.env.NODE_ENV) === null || _a === void 0 ? void 0 : _a.toLowerCase()) || "development";
return index.replace(":env", environment);
}
function generateHeaders({ apiKey, contentType = "application/json", }) {
return {
headers: {
Authorization: `ApiKey ${apiKey}`,
"Content-Type": contentType,
},
};
}
class ElasticAliasHelper {
constructor({ apiKey, apiConfig }) {
this.post = ({ url, apiKey, payload }) => {
const headers = generateHeaders({ apiKey });
return axios_1.default.post(url, payload, headers);
};
this.postNdJson = ({ url, apiKey, payload }) => {
const headers = generateHeaders({ apiKey, contentType: "application/x-ndjson" });
return axios_1.default.post(url, payload, headers);
};
this.searchDocumentById = ({ id }) => (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
const result = yield this.post({
url: formatAliasName(this.apiConfig.search),
apiKey: this.apiKey,
payload: {
query: {
term: {
_id: id,
},
},
},
});
return result.data;
});
this.countByQuery = ({ query }) => (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
const result = yield this.post({
url: formatAliasName(this.apiConfig.count),
apiKey: this.apiKey,
payload: {
query,
},
});
return result.data;
});
this.searchByQuery = ({ query, size, from, sort, aggs, }) => (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
const payload = {
query,
size,
from,
sort,
aggs,
};
Object.keys(payload).forEach((key) => typeof payload[key] === "undefined" && delete payload[key]);
const result = yield this.post({
url: formatAliasName(this.apiConfig.search),
apiKey: this.apiKey,
payload,
});
return result.data;
});
this.updateDocumentWithUpsert = ({ id, payload, index, routing, }) => (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
var _a;
if (typeof index === "undefined") {
const searchDocument = yield this.searchDocumentById({ id });
if (typeof searchDocument === "undefined" || searchDocument.hits.total.value === 0) {
return this.create({ id, payload, routing });
}
index = (_a = searchDocument.hits.hits[0]) === null || _a === void 0 ? void 0 : _a._index;
}
let url = formatAliasName(this.apiConfig.update(index).replace(":id", id));
if (typeof routing !== "undefined") {
url += `?routing=${routing}`;
}
return this.post({
url,
apiKey: this.apiKey,
payload: {
doc: payload,
doc_as_upsert: true,
},
});
});
this.updateByQuery = ({ query, script, refresh = false, }) => (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
let url = formatAliasName(this.apiConfig.updateQuery);
if (refresh) {
url += "?refresh";
}
return this.post({
url,
apiKey: this.apiKey,
payload: {
query,
script,
},
});
});
this.deleteByQuery = ({ query }) => (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
const result = yield this.post({
url: formatAliasName(this.apiConfig.deleteQuery),
apiKey: this.apiKey,
payload: {
query,
},
});
return result;
});
this.insertBulk = ({ bulkPayload }) => (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
const result = yield this.postNdJson({
url: formatAliasName(this.apiConfig.createBulk),
apiKey: this.apiKey,
payload: bulkPayload,
});
return result;
});
this.create = ({ id, payload, routing = "", }) => (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
let url = formatAliasName(this.apiConfig.create.replace(":id", id));
if (routing !== "") {
url += `?routing=${routing}`;
}
return this.post({
url,
apiKey: this.apiKey,
payload,
});
});
this.apiKey = apiKey;
this.apiConfig = apiConfig;
}
}
ElasticAliasHelper.getInstance = ({ apiKey, apiConfig }) => {
if (!ElasticAliasHelper.instance) {
ElasticAliasHelper.instance = new ElasticAliasHelper({ apiKey, apiConfig });
}
return ElasticAliasHelper.instance;
};
exports.default = ElasticAliasHelper;
//# sourceMappingURL=elastic_alias.helpers.js.map