This library was heavily inspired by a JavaScript version of azure functions middleware, most of the credit goes to them Azure Middleware Engine. It is slimmed down compared to that, and is a fit for purpose at the moment.
@polarpop/azure-middleware
relies on Azure Serverless Functions by Microsoft. To get started with their
sdk, please read the following before installing Node.JS SDK
npm install @polarpop/azure-middleware --save
yarn add @polarpop/azure-middleware
I could not find any kind of TypeScript library that was built for this. Using a middleware layer gives you consistent coding standards when it comes to input validation, request guards (even though you can technically do this through the API gateway of Azure), and cleaner code logic.
I have found that developers like to stick to what they know. Since all the modern web frameworks use a middleware layer, I figured Azure Serverless functions should have one as well. Without all the hassle of generating adapters for popular frameworks like Express, Hapi, Koa, or Fastify.
The plugins library is housed in a seperate repository see all available plugins here.
See the examples for further examples on how to use @polarpop/azure-middleware
import { HttpRequest } from '@azure/functions';
import azureMiddleware from '@polarpop/azure-middleware';
const app = azureMiddleware<HttpRequest>();
app.use((context, req) => {
context.log.info(`Logging Request Information`);
if (req.method === 'POST') {
context.log.info(`${JSON.stringify(req.body)}`);
}
context.next();
});
app.use((context, req) => {
if (req.method === 'POST') {
context.res = {
status: 200,
body: {
message: `We have logged the request`,
},
};
}
context.end();
});
export default app.listen();
You will need to install the plugins library you want.
npm install --save @polarpop/azure-middleware-multipart
import { HttpRequest } from '@azure/functions';
import multipart from '@polarpop/azure-middleware-multipart';
import azureMiddleware from '@polarpop/azure-middleware';
const app = azureMiddleware<HttpRequest>();
app.use(multipart());
app.use((context, req) => {
if (context.fields) {
context.res = {
status: 200,
body: {
fields: JSON.stringify(context.fields),
},
};
}
context.done();
});
export default app.listen();
import azureMiddleware from '@polarpop/azure-middleware';
const app = azureMiddleware();
interface EventInstance {
id: string;
}
app.use<EventInstance>((context, input) => {
if (input.id === 'my-user-id') {
context.next(undefined, { id: input.id });
}
context.next();
});
export default app.listen();
You can also use multiple input triggers
import { HttpRequest } from '@azure/functions';
import multipart from '@polarpop/azure-middleware-multipart';
import azureMiddleware from '@polarpop/azure-middleware';
const app = azureMiddleware<HttpRequest>();
app.use(multipart());
app.use((context, req) => {
if (context.multipart.fields) {
context.res = {
status: 200,
body: {
fields: JSON.stringify(context.fields),
},
};
context.done();
} else {
context.next();
}
});
interface EventInstance {
id: string;
}
app.use<EventInstance>((context, input) => {
if (input.id) {
context.log.info(`This was a event instead of http request.`);
context.done();
}
});
export default app.listen();
Generated using TypeDoc