Skip to content

接受助手

¥Accepts Helper

Accepts Helper 有助于处理请求中的 Accept 标头。

¥Accepts Helper helps to handle Accept headers in the Requests.

导入

¥Import

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

accepts()

accepts() 函数查看 Accept 标头,例如 Accept-Encoding 和 Accept-Language,并返回正确的值。

¥The accepts() function looks at the Accept header, such as Accept-Encoding and Accept-Language, and returns the proper value.

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 type

AcceptHeader 类型的定义如下。

¥The definition of the AcceptHeader type is as follows.

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

选项

¥Options

required 标头:AcceptHeader

¥required header: AcceptHeader

目标接受标头。

¥The target accept header.

<徽章类型="danger" 文本="required" /> 支持:string[]

¥required supports: string[]

你的应用支持的标头值。

¥The header values which your application supports.

required 默认:string

¥required default: string

默认值。

¥The default values.

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

自定义匹配函数。

¥The custom match function.

Hono v4.7 中文网 - 粤ICP备13048890号