7.0.0 • Published 11 months ago

miid v7.0.0

Weekly downloads
11
License
MIT
Repository
github
Last release
11 months ago

🏯 Miid

A type-safe middleware system

Example

import { compose, createKey, Stack } from 'miid';

const ACtx = createKey<string>({ name: 'ACtx', defaultValue: 'A' });

const mid = compose<Stack, 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.get(ACtx.Consumer));
    console.log(ctx.debug());
    return next(ctx.with(ACtx.Provider('a3')));
  }
);
const mid2 = compose(mid, (ctx, next) => {
  console.log('done');
  console.log(ctx.debug());
  return next(ctx);
});
mid2(new Stack(), () => {
  console.log('done 2');
  return 'nope2';
});

Installation

npm install miid
# or
yarn add miid

Extending Stack

You can create your own Stack:

class CustomStack extends Stack {
  // You need to override the `with` method to return a new instance of your CustomStack
  with(...keys: Array<KeyProvider<any>>): CustomStack {
    // Use the static `applyKeys` method to apply keys to the current instance
    return Stack.applyKeys<CustomStack>(this, keys, (internal) => new CustomStack(internal));
  }
}

const custom = new CustomStack();
expect(custom instanceof CustomStack).toBe(true);
expect(custom instanceof Stack).toBe(true);

If you want to pass custom arguments to yout CustomStack you need to override the withInternal method:

class ParamsStack extends Stack {
  // You can pass your own parameters to the constructor
  constructor(public readonly param: string, internal: StackInternal<ParamsStack> | null = null) {
    super(internal);
  }

  with(...keys: Array<KeyProvider<any>>): ParamsStack {
    return Stack.applyKeys<ParamsStack>(this, keys, (internal) => new ParamsStack(this.param, internal));
  }
}

const custom = new ParamsStack('some value');
expect(custom.param).toBe('some value');
expect(custom instanceof ParamsStack).toBe(true);
expect(custom instanceof Stack).toBe(true);
7.0.0-0

11 months ago

6.0.5

11 months ago

6.0.4

11 months ago

7.0.0

11 months ago

6.0.3

2 years ago

6.0.2

2 years ago

5.1.0

2 years ago

5.0.0

2 years ago

6.0.1

2 years ago

6.0.0

2 years ago

4.0.5

3 years ago

4.0.4

3 years ago

4.0.3

3 years ago

4.0.2

3 years ago

4.0.1

3 years ago

4.0.0

3 years ago

4.0.0-1

3 years ago

4.0.0-0

3 years ago

3.0.4

3 years ago

3.0.3

3 years ago

3.0.2

3 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.1.0

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago