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

@frudisch/azure-function-middleware

v0.0.5

Published

Middleware for azure functions to handle authentication, authorization, error handling and logging

Downloads

7

Readme

AZure FUnction MIddleware

AzFuMi provides a simple way to use the middleware pattern for Azure Functions with NodeJS in order to define cross-cutting functionality such as schema validation, authorization and error handling.

Usage

The interface is simple to use and could be easily expanded:

const schema = Joi.object().keys({
    name: Joi.string().min(3).max(30).required(),
});

const functionHandler = async (context: Context, req: HttpRequest): Promise<void> => {
    context.res = { status: 201 };
};

export default middleware(functionHandler, [validation(schema)]);

The goal is to provide a minimal feature set with generic functionality and an easy interface to create other middleware functions, which could be used for an Azure function.

Error Handling

The middleware provides a central way to handle errors occurring in the control flow of the function. Every error thrown within the function gets caught by the middleware and processed. To define the correct response to a specific error the following structure can be thrown:

export class ApplicationError<T> extends Error {
    status: number;
    body?: T;
}

Any error thrown in the function with this signature is getting returned to the caller with the defined status and body.

Generic Functions

For additional generic functionality like request validation or authorization functions could be defined. The functions need to have the following structure:

export type MiddlewareFunction = (context: Context, request: HttpRequest) => Promise<void>;

The function receives the Azure Function context and request to operate. The function needs to be passed when the middleware is configured.

export default middleware(functionHandler, [validation(schema)]);

In the above example a validation function is passed with a schema. All passed functions are called before the in the defined order before the handler function containing the logic for the request is called.

Validation

The function to validate requests is based on Joi. The usage is fairly simply:

export default middleware(functionHandler, [validation(schema)]);

The passed schema is a Joi ObjectSchema to check the passed request against. When the request is valid against the schema, the next middleware function gets called. In case the check of the request against the schema is invalid, the middleware function throws an error, canceling the request and returning aan 400 - Bad Request with the Joi error message.

The body of the response could be customized by adding a transformer like in the following example. The passed message is the Joi error message.

export default middleware(handler, [
  validation(accountWithConnectionRequestSchema(), (message) => ({
    type: 'Invalid  request object',
    detail: message,
  })),
])

Authorization

To authorize a request the middleware function authorization could be used. The function is verifying a request parameter against a JWT Bearer Token. The information get extracted using two functions for the correct parameter and token counterpart.

export default middleware(functionHandler, [authorization([])]);

The passed values in the array needs to be defined based on the following structure:

export type Rule<T> = {
    parameterExtractor: (parameters: ContextBindingData) => string;
    jwtExtractor: (jwt: T) => string;
};