Skip to content

chrisharrison/simple-middleware-pipeline

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

simple-middleware-pipeline

A simple Typescript middleware pipeline.

Installation

yarn add @chrisharrison/simple-middleware-pipeline

Usage

First you need to setup a new pipeline:

import { Pipeline } from '@chrisharrison/simple-middleware-pipeline';

const pipeline = new Pipeline([]);

You then run whatever you want to wrap middleware around via the pipeline:

const base = () => 'hello world';

const result = pipeline.run(base);

console.log(result);

Because no middleware has been added to the pipeline yet, this will output:

"hello world"

The following type describes middleware implementation:

type Middleware = (next: (...args: any[]) => any, ...rest: any) => any;

So an example middleware might be:

const uppercaseMiddleware = (next: () => any): string => {
    const incoming = next();
    if (typeof incoming === 'string') {
        return incoming.toUpperCase();
    }
    return '';
};

You would then add the middleware to the pipeline like so:

const pipeline = new Pipeline([uppercaseMiddleware]);

Running the pipeline now would produce:

"HELLO WORLD"

Dealing with arguments

Middleware can also pickup and modify arguments passed to the initial function:

const argumentModifyingMiddleware = (next: (name: string) => string, name: string): string => {
    return next(name.toUpperCase());
};

const pipeline = new Pipeline([argumentModifyingMiddleware]);

const result = pipeline.run((name: string) => `Hello, ${name}. Nice to meet you.`, 'matthew');

console.log(result);

produces:

"Hello, MATTHEW. Nice to meet you."

Testing

Tests are written with Jest. Specifications live next to the system under test (i.e. the test file goes next to the file it's testing).

To run tests:

yarn test

About

A simple Typescript based middleware bus

Resources

Stars

Watchers

Forks

Packages

No packages published