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>), meaningHttpResponse.ok<User>(userData)gives you perfect autocomplete and type safety out of the box. No moreanycasting. - 35+ Error Factory Methods ð: Throwing errors is expressive and readable. Just
throw HttpError.notFound('User missing')orHttpError.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_moduleslight 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: "..." }
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)