1.0.40 • Published 10 months ago

@v1nnyc/common v1.0.40

Weekly downloads
-
License
ISC
Repository
-
Last release
10 months ago

Custom Error Handling, Middleware, and Event Library

This repository contains a custom error handling, middleware, and event library for Node.js applications, designed to simplify error management, authentication handling, and event-driven architecture in your projects.

Table of Contents

Installation

To install the library, run:

npm install --save @v1nnyc/commons

Usage

Import the error classes, middlewares, and event classes you need from the library.

Error Classes

The library provides the following custom error classes:

  • BadRequestError: For handling bad request errors.
  • CustomError: A base class for creating custom errors.
  • DatabaseConnectionError: For handling database connection errors.
  • NotAuthorizedError: For handling authorization errors.
  • NotFoundError: For handling not found errors.
  • RequestValidationError: For handling request validation errors.

Example usage:

const { BadRequestError } = require("@v1nnyc/commons");

// Throw a bad request error
throw new BadRequestError("Invalid input");

Middlewares

The library provides the following middlewares:

  • currentUser: To get the current user from the JWT token.
  • errorHandler: To handle errors in a standardized manner.
  • requireAuth: To ensure the user is authenticated before accessing a route.
  • validateRequest: To validate incoming requests using provided validation schemas.

Example usage:

const {
  currentUser,
  errorHandler,
  requireAuth,
  validateRequest,
} = require("@v1nnyc/commons");
const express = require("express");
const app = express();

app.use(currentUser());
app.use(errorHandler);
app.get("/protected", requireAuth, (req, res) => {
  res.send("You are authenticated!");
});
app.post("/validate", validateRequest(validationSchema), (req, res) => {
  res.send("Request is valid!");
});

Publisher and Listener

The library includes abstract Publisher and Listener classes for implementing event-driven architecture using Node.js and NATS Streaming.

  • Publisher: An abstract class for publishing events.
  • Listener: An abstract class for listening to events and processing them.

Example usage:

const { Publisher, Listener } = require("@v1nnyc/commons");
const { Stan } = require("node-nats-streaming");

// Create a NATS Streaming client
const stan = Stan.connect("cluster-id", "client-id", {
  url: "http://localhost:4222",
});

// Extend the Publisher class to create a custom publisher
class MyEventPublisher extends Publisher {
  subject = "my-event";
}

// Extend the Listener class to create a custom listener
class MyEventListener extends Listener {
  subject = "my-event";
  queueGroupName = "my-queue-group";

  onMessage(data, msg) {
    console.log("Message received:", data);
    msg.ack();
  }
}

// Create a publisher and listener instance
const publisher = new MyEventPublisher(stan);
const listener = new MyEventListener(stan);

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

// Start listening for events
listener.listen();

Contributing

Contributions are welcome. Please submit a pull request or open an issue to discuss your ideas or report bugs.

License

This library is released under the ISC License.

1.0.39

10 months ago

1.0.40

10 months ago

1.0.35

1 year ago

1.0.34

1 year ago

1.0.33

1 year ago

1.0.32

1 year ago

1.0.31

1 year ago

1.0.30

1 year ago

1.0.29

1 year ago

1.0.28

1 year ago

1.0.27

1 year ago

1.0.26

1 year ago

1.0.25

1 year ago

1.0.24

1 year ago

1.0.23

1 year ago

1.0.22

1 year ago

1.0.21

1 year ago

1.0.20

1 year ago

1.0.19

1 year ago

1.0.18

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.15

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago