本文へ移動

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 ライセンスで公開されています。