1.0.1 âĒ Published 1 year ago
nexus-res v1.0.1
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);
};