@kunleticket/common
TypeScript icon, indicating that this package has built-in type declarations

1.0.66 • Public • Published

Introduction

@kunleticket/common is a powerful package designed to simplify the development process by providing essential utilities, error handling mechanisms, and middleware functions such as authorization and authentication in node.js and express applications. It is particularly useful in microservice architectures, enabling seamless communication between services through message queuing. @kunleticket/common, supports both typescript and javascript.

Installation

To install the package, use npm:

npm install @kunleticket/common

Middlewares

1. Error Handler

The error handler middleware provided by @kunleticket/common is a crucial component for managing errors in your application. To use the error handler, follow these steps:

  1. Import the error handler into your project:

    import { errorHandler } from "@kunleticket/common";
  2. Set up your Express application and add other middlewares and routers:

    const express = require("express");
    const app = express();
    
    // ...other middlewares and routers
  3. Add the error handler at the end of your middlewares, just before starting the server:

    app.use(errorHandler());

2. NotFound Error

This throws a 404 error, with any custom message of your choice

import {NotFoundError} from @kunleticket/common

Example

  import mongoose from "mongoose" //assuming you are using mongoose as a DB
  import express, {Request, Response} from "express"//assume u use express

  const router= express.Router()
  //query for a document for example
  router.get("/find-user-by-id/:id", (req:Request, res:Response)=>{
    
    const user= await User.findById(req.params.id)

    if(!user){
      throw NotFoundErrror("your custom message e.g, user is not found..")
    }
  })

3. NotAuthorized Error

This helps you throw a 401 statuscode, with a message Unauthorized acccess

import {NotAuthorized} from @kunleticket/common

Example

    if(){//Add your condition
      throw NotAuthorizedError()
    }
4. currentUser MiddleWare

This middle ware helps you handle jsonwebtoken verification and setting session i.e(request.currentUser) to be the payload stored in a jwt in cookies, the cookie should be called "secretoken", so u cabn store your jwt inside the coookie, when a user logs into your application or just got some authorization of some sort

  import {currentUser} from "@kunletiket/common"
  import express from "express"

  const router= express.Router()  
  //Add this middle ware just before your routes or protected route,
  router.use(currentUser);

  router.post("/route1")
5. requireAuth MiddleWare

This middle ware helps you handle protected route very easily, this middleware works hand in hand with the currentUSer middle ware, so for the middle ware to function properly you need to use the currentUser middleware just before it.

  import {requireAuth, currentUser} from "@kunletiket/common"
  import express from "express"

  const router= express.Router()  
  //Add this middle ware just before all your protected route
  router.use(currentUser)
  router.use(requreAuth);

  router.post("/protected-route1")
  router.post("/protected-route2")
  router.post("/protected-route3")

By following these steps, you ensure that errors in your application are properly handled, providing a seamless experience for both developers and users.

Additional Features

In addition to error handling, @kunleticket/common provides various other functionalities to enhance your development process, making it an indispensable tool for building robust applications in a microservices architecture.

For any issues or inquiries, feel free to reach out to me at +234 7039365725 Happy coding! 🚀

Readme

Keywords

none

Package Sidebar

Install

npm i @kunleticket/common

Weekly Downloads

37

Version

1.0.66

License

ISC

Unpacked Size

32.9 kB

Total Files

66

Last publish

Collaborators

  • kunle001