npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@azure-functions-middlewares/core

v1.1.0

Published

The most complete middleware solution for ⚡ Azure Functions.

Downloads

7

Readme

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