🏯 Miid Build Status codecov

A type-safe middleware system

Example

import { compose, createContext } from 'miid';

const ACtx = createContext<string>('A');

const mid = compose<string>(
  (ctx, next) => {
    console.log('middleware 1');
    console.log(ctx.debug());
    return next(ctx.with(ACtx.Provider('a1')));
  },
  (ctx, next) => {
    console.log('middleware 2');
    console.log(ctx.debug());
    return next(ctx.with(ACtx.Provider('a2')));
  },
  (ctx, next) => {
    console.log('middleware 3');
    console.log(ctx.read(ACtx.Consumer));
    console.log(ctx.debug());
    return next(ctx.with(ACtx.Provider('a3')));
  }
);
const mid2 = compose(mid, async (ctx, next) => {
  console.log('done');
  console.log(ctx.debug());
  return next(ctx);
});
runMiddleware(mid2, () => {
  console.log('done 2');
  return 'nope2';
}).then((res) => {
  console.log({ res });
});

Installation

npm install miid
# or
yarn add midd