attach-ware
Middleware with configuration
Last updated 6 years ago by wooorm .
MIT · Repository · Bugs · Original npm
$ cnpm install attach-ware 
SYNC missed versions from official npm registry.

attach-ware

Build Coverage Downloads Size

Middleware with configuration.

Install

npm:

npm install attach-ware

Use

x.js:

module.exports = x

function x(ctx, options) {
  return options.condition ? transform : null

  function transform(req, res, next) {
    res.x = 'hello'
    next()
  }
}

y.js:

module.exports = y

function y(ctx, options) {
  return options.condition ? transform : null

  function transform(req, res, next) {
    res.y = 'world'
    next()
  }
}

index.js:

var ware = require('attach-ware')(require('ware'))
var x = require('./x')
var y = require('./y')

var middleware = ware()
  .use(x, {condition: true})
  .use(y, {condition: false})

middleware.run({}, {}, done)

function done(err, req, res) {
  if (err) throw err
  console.log(res.x) // => "hello"
  console.log(res.y) // => undefined
}

API

AttachWare = attachWare(Ware)

Create a new AttachWare based on the given middleware constructor.

Parameters
Returns

Function.

AttachWare()

Create configurable middleware. Works just like the given Ware.

AttachWare#use(attacher[, input...])

Signatures
  • attachWare.use(attacher[, input...])
  • attachWare.use(attachers[, input...])
  • attachWare.use(list)
  • attachWare.use(matrix)
Parameters
  • attacher (Function) — One attacher
  • attachers (Array.<Function>) — List where each value is an attacher
  • list (Array) — List where the first value is an attacher, and further values are input
  • matrix (Array) — Matrix where each entry is a list

Invokes attacher with context and all input.

If attacher returns another function (fn, which can be synchronous, asynchronous, or a generator function), that function is added to the middleware, and will be invoked when run() is invoked like normal middleware.

AttachWare#context

The first argument for attachers. When this is falsey, the instance itself is used.

License

MIT © Titus Wormer

Current Tags

  • 2.0.5                                ...           latest (6 years ago)

3 Versions

  • 2.0.5                                ...           6 years ago
  • 1.1.1                                ...           10 years ago
  • 2.0.4                                ...           6 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (8)
Dependents (0)
None

Copyright 2013 - present © cnpmjs.org