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

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

HttpReply

A lightweight, flexible Node.js utility for standardizing HTTP responses in Express, Fastify, or custom server frameworks. HttpReply provides a consistent, customizable way to format and send HTTP responses, with support for timestamps, error logging, and custom adapters. Available as both ES6 modules and CommonJS, with no need for .default in ES module imports.

Features

  • Framework Agnostic: Compatible with Express, Fastify, or custom frameworks via adapters.
  • Standardized Responses: Ensures consistent response structure across your API.
  • Configurable Options: Customize response fields, timestamps, logging, and more.
  • Static Methods: Send responses without instantiating the class.
  • Error Handling: Built-in validation and logging for invalid configurations or response objects.
  • TypeScript Support: Fully typed with included TypeScript declarations.
  • ES6 and CommonJS Support: Use with import or require, no .default required for ES modules.

Installation

Install the package via npm:

npm install http-reply

Usage

Importing the Package

The package supports both ES6 modules and CommonJS:

// ES6 Module
import { HttpReply } from 'http-reply';

// CommonJS
const { HttpReply } = require('http-reply');

Basic Example (Express)

Use HttpReply to send standardized responses in your Express or Fastify application.

import { HttpReply } from 'http-reply';
import express from '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

Create a centralized HttpReply instance for consistent configuration across your application.

Create a file (e.g., responder.js):

import { HttpReply } from 'http-reply';

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

export default reply;

Use it in your routes:

import reply from './responder';
import express from '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'));

Static Methods

Use static methods for quick responses without instantiation:

import { HttpReply } from 'http-reply';

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 provides methods for 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

Customize HttpReply with the following options when creating an instance:

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 for non-Express/Fastify frameworks.

Custom Adapter Example

For non-Express/Fastify frameworks, provide a custom adapter:

import { HttpReply } from 'http-reply';

const reply = new HttpReply({
  adapter: (res, statusCode, payload) => {
    res.setStatusCode(statusCode);
    res.setBody(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 },
  "code": 200
}

With includeTimestamp: true and dateFormat: 'iso':

{
  "message": "User fetched",
  "data": { "id": 1, "name": "John" },
  "metaData": { "total": 1 },
  "code": 200,
  "timestamp": "2025-06-13T17:25:00.000Z"
}

Dependencies

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

Contributing

We welcome contributions! To contribute:

  • 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.

Please ensure your code follows the project's coding standards and includes tests where applicable.

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 13 Jun 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