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

目标接受标头。

🌐 The target accept header.

required 支持:string[]

你的应用支持的标头值。

🌐 The header values which your application supports.

required 默认: string

默认值。

🌐 The default values.

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

自定义匹配函数。

🌐 The custom match function.

Hono 中文网 - 粤ICP备13048890号