1.0.0 • Published 6 years ago

nanomiddleware v1.0.0

Weekly downloads
8
License
MIT
Repository
github
Last release
6 years ago

nanomiddleware

package version package downloads standard-readme compliant package license make a pull request

Simple middleware engine

Table of Contents

Install

This project uses node and npm.

$ npm install nanomiddleware
$ # OR
$ yarn add nanomiddleware

Usage

import Middleware from 'nanomiddleware'

const mid = new Middleware()

function sleep (time = 500) {
  return new Promise((resolve) => {
    setTimeout(resolve, 500)
  })
}

mid.use(async function (...args) {
  await sleep()
  console.log(args)
  return 5
})

mid.use(async function (...args) {
  await sleep(1000)
  console.log(args)
  return 8
})

mid.use([1, 2, 3, 4, async function (...args) {
  await sleep(1000)
  console.log(args)
  return {success: true}
}])

mid.use(async function (...args) {
  await sleep(400)
  console.log('Ran first', args)
  return 100
}, {before: true})

mid.run('hi')
// Ran first [ 'hi' ]
// [ 100 ]
// [ 5 ]
// [ 4 ]

Contribute

  1. Fork it and create your feature branch: git checkout -b my-new-feature
  2. Commit your changes: git commit -am 'Add some feature'
  3. Push to the branch: git push origin my-new-feature
  4. Submit a pull request

License

MIT