8.0.1 • Published 10 days ago

@dldc/compose v8.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
10 days 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);
8.0.1

10 days ago

8.0.0

18 days ago

7.2.4

28 days ago

7.2.3

3 months ago

7.2.2

5 months ago

7.2.1

6 months ago

7.2.0

7 months ago

7.1.2

7 months ago

7.1.1

8 months ago

7.1.0

8 months ago

7.0.7

10 months ago

7.0.6

10 months ago

7.0.5

10 months ago

7.0.4

10 months ago

7.0.3

10 months ago

7.0.2

10 months ago

7.0.1

11 months ago