@matthieugi/azurefunctionmiddleware v1.0.6
Azure Function Middleware Helper
Here is a simple module to implement middleware patter for Azure Function HTTP handler, similar to what already exists for Express.js. While there are already a few modules similar to this one, creating this module was also a simple exercice I did to better understand the pattern with Azure Fucntion.
How it works
The middleware pattern execute functions sequencialy to check or enhance a request parameter before executing core logic in the function. This purpose is to keep your Azure Functions simple and focused to your core logic. Because middleware are executed at runtime when client request is received, you will want to keep all your reusable context out of scope of middlewares (DB initialization, service discovery...). Middleware is a great use for JWT verification, data structure check or other prerequisites check.
The Middleware class enhance the context object of the request with a next() function used to stack multiple middlewares.
If you detect an Error, Middleware expects that you feed the client response before throwing the error. Therefore, you manage data sent back to client.
How to use it
- Install module
in your Auzre function project, link the module
npm install @matthieugi/azurefunctionmiddleware
Then import it in your code
import { AzureFunctionMiddleware, ContextMiddleware, Middleware } from 'azurefunctionmiddleware';
- Initiate Middleware class
const middleware = new Middleware();
- define and stack your middlewares
First, you will need to create your middlewares. You should always call and await context.next() to trigger next element in the stack execution. Here is a simple request logger. ContextMiddleware is a child type of Azure Function "Context" type, enhanced with the next function.
const logger: AzureFunctionMiddleware = async function(context: ContextMiddleware) {
context.log.info(context.req);
await context.next();
}
Then stack your middleware. You can chain use function to stack all your middlewares or link to Azure Function
middleware = middleware.use(logger);
middleware = middleware.use(myOtherMiddleware).use(myAzureFunction)
- Return default handler
Export default handler.
export default middleware.listen();
Other
Please create an issue if you have any questions or comment !