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

keep-or-skip

v1.0.7

Published

Dynamically execute or skip express middlewares

Downloads

1,553

Readme

keep-or-skip

Dynamically execute or skip express middlewares

This wrapper allows you to choose one or more middlewares whose execution is based on a certain condition. The aforementioned condition could be, for instance, a specific request value.

CI NPM version NPM downloads

Installation

npm i keep-or-skip --save

API

keepOrSkip(middlewares, predicate[, debug])
  • middlewares <Function> | <Function[]> A middleware or an array of middlewares to be handled dynamically.
  • predicate <Function> A function returning a boolean value affecting the execution of the speciefied middlewares. This function takes as input two optional parameters, the request and the response objects. If the predicate does not return a boolean value, middlewares will be skipped.
  • debug <Boolean> An optional boolean parameter enabling a warning log which notifies whether predicate returned a boolean value. Default: false.
  • returns An array of middlewares.

If the parameters' type does not match with those required, an error will be thrown. In pariticular, the error will be an instance of KeepOrSkipError.

Example

Consider the following example:

const express = require('express')
const keepOrSkip = require('keep-or-skip')

const app = express()

function setValue(req, res, next) {
    req.value = 1
    next()
}

function middlewareOne(req, res, next) {
    req.message = 'Hi from middlewareOne'
    next()
}

function middlewareTwo(req, res, next) {
    req.message = 'Hi from middlewareTwo'
    next()
}

function respond(req, res, next) {
    res.status(200).json({
        message: req.message
    })
}

const maybeMiddlewareOne = keepOrSkip(middlewareOne, req => req.value < 0)
const maybeMiddlewareTwo = keepOrSkip(middlewareTwo, req => req.value >= 0)

app.get('/',
    setValue,
    maybeMiddlewareOne,
    maybeMiddlewareTwo,
    respond
)

app.listen(3000)

http://localhost:3000 will produce the following result, middlewareOne is skipped and middlewareTwo is executed:

{
    "message": "Hi from middlewareTwo"
}

Debug

In case predicate does not return a boolean value, middlewares will be skipped. In this situation it's possible to log a warning by using the debug mode.

It's possible to activate the debug globally, directly on the imported module:

const keepOrSkip = require('keep-or-skip')
keepOrSkip.debug(true)

the above code sets the debug mode to true every time keepOrSkip is used unless otherwise specified in the single calls as shown in the following example:

const keepOrSkip = require('keep-or-skip')

// Set debug mode globally.
keepOrSkip.debug(true)

// Specifying the debug parameter at the time of use will overwrite the global debug variable.
keepOrSkip(myMiddlewares, myPredicate, false) // Debug OFF.

keepOrSkip(myMiddlewares, myPredicate) // Debug ON because of the global debug variable.