New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

http-reply

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

http-reply

A lightweight Node.js utility for sending consistent, standardized HTTP responses across your API endpoints

Source
npmnpm
Version
0.2.1
Version published
Weekly downloads
5
Maintainers
1
Weekly downloads
 
Created
Source

HttpReply

A lightweight, flexible Node.js utility for standardizing HTTP responses in Express, Fastify, or custom server frameworks. HttpReply provides a consistent way to format and send HTTP responses with customizable options, including support for timestamps, error logging, and custom adapters.

Features

  • Framework Agnostic: Works with Express, Fastify, or custom adapters.
  • Standardized Responses: Ensures consistent response structure across your API.
  • Configurable: Customize response fields, timestamps, and logging behavior.
  • Static Methods: Easily send responses without instantiating the class.
  • Error Handling: Built-in validation and logging for invalid configurations or response objects.
  • TypeScript Support: Fully typed for TypeScript users (types included).

Installation

Install the package via npm:

npm i http-reply

Usage

Basic Setup

Require the HttpReply class and use it in your Express or Fastify application.

const HttpReply = require("http-reply");

// Express example
const express = require("express");
const app = express();

app.get("/example", (req, res) => {
  HttpReply.success(res, {
    message: "Operation successful",
    data: { user: "John Doe" },
    metaData: { requestId: "12345" },
  });
});

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

Centralized Configuration

You can create a centralized HttpReply instance with predefined configuration in a dedicated file (e.g., responder.js) and import it across your application.

Create a file named responder.js:

const HttpReply = require('http-reply');

const reply = new HttpReply({
  includeTimestamp: true,
  dateFormat: 'iso',
  enableLogging: true,
});

module.exports = reply;

Then, import and use it in your routes:

const reply = require("./responder");
const express = require("express");
const app = express();

app.get("/example", (req, res) => {
  reply.success(res, {
    message: "Custom response",
    data: { id: 1 },
  });
});

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

Creating an Instance with Custom Configuration

You can instantiate HttpReply with custom configuration options.

const HttpReply = require("http-reply");

const reply = new HttpReply({
  includeTimestamp: true,
  dateFormat: "iso",
  enableLogging: true,
  customFields: { version: "1.0.0" },
});

app.get("/custom", (req, res) => {
  reply.success(res, {
    message: "Custom response",
    data: { id: 1 },
  });
});

Static Methods

Use static methods for quick responses without instantiation.

HttpReply.created(res, {
  message: "User created",
  data: { id: 123, name: "Jane Doe" },
});

HttpReply.notFound(res, {
  message: "Resource not found",
  error: "Invalid ID",
});

Supported Response Methods

HttpReply supports the following response methods, each corresponding to common HTTP status codes:

MethodStatus CodeDescription
success200Successful request
created201Resource created successfully
accepted202Request accepted for processing
noContent204No content to return
badRequest400Invalid request
unauthorized401Authentication required
forbidden403Access denied
notFound404Resource not found
conflict409Resource conflict
tooManyRequests429Rate limit exceeded
error500Internal server error
notImplemented501Feature not implemented
serviceUnavailable503Service temporarily unavailable
responseCustomGeneric response with custom status code

Configuration Options

When creating an instance of HttpReply, you can pass a configuration object with the following options:

OptionTypeDefaultDescription
includeTimestampBooleanfalseInclude a timestamp in the response (iso or unix format).
includeCodeBooleantrueInclude the status code in the response body.
includeMessageBooleantrueInclude the message in the response body.
includeErrorBooleantrueInclude error details in the response body.
includeMetaDataBooleantrueInclude metadata in the response body.
enableLoggingBooleantrueEnable error logging for invalid configurations or response objects.
stringifyBooleanfalseStringify the response body before sending (useful for custom adapters).
customFieldsObject{}Additional fields to include in every response.
dateFormatString'unix'Format for timestamps ('iso' or 'unix').
adapterFunctionnullCustom adapter function for non-Express/Fastify frameworks.

Custom Adapter Example

For frameworks other than Express or Fastify, provide a custom adapter.

const reply = new HttpReply({
  adapter: (res, statusCode, payload) => {
    // Custom response handling
    res.writeStatusCode(statusCode);
    res.writeBody(payload);
    return res;
  },
});

reply.success(res, {
  message: "Custom adapter response",
  data: { key: "value" },
});

Example Response Output

Using the success method with default configuration:

HttpReply.success(res, {
  message: "User fetched",
  data: { id: 1, name: "John" },
  metaData: { total: 1 },
});

Output:

{
  "message": "User fetched",
  "data": { "id": 1, "name": "John" },
  "metaData": { "total": 1 }
}

With includeTimestamp: true and dateFormat: 'iso':

{
  "message": "User fetched",
  "data": { "id": 1, "name": "John" },
  "metaData": { "total": 1 },
  "timestamp": "2025-05-27T17:52:00.000Z"
}

Dependencies

  • Node.js >= 12.x
  • Express or Fastify (optional, depending on your framework)

Contributing

Contributions are welcome! Please follow these steps:

  • Fork the repository.
  • Create a new branch (git checkout -b feature/your-feature).
  • Commit your changes (git commit -m 'Add your feature').
  • Push to the branch (git push origin feature/your-feature).
  • Open a Pull Request.

License

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

Contact

For questions or support, open an issue on the GitHub repository or contact the maintainer at dev182000@gmail.com.

Keywords

http

FAQs

Package last updated on 27 May 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts