Skip to content

typescript-package/middleware

Repository files navigation

@typescript-package/middleware

npm version GitHub issues GitHub license

A lightweight TypeScript library for middleware.

Table of contents

Features

  • Core abstraction: Class for both arguments-based (array) and object-based (context) middleware.
  • Base abstraction: Extends core abstraction with functionality to build new arguments-based middlewares.
  • Concrete class: Extension of base for initialization.
  • Context middleware: Context object-based ContextMiddleware base and concrete middleware built on MiddlewareBase.

Installation

1. Install the package

npm install @typescript-package/middleware --save-peer

Api

ContextMiddlewareBase

import { ContextMiddlewareBase } from '@typescript-package/middleware';

MiddlewareCore

import { MiddlewareCore } from '@typescript-package/middleware';

MiddlewareBase

import { MiddlewareBase } from '@typescript-package/middleware';

Middleware

import { Middleware } from '@typescript-package/middleware';

// Initialize.
const middleware = new Middleware();

// Add middleware.
middleware.use((args, next) => {
  console.log('Middleware 1 executed with args:', args);

  // Modify args.
  args[0].newKey = 'newValue';

  // Execute next.
  next();
});

middleware.use((args, next) => {
  console.log('Middleware 2 executed with args:', args);

  // Execute next.
  next();
});

// logs
// Middleware 1 executed with args: [ { key: 'value' } ]
// Middleware 2 executed with args: [ { key: 'value', newKey: 'newValue' } ]
middleware.execute({ key: 'value' });


// Async
middleware.use(async (args, next) => {
  console.log('Async middleware 1 start with args:', args);
  args[0].newKey = 'newValue';
  await new Promise(resolve => setTimeout(resolve, 2000));
  console.log('Async middleware end');
  next();
});

middleware.use(async (args, next) => {
  console.log('Async middleware 2 start with args:', args);
  next();
});

middleware.onComplete((args) => {
  console.log('All middlewares completed with args:', args);
});

middleware.executeAsync({ key: 'value' });

ContextMiddleware

import { ContextMiddleware } from '@typescript-package/middleware';

const contextMiddleware = new ContextMiddleware<{ req: string; res?: string }>(
  (context, next: () => void) => {
    console.log('Middleware 0 executed with args:', context);
    next();
  },
  (context, next: () => void) => {
    console.log('Middleware 00 executed with args:', context);
    next();
  }
);

contextMiddleware.use((context, next) => {
  console.log('Middleware 1 executed with args:', context);
  context.req = 'newValue';
  next();
});


contextMiddleware.use((context, next) => {
  console.log('Middleware 2 executed with args:', context);
  next();
});

contextMiddleware.execute({ req: 'value' });

Contributing

Your contributions are valued! If you'd like to contribute, please feel free to submit a pull request. Help is always appreciated.

Support

If you find this package useful and would like to support its and general development, you can contribute through one of the following payment methods. Your support helps maintain the packages and continue adding new.

Support via:

or via Trust Wallet

Code of Conduct

By participating in this project, you agree to follow Code of Conduct.

GIT

Commit

Please follow the following commit message conventions:

Versioning

The package follows Semantic Versioning 2.0.0 for all releases. The versioning format is:

Given a version number MAJOR.MINOR.PATCH, increment the:

  • MAJOR version when you make incompatible API changes,
  • MINOR version when you add functionality in a backwards-compatible manner, and
  • PATCH version when you make backwards-compatible bug fixes.

Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.

FAQ How should I deal with revisions in the 0.y.z initial development phase?

The simplest thing to do is start your initial development release at 0.1.0 and then increment the minor version for each subsequent release.

How do I know when to release 1.0.0?

If your software is being used in production, it should probably already be 1.0.0. If you have a stable API on which users have come to depend, you should be 1.0.0. If you’re worrying a lot about backwards compatibility, you should probably already be 1.0.0.

License

MIT © typescript-package (license)

Related packages

About

A lightweight TypeScript library for middleware.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •