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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@jasserabedrazzek/handlehelper

v1.0.2

Published

A lightweight TypeScript helper library for Express.js providing async error handling and standardized API responses.

Readme


HandleHelper

npm version npm downloads GitHub stars GitHub forks GitHub issues GitHub license Top Language Code Style PRs Welcome


HandleHelper is a lightweight TypeScript library for Express.js that simplifies async error handling and API responses. It provides easy-to-use wrappers for asynchronous route handlers and a standardized way to send JSON responses.


Features

  • Wrap async Express route handlers to catch errors automatically.
  • Standardize API responses with a consistent structure.
  • Fully written in TypeScript.
  • Compatible with Express 5.x.

Installation

npm install @jasserabedrazzek/handlehelper express
# or
yarn add @jasserabedrazzek/handlehelper express

Note: express is a peer dependency.


Quick Start

import express from "express";
import { asyncHandler, apiResponse } from "@jasserabedrazzek/handlehelper";

const app = express();

// Async route example
app.get('/hello', asyncHandler(async (req, res) => {
    const message = await Promise.resolve("Hello World!");
    apiResponse(res, { message, data: { message } });
}));

// Global error handler
app.use((err, req, res, next) => {
    console.error(err);
    apiResponse(res, {
        statusCode: 500,
        success: false,
        message: "Internal Server Error",
        data: null
    });
});

app.listen(3000, () => console.log("Server running on port 3000"));

Usage Details

1. Async Error Handling

Instead of manually wrapping each route with try/catch, use asyncHandler:

app.get("/users", asyncHandler(async (req, res) => {
    const users = await getUsersFromDB();
    apiResponse(res, {
        statusCode: 200,
        success: true,
        message: "Users fetched successfully",
        data: users
    });
}));

Explanation:

  • asyncHandler(fn) wraps your async function and automatically forwards errors to Express's next() middleware.
  • Reduces repetitive try/catch code.

2. Standard API Responses

Use apiResponse to send consistent responses:

app.get("/example", (req, res) => {
    apiResponse(res, {
        statusCode: 200,
        success: true,
        message: "Request successful",
        data: { foo: "bar" },
        extraInfo: "Optional extra fields are supported"
    });
});

Response Structure:

{
    "success": true,
    "message": "Request successful",
    "data": { "foo": "bar" },
    "extraInfo": "Optional extra fields are supported"
}

Default values:

  • statusCode200
  • successtrue
  • message""
  • datanull

API Reference

asyncHandler(asyncFn)

  • Description: Wraps an async Express route function to automatically catch errors and forward them to next().

  • Parameters:

    • asyncFn: (req: Request, res: Response, next: NextFunction) => Promise<any>
  • Returns: Wrapped async function.

apiResponse(res, options)

  • Description: Sends a standardized JSON response.

  • Parameters:

    • res: Response → Express response object
    • options: IResponseOptions → Options object:
interface IResponseOptions {
    statusCode?: number;
    success?: boolean;
    message?: string;
    data?: any;
    [key: string]: any; // Additional optional fields
}

TypeScript Support

Fully typed. You can import asyncHandler and apiResponse in TypeScript projects without extra type definitions.


Flow Diagram

      +-----------------+
      |  Express Route  |
      +--------+--------+
               |
               v
      +-----------------+
      | asyncHandler(fn)|  <-- wraps async function
      +--------+--------+
               |
         Executes async
               v
      +-----------------+
      |    Route Code   |
      +--------+--------+
               |
               v
      +-----------------+
      |  apiResponse()  |  <-- sends standardized JSON
      +--------+--------+
               |
               v
      +-----------------+
      |   Express Res   |
      +-----------------+

License

MIT License

License: MIT


Contributing

Contributions are welcome! Open an issue or pull request for improvements, bug fixes, or new features.