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

asap-express

v0.0.0

Published

Funky wrappers for express

Downloads

2

Readme


Features

  • Overwrites the original res.send function in order to give a standarized form to your messages
  • Adds new sender methods (.i.e. res.error, res.exception)
  • Uses async-hooks in order to provide each request with its own context

Coming up:

  • Monkey Patch middleware functions based on conditions
  • Before and after handlers for api requests
  • Try-Catch handler for requests

Install

npm install asap-express --save

Quick Guide

Basic Usage

We initialise an AsapExpress instance by doing the following:

const asapExpress = require('asap-express');
const PORT = 2000;
// the namespace set here 'a$ap' can be used to get the same AsapExpress instance
// from other files.
const app = asapExpress('a$ap', PORT);
// use default configuration
app.default();

The app object is a normal expressjs instance (with some added functionality). The express() returned object is explained here. Afterwards we can start defining or overwritting the existing sender methods

// overwrites the original res.send function
app.asapSend(data => ({
  meta: {
    date: new Date()
  },
  data: {
    msg: data
  }
}));

In the following step we provide a new res.error function which will be used for error reporting:

// sets the res.error return message
app.asapMethod(
  'error',
  error => {
    if (error instanceof Error) {
      return {
        error: {
          message: error.message
        },
        meta: {
          stack: error.stack
        }
      };
    }
    return {
      error: {
        message: error
      },
      meta: {
        undefinedError: true
      }
    };
  },
  // default status code to be sent when this function is used
  400
);

After we define our sender methods we can normally use them in our api calls.

/**
*  {
*    meta: {
*      date: '2021-03-04T22:45:32.859Z'
*    },
*    data: {
*      msg: { hello: 'world' }
*    }
*  }
*/
app.get('/', (req, res, next) => {
  return res.send({ hello: 'world' );
});


/**
*  {
*    meta: {
*      date: '2021-03-04T22:45:32.859Z'
*    },
*    data: {
*      msg: 'HelloWorld'
*    }
*  }
*/
app.get('/string', (req, res, next) => {
  return res.send('HelloWorld');
});

/**
*  {
*    meta: {
*      stack: '.....'
*    },
*    error: {
*      message: 'OMG'
*    }
*  }
*/
app.get('/error', (req, res, next) => {
  return res.error(new Error('OMG!'));
});

Request Context

Via the use of async hooks we are now able to create a unique context for each request and then store data there (by using the cls-hooks libary).

Using the functions AsapExpress.contextGet(key) and AsapExpress.contextSet(key, data) we are able to access the request context storage from any place in the app. The context is deleted when the HTTP request has been handled.

function getFromStore() {
  return asapExpress('a$ap').contextGet('mykey');
}

function storeSth() {
  asapExpress('a$ap').contextSet('mykey', 'thisonewasstoredinthestore');
  return getFromStore();
}

/**
*  {
*    id: 'mytestuserid',
*    data: 'thisonewasstoredinthestore'
*  }
*/
app.get('/', (req, res, next) => {
  const storeData = storeSth();
  return res.send({ id: 'mytestuserid', data: storeData });
});

Contributing

Any contribition is welcome. Please let me know of your ideas of new features, fixes, bugs, etc. I am using a combination of prettier and eslint for code formatting. Please before pushing execute npm run lint:fix . in order to be sure that the code is formatted based on the above.