@dotcom-reliability-kit/serialize-request

中文文档
5.0.2发布于 2026/11/11MITTypeScriptES Module零依赖近 30 天下载 4411

A utility function to serialize a request object in a way that's friendly to loggers, view engines, and converting to JSON

原始使用文档
文档目录7 节
  1. Usage
  2. serializeRequest
  3. Configuration options
  4. SerializedRequest type
  5. Migrating
  6. Contributing
  7. License

@dotcom-reliability-kit/serialize-request

A utility function to serialize a request object (Express or IncomingMessage) in a way that's friendly to loggers, view engines, and converting to JSON. This module is part of FT.com Reliability Kit.

Usage

Install @dotcom-reliability-kit/serialize-request as a dependency:

npm install --save @dotcom-reliability-kit/serialize-request

Include in your code:

import serializeRequest from '@dotcom-reliability-kit/serialize-request';

[!TIP] If you're using this package with TypeScript, we recommend using the following settings in your tsconfig.json file to avoid type errors:

{
    "esModuleInterop": true,
    "module": "nodenext",
    "moduleResolution": "nodenext"
}

serializeRequest

The serializeRequest function accepts a request-like object (e.g. an instance of Express.Request or an object with method and url properties) and returns a plain JavaScript object (conforming to the SerializedRequest type) which contains the relevant properties. The example below assumes that app is an Express application:

app.get('/fruit/:fruitId', (request, response, next) => {
    console.log(serializeRequest(request));
    next();
});
// {
//     id: 'request137',
//     method: 'GET',
//     url: '/fruit/feijoa',
//     headers: {
//         accept: '*/*'
//     },
//     route: {
//         path: '/fruit/:fruitId',
//         params: { fruitId: 'feijoa' }
//     }
// }

You can also pass in a plain object if you already have one that looks like a request:

serializeRequest({
    method: 'get',
    url: '/hello'
});

Configuration options

Config options can be passed into the serializeRequest function as a second argument. It expects an object with any of the keys below.

serializeRequest(request, {
    // Config options go here
});

options.includeHeaders

An array of request headers to include in the serialized request object. This must be an Array of Strings, with each string being a header name. It's important that you do not include headers which include personally-identifiable-information, API keys, or other privileged information.

This option defaults to:

[
    'accept',
    'accept-encoding',
    'accept-language',
    'content-type',
    'referer',
    'user-agent'
]

Example of usage:

serializeRequest(request, {
    includeHeaders: [
        'accept',
        'content-length',
        'content-type',
        'user-agent'
    ]
});

The default set of headers is also available to use, so that you don't need to repeat them if you want to add new included headers. It can be accessed as DEFAULT_INCLUDED_HEADERS:

import serializeRequest, { DEFAULT_INCLUDED_HEADERS } from '@dotcom-reliability-kit/serialize-request';

serializeRequest(request, {
    includeHeaders: [
        ...DEFAULT_INCLUDED_HEADERS,
        'my-custom-header'
    ]
});

SerializedRequest type

The SerializedRequest type documents the return value of the serializeRequest function. It will have the following properties, extracting them from a given request object.

SerializedRequest.id

This is extracted from the request.headers['x-request-id'] property and is always cast to a String. It defaults to null.

SerializedRequest.method

This is extracted from the request.method property and is always cast to a String and switched to uppercase. It defaults to "-".

SerializedRequest.url

This is extracted from the request.url property and is always cast to a String. It defaults to "/".

SerializedRequest.headers

This is extracted from the request.headers property and is filtered to only include the headers specified in the includeHeaders option. It defaults to an empty object.

SerializedRequest.route

This is an object extracted from request.route.path (string) and request.params (object) if they are present and conform to the same properties on an Express Request object. It defaults to undefined.

Migrating

Consult the Migration Guide if you're trying to migrate to a later major version of this package.

Contributing

See the central contributing guide for Reliability Kit.

License

Licensed under the MIT license.
Copyright © 2022, The Financial Times Ltd.

版本与发布记录

@dotcom-reliability-kit/serialize-request 共发布 25 个版本,首次发布于 2022/05/25;过去 12 个月发布了 4 个版本。

版本发布日期距今
5.0.2latest2026/11/112 天前
5.0.12026/07/144 个月前
5.0.02026/06/234 个月前
5.0.0-beta.1prerelease2026/06/035 个月前
4.2.02025/10/291 年前
4.1.02025/05/081.5 年前

关于 @dotcom-reliability-kit/serialize-request 的常见问题

@dotcom-reliability-kit/serialize-request 是什么?

A utility function to serialize a request object in a way that's friendly to loggers, view engines, and converting to JSON

如何安装 @dotcom-reliability-kit/serialize-request?

在项目目录运行 npm install @dotcom-reliability-kit/serialize-request,或使用 pnpm add / yarn add / bun add。

@dotcom-reliability-kit/serialize-request 支持 TypeScript 吗?

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

@dotcom-reliability-kit/serialize-request 支持 ESM 还是 CommonJS?

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

@dotcom-reliability-kit/serialize-request 需要什么版本的 Node.js?

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

@dotcom-reliability-kit/serialize-request 可以商用吗?

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

@dotcom-reliability-kit/serialize-request 的下载量有多少?

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

@dotcom-reliability-kit/serialize-request 还在维护吗?

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