npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@kunleticket/common

v1.0.66

Published

`@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

Downloads

53

Readme

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