Overview
Bundle of TypeScript helper functions created for internal use by the Bothive team.
Helpers:
arrayHelpers
dateHelpers
errorHelpers
generatorHelpers
stringHelpers
timeoutHelpers
loggingHelpers
elasticAliasHelper
Getting Started
Install:
npm i -E @bothive/helpers
Example:
import { stringHelpers } from "@bothive/helpers";
stringHelpers.firstToUppercase("hello");
Making Changes
When making changes follow these steps.
- Clone/fork repository
npm ci
- Checkout
development
branch - Make changes
- Test changes (see
Package Testing
below) - Open pull request to
master
branch - Wait for PR approval!
- Publish package with np
npm run release
Package Testing
We should always test the package locally without having to publish it to the registry first and produce unnecessary version bumps:
- Build the package locally with
npm run build
. - Install it inside the target project with
npm i /path/to/package
Unit Testing
The codebase can be unit tested with following commands.
Run all tests.
npm run test
Check the coverage of all the tests.
npm run coverage
Run tests continuously until one fails.
npm run test:run
Helpers explanation
ElasticAliasHelper
ElasticAliasHelper
Description
This helper exports a singleton helper class to interact with the Elastic REST API (https://www.elastic.co/guide/en/cloud/current/ec-restful-api.html). It's made specifically to use with aliases, not indices*. This way you can use the most important requests on alias level with this package. Just initiate an instance with the API Key and API configuration to interact with Elasticsearch.
*
It's possible to interact with indices using the post and postNdJson functions.
But all other functions are made to interact with aliases.
Example:
import { ElasticAliasHelper } from "@bothive/helpers";
const apiKey = "apiKey";
const apiConfig = {
count: `<elasticsearch_host>/search-inbox-:env/_count`,
search: `<elasticsearch_host>/search-inbox-:env/_search`,
create: `<elasticsearch_host>/write-inbox-:env/_doc/:id`,
update: (index: string) => `<elasticsearch_host>/${index}/_update/:id`,
updateQuery: `<elasticsearch_host>/search-inbox-:env/_update_by_query`,
deleteQuery: `<elasticsearch_host>/search-inbox-:env/_delete_by_query`,
createBulk: `<elasticsearch_host>/_bulk`,
}
const helper = ElasticAliasHelper.getInstance({
apiKey,
apiConfig
});
export default helper;
Dependencies
"axios": "0.27.2"
Functions
getInstance
{ apiKey: string; apiConfig: any }
post
{ url: string; apiKey: string; payload: any }
postNdJson
{ url: string; apiKey: string; payload: string }
searchDocumentById
{ id: string }
countByQuery
{ query: any }
searchByQuery
{ query: any; size?: number; from?: number; sort?: any; aggs?: any }
updateDocumentWithUpsert
{ id: string; payload: any; index?: string; routing?: string }
updateByQuery
{ query: any; script: any; refresh?: boolean }
deleteByQuery
{ query: any }
insertBulk
{ bulkPayload: string }
create
{ id: string; payload: any; routing?: string }