co-compose
    TypeScript icon, indicating that this package has built-in type declarations

    6.1.4 • Public • Published

    Co Compose

    Compose an array of functions to be executed one after the other. Similar to Koa and AdonisJS middlewares.

    gh-workflow-image typescript-image npm-image license-image synk-image

    Co compose composes an array of middleware to be executed in sequence. The library is framework independent and can be used in any Javascript/Typescript project.

    Benchmarks
    Co Compose x 521,393 ops/sec ±1.41% (82 runs sampled)
    fastseries x 114,855 ops/sec ±6.97% (42 runs sampled)
    middie x 109,021 ops/sec ±6.62% (42 runs sampled)
    

    Fastest is Co Compose

    Table of contents

    Installation

    npm i co-compose
    
    # yarn
    yarn add co-compose

    Usage

    Checkout the following example to run an array of middleware functions.

    import { Middleware } from 'co-compose'
    async function fn1(next) {
      console.log('executing fn1')
      await next()
    }
    
    async function fn2(next) {
      console.log('executing fn2')
      await next()
    }
    
    const middleware = new Middleware()
    middleware.register([fn1, fn2])
    
    await middleware.runner().run([])

    Passing values

    You can also pass values to all middleware functions. An array of values passed to runner.run() will be passed to middleware functions as multiple arguments.

    async function fn1(ctx, next) {
      ctx.stack.push('fn1')
      await next()
    }
    
    async function fn2(ctx, next) {
      ctx.stack.push('fn2')
      await next()
    }
    
    const ctx = {
      stack: [],
    }
    
    await middleware.runner().run([ctx])
    assert.deepEqual(ctx.stack, ['fn1', 'fn2'])

    Custom executors

    The default behavior is to define middleware as functions. However, you can define them in any shape and then stick a custom executor to execute them.

    Check the following example where ES6 classes are used.

    class Middleware1 {
      async handle(ctx, next) {
        ctx.stack.push('fn1')
        await next()
      }
    }
    
    class Middleware2 {
      async handle(ctx, next) {
        ctx.stack.push('fn2')
        await next()
      }
    }
    
    const middleware = new Middleware()
    const ctx = {
      stack: [],
    }
    
    middleware.register([Middleware1, Middleware2])
    
    await middleware
      .runner()
      .executor(async function (MiddlewareClass, params) {
        const instance = new MiddlewareClass() // 👈
        await instance.handle(...params) // 👈
      })
      .run([ctx])

    Final Handler

    The final handler is a executed when the entire middleware chain ends by calling next. This makes it easier to execute custom functions, which are not part of the chain, however must be executed when chain ends.

    Also, the arguments for the final handler can be different from the middleware arguments

    async function fn1(ctx, next) {
      ctx.stack.push('fn1')
      await next()
    }
    
    async function finalHandler() {
      ctx.stack.push('final handler')
    }
    
    const ctx = {
      stack: [],
    }
    
    await middleware.runner().finalHandler(finalHandler, [ctx]).run([ctx])
    
    assert.deepEqual(ctx.stack, ['fn1', 'final handler'])

    Install

    npm i co-compose

    DownloadsWeekly Downloads

    13,889

    Version

    6.1.4

    License

    MIT

    Unpacked Size

    14.2 kB

    Total Files

    11

    Last publish

    Collaborators

    • virk