npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

handle-res

v2.0.0

Published

A small helper library to handle response calls in express-like frameworks

Downloads

1

Readme

handleRes

JavaScript Style Guide

codecov CircleCI

A small (< 60 loc) helper library to handle response calls in express like frameworks.

Install

npm install handle-res

Usage

The handleRes module is split into 2 main methods, .accept, and .reject.

The reason that .reject sends back the Status: OK http code is to allow developers to create their own failure context. Use .reject when you are able to determine the error. For example, if a user enters a wrong password to your verifyLogin route, you should do:

app.get('/verifyLogin', function (req, res) {
  // check is password is valid somehow
  const passwordValid = false
  if (!passwordValid) {
    return handleRes(res).reject(
      'Password invalid',
      401, // this could be any code you want
      { /* and you can add extra context here */ }
    })
  }
})

Of course the above could be made more sophisticated via automatic error codes, custom error types, etc.

Quick Reference

const express = require('express')
const handleRes = require('handle-res')

const app = express()

app.get('/success', function (req, res) {
  // .accept appends a truthy ok flag to the response body
  let sent = handleRes(res).accept({
    // json body
    data: {}
  })

  // the above will call res.status(200).json({ ok: true, data: {} })

  console.log(sent) // true

  sent = handle(res).accept({
    data2: {}
  })

  // the above will not be sent, because we have already responded to the request
  console.log(sent) // false
})

app.get('/known-failure', function (req, res) {
  // .reject accepts a message, code, and trace object
  handleRes(res).reject('Known failure!', 418, {
    route: req.originalUrl,
    code: 102301
  })

  /**
   * the above will call
   * res.status(418).json({
   *   ok: false,
   *   message: 'Known failure!',
   *   status: 418,
   *   trace: {
   *     route: '/known-failure'
   *     code: 102301
   *   }
   * })
   */
})

API

Api documentation, generated by jsdoc