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

@ltvn/http-response-builder

v1.1.35

Published

A TypeScript library for building consistent HTTP responses with support for custom status codes

Readme

HTTP Response Builder

A TypeScript library for building consistent HTTP responses with support for custom status codes.

Features

  • Builder pattern for constructing HTTP responses
  • Type-safe response data handling with generics
  • Built-in pagination support
  • Metadata support for additional response information
  • Comprehensive HTTP status codes and messages
  • Fluent interface for method chaining

Installation

npm install @ltvn/http-response-builder
# or
yarn add @ltvn/http-response-builder

Usage

Basic Usage

import { HttpResponseBuilder } from '@ltvn/http-response-builder';

// Create a success response
const response = HttpResponseBuilder
    .ok()
    .setData({ message: 'Hello World' })
    .build();

// Result:
// {
//     status: 200,
//     message: "OK",
//     data: { message: "Hello World" }
// }

With Pagination

import { HttpResponseBuilder, Paging } from '@ltvn/http-response-builder';

const response = HttpResponseBuilder
    .ok()
    .setData([/* your data */])
    .setPaging(new Paging(1, 10, 100))
    .build();

// Result:
// {
//     status: 200,
//     message: "OK",
//     data: [/* your data */],
//     paging: {
//         page: 1,
//         size: 10,
//         total: 100
//     }
// }

With Metadata

const response = HttpResponseBuilder
    .ok()
    .setData({/* your data */})
    .setMetadata({
        timestamp: new Date().toISOString(),
        version: '1.0.0'
    })
    .build();

// Result:
// {
//     status: 200,
//     message: "OK",
//     data: {/* your data */},
//     metadata: {
//         timestamp: "2024-03-14T12:00:00.000Z",
//         version: "1.0.0"
//     }
// }

Custom Response

const response = HttpResponseBuilder
    .customResponse()
    .setStatus(418)  // I'm a teapot
    .setMessage("Custom message")
    .setData({ message: "Hello World" })
    .build();

Response Format

All responses follow this format:

{
    status: number;      // HTTP status code
    message: string;     // Status message
    data?: T;           // Response data (generic type)
    paging?: {          // Optional pagination info
        page: number;
        size: number;
        total: number;
    };
    metadata?: Record<string, any>;  // Optional additional metadata
}

Available Status Codes

Standard HTTP Status Codes (100-599)

  • 1xx: Information
  • 2xx: Success
  • 3xx: Redirection
  • 4xx: Client Error
  • 5xx: Server Error

Custom Status Codes (>=4000)

  • 4000: SUCCESS
  • 4001: INVALID_MAIL_FORMAT
  • 4002: ACCOUNTALREADY
  • 4003: EMAIL_ALREADY_EXISTS
  • 4004: USER_NOT_FOUND
  • 4005: WRONG_PASS
  • 4006: EMAIL_TIMEOUT
  • 4007: AUTHEN_NOT_MATCH
  • 4008: INVALID_EMAIL

Usage with NestJS

Controller Example

import { Controller, Get } from '@nestjs/common';
import { HttpResponseBuilder, Paging } from '@ltvn/http-response-builder';

interface UserDTO {
  id: number;
  name: string;
  email: string;
}

@Controller('users')
export class UsersController {
  @Get()
  async getUsers() {
    const users: UserDTO[] = [
      { id: 1, name: 'John Doe', email: '[email protected]' },
      { id: 2, name: 'Jane Doe', email: '[email protected]' }
    ];

    const totalUsers = users.length;
    const paging = new Paging(1, 10, totalUsers);

    return HttpResponseBuilder.ok<UserDTO[]>()
      .setData(users)
      .setPaging(paging)
      .setMetadata({ timestamp: new Date() })
      .build();
  }

  @Get(':id')
  async getUser() {
    const user: UserDTO = { 
      id: 1, 
      name: 'John Doe', 
      email: '[email protected]' 
    };

    return HttpResponseBuilder.ok<UserDTO>()
      .setData(user)
      .build();
  }
}

Usage with Express

Route Handler Example

import express from "express";
import { HttpResponseBuilder } from "@ltvn/http-response-builder";

const app = express();

app.get("/api/photo/list", function (req, res) {
  const photoList = [
    {
      id: "001",
      name: "photo001.jpg",
      type: "jpg",
      dataUrl: "http://localhost:3000/data/photo001.jpg",
    },
    {
      id: "002",
      name: "photo002.jpg",
      type: "jpg",
      dataUrl: "http://localhost:3000/data/photo002.jpg",
    },
  ];

  const response = HttpResponseBuilder.ok()
    .setData(photoList)
    .setMessage("Get photo list")
    .build();
    
  res.json(response);
});

const server = app.listen(3000, () => {
  console.log("Server is running on port 3000");
});

Error Handling

The builder includes validation and will throw TypeError for invalid inputs:

  • Invalid status codes (must be between 100 and 599, or >= 4000 for custom codes)
  • Invalid message types (must be string)
  • Invalid paging instance
  • Invalid metadata type (must be a non-null object)

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

References

This project was inspired by and built upon the work of @anot/http-response-builder. We would like to acknowledge their contribution to the open-source community.

License

This project is licensed under the ISC License