Examples
Middleware Integration

Middleware Integration Example

This example demonstrates how to use middleware in ZoopFlow to add cross-cutting concerns and functionality to your flows and steps.

What You'll Learn

  • How to create and register middleware
  • How to use middleware for input/output transformation
  • How to implement cross-cutting concerns like logging, monitoring, and validation
  • How to chain multiple middleware together
  • How to implement specialized middleware like PII masking and encryption

Files in this Example

  • basic-middleware.ts - Shows the basic middleware pattern
  • pii-masking-middleware.ts - Implements a middleware for masking PII data
  • middleware-chain.ts - Demonstrates chaining multiple middleware together
  • tag-processing-middleware.ts - Shows tag-based processing middleware
  • run-example.ts - Entry point to run the example

Middleware Concepts

In ZoopFlow, middleware:

  • Intercepts execution before and/or after steps or flows
  • Can transform inputs and outputs
  • Implements cross-cutting concerns like logging, tracing, and validation
  • Enables advanced functionality like PII masking, encryption, and data transformation
  • Can be combined and chained for complex processing

How to Run

# From the project root
npm install
npx ts-node docs/new-structure/examples/middleware-integration/run-example.ts

Expected Output

The example will:

  1. Show middleware intercepting and modifying flow execution
  2. Demonstrate PII masking for sensitive data
  3. Show a chain of middleware applied in sequence
  4. Illustrate tag-based middleware processing

Output will show the transformations applied by each middleware.

Next Steps

  • Create your own custom middleware for specific use cases
  • Implement advanced middleware for data transformation or validation
  • Combine middleware with schema validation in the Schema Validation Example
  • Explore error handling in middleware with the Error Handling Example