1.0.4 • Published 7 months ago

@muzman/node-toolkit v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

@muzman/node-toolkit

@muzman/node-toolkit is a custom npm package written in TypeScript, designed to easily integrate authentication middleware and custom error/result handlers into your Express applications.

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.

Prerequisites

Make sure you have the latest version of Node.js installed. This package assumes you have Express installed in your project.

Installation

Easily add this npm package to your project with:

npm i @muzman/node-toolkit

or

yarn add @muzman/node-toolkit

New Features in @muzman/node-toolkit

NATS Streaming Integration

@muzman/node-toolkit now includes support for NATS Streaming Server, providing a robust solution for event-based communication between microservices. Below are the newly added features regarding NATS:

Event Publishing

import { Publisher } from "@muzman/node-toolkit";
import { Subjects } from "@muzman/node-toolkit";

// Specific event class
class MyEventPublisher extends Publisher<MyEvent> {
  subject: Subjects.MyEvent = Subjects.MyEvent;
}

// Usage
const publisher = new MyEventPublisher(natsClientInstance);

// Publish an event
publisher.publish({
  //...event data
});

Event Listening

import { Listener } from "@muzman/node-toolkit";
import { Message } from "node-nats-streaming";
import { Subjects } from "@muzman/node-toolkit";

// Specific event class
class MyEventListener extends Listener<MyEvent> {
  subject: Subjects.MyEvent = Subjects.MyEvent;
  queueGroupName = "my-service-queue-group";

  onMessage(data: MyEvent["data"], msg: Message) {
    console.log(`Event data: `, data);

    // ...handle the event

    // Acknowledge that we received the message
    msg.ack();
  }
}

// Usage
const listener = new MyEventListener(natsClientInstance);
listener.listen();

Advanced Types Usage

To enhance the development experience, @muzman/node-toolkit introduces a sophisticated system for handling types, making it easier to manage and validate data structures in your applications.

Usage of Types With the new feature, you can import various predefined types from the framework, enhancing consistency, and reliability in your codebase.

import { UserAttrs } from "@muzman/node-toolkit";

// Use the types in your application logic
const userData: UserAttrs = {
  // ... your user data fields
};

Comprehensive Usage Guide

After installing the @muzman/node-toolkit, you can utilize its features within your application. Here's how you can use each part of the framework:

1-Auth Middleware (requireAuth): This middleware function is used to verify the authentication of users. If the token is valid, it allows the request to proceed; otherwise, it responds with an error.

import { requireAuth } from "@muzman/node-toolkit";

// Protect your routes with requireAuth
app.use("/protected-route", requireAuth, (req, res) => {
  // Your code here. This will run after successful authentication.
});

2-Error Handling (errorHandler): This function handles errors within your application. You can customize the status code and the message returned to the client.

import { errorHandler } from "@muzman/node-toolkit";

// Usage in your Express error-handling middleware.
app.use((err, req, res, next) => {
  // Customize the status code and message as per your requirements.
  errorHandler(req, res, "Custom error message", 500);
});

3-Result Handling (resultHandler): This function allows you to standardize the response you send from your API routes, ensuring you maintain consistency throughout your application.

import { resultHandler } from "@muzman/node-toolkit";

app.get("/some-route", (req, res) => {
  // You can customize the payload and status code for the response.
  resultHandler(req, res, { data: "This is the response data." }, 200);
});

These are the core functionalities provided by @muzman/node-toolkit. By integrating this into your application, you ensure streamlined error handling, response management, and secure routes with authentication.

Contributing

Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated. Please refer to the CONTRIBUTING.md for more information.

License

Distributed under the ISC License. See LICENSE.md for more information.