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

@techsolu/api-wrapper

v1.1.0

Published

Standardize API responses with consistent JSON structure

Downloads

6

Readme

API Response Wrapper

A lightweight, zero-dependency npm package that standardizes API responses across your Node.js projects. Enforce consistent JSON response structures and reduce boilerplate code in your backend routes.

📦 Installation

npm i @techsolu/api-wrapper

🚀 Quick Start

Basic Usage

import { success, error } from '@techsolu/api-wrapper';

// Success response
const successResponse = success({ id: 1, name: 'John' }, 'User created successfully');

// Error response
const errorResponse = error('User not found', 404, { userId: 123 });

Express Integration

import express from 'express';
import { middleware } from '@techsolu/api-wrapper';

const app = express();
app.use(middleware());

app.get('/users', async (req, res) => {
  try {
    const users = await fetchUsersFromDB();
    res.apiSuccess(users, 'Users fetched successfully');
  } catch (err) {
    res.apiError('Failed to fetch users', 500);
  }
});

app.listen(3000);

✨ Key Benefits

1. Consistent Response Structure

Eliminate inconsistent response formats across your API endpoints. Every response follows the same predictable structure:

Success Response:

{
  "success": true,
  "message": "Operation completed successfully",
  "data": { "id": 1, "name": "John Doe" }
}

Error Response:

{
  "success": false,
  "message": "Resource not found",
  "error": {
    "code": 404,
    "details": { "resourceId": 123 }
  }
}

2. Reduced Boilerplate Code

Stop repeating the same response formatting code in every route handler:

Before:

app.get('/users', (req, res) => {
  // Inconsistent formatting across different developers
  res.json({ status: 'success', data: users, message: 'OK' });
  // or
  res.json({ ok: true, result: users });
  // or
  res.json(users); // No structure at all
});

After:

app.get('/users', (req, res) => {
  res.apiSuccess(users, 'Users fetched successfully');
});

3. Frontend Developer Happiness

Frontend developers can write consistent response handling logic:

// Consistent response handling
const response = await fetch('/api/users');
const data = await response.json();

if (data.success) {
  // Work with data.data
  setUsers(data.data);
} else {
  // Handle error with data.error.code and data.message
  showError(data.message, data.error.code);
}

4. Built-in Error Handling

Standardized error responses with proper HTTP status codes and optional details:

// Simple error
res.apiError('Validation failed', 400);

// Error with details
res.apiError('Invalid input', 422, {
  fields: {
    email: 'Must be valid email',
    password: 'Minimum 8 characters'
  }
});

5. Framework Agnostic

Works with Express, Fastify, and any Node.js HTTP server:

Fastify Integration:

import Fastify from 'fastify';
import { ResponseWrapper } from '@techsolu/api-wrapper';

const fastify = Fastify();
const wrapper = new ResponseWrapper();

wrapper.decorateFastify(fastify);

fastify.get('/users', async (request, reply) => {
  reply.apiSuccess(users, 'Users fetched successfully');
});

6. Customizable Configuration

Configure default messages and error details globally:

import { configure } from '@techsolu/api-wrapper';

configure({
  defaultSuccessMessage: 'Request processed successfully',
  defaultErrorMessage: 'An unexpected error occurred',
  includeErrorDetails: process.env.NODE_ENV === 'development'
});

7. TypeScript Support

Full TypeScript support with complete type definitions:

import { success, error, ApiResponse } from '@techsolu/api-wrapper';

// Type-safe responses
const response: ApiResponse<User> = success(user, 'User created');

// Type-safe error handling
if (!response.success) {
  console.log(response.error.code); // TypeScript knows this exists
}

📖 API Reference

Core Functions

success(data, message?)

Creates a standardized success response.

success({ id: 1 }, 'User created');
// { success: true, message: "User created", data: { id: 1 } }

error(message?, code?, details?)

Creates a standardized error response.

error('Not found', 404, { resource: 'user' });
// { success: false, message: "Not found", error: { code: 404, details: { resource: "user" } } }

Middleware

middleware()

Express middleware that adds res.apiSuccess() and res.apiError() methods.

app.use(middleware());
// Now you can use res.apiSuccess() and res.apiError() in all routes

Configuration

configure(config)

Update global configuration settings.

configure({
  defaultSuccessMessage: 'Success',
  defaultErrorMessage: 'Error',
  includeErrorDetails: true
});

🛠️ Advanced Usage

Custom Instance Creation

Create multiple instances with different configurations:

import { ResponseWrapper } from '@techsolu/api-wrapper';

const publicApiWrapper = new ResponseWrapper({
  defaultSuccessMessage: 'OK',
  includeErrorDetails: false
});

const internalApiWrapper = new ResponseWrapper({
  defaultSuccessMessage: 'Operation completed',
  includeErrorDetails: true
});

Error Handling Middleware

Standardize error handling across your application:

app.use((err, req, res, next) => {
  if (err instanceof ValidationError) {
    res.apiError('Validation failed', 422, err.details);
  } else if (err instanceof AuthenticationError) {
    res.apiError('Unauthorized', 401);
  } else {
    res.apiError('Internal server error', 500);
  }
});

🔧 Configuration Options

| Option | Type | Default | Description | |--------|------|---------|-------------| | defaultSuccessMessage | string | 'Operation completed successfully' | Default message for success responses | | defaultErrorMessage | string | 'An error occurred' | Default message for error responses | | includeErrorDetails | boolean | false | Whether to include error details in responses |

🤝 Contributing

We welcome contributions! Please feel free to submit pull requests or open issues for bugs and feature requests.

📄 License

MIT License - feel free to use this package in your commercial projects.

🚀 Performance

  • Zero dependencies - No additional packages required
  • Lightweight - Minimal impact on bundle size
  • Fast - Optimized for performance-critical applications

Start standardizing your API responses today and make your frontend developers (and your future self) much happier! 🎉