@effectionx/middleware

中文文档
0.1.1发布于 2026/03/23MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 5.9万

Type-safe composable middleware for generators and Effection operations

原始使用文档
文档目录5 节
  1. Quick Start
  2. Plain Functions
  3. API
  4. Middleware<TArgs, TReturn>
  5. combine(middlewares)

Middleware

Type-safe composable middleware for generators and Effection operations


Middleware lets you wrap any function with a chain of interceptors that can inspect arguments, transform return values, or short-circuit execution entirely. The combine() function composes an array of middleware into a single middleware, executing left-to-right.

Because middleware is generic over its return type, generator functions compose through combine() the same way plain functions do. This means you can build middleware pipelines where each layer sets up Effection resources, establishes context, or manages cleanup — and everything below it in the stack inherits that context automatically.

Quick Start

import { combine } from "@effectionx/middleware";
import type { Middleware } from "@effectionx/middleware";
import type { Operation } from "effection";
import { createContext } from "effection";

type Handler = Middleware<[Request], Operation<Response>>;

const DatabaseConnection = createContext<Connection>("database");

// Each middleware is a generator — it's still running while inner layers execute
const withDatabase: Handler = function* (args, next) {
  const conn = yield* connect(process.env.DATABASE_URL);
  yield* DatabaseConnection.set(conn);
  try {
    return yield* next(...args);
  } finally {
    yield* conn.close();
  }
};

const withTransaction: Handler = function* (args, next) {
  const conn = yield* DatabaseConnection.expect();
  const tx = yield* conn.begin();
  try {
    const response = yield* next(...args);
    yield* tx.commit();
    return response;
  } catch (error) {
    yield* tx.rollback();
    throw error;
  }
};

The core handler uses context set up by middleware — but never receives any of it as parameters:

function* handleRequest(request: Request): Operation<Response> {
  const conn = yield* DatabaseConnection.expect();
  const posts = yield* conn.query("SELECT * FROM posts");
  return Response.json(posts);
}

Compose it all together:

const handle = combine([withDatabase, withTransaction]);

function* processRequest(request: Request): Operation<Response> {
  return yield* handle([request], handleRequest);
}

When handleRequest returns (or throws, or is cancelled), the stack unwinds in reverse: withTransaction (commit or rollback) → withDatabase (close connection). Structured concurrency guarantees that no resources leak, even if the request is cancelled mid-flight.

Plain Functions

Middleware also works with plain synchronous functions — there's no Effection dependency. This is useful for argument validation, logging, or result transformation:

import { combine } from "@effectionx/middleware";
import type { Middleware } from "@effectionx/middleware";

const logger: Middleware<[number, number], number> = (args, next) => {
  console.log("adding", args);
  const result = next(...args);
  console.log("result", result);
  return result;
};

const doubler: Middleware<[number, number], number> = (args, next) =>
  next(...args) * 2;

const add = combine([logger, doubler]);

add([3, 4], (a, b) => a + b);
// adding [3, 4]
// result 14
// => 14

API

Middleware<TArgs, TReturn>

The type of a single middleware function. It receives the arguments as a tuple and a next function to delegate to the next middleware (or the core function).

import type { Middleware } from "@effectionx/middleware";

const timer: Middleware<[string], string> = (args, next) => {
  const start = performance.now();
  const result = next(...args);
  console.log(`took ${performance.now() - start}ms`);
  return result;
};

A middleware can:

  • Pass through: call next(...args) and return its result
  • Transform arguments: call next() with different arguments
  • Transform the return value: modify what next() returns
  • Short-circuit: return a value without calling next() at all

combine(middlewares)

Compose an array of middleware into a single middleware. Middlewares execute left-to-right: the first in the array runs outermost.

import { combine } from "@effectionx/middleware";

const composed = combine([logger, validator, retry]);
const result = composed(["hello"], coreFn);
// Execution: logger → validator → retry → coreFn

The returned value is itself a Middleware, so it can be nested inside other combine() calls or passed anywhere a middleware is expected.

版本与发布记录

@effectionx/middleware 共发布 2 个版本,首次发布于 2026/03/11;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.1.1latest2026/03/237 个月前
0.1.02026/03/118 个月前

关于 @effectionx/middleware 的常见问题

@effectionx/middleware 是什么?

Type-safe composable middleware for generators and Effection operations

如何安装 @effectionx/middleware?

在项目目录运行 npm install @effectionx/middleware,或使用 pnpm add / yarn add / bun add。

@effectionx/middleware 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@effectionx/middleware 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@effectionx/middleware 可以商用吗?

@effectionx/middleware 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@effectionx/middleware 的下载量有多少?

根据 npm 官方下载统计,@effectionx/middleware 在 2026/10/13 – 2026/11/11 这 30 天内被下载 59,172 次,日均约 1,972 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@effectionx/middleware 还在维护吗?

最新版本 0.1.1 发布于 2026/03/23(7 个月前);至今共发布 2 个版本,其中过去 12 个月发布了 2 个;首次发布于 2026/03/11。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。