1.0.1 â€Ē Published 1 year ago

nexus-res v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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 the res.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);
};

📖 Documentation (WIP)

You access the documentation by clicking here 👈