跳转到正文

Accepts 辅助工具 ​

Accepts 辅助工具用于处理请求中的 Accept 请求头。

导入 ​

ts
import { Hono } from 'hono'
import { accepts } from 'hono/accepts'

accepts() ​

accepts() 函数检查 Accept-Encoding、Accept-Language 等 Accept 请求头,并返回合适的值。

ts
import { accepts } from 'hono/accepts'

app.get('/', (c) => {
  const accept = accepts(c, {
    header: 'Accept-Language',
    supports: ['en', 'ja', 'zh'],
    default: 'en',
  })
  return c.json({ lang: accept })
})

AcceptHeader 类型 ​

AcceptHeader 类型的定义如下。

ts
export type AcceptHeader =
  | 'Accept'
  | 'Accept-Charset'
  | 'Accept-Encoding'
  | 'Accept-Language'
  | 'Accept-Patch'
  | 'Accept-Post'
  | 'Accept-Ranges'

选项 ​

required header: AcceptHeader ​

目标 Accept 请求头。

required supports: string[] ​

应用支持的请求头值。

required default: string ​

默认值。

optional match: (accepts: Accept[], config: acceptsConfig) => string ​

自定义匹配函数。

基于 MIT 许可证发布。