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 🙏

© 2026 – Pkg Stats / Ryan Hefner

n-helper

v1.0.5

Published

A package for response helpers

Downloads

19

Readme

Response Helper Library

Response Helper is a Node.js utility library designed to simplify API response handling, file uploads, OTP generation, and input validation. This library provides developers with reusable and standardized methods for creating robust backend applications.


Installation

Install the package via npm:

npm install n-helper

Table of Contents


Features

  • Standardized Responses: Quickly send success, error, and failure responses in a consistent format.
  • File Upload: Upload files to specific directories with unique naming conventions.
  • OTP Generation: Generate secure numeric OTPs of any length.
  • Unix Timestamps: Retrieve the current timestamp in Unix format.
  • Validation Checker: Simplify data validation with structured error messages.
  • Dynamic Object Validation: Validate and merge required and optional object fields.

Usage

First, require the package in your application:

const helper = require("n-helper");

Methods

1. success(res, message = "", body = {})

Send a success response with a 200 status code.

Parameters:

  • res (Response): Express response object
  • message (String): Success message
  • body (Object): Response data

Returns:

{
  success: true,
  code: 200,
  message: "Success message",
  body: {}
}

2. error(res, err, req = null)

Send an error response with appropriate status code.

Parameters:

  • res (Response): Express response object
  • err (Error|String): Error object or message
  • req (Request): Optional Express request object for redirects

Returns:

{
  success: false,
  code: 403,
  message: "Error message",
  body: {}
}

3. failed(res, message = "", code)

Send a failed response with a 400 status code.

Parameters:

  • res (Response): Express response object
  • message (String): Failure message
  • code (Status code): Failure Status code

Returns:

{
  success: false,
  code: 400,
  message: "Failure message",
  body: {}
}

4. unixTimestamp()

Get current Unix timestamp.

Returns:

  • Number: Current Unix timestamp

5. checkValidation(validator)

Check validation results and format error messages.

Parameters:

  • validator (Object): Validation object

Returns:

  • String: Formatted error message or empty string

6. fileUpload(file, folder = "users")

Upload file with unique name to specified directory.

Parameters:

  • file (File): File object to upload
  • folder (String): Target folder name

Returns:

{
  fileName: "generated_unique_name.ext",
  uploadPath: "/path/to/file"
}

7. genOtp(length = 6)

Generate numeric OTP of specified length.

Parameters:

  • length (Number): Length of OTP (default: 6)

Returns:

  • Number: Generated OTP

8. validObjectApi(required, nonRequired, res)

Validate and merge required and optional fields.

Parameters:

  • required (Object): Required fields
  • nonRequired (Object): Optional fields
  • res (Response): Express response object

Returns:

  • Object: Merged and validated object

Examples

Basic Response Usage

const helper = require("n-helper");

app.get("/api/users", async (req, res) => {
  try {
    const users = await User.find();
    return helper.success(res, "Users retrieved successfully", users);
  } catch (err) {
    return helper.error(res, err);
  }
});

File Upload Example

app.post("/api/upload", async (req, res) => {
  try {
    const { fileName } = await helper.fileUpload(req.files.image, "profiles");
    return helper.success(res, "File uploaded successfully", { fileName });
  } catch (err) {
    return helper.error(res, err);
  }
});

Object Validation Example

app.post("/api/user", async (req, res) => {
  const required = {
    username: req.body.username,
    email: req.body.email,
  };

  const optional = {
    phone: req.body.phone,
  };

  const validData = await helper.validObjectApi(required, optional, res);
  // Process validData...
});

Contributing

We welcome contributions! Please follow these steps:

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.