@kevboutin/azure-middy-core
v0.2.14
Published
A Node.js middleware engine for Azure functions (core package)
Maintainers
Readme
Azure-Middy-Core
Install
To install middy you can use NPM:
npm install --save @kevboutin/azure-middy-coreUsage
The core middleware engine provides a powerful way to compose Azure Functions with middleware.
JavaScript (CommonJS)
const middy = require("@kevboutin/azure-middy-core");
const baseHandler = async (req, context) => {
return {
body: JSON.stringify({ message: "Success" }),
};
};
const handler = middy(baseHandler)
.use(loggingMiddleware)
.use(errorHandlingMiddleware);
module.exports = { handler };TypeScript
import middy, {
MiddyInstance,
Middleware,
Plugin,
} from "@kevboutin/azure-middy-core";
import type { AzureFunctionRequest } from "@kevboutin/azure-middy-types";
const baseHandler = async (req: any, context: any) => {
return {
body: JSON.stringify({ message: "Success" }),
};
};
const loggingMiddleware: Middleware = {
before: async (request: AzureFunctionRequest) => {
console.log("Request:", request.req.method, request.req.url);
},
after: async (request: AzureFunctionRequest) => {
console.log("Response:", request.response);
},
onError: async (request: AzureFunctionRequest) => {
console.error("Error:", request.error);
},
};
const monitoringPlugin: Plugin = {
requestStart: () => console.log("Request started"),
requestEnd: () => console.log("Request ended"),
};
const handler: MiddyInstance = middy(baseHandler, monitoringPlugin).use(
loggingMiddleware,
);
export { handler };TypeScript Support
This package includes full TypeScript support with:
- Type Definitions: Complete type definitions for all middleware interfaces and functions
- Type Safety: Full type checking for middleware, plugins, and request objects
- IntelliSense: Enhanced IDE support with autocomplete and type hints
Available Types
import {
MiddyInstance,
Middleware,
Plugin,
MiddlewareFunction,
BaseHandler,
} from "@kevboutin/azure-middy-core";
import type { AzureFunctionRequest } from "@kevboutin/azure-middy-types";TypeScript Configuration
To use TypeScript with this package, ensure your tsconfig.json includes:
{
"compilerOptions": {
"esModuleInterop": true,
"moduleResolution": "node"
}
}Documentation and examples
For more documentation and examples, refer to the main Azure-middy monorepo on GitHub.
Contributing
Everyone is very welcome to contribute to this repository. Feel free to raise issues or to submit Pull Requests.
License
Licensed under MIT License. Copyright (c) 2024 Kevin Boutin, and the Azure-Middy team.
