1.0.40 • Published 2 years ago

@v1nnyc/common v1.0.40

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years 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

2 years ago

1.0.40

2 years ago

1.0.35

2 years ago

1.0.34

2 years ago

1.0.33

2 years ago

1.0.32

2 years ago

1.0.31

2 years ago

1.0.30

2 years ago

1.0.29

2 years ago

1.0.28

2 years ago

1.0.27

2 years ago

1.0.26

2 years ago

1.0.25

2 years ago

1.0.24

2 years ago

1.0.23

2 years ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.15

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago