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

@coconut-packages/basic-utilities

v1.0.1

Published

`ResponseHandler` is a lightweight JavaScript utility designed to standardize response structures in your application. It helps ensure consistency and clarity when returning success, error, or validation responses from APIs or internal services.

Readme

ResponseHandler Utility

ResponseHandler is a lightweight JavaScript utility designed to standardize response structures in your application. It helps ensure consistency and clarity when returning success, error, or validation responses from APIs or internal services.

Features

  1. Success Responses: Return a structured success response with optional metadata.
  2. Error Responses: Handle errors with detailed messages, custom error codes, and support for multiple errors.
  3. Validation Errors: Provide a consistent format for validation-related errors.
  4. Not Found Responses: Simplify responses for missing resources.
  5. Custom Metadata: Include additional metadata like pagination details.
  6. Centralized Logging (Optional): Log errors for debugging purposes.

Installation

Simply copy the ResponseHandler class into your project. You can also export it as a module if using a modern JavaScript environment.


Usage

1. Success Responses

const successResponse = ResponseHandler.success(
  { id: 1, name: "Sample Data" },
  "Data fetched successfully",
  200,
  { page: 1, limit: 10 } // Metadata
);
console.log(successResponse);

Output:

{
  "status": "success",
  "statusCode": 200,
  "message": "Data fetched successfully",
  "data": {
    "id": 1,
    "name": "Sample Data"
  },
  "meta": {
    "page": 1,
    "limit": 10
  }
}

2. Error Responses

const errorResponse = ResponseHandler.error(
  "Database connection failed",
  500,
  ["Connection timeout", "Invalid credentials"],
  "DB_CONN_ERROR"
);
console.log(errorResponse);

Output:

{
  "status": "error",
  "statusCode": 500,
  "message": "Database connection failed",
  "errorCode": "DB_CONN_ERROR",
  "errors": ["Connection timeout", "Invalid credentials"]
}

3. Validation Error Responses

const validationResponse = ResponseHandler.validationError(
  [{ field: "email", message: "Email is required" }],
  "Validation errors occurred"
);
console.log(validationResponse);

Output:

{
  "status": "fail",
  "statusCode": 400,
  "message": "Validation errors occurred",
  "errorCode": "VALIDATION_ERROR",
  "errors": [
    {
      "field": "email",
      "message": "Email is required"
    }
  ]
}

4. Not Found Responses

const notFoundResponse = ResponseHandler.notFound("User not found", 404, { userId: 123 });
console.log(notFoundResponse);

Output:

{
  "status": "error",
  "statusCode": 404,
  "message": "User not found",
  "meta": {
    "userId": 123
  }
}

Methods

success(data, message, statusCode, meta)

  • data: (Object) The main response data.
  • message: (String) A message describing the success.
  • statusCode: (Number) The HTTP status code (default: 200).
  • meta: (Object) Optional metadata (e.g., pagination).

error(message, statusCode, errors, errorCode)

  • message: (String) Error message.
  • statusCode: (Number) HTTP status code (default: 500).
  • errors: (Array or Object) Details about the errors (default: []).
  • errorCode: (String) A custom error code for debugging.

validationError(errors, message, statusCode, errorCode)

  • errors: (Array) An array of validation errors (e.g., field-level errors).
  • message: (String) A message describing the validation failure.
  • statusCode: (Number) HTTP status code (default: 400).
  • errorCode: (String) A custom error code (default: "VALIDATION_ERROR").

notFound(message, statusCode, meta)

  • message: (String) A message describing the missing resource.
  • statusCode: (Number) HTTP status code (default: 404).
  • meta: (Object) Additional context for the not-found response.

Centralized Error Logging (Optional)

The logError method can be used to log errors for debugging purposes.

ResponseHandler.logError(errorResponse);

Contribution

Feel free to fork this repository and submit pull requests for additional features or improvements.


License

This utility is released under the MIT License. Use it freely in your projects.