Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@mellkam/middleware

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@mellkam/middleware

Universal and lightweight middleware utility

  • 0.1.2
  • unpublished
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

Introduction

This TypeScript library provides a simple implementation of middleware pattern which can be used to wrap or augment functions with additional functionality without modifying their original implementation.

Usage

  1. Creating a Middleware object:
const myFunction = (name: string, age: number) => {
  console.log(`${name} is ${age} years old.`);
};

const myMiddleware = middleware(myFunction)
  .use((args, next) => {
    console.log(`Before function execution`);
    return next(...args);
    console.log(`After function execution`);
  }).run;

myMiddleware("Alice", 25);

// Before function execution
// Alice is 25 years old.
// After function execution

Before/after concept

const mv: MiddlewareFn = (args, next) => {
  //
  // BEFORE
  //

  const result = next(args);

  //
  // AFTER
  //

  return result;
};

Execution order (LIFO)

Since everything is a middleware, the order of execution is important.

Middlewares are executed in LIFO order ("Last In, First Out").

Everytime you push a new middleware to the stack, it is added as a new onion layer on top of all existing ones.

Example

middleware((a, b) => {
  console.log("Actual function logic");
  return a + b;
})
  .use((args, next) => {
    console.log("A: Before logic");

    const result = next(...args);

    console.log("A: After logic");
    return result;
  })
  .use((args, next) => {
    console.log("B: Before logic");

    const result = next(...args);

    console.log("B: After logic");
    return result;
  })
  .run(10, 5);

Execution order:

  1. B Before logic
  2. A Before logic
  3. Actual function logic
  4. A After logic
  5. B After logic

Fetch example

const modifiedFetch = middleware(fetch).use(async (args, next) => {
  const res = await next(...args);
  if (res.ok) {
    return res;
  }

  throw new Error(await res.text());
}).run;

console.log(await modifiedFetch("https://example.com"));

Keywords

FAQs

Package last updated on 29 Apr 2023

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc