Library for important resources when there is a chance that these resources will be used in many projects
$ npm install @kernelsoftware/shared
Before starting, make sure you've met the following requirements:
- You have installed both
node@18.x
andnpm
;
This library provides a robust set of functionalities designed to assist in application development:
-
Logging: A logging system using the
Logger
module. -
Base Entity: The
BaseEntity
offers an initial structure for your entities. -
Base Model: The
BaseModel
offers an initial structure for your models. -
Response Management: Effortlessly manage your application's responses with a variety of tools, including:
ApplicationResult
: A generic application response manager.ApplicationResultSuccess
: Specifically designed for handling successful responses.ApplicationResultError
: For handling generic error responses.ApplicationResultNotFound
: To address 'Not Found' scenarios.ApplicationResultUnauthorized
: For handling unauthorized accesses.ApplicationResultForbidden
: To manage 'Access Forbidden' scenarios.
-
Currency Conversion:
centavosToReal
: Convert centavo values (integer representation of currency) to its real (BRL) formatted string counterpart, e.g., converting 100 centavos to R$1,00.realToCentavos
: Convert a real (BRL) formatted string to its centavo integer representation, e.g., converting R$1,00 to 100 centavos.
-
Environment Variable Management:
getEnvOrThrow
: Retrieve the value of a specified environment variable or throw an error if it's not set.getEnvOrDefault
: Retrieve the value of a specified environment variable or return a default value if it's not set.
-
HTTP Error Handling:
ErrorHttpHandler
: Provides an implementation to handle validation errors in an HTTP context, logging the errors and returning a formatted response.
-
Base Controller:
BaseController
: Abstract controller offering ahandleResult
method to convert application results into HTTP responses. Useful for keeping the responses consistent.
-
IUsecase:
IUsecase
: Provides a contract to usecases methods.
-
Data Transfer Object (DTO) Utilities:
BaseDTO
: Offers static validation utility for validating DTOs based on class-validator rules and converting plain objects to DTO instances.
-
Object Key Conversion Utilities:
toCamelCase(obj: object)
: Converts the keys of an object tocamelCase
format. Useful for standardizing object keys to camelCase before saving to databases or sending to APIs.toSnakeCase(obj: object)
: Converts the keys of an object tosnake_case
format. Useful for APIs that require snake_case keys in payloads.
-
DynamoHelper: This utility class helps with common DynamoDB operations.
upsert(Model: ModelType<AnyItem>, data: Record<string, unknown>)
: Performs an upsert operation. Inserts the record if it does not exist or updates it if it does. TheModel
parameter specifies the DynamoDB model, and thedata
parameter specifies the data to be inserted or updated.findOne(model: ModelType<AnyItem>, key: string, value: string)
: Finds a single record that matches the specified key and value. Returnsnull
if no match is found.