Skip to content

parkhub/circe-middleware

Repository files navigation

Circe Middleware

A simple middleware module that you know and love. Using the "next" pattern means you can have async/sync middleware!

Build Status Code Coverage Dependencies version Apache License PRs Welcome Roadmap Semantic Release Commitizen

Watch on GitHub Star on GitHub Tweet

Install

npm install @parkhub/circe-middleware

Usage

import circeMiddleware from '@parkhub/circe-middleware';

const middlewareFnOne = (value, next) => {
  // value is 'starting value'

  // Call next and pass params with any arity(as long as next middleware expects the same arity)
  next('Satsuki!');
};

const middlewareFnTwo = (value, next) => {
  // Value is 'Satsuki!'

  // Call next and pass params with any arity(as long as next middleware expects the same arity)
  next({ going: 'gone' }, 1);
};

const middlewareFnThree = (obj, int, next) => {
  // obj is { going: 'gone' } and int is 1

  next('done');
}

// The middleware is executed in the order of the passed array
const middleware = circeMiddleware([middlewareFnOne, middlewareFnTwo]);

// This middleware will be executed after the first two passed into the middleware factory
middleware.use(middlewareFnThree);

middleware.run('starting value', (finalValue) => {
  // finalValue is 'done'
});

Middlewares

A middleware is a function that receives an arbitrary number of parameters with the last one being the next function. You have to be conscious of the order of the middlware and what it accepts as parameters

API

use(middlewareFn)

middlewareFn - a function with signature of (arg1, arg2, ...argX, next)

Adds a middleware to the middleware stack.

run(...args, () => {})

The parameters passed into the run method are first with the last parameter being the FINAL function the middlewares