@fastify/accepts

中文文档
5.0.7发布于 2026/10/09MITTypeScriptCommonJS构建来源已验证近 30 天下载 55.2万

Add accept parser to fastify

原始使用文档
文档目录5 节
  1. Install
  2. Compatibility
  3. Usage
  4. Options
  5. License

@fastify/accepts

CI NPM version neostandard javascript style

Add an accepts parser to Fastify.

Install

npm i @fastify/accepts

Compatibility

Plugin versionFastify version
>=5.x^5.x
^4.x^4.x
>=2.x <4.x^3.x
^1.x^2.x
^1.x^1.x

Please note that if a Fastify version is out of support, then so are the corresponding versions of this plugin in the table above. See Fastify's LTS policy for more details.

Usage

const fastify = require('fastify')
const Boom = require('@hapi/boom')

fastify.register(require('@fastify/accepts'))

fastify.post('/', function (req, reply) {
  const accept = req.accepts() // Accepts object
  switch(accept.type(['json', 'html'])) {
    case 'json':
      reply.type('application/json').send({hello: 'world'})
      break
    case 'html':
      reply.type('text/html').send('<b>hello, world!</b>')
      break
    default:
      reply.send(Boom.notAcceptable('unacceptable'))
      break
  }
})

See accepts package for all available APIs.

This plugin adds to Request object all Accepts object methods.

fastify.post('/', function (req, reply) {
  req.charset(['utf-8'])
  req.charsets()
  req.encoding(['gzip', 'compress'])
  req.encodings()
  req.language(['es', 'en'])
  req.languages()
  req.type(['image/png', 'image/tiff'])
  req.types()
})

Options

  • decorateReply If true, the Reply object will be decorated with the requestAccepts, requestTypes, requestCharsets, requestEncodings, and requestLanguages methods, which will return the corresponding values from the Request object. Default: false.

License

Licensed under MIT.

版本与发布记录

@fastify/accepts 共发布 16 个版本,首次发布于 2022/04/27;过去 12 个月发布了 4 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
5.0.7latest2026/10/091 个月前
5.0.62026/09/102 个月前
5.0.52026/09/052 个月前
5.0.42025/12/0111 个月前
5.0.32025/09/281.1 年前
5.0.22024/12/041.9 年前

其他发布通道:next → 5.0.0。

关于 @fastify/accepts 的常见问题

@fastify/accepts 是什么?

Add accept parser to fastify

如何安装 @fastify/accepts?

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

@fastify/accepts 支持 TypeScript 吗?

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

@fastify/accepts 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@fastify/accepts 可以商用吗?

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

@fastify/accepts 的下载量有多少?

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

@fastify/accepts 还在维护吗?

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