DEV Community

Matteo Teodori
Matteo Teodori

Posted on

TS-first library to standardize your API responses and errors

Hey everyone! 👋

If you've ever built a Node.js API, you know the struggle of keeping your HTTP responses and error formats consistent across controllers, middlewares, and services. You often end up writing the same custom error classes and response wrappers over and over again.

That's exactly why I built http-response-kit — to solve this once and for all. It's a clean, professional, and zero-dependency utility that provides a rock-solid standard for all your HTTP payloads.

âœĻ What makes it great?

  • 100% Type-Safe ðŸ›Ąïļ: Built with TypeScript from the ground up. It fully supports Generics (<T>), meaning HttpResponse.ok<User>(userData) gives you perfect autocomplete and type safety out of the box. No more any casting.
  • 35+ Error Factory Methods 🛑: Throwing errors is expressive and readable. Just throw HttpError.notFound('User missing') or HttpError.tooManyRequests('Slow down', 60). It covers all standard 4xx and 5xx codes.
  • Predictable Response Structure ðŸ“Ķ: Every success or error response follows a strict, predictable JSON interface. Your frontend team will love you.
  • Zero Dependencies ðŸŠķ: Keep your node_modules light and your startup times fast.
  • Highly Customizable ⚙ïļ: Exposes a global configure() method. You can override default messages, toggle timestamps, or even plug in a custom response transformer.

ðŸ’ŧ Quick Example:

// Throw an error anywhere (Controller, Service, Middleware...)
throw HttpError.badRequest('Invalid payload', { fields: ['email'] });

// Return a structured success response with generics!
return HttpResponse.success<User>({
  data: { id: 1, name: 'John Doe' },
  message: 'User retrieved successfully'
});
// Outputs: { success: true, status_code: 200, data: { id: 1, ... }, message: "..." }
Enter fullscreen mode Exit fullscreen mode

It works beautifully with Express, Fastify, NestJS, native Node, or any other framework you are using. Just catch the errors in your global error handler and stream them via HttpResponse.fromError(err).

I'd love for you to check it out, try it in your next backend project, and let me know your thoughts! Feedback, suggestions, or PRs are super welcome.

🔗 NPM: npm i http-response-kit
🔗 GitHub: github.com/matteo-teodori/http-response-kit

Happy coding! ðŸ’ŧðŸ”Ĩ

Top comments (0)