middleware-pipeline
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

GitHub Workflow Status: CI Version Coverage License

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.

Readme

Keywords

Package Sidebar

Install

npm i middleware-pipeline

Weekly Downloads

0

Version

0.1.0

License

MIT

Unpacked Size

379 kB

Total Files

12

Last publish

Collaborators

  • muniftanjim