botbuilder-core
Version:
Core components for Microsoft Bot Builder. Components in this library can run either in a browser or on the server.
106 lines • 3.8 kB
TypeScript
/**
* @module botbuilder
*/
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { TurnContext } from './turnContext';
/**
* Interface implemented by object based middleware.
*/
export interface Middleware {
/**
* Called each time the bot receives a new request.
*
* @remarks
* Calling `await next();` will cause execution to continue to either the next piece of
* middleware in the chain or the bots main logic if you are the last piece of middleware.
*
* Your middleware should perform its business logic before and/or after the call to `next()`.
* You can short-circuit further execution of the turn by omitting the call to `next()`.
*
* The following example shows a simple piece of logging middleware:
*
* ```JavaScript
* class MyLogger {
* async onTurn(context, next) {
* console.log(`Leading Edge`);
* await next();
* console.log(`Trailing Edge`);
* }
* }
* ```
* @param context Context for current turn of conversation with the user.
* @param next Function to call to continue execution to the next step in the middleware chain.
*/
onTurn(context: TurnContext, next: () => Promise<void>): Promise<void>;
}
/**
* Signature implemented by function based middleware.
*
* ```TypeScript
* type MiddlewareHandler = (context: TurnContext, next: () => Promise<void>) => Promise<void>;
* ```
*/
export declare type MiddlewareHandler = (context: TurnContext, next: () => Promise<void>) => Promise<void>;
/**
* 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)`.
*
* ```JavaScript
* const { MiddlewareSet } = require('botbuilder');
*
* const set = new MiddlewareSet();
* set.use(async (context, next) => {
* console.log(`Leading Edge`);
* await next();
* console.log(`Trailing Edge`);
* });
* ```
*/
export declare class MiddlewareSet implements Middleware {
private middleware;
/**
* Creates a new MiddlewareSet instance.
*
* @param {...any} middlewares One or more middleware handlers(s) to register.
*/
constructor(...middlewares: (MiddlewareHandler | Middleware)[]);
/**
* Processes an incoming activity.
*
* @param context [TurnContext](xref:botbuilder-core.TurnContext) object for this turn.
* @param next Delegate to call to continue the bot middleware pipeline.
* @returns {Promise<void>} A Promise representing the async operation.
*/
onTurn(context: TurnContext, next: () => Promise<void>): Promise<void>;
/**
* Registers middleware handlers(s) with the set.
*
* @remarks This example adds a new piece of middleware to a set:
* ```JavaScript
* set.use(async (context, next) => {
* console.log(`Leading Edge`);
* await next();
* console.log(`Trailing Edge`);
* });
* ```
* @param {...any} middlewares One or more middleware handlers(s) to register.
* @returns The updated middleware set.
*/
use(...middlewares: (MiddlewareHandler | Middleware)[]): this;
/**
* Executes a set of middleware in series.
*
* @param context Context for the current turn of conversation with the user.
* @param next Function to invoke at the end of the middleware chain.
* @returns A promise that resolves after the handler chain is complete.
*/
run(context: TurnContext, next: () => Promise<void>): Promise<void>;
}
//# sourceMappingURL=middlewareSet.d.ts.map