nexus-res
v1.0.1
Published
Generate consistent and informative HTTP responses for your Node.js apps easily.
Downloads
15
Maintainers
Readme
Main features
- 📦 Contains all necessary HTTP response codes (1XX, 2XX, 3XX, 4XX , 5XX)
- 🌳 Tree-shakeable
- 🤌 Dependency-free
- 🪶 Lightweight
- 🔵 Written in TypeScript
- 🤖 Compatible with
node >= 14
- 🛠 Configurable
Installation 💾
To install nexus-res, simply run the following command in your terminal:
npm install nexus-res
Or with yarn
yarn add nexus-res
Configuration ⚙️
nexus-res can be configured to match your server environment. You can configure the following properties for now inside the package.json :
By default the serverType is set to "express", this means that the
send
function will use theres.json
method
{
"serverType": "express" // | "fastify" | "koa" | "hapi"
}
Usage 📖
To use nexus-res in your Node.js application, import the desired response class and use it to generate an HTTP response:
Example with an Express.js like route (JS)
const { Ok } = require('nexus-res');
export default async (req, res) => {
const user = req.user;
// 🎉 Create a new Ok response with the new version
return Ok({
metadata: user,
}).send(res);
};
Example with an Express.js like route (TS)
import { Ok } from 'nexus-res';
import { User } from './types';
export default async (req, res) => {
const user: User = req.user;
// 🎉 Create a new Ok response with the new version
return Ok<User>({
metadata: user, // 👈 The type of the metadata is inferred from the generic type
}).send(res);
};