1.0.66 • Published 5 months ago

@kunleticket/common v1.0.66

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

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! 🚀

1.0.62

6 months ago

1.0.61

6 months ago

1.0.66

5 months ago

1.0.65

6 months ago

1.0.64

6 months ago

1.0.63

6 months ago

1.0.57

9 months ago

1.0.56

9 months ago

1.0.55

11 months ago

1.0.26

1 year ago

1.0.29

1 year ago

1.0.28

1 year ago

1.0.27

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.37

1 year ago

1.0.36

1 year ago

1.0.35

1 year ago

1.0.34

1 year ago

1.0.39

1 year ago

1.0.38

1 year ago

1.0.40

1 year ago

1.0.44

1 year ago

1.0.43

1 year ago

1.0.42

1 year ago

1.0.41

1 year ago

1.0.48

12 months ago

1.0.47

12 months ago

1.0.46

1 year ago

1.0.45

1 year ago

1.0.49

12 months ago

1.0.51

12 months ago

1.0.54

11 months ago

1.0.53

12 months ago

1.0.52

12 months 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.14

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