nucleodeti.utils
v1.2.7
Published
Nucleo Express Utilities is a TypeScript module that provides helper classes and middlewares for common patterns when working with Express.js.
Downloads
8
Readme
Nucleo Express Utilities
Nucleo Express Utilities is a TypeScript module that provides helper classes and middlewares for common patterns when working with Express.js.
Installation
npm install nucleodeti.utils
Usage
NucleoHelpers
The NucleoHelpers
class provides helper methods for handling common response patterns.
sendResponse(response, content)
Sends a JSON response with the specified status, message, and optional content.
response
: The Express response object.content
: An object containing status, message, and optional content.
Example:
const nucleoHelpers = new NucleoHelpers();
nucleoHelpers.sendResponse(res, { status: 200, message: 'Success', content: { data: 'Some data' } });
sendErrorResponse(response, err)
Sends a generic internal server error response.
response
: The Express response object.err
: The error object.
Example:
const nucleoHelpers = new NucleoHelpers();
nucleoHelpers.sendErrorResponse(res, new Error('Something went wrong'));
NucleoMiddlewares
The NucleoMiddlewares
class contains common middleware functions.
dtoValidator(dtoClass)
Validates the request body against a specified Data Transfer Object (DTO).
dtoClass
: The class of the DTO to validate against.
Example:
const nucleoMiddlewares = new NucleoMiddlewares();
app.post('/endpoint', nucleoMiddlewares.dtoValidator(MyDTO), (req, res) => {
// Your route logic here
});
Contributing
Feel free to open issues and pull requests.
License
This project is licensed under the MIT License - see the LICENSE.md file for details.