MiddlewareSet class

A set of Middleware plugins.

Remarks

The set itself is middleware so you can easily package up a set of middleware that can be composed into an adapter with a single adapter.use(mySet) call or even into another middleware set using set.use(mySet).

const { MiddlewareSet } = require('botbuilder');

const set = new MiddlewareSet();
set.use(async (context, next) => {
   console.log(`Leading Edge`);
   await next();
   console.log(`Trailing Edge`);
});

Constructors

MiddlewareSet((context: TurnContext, next: () => Promise<void>) => Promise<void> | Middleware[])

Creates a new MiddlewareSet instance.

Methods

onTurn(TurnContext, () => Promise<void>)

Processes an incoming activity.

run(TurnContext, () => Promise<void>)

Executes a set of middleware in series.

use((context: TurnContext, next: () => Promise<void>) => Promise<void> | Middleware[])

Registers middleware handlers(s) with the set.

Constructor Details

MiddlewareSet((context: TurnContext, next: () => Promise<void>) => Promise<void> | Middleware[])

Creates a new MiddlewareSet instance.

new MiddlewareSet(middlewares: (context: TurnContext, next: () => Promise<void>) => Promise<void> | Middleware[])

Parameters

middlewares

(context: TurnContext, next: () => Promise<void>) => Promise<void> | Middleware[]

One or more middleware handlers(s) to register.

Method Details

onTurn(TurnContext, () => Promise<void>)

Processes an incoming activity.

function onTurn(context: TurnContext, next: () => Promise<void>): Promise<void>

Parameters

context
TurnContext

(xref:botbuilder-core.TurnContext) object for this turn.

next

() => Promise<void>

Delegate to call to continue the bot middleware pipeline.

Returns

Promise<void>

A Promise representing the async operation.

run(TurnContext, () => Promise<void>)

Executes a set of middleware in series.

function run(context: TurnContext, next: () => Promise<void>): Promise<void>

Parameters

context
TurnContext

Context for the current turn of conversation with the user.

next

() => Promise<void>

Function to invoke at the end of the middleware chain.

Returns

Promise<void>

A promise that resolves after the handler chain is complete.

use((context: TurnContext, next: () => Promise<void>) => Promise<void> | Middleware[])

Registers middleware handlers(s) with the set.

function use(middlewares: (context: TurnContext, next: () => Promise<void>) => Promise<void> | Middleware[]): this

Parameters

middlewares

(context: TurnContext, next: () => Promise<void>) => Promise<void> | Middleware[]

One or more middleware handlers(s) to register.

Returns

this

The updated middleware set.

Remarks

This example adds a new piece of middleware to a set:

set.use(async (context, next) => {
   console.log(`Leading Edge`);
   await next();
   console.log(`Trailing Edge`);
});