middleware-pipeline
v0.1.0
Published
Simple Middleware Pipeline
Downloads
16
Readme
Middleware Pipeline
Simple Middleware Pipeline
Installation
# using yarn:
yarn add middleware-pipeline
# using npm:
npm install --save middleware-pipeline
Usage
import { Pipeline } from 'middleware-pipeline'
import type { Middleware } from 'middleware-pipeline'
type Context = {
value: number
}
// create a middleware pipeline
const pipeline = Pipeline<Context>(
// with an initial middleware
(ctx, next) => {
console.log(ctx)
next()
}
)
// add some more middlewares
pipeline.push(
(ctx, next) => {
ctx.value = ctx.value + 21
next()
},
(ctx, next) => {
ctx.value = ctx.value * 2
next()
}
)
const terminatingMiddleware: Middleware<Context> = (ctx, next) => {
console.log(ctx)
// not calling `next()`
}
// add the terminating middleware
pipeline.push(terminatingMiddleware)
// add another one for fun ¯\_(ツ)_/¯
pipeline.push((ctx, next) => {
console.log('this will not be logged')
})
// execute the pipeline with initial value of `ctx`
pipeline.execute({ value: 0 })
License
Licensed under the MIT License. Check the LICENSE file for details.