跳至內容

Accepts 助手

Accepts 助手可以協助處理請求中的 Accept 標頭。

導入

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

accepts()

accepts() 函數會查看 Accept 標頭(例如 Accept-Encoding 和 Accept-Language),並返回適當的值。

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'

選項

必填 header: AcceptHeader

目標 accept 標頭。

必填 supports: string[]

應用程式支援的標頭值。

必填 default: string

預設值。

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

自訂比對函數。

以 MIT 許可證發布。