Socket
Book a DemoInstallSign in
Socket

@wroud/flow-middleware

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wroud/flow-middleware

A lightweight middleware management library for JavaScript and TypeScript, facilitating middleware chains with re-runs, error handling, and disposability.

latest
npmnpm
Version
0.8.2
Version published
Maintainers
1
Created
Source

@wroud/flow-middleware

ESM-only package NPM version

@wroud/flow-middleware is a lightweight middleware management library for JavaScript and TypeScript. It facilitates the creation and execution of middleware chains with support for re-runs, error handling, and disposability. Inspired by modern middleware patterns, it leverages TypeScript for type safety and ESM for optimal performance.

Features

  • Modern JavaScript: Utilizes ES modules and ESNext syntax for advanced performance optimizations.
  • TypeScript: Written in TypeScript for type safety and enhanced developer experience.
  • Middleware Chains: Easily create and manage middleware chains with support for asynchronous operations.
  • Re-run Capabilities: Middlewares can trigger re-execution of the middleware chain based on external events.
  • Error Handling: Dedicated error-handling middlewares to manage and respond to errors gracefully.
  • Subscription Management: Efficiently handle subscriptions with automatic cleanup to prevent memory leaks.
  • Disposability: Cleanly dispose of middleware requests and all associated subscriptions.

Installation

Install via npm:

npm install @wroud/flow-middleware

Install via yarn:

yarn add @wroud/flow-middleware

Documentation

For detailed usage and API reference, visit the documentation site.

Example

import { FlowMiddleware } from "@wroud/flow-middleware";
import type {
  IMiddleware,
  IErrorMiddleware,
} from "@wroud/flow-middleware/interfaces";

/**
 * Simple Middleware Example
 */
const simpleMiddleware: IMiddleware<{ message: string }> = async (
  req,
  next,
  triggerReRun,
  subscribe,
) => {
  console.log("Middleware: Processing message:", req.message);
  await next();
};

/**
 * Simple Error Middleware Example
 */
const simpleErrorMiddleware: IErrorMiddleware<{ message: string }> = async (
  error,
  req,
  next,
  triggerReRun,
  subscribe,
) => {
  console.error("ErrorMiddleware: An error occurred:", error.message);
  // Handle error, modify request data, or trigger re-run
  req.message = "Error handled";
  triggerReRun();
};

const middleware = new FlowMiddleware();

// Register middlewares
middleware.register(simpleMiddleware);
middleware.registerErrorMiddleware(simpleErrorMiddleware);

// Create a new request with initial data
const request = middleware.createRequest({ message: "Hello, FlowMiddleware!" });

// Execute the middleware chain
(async () => {
  try {
    await request.execute();
  } catch (error) {
    console.error("Main: Error executing middleware chain:", error);
  }
})();

Changelog

All notable changes to this project will be documented in the CHANGELOG file.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Keywords

middleware

FAQs

Package last updated on 22 Apr 2025

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