# @matthieugi/azurefunctionmiddleware

> Simple Middleware helper for Azure Function

Latest version **1.0.6** (published 2021-03-01) · GPL-3.0-or-later license · 0 weekly downloads

## Install

```sh
npm install @matthieugi/azurefunctionmiddleware
pnpm add @matthieugi/azurefunctionmiddleware
yarn add @matthieugi/azurefunctionmiddleware
bun add @matthieugi/azurefunctionmiddleware
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.6 |
| Published | 2021-03-01 |
| First published | 2021-02-15 |
| Weekly downloads | 0 |
| License | GPL-3.0-or-later |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 50.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Matthieu Girard |
| Maintainers | matthieugi |

## Links

- npm: https://www.npmjs.com/package/@matthieugi/azurefunctionmiddleware
- Repository: https://github.com/matthieugi/Azure-Function-Middleware
- Issues: https://github.com/matthieugi/Azure-Function-Middleware/issues
- npm.io page: https://npm.io/package/@matthieugi/azurefunctionmiddleware

## Dependencies (1)

- [@azure/functions](https://npm.io/package/@azure/functions.md) ^1.2.2

## Recent versions

- 1.0.6 (latest) — 2021-03-01
- 1.0.5 — 2021-02-16
- 1.0.4 — 2021-02-16
- 1.0.3 — 2021-02-16
- 1.0.2 — 2021-02-16
- 1.0.1 — 2021-02-15
- 1.0.0 — 2021-02-15

## README

# 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 

1. Install module


in your Auzre function project, link the module

```console
npm install @matthieugi/azurefunctionmiddleware
```
Then import it in your code

```javascript
import { AzureFunctionMiddleware, ContextMiddleware, Middleware } from 'azurefunctionmiddleware';
```

2. Initiate Middleware class

```javascript
const middleware = new Middleware();
```

3. 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.


```javascript
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

```javascript
middleware = middleware.use(logger);
middleware = middleware.use(myOtherMiddleware).use(myAzureFunction)
```

4. Return default handler 

Export default handler.

```javascript
export default middleware.listen();
```

## Other

Please create an issue if you have any questions or comment !

---
_Source: https://npm.io/package/@matthieugi/azurefunctionmiddleware · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
