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

connect-sequence

v2.1.1

Published

A node.js module to run connect-like middlewares in sequence

Downloads

5,616

Readme

connect-sequence GitHub license GitHub tag

NPM

node: LTS/argon node: LTS/argon npm

Build Status Coverage Status NPM

Code Climate Issue Count

JavaScript Style Guide Semver 2.0 Gitter

A node.js module to run connect-like middlewares in sequence

What is connect-sequence

This module is intended to be used in a connect or express application.

In an express application, you manipulate middlewares. The request walkthru the middlewares you registred in the same order you registred it.

This is super cool but sometimes, we would want to register the middleware sequence dynamically, i.e. at runtime.

connect-sequence aims to make a such thing super-easy!

You'll find connect-sequence on these platforms:

Suscribe to new releases on libraries.io!

Installation

With node package manager (recommanded but not required)

npm install --save connect-sequence

API reference

ConnectSequence API reference

Usage

The following example is stupid simple.

In the usage example the conditions tested before pushing the products middlewares in the middlewares array coud/should simply be tested in each middleware that need to test a conditions on the req object before doing its stuff.

You know the real cases when you need to use the "connect-sequence patern". In this example I just show how to use it.

I often use this patern when I write some usefull tiny middlewares highly reusable in differents contexts, and these contexts shoud be tested out of these middlewares to keep it clean and really reusable.

/**
 * Product API
 * @module
 */

var ConnectSequence = require('connect-sequence')
var productsController = require('./products.controller')

module.exports = productRouter

function productRouter (app) {
  app.route('/api/products/:productId')
  .get(function (req, res, next) {
    // Create a ConnectSequence instance and setup it with the current `req`,
    // `res` objects and the `next` callback
    var seq = new ConnectSequence(req, res, next)

    // build the desired middlewares sequence thanks to:
    // - ConnectSequence#append(mid0, ..., mid1),
    // - ConnectSequence#appendList([mid0, ..., mid1])
    // - and ConnectSequence#appendIf(condition, mid)

    if (req.query.filter) {
      seq.append(productsController.filter)
    }

    if (req.query.format) {
      seq.append(
        productsController.validateFormat,
        productsController.beforeFormat,
        productsController.format,
        productsController.afterFormat
      )
    }

    // unless #run(), the other methods are chainable:

    // append the productsController.prepareResponse middleware to the sequence
    // only if the condition `req.query.format && req.formatedProduct` is true
    // at the moment where the middleware would be called.
    // So the condition is tested after the previous middleware is called and thus
    // if the previous modifies the `req` object, we can test it.
    seq.appendIf(isProductFormatted, productsController.prepareResponse)
    .append(productsController.sendResponse)
    // run the sequence
    .run()
  })

  app.param('productId', function (req, res, next, id) {
    // ... yield the product by ID and bind it to the req object
  })

  function isProductFormatted (req) {
    return Boolean(req.formatedProduct)
  }
}

Contribute

JavaScript Style Guide

The javascript source files are located in the lib folder and the unit test files are located in the tests folder.

We use the Standard Javascript Code Style to keep the code clean and nice.

We use Gulp some gulp plugins and some other node modules as devDependendies to automate the developement tasks:

Finally, we use the Semver 2.0 (Semantic Versioning) to standardize the release version numbers (major/minor/path/pre-release).

Your contributions posting issues and pull requests are welcome!

Development

Ensure you are not in production environement to install the devDependencies

$ NODE_ENV=development npm install

Then you can start coding in a Test Driven Development environement with gulp, mocha and chai

$ npm run TDD

The script will lint the lib and test files (but not break on error), run all the unit tests and then it will restart the tests on file change.

Credits

  • Rémi Becheras (https://github.com/rbecheras)
  • Groupe SIRAP (https://github.com/sirap-group)

Copyright

Copyright © 2016 SIRAP Group, All Rights Reserved

License

This project is licensed under the MIT license