@lokalise/node-core v13.1.0
node-core 🧬
Core libraries for Node.js backend services.
See docs for further instructions on how to use.
Default Logging Configuration
The library provides methods to resolve the default logging configuration. Public methods available are:
resolveLoggerConfiguration(), which accepts as parameter anappConfig, defined by thelogLeveland thenodeEnv. If the environment is production, the output will be logged in JSON format to be friendly with any data storage. Otherwise, the output will be logged with coloring and formatting to be visible for debugging purposes and help developers.The method returns a logger configuration that should be used with
pinolibrary as in the following example:const loggerConfig = resolveLoggerConfiguration({ logLevel: 'warn', nodeEnv: 'production', redact: { paths: ['path1', 'path2'], }, }) const logger = pino(loggerConfig)resolveMonorepoLoggerConfiguration(), which accepts as parameter anappConfig, defined by thelogLeveland thenodeEnv. It mostly behaves the same asresolveLoggerConfiguration, with the exception of execution indevelopment environments. Since monorepo services are usually ran concurrently, logs fromstdoutaren't easily accessible. For this reason this logging configuration writes development logs into files.The method returns a logger configuration that should be used with
pinolibrary as in the following example:const loggerConfig = resolveMonorepoLoggerConfiguration({ logLevel: 'warn', nodeEnv: 'production', append: false, // targetFile: './logs/service.log' -- optional parameter, you can specify exact path for writing logs }) const logger = pino(loggerConfig)
ConfigScope
ConfigScope is a class that provides a way to encapsulate a single config source (e. g. process.env) and produce a set of values out of it, defining constraints and transformations for them.
Once the class is instantiated, you can leverage the following ConfigScope methods:
Mandatory Configuration Parameters
getMandatory(), returns the value of a mandatory configuration parameter. If the value is missing, anInternalErroris thrown. Parameters are:param, the configuration parameter name;
getMandatoryInteger(), returns the value of a mandatory configuration parameter and validates that it is a number. If the value is missing or is not a number, anInternalErroris thrown. Parameters are:param, the configuration parameter name;
getMandatoryOneOf(), returns the value a mandatory configuration parameter and validates that it is one of the supported values. If the value is missing or is not supported, anInternalErroris thrown. The method also serves as a type guard, narrowing the type of the passed value down to one of the supported options. Parameters are:param, the configuration parameter name;supportedValues;
getMandatoryValidatedInteger(), similar togetMandatoryInteger(), but also takes avalidatorin input and will throw anInternalErrorif the number is not valid. See Validators and Transformers for more information. Parameters are:param, the configuration parameter name;validator;
getMandatoryTransformed(), callsgetMandatory()and returns the result of thetransformerfunction applied to the configuration parameter value. See Validators and Transformers for more information. Parameters are:param, the configuration parameter name;transformer.
Optional Configuration Parameters
getOptionalNullable(), returns the value of an optional configuration parameter. If the value is missing, it is set to the provided default value.Parameters are:param, the configuration parameter name;defaultValue, which can be nullable;
getOptional(), similar togetOptionalNullable(), butdefaultValuecannot be nullable. The return value is always a string;getOptionalIntegerNullable(), returns the value of an optional configuration parameter and validates that it is a number. If the value is missing, it is set to the provided default value. If it is not a number, anInternalErroris thrown. Parameters are:param, the configuration parameter name;defaultValue, which can be nullable;
getOptionalInteger, similar togetOptionalIntegerNullable(), butdefaultValuecannot be nullable. The return value is always a number;getOptionalValidated(), similar togetOptional(), but also takes avalidatorin input and will throw anInternalErrorif the value is not valid. See Validators and Transformers for more information. Parameters are:param, the configuration parameter name;validator;
getOptionalValidatedInteger(), similar togetOptionalValidated(), but expects and returnsnumberinstead. See Validators and Transformers for more information. Parameters are:param, the configuration parameter name;validator;
getOptionalTransformed(), similar togetOptional(), but also takes atransformerin input and the result of thetransformerfunction applied to the configuration parameter value. See Validators and Transformers for more information. Parameters are:param, the configuration parameter name;defaultValue,transformer;
getOptionalBoolean(), returns the value of an optional configuration parameter and validates that it is a boolean. It the value is missing, it is assigned thedefaultValue. If it is not a boolean, anInternalErroris thrown. Parameters are:param, the configuration parameter name;defaultValue.
getOptionalOneOf(), returns the value of an optional configuration parameter, if the value is missing, it falls back to the specified default value, and validates that it is one of the supported values. If the value is not supported, anInternalErroris thrown. Parameters are:param, the configuration parameter name;defaultValuesupportedValues;
Environment Configuration Parameter
isProduction(), returns true if the environment is production;isDevelopment(), returns true if the environment is not production;isTest(), returns true if the environment is test.
Validators and Transformers
Ad-hoc validators and transformers can be built leveraging the EnvValueValidator and the EnvValueTransformer types exposed by the library. Alternatively, the following validators and transformers are already provided out of the box:
Validators
createRangeValidator(), which acceptsgreaterOrEqualThanandlessOrEqualThanand validates that a numeric value ranges between those numbers.
Transformers
ensureClosingSlashTransformer(), which accepts avalueas parameter, that can be a string or nullable, and adds a closing slash if it is missing and the value is defined.
Error Handling
The library provides classes and methods for error handling.
Global Error Handler
Public methods to leverage a global error handler are provided to be used when the process is run outside of the context of the request (e. g. in a queue where no one would catch an error if thrown):
resolveGlobalErrorLogObject(), which acceptserrand optionallycorrelationIDas parameters and converts the plain error into a serializable object. If the error is not a built-inErrortype and doesn't have any message, a fixed string is returned instead;executeAndHandleGlobalErrors(), which accepts theoperationparameter and will return the result of executing such operation. If an error is thrown during the execution of the operation,resolveGlobalErrorLogObject()is called to log the error and the process is terminated;executeAsyncAndHandleGlobalErrors(), which acceptsoperationand optionallystopOnErroras parameters and will return the result of executing such operation asynchronously. If an error is thrown during the execution of the operation,resolveGlobalErrorLogObject()is called to log the error and the process is terminated only ifstopOnErroristrue.stopOnErrordefaults totrueif not provided.
Errors
The library exposes classes for the following errors:
InternalError, which issues a500status code and is not exposed in the global error handler. It expects the following parameters:message;errorCode;details– (optional);cause– (optional).
PublicNonRecoverableError, which issues the HTTP status code provided and signals that the user did something wrong, hence the error is returned to the consumer of the API. It expects the following parameters:message;errorCode;details– (optional);cause– (optional);httpStatusCode– (optional). Defaults to500;
Either
The library provides the type Either for error handling in the functional paradigm. The two possible values are:
resultis defined,erroris undefined;erroris defined,resultis undefined.
It's up to the caller of the function to handle the received error or throw an error.
Read this article for more information on how Either works and its benefits.
Additionally, DefiniteEither is also provided. It is a variation of the aforementioned Either, which may or may not have error set, but always has result.
waitAndRetry
There is helper function available for writing event-driven assertions in automated tests, which rely on something eventually happening:
import { waitAndRetry } from '@lokalise/node-core'
const result = await waitAndRetry(
() => {
return someEventEmitter.emittedEvents.length > 0
},
20, // sleepTime between attempts
30, // maxRetryCount before timeout
)
expect(result).toBe(false) // resolves to what the last attempt has returned
expect(someEventEmitter.emittedEvents.length).toBe(1)Encryption
EncryptionUtility- small class for encrypting/decrypting using aes-256-gcm. Adapted from: https://github.com/MauriceButler/cryptr
Hashing
HashUtils- utils for hashing using sha256/sha512 algorithms
Checksum
ChecksumUtils- utils for insecure hashing using the MD5 algorithm
Streams
StreamUtils- utils for temporary persisting of streams for length calculation and reuse
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago