Socket
Book a DemoInstallSign in
Socket

@polarpop/azure-middleware

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Package was removed
Sorry, it seems this package was removed from the registry

@polarpop/azure-middleware

Express like middleware for Azure Serverless Functions

0.0.3
unpublished
latest
Source
npmnpm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

npm type definitions Dependencies

@polarpop/azure-middleware

Express like middleware, but without the overhead of creating an express app for azure functions.

This library was heavily inspired by a JavaScript version of azure functions middleware, most of the credit goes to them Azure Middleware Engine. It is slimmed down compared to that, and is a fit for purpose at the moment.

Examples | Getting Started | Documentation

Installation

@polarpop/azure-middleware relies on Azure Serverless Functions by Microsoft. To get started with their sdk, please read the following before installing Node.JS SDK

With NPM

npm install @polarpop/azure-middleware --save

With Yarn

yarn add @polarpop/azure-middleware

Motivation

I could not find any kind of TypeScript library that was built for this. Using a middleware layer gives you consistent coding standards when it comes to input validation, request guards (even though you can technically do this through the API gateway of Azure), and cleaner code logic.


I have found that developers like to stick to what they know. Since all the modern web frameworks use a middleware layer, I figured Azure Serverless functions should have one as well. Without all the hassle of generating adapters for popular frameworks like Express, Hapi, Koa, or Fastify.

Plugins

The plugins library is housed in a seperate repository see all available plugins here.

Usage

See the examples for further examples on how to use @polarpop/azure-middleware

HTTP Trigger

import { HttpRequest } from '@azure/functions';
import azureMiddleware from '@polarpop/azure-middleware';

const app = azureMiddleware<HttpRequest>();

app.use((context, req) => {
  context.log.info(`Logging Request Information`);
  if (req.method === 'POST') {
    context.log.info(`${JSON.stringify(req.body)}`);
  }
  context.next();
});

app.use((context, req) => {
  if (req.method === 'POST') {
    context.res = {
      status: 200,
      body: {
        message: `We have logged the request`,
      },
    };
  }
  context.end();
});

export default app.listen();

HTTP Trigger with plugins

You will need to install the plugins library you want.

npm install --save @polarpop/azure-middleware-multipart
import { HttpRequest } from '@azure/functions';
import multipart from '@polarpop/azure-middleware-multipart';
import azureMiddleware from '@polarpop/azure-middleware';

const app = azureMiddleware<HttpRequest>();

app.use(multipart());

app.use((context, req) => {
  if (context.fields) {
    context.res = {
      status: 200,
      body: {
        fields: JSON.stringify(context.fields),
      },
    };
  }

  context.done();
});

export default app.listen();

EventGrid Trigger

import azureMiddleware from '@polarpop/azure-middleware';

const app = azureMiddleware();

interface EventInstance {
  id: string;
}

app.use<EventInstance>((context, input) => {
  if (input.id === 'my-user-id') {
    context.next(undefined, { id: input.id });
  }

  context.next();
});

export default app.listen();

Combined Triggers

You can also use multiple input triggers

import { HttpRequest } from '@azure/functions';
import multipart from '@polarpop/azure-middleware-multipart';
import azureMiddleware from '@polarpop/azure-middleware';

const app = azureMiddleware<HttpRequest>();

app.use(multipart());

app.use((context, req) => {
  if (context.multipart.fields) {
    context.res = {
      status: 200,
      body: {
        fields: JSON.stringify(context.fields),
      },
    };

    context.done();
  } else {
    context.next();
  }
});

interface EventInstance {
  id: string;
}

app.use<EventInstance>((context, input) => {
  if (input.id) {
    context.log.info(`This was a event instead of http request.`);
    context.done();
  }
});

export default app.listen();

Keywords

Azure Functions

FAQs

Package last updated on 27 Dec 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.