@fishka/express

中文文档
0.9.25发布于 2026/01/29MITTypeScriptESM + CJS近 30 天下载 6161

Express.js extension with built-in validation, and type safety

原始使用文档
文档目录20 节
  1. Installation
  2. Overview
  3. Quick Start
  4. Utilities
  5. pathParam(req, name, validator?)
  6. queryParam(req, name, validator?)
  7. body(req, validator)
  8. patchExpressAsyncErrors()
  9. addErrorHandling(app)
  10. Parameter Validation
  11. Available Operators
  12. Parameter Requirements
  13. Authentication
  14. Rate Limiting
  15. HTTP Errors
  16. Configuration
  17. Request ID
  18. Process Handlers
  19. Complete Example
  20. License

Express API

Functional utilities for Express.js with type-safe validation and error handling.

Installation

npm install @fishka/express

Overview

This package provides standalone utility functions that work directly with Express. Each feature can be used independently - mix and match what you need.

Quick Start

import express from 'express';
import { addErrorHandling, body, patchExpressAsyncErrors, pathParam, queryParam } from '@fishka/express';
import { assertString } from '@fishka/assertions';

const app = express();
app.use(express.json());

// Enable automatic async error catching (call once at startup)
patchExpressAsyncErrors();

// GET /users/:id - with typed path params
app.get('/users/:id', async (req, res) => {
  const id = pathParam(req, 'id'); // string - validated inline
  res.json({ id, name: 'John' });
});

// GET /users - with query params
app.get('/users', async (req, res) => {
  const page = queryParam(req, 'page'); // string - throws 400 if missing
  res.json({ page, users: [] });
});

// POST /users - with body validation
app.post('/users', async (req, res) => {
  const data = body(req, { name: assertString });
  res.json({ id: 1, name: data.name }); // validators infer a type for the data!
});

// Add error handling middleware (must be last)
addErrorHandling(app);

app.listen(3000);

Utilities

pathParam(req, name, validator?)

Get and validate a path parameter from Express request.

import { pathParam, transform, toInt, minLength } from '@fishka/express';

app.get('/users/:id', async (req, res) => {
  // Simple - returns string, throws 400 if missing
  const id1 = pathParam(req, 'id');

  // With validation - transform to number
  const id2 = pathParam(req, 'id', transform(toInt()));

  // Transform with validation - string min length
  const slug = pathParam(req, 'slug', transform(minLength(3)));

  res.json({ id: '...' });
});

queryParam(req, name, validator?)

Get and validate a query parameter from Express request.

import { queryParam, transform, toInt, min, range, oneOf } from '@fishka/express';

app.get('/users', async (req, res) => {
  // Simple - returns string, throws 400 if missing
  const search = queryParam(req, 'search');

  // With validation - transform to number with min value
  const page = queryParam(req, 'page', transform(toInt(), min(1)));

  // With validation - number range
  const limit = queryParam(req, 'limit', transform(toInt(), range(1, 100)));

  // With validation - enum
  const sort = queryParam(req, 'sort', transform(oneOf('asc', 'desc')));

  res.json({ page, limit, users: [] });
});

body(req, validator)

Get and validate the request body.

import { body } from '@fishka/express';
import { assertString, assertNumber } from '@fishka/assertions';

app.post('/users', async (req, res) => {
  // Object assertion - validates and infers a type.
  const data = body(req, {
    name: assertString,
    age: assertNumber,
  });

  res.json({ id: 1, name: data.name });
});

patchExpressAsyncErrors()

Patches Express to automatically catch async errors. Call once at application startup.

import { patchExpressAsyncErrors } from '@fishka/express';

// Call before defining routes
patchExpressAsyncErrors();

// Now async route handlers don't need try/catch
app.get('/users', async (req, res) => {
  const users = await db.users.findAll(); // Errors are caught automatically
  res.json(users);
});

addErrorHandling(app)

Adds centralized error handling middleware. Must be called after all routes.

import { addErrorHandling, HttpError } from '@fishka/express';

// Define routes...
app.get('/users/:id', async (req, res) => {
  const user = await db.users.findById(req.params.id);
  assertHttp(user, 404, 'User not found');
  res.json(user);
});

// Add error handling last
addErrorHandling(app);

Parameter Validation

Use transform() to validate and transform parameters. All operators are composable:

import { transform, toInt, minLength, matches, min, range, oneOf } from '@fishka/express';

app.get('/users/:id', async (req, res) => {
  const id = pathParam(req, 'id', transform(toInt())); // string → number
  const page = queryParam(req, 'page', transform(toInt(), min(1))); // number >= 1
  const limit = queryParam(req, 'limit', transform(toInt(), range(1, 100))); // number 1-100
  const sort = queryParam(req, 'sort', transform(oneOf('asc', 'desc'))); // enum
  const search = queryParam(req, 'search', transform(minLength(3))); // string min 3 chars

  res.json({ id, page, limit });
});

Available Operators

Transformations (string → T):

  • toInt() - parse to integer
  • toNumber() - parse to number
  • toBool() - parse 'true'/'false' to boolean
  • oneOf('a', 'b') - enum values

String validators:

  • minLength(n) - minimum length
  • maxLength(n) - maximum length
  • matches(/regex/) - regex match
  • trim - trim whitespace
  • lowercase / uppercase - case transform

Number validators:

  • min(n) - minimum value
  • max(n) - maximum value
  • range(min, max) - value range

Generic:

  • transform(...ops) - chain of validators/transformers
  • check(predicate, msg) - custom validation with predicate
  • validator(fn) - custom validator returning string|undefined
  • map(fn) - transform value

Parameter Requirements

  • pathParam(req, 'name') - returns string (throws 400 if missing)
  • queryParam(req, 'name') - returns string (throws 400 if missing/empty)
  • queryParam(req, 'name', validator) - returns validated value (throws 400 if missing/empty/invalid)
  • All parameters are required - missing or empty values throw BAD_REQUEST

Authentication

import { createAuthMiddleware, BasicAuthStrategy, getAuthUser } from '@fishka/express';

const auth = new BasicAuthStrategy(async (user, pass) =>
  user === 'admin' && pass === 'secret' ? { id: '1', role: 'admin' } : null,
);

app.get('/profile', createAuthMiddleware(auth), async (req, res) => {
  const user = getAuthUser(req);
  res.json({ id: user.id });
});

Rate Limiting

import { createRateLimiterMiddleware } from '@fishka/express';

app.use(
  await createRateLimiterMiddleware({
    points: { read: 100, write: 50 },
    duration: 60,
  }),
);

HTTP Errors

Use HttpError for specific status codes:

import { HttpError, HTTP_UNAUTHORIZED, HTTP_NOT_FOUND } from '@fishka/express';

app.get('/users/:id', async (req, res) => {
  const user = await db.users.findById(req.params.id);

  if (!req.headers.authorization) {
    throw new HttpError(HTTP_UNAUTHORIZED, 'Authorization required');
  }

  if (!user) {
    throw new HttpError(HTTP_NOT_FOUND, 'User not found');
  }

  res.json(user);
});

Use assertHttp for inline assertions:

import { assertHttp, HTTP_UNAUTHORIZED, HTTP_NOT_FOUND } from '@fishka/express';

app.get('/admin', async (req, res) => {
  assertHttp(req.headers.authorization, HTTP_UNAUTHORIZED, 'Authorization required');

  const user = await db.users.findById(req.params.id);
  assertHttp(user, HTTP_NOT_FOUND, 'User not found');
  assertHttp(user.isAdmin, HTTP_FORBIDDEN, 'Admin access required');

  res.json({ admin: true });
});

Configuration

Request ID

import { configureExpressApi, createTlsMiddleware } from '@fishka/express';

// Enable request ID with custom header name
configureExpressApi({
  requestIdHeader: 'x-request-id', // or 'x-correlation-id', 'trace-id', etc.
  trustRequestIdHeader: true, // default: true, if we trust and propagate request ID passed by client.
});

// Add TLS middleware to track request context
app.use(createTlsMiddleware());

Process Handlers

Handle uncaught errors and graceful shutdown:

import { installProcessHandlers } from '@fishka/express';

installProcessHandlers({
  onUncaughtException: err => sendToMonitoring(err),
  onUnhandledRejection: reason => sendToMonitoring(reason),
  onShutdown: async () => {
    await database.close();
    await server.close();
  },
  shutdownTimeout: 15000,
});

Complete Example

import express from 'express';
import {
  addErrorHandling,
  body,
  min,
  patchExpressAsyncErrors,
  pathParam,
  queryParam,
  range,
  toInt,
  transform,
} from '@fishka/express';
import { assertString } from '@fishka/assertions';
import { assertHttp } from './api.types';

const app = express();
app.use(express.json());

// Enable automatic async error catching
patchExpressAsyncErrors();

// Routes
app.get('/users/:id', async (req, res) => {
  const id = pathParam(req, 'id', transform(toInt()));
  const user = await db.users.findById(id);
  assertHttp(user, 404, 'User not found');
  res.json(user);
});

app.get('/users', async (req, res) => {
  const page = queryParam(req, 'page', transform(toInt(), min(1)));
  const limit = queryParam(req, 'limit', transform(toInt(), range(1, 100)));

  const users = await db.users.findAll({ page, limit });
  res.json({ users, page, limit });
});

app.post('/users', async (req, res) => {
  const data = body(req, { name: assertString });
  const user = await db.users.create(data);
  res.status(201).json(user);
});

// Error handling (must be last)
addErrorHandling(app);

app.listen(3000);

License

MIT

版本与发布记录

@fishka/express 共发布 21 个版本,首次发布于 2026/01/07;过去 12 个月发布了 21 个版本。

版本发布日期距今
0.9.25latest2026/01/299 个月前
0.9.242026/01/149 个月前
0.9.232026/01/149 个月前
0.9.222026/01/149 个月前
0.9.212026/01/149 个月前
0.9.202026/01/149 个月前

关于 @fishka/express 的常见问题

@fishka/express 是什么?

Express.js extension with built-in validation, and type safety

如何安装 @fishka/express?

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

@fishka/express 支持 TypeScript 吗?

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

@fishka/express 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@fishka/express 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@fishka/express 可以商用吗?

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

@fishka/express 的下载量有多少?

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

@fishka/express 还在维护吗?

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