@basic-tools/node-platform-tools-lib
v1.0.6
Published
Tools to use in API or lambdas or any node component.
Readme
node-platform-tools-lib
This package provides various utilities for your APIs or scripts, regardless of the framework you are using:
- AxiosLogger
- DeployEnvironments
- HandlerError
- JsonLoaderFromFile
- PackageJsonReader
Configuration
- Add in your dependencies in your package.json
"dependencies": {
"@basic-tools/node-platform-tools-lib": "^1.0.0",
},- You need declare EXECUTION_MODE environment in your envs: These are the availables values:
development | test | staging | production
# .env file
EXECUTION_MODE=productionIf EXECUTION_MODE env don't exist it will be taken from NODE_ENV
Usages
AxiosLogger
- Init AxiosLogger in your code:
For Example in your
main.tsorindex.ts
import { AxiosLogger } from '@basic-tools/node-platform-tools-lib';
AxiosLogger.config();ExecutionsModes
- Use it anywhere in your code.
import { ExecutionsModes } from '@basic-tools/node-platform-tools-lib';
ExecutionsModes.isDevelopment()HandlerError
- Create a middleware where you can asociate Handler Error Function:
const handler = new HandlerError();
handler.handle(request, response, exception);- Use it anywhere in your code. Example this will return a HTTP 400 response with next message in body response
throw new BusinessValidationError({msgToShow: 'Tu reserva no fue aprobada. Revisa con tu banco'});For example for NestJS:
import { ArgumentsHost, Catch, ExceptionFilter } from '@nestjs/common';
import { Request, Response } from 'express';
import { HttpAdapterHost } from '@nestjs/core';
import { HandlerError } from '@basic-tools/node-platform-tools-lib';
@Catch()
export class UnknownExceptionFilter implements ExceptionFilter {
constructor(private readonly httpAdapterHost: HttpAdapterHost) {
}
catch(exception: any, host: ArgumentsHost) {
try {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
const request = ctx.getRequest<Request>();
const handler = new HandlerError();
handler.handle(request, response, exception);
} catch (e) {
console.error('Something extrange happened try to print errors');
}
}
}JsonLoaderFromFile
- Use in any place of your code
import { JsonLoaderFromFile } from '@basic-tools/node-platform-tools-lib';
const jsonLoaded: any = JsonLoaderFromFile.resolve(pathToFile);PackageJsonReader
- Use in any place of your code
import { PackageJsonReader } from '@basic-tools/node-platform-tools-lib';
const currentAppVersion = PackageJsonReader.getVersion(`project.json`);
const allInfo = PackageJsonReader.all();