0.2.2 • Published 3 years ago

@azure-functions-middlewares/jwt v0.2.2

Weekly downloads
18
License
MIT
Repository
github
Last release
3 years ago

azure-functions-middlewares

The most complete middleware solution for ⚡ Azure Functions.

npm bundle size npm Libraries.io dependency status for latest release

Features

How to use

Simplest usage

Install latest core version at NPM npm

$ npm install --save @azure-functions-middlewares/core

Listen to the cascade exporting it as the function entry point:

const FunctionCascade = require('@azure-functions-middlewares/core');

module.exports = new FunctionCascade()
  .use(async (context) => {
    context.res.body = { message: 'My first middleware!' };
  })
  .listen();

Tips!

  • Always call listen() at the end of cascade
  • Always use async functions as middlewares. This cascade module doesn't supports sync functions anymore, we are at 21th century.
  • Do not return anything inside your middleware function, unless you want to throw an error. Always use context.res to output what you need.

Capturing errors

Errors thrown or anything returned by a middleware will stop the cascade execution and will call cascade's catchFunction.

A default catchFunction is set to log the error at Azure Function's context and to return a HTTP 500 status.

If you want to catch function, use the catch() method:

const FunctionCascade = require('@azure-functions-middlewares/core');
const app = new FunctionCascade();

// app.use(...); // middlewares

app.catch((context, error) => {
  context.res.status = 404;
  context.res.headers['X-Message'] = error;
});

module.exports = app.listen();

Customizing execution order

This module executes the middleware cascade in three phases. You can customize the execution phase of your middleware by passing a phase argument to use() or useIf() methods:

app.use((context) => {
  context.log('This will be executed at the last phase');
}, app.Phases.POST_PROCESSING);

app.use((context) => {
  context.log('This will be executed at the first phase');
}, app.Phases.PRE_PROCESSING);

app.use((context) => {
  context.log('This will be executed at the second phase');
});

Phases constants to use as phase argument are exposed into cascade's property Phases:

app.Phases.PRE_PROCESSING => first phase
app.Phases.MAIN => second phase
app.Phases.POST_PROCESSING => last phase

Conditional middlewares

You can conditionally execute a middleware by passing an evaluation function to the useIf method.

Example:

const isPostRequest = (context) => context.req.method === 'POST';

app.useIf(isPostRequest, (context) => {
  context.log('This will be executed only if is a HTTP POST');
});

Stoping the cascade execution

You can stop the cascade execution at any time by returning the second middleware argument, STOP_SIGNAL.

Example:

app.useIf((context, STOP_SIGNAL) => {
  if (!req.query.access_token) {
    context.res.status = 401;

    return STOP_SIGNAL;
  }
});

License

GitHub

0.2.2

3 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago