Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

nexus-res

adevinwild18MITdeprecated1.0.1TypeScript support: included

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

Generate consistent and informative HTTP responses for your Node.js apps easily.

node, node.js, nexus, http, http-responses, responses, response, http-response, http-response-generator, response-generator, premade-responses, lightweight, backend, api, rest, rest-api, restful, restful-api, api-responses, api-response, api-response-generator, structured, consistent, typescript, types, typed

readme

nexus-res

nexus-res


<kbd>nexus-res</kbd> is a lightweight and dependency-free library that provides a structured and easy-to-use way to generate consistent and informative HTTP responses for your Node.js applications.


nexus-res units tests nexus-res version nexus-res is released under the MIT license. PRs welcome!



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 <kbd>nexus-res</kbd>, simply run the following command in your terminal:

npm install nexus-res

Or with yarn

yarn add nexus-res

Configuration ⚙️

<kbd>nexus-res</kbd> can be configured to match your server environment. You can configure the following properties for now inside the <kbd>package.json</kbd> :

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 <kbd>nexus-res</kbd> 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 👈