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

smart-response-wrapper

v1.0.0

Published

A generic API response wrapper for standardized responses

Readme

API Response Wrapper 🚀

npm version License: MIT

A lightweight, framework-agnostic utility to standardize your API responses. Ensure consistent JSON structures across your entire project, complete with built-in pagination helpers and error handling.

✨ Features

  • Standardized Structure: Every response follows a predictable format (success, statusCode, message, data, errors, timestamp, meta).
  • Framework Agnostic: Works perfectly with Express, Fastify, Koa, or even plain Node.js and Browser environments.
  • TypeScript Support: Full type safety with generic support for your data models.
  • Smart Pagination: Built-in array pagination helper with automatic metadata generation.
  • Modern Build: Supports both ESM and CommonJS.

📦 Installation

npm install api-response-wrapper

🚀 Usage Examples

Using with Express.js

Standardize your route completions with ease.

import express from 'express';
import { ResponseWrapper, paginateArray } from 'api-response-wrapper';

const app = express();

app.get('/users', (req, res) => {
  try {
    const users = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
    
    // Using the pagination helper
    const paginated = paginateArray(users, 1, 10);
    
    // Return a standardized success response
    return res.status(200).json(
      ResponseWrapper.success(paginated.data, 200, 'Users fetched successfully', paginated.meta)
    );
  } catch (error) {
    return res.status(500).json(
      ResponseWrapper.error(500, 'Failed to fetch users')
    );
  }
});

Using with Vanilla JavaScript (Browser or Node)

Perfect for clients or simple utility scripts.

import { ResponseWrapper } from 'api-response-wrapper';

function handleAPI() {
  const data = { profile: 'Antigravity' };
  
  const response = ResponseWrapper.success(data, 201, 'Profile Created');
  
  console.log(response);
  /*
  {
    success: true,
    statusCode: 201,
    message: 'Profile Created',
    data: { profile: 'Antigravity' },
    errors: null,
    timestamp: '2024-04-15T...',
  }
  */
}

📖 API Reference

ResponseWrapper Class

static success<T>(data, statusCode?, message?, meta?)

Returns a success response object.

  • data: The payload of the response.
  • statusCode: (Optional) HTTP status code (default: 200).
  • message: (Optional) Description message (default: 'Success').
  • meta: (Optional) Pagination metadata object.

static error(statusCode?, message?, errors?)

Returns an error response object.

  • statusCode: (Optional) HTTP status code (default: 500).
  • message: (Optional) Error message (default: 'An error occurred').
  • errors: (Optional) Detailed error object or array.

paginateArray<T>(items, page?, limit?)

A utility function to slice an array and generate pagination metadata.

  • items: The full array of items to paginate.
  • page: Current page number (default: 1).
  • limit: Items per page (default: 10).

Returns: { data: T[], meta: PaginationMeta }


ApiError Class

A custom Error class that includes a statusCode and errors field.

throw new ApiError(404, 'User not found');

🛠️ Data Interfaces

PaginationMeta

interface PaginationMeta {
  page: number;
  limit: number;
  totalElements?: number;
  totalPages?: number;
  hasNextPage?: boolean;
  hasPrevPage?: boolean;
}

📄 License

MIT © Chamodya Ganegamage