hapi について
Hapi でサーバーアプリケーションを開発する人向けのリファレンススキルです。サーバー設定、ルート、認証、検証、ライフサイクルフック、プラグイン、TypeScript のパターンを扱います。
主な機能
- 01サーバー設定とプラグイン
- 02ルートハンドラーと検証
- 03三層認証パターン
- 04ライフサイクルと拡張フック
- 05TypeScript のルートとプラグインのパターン
活用シーン
- 01Hapi サーバーの起動
- 02検証付きルートの定義
- 03ルート認証の設定
- 04server.inject() によるリクエストテスト
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ damusix/ skills --skill hapiインストール後、Claude Code で/hapiを入力して読み込まれたか確認します。
同梱ファイル
SKILL.md と一緒にインストールされる参考資料やスクリプト。
.tessl-plugin/plugin.jsonreference/auth/basic.mdreference/file-serving/directory-handler.mdfile-handler.mdoverview.mdreference/jwt-auth/overview.mdtoken-api.mdvalidate.mdreference/lifecycle/boom.mdbounce.mdmethods.mdoverview.mdrequest-object.mdresponse-marshal.mdresponse-object.mdresponse-toolkit.mdreference/plugins/overview.mdreference/route/auth.mdcache-cors.mdhandler.mdmisc.mdoverview.mdpayload.mdpre.mdresponse.mdsecurity.mdvalidation.mdreference/server/auth.mdcache.mdcatbox-fs.md詳細
Hapi
クイックスタート
const server = Hapi.server({ port: 3000 });
server.route({ method: 'GET', path: '/', handler: () => 'ok' });
await server.start();
重要なルール
- デコレーションとメソッドで構成する - デコレーションでサービスを公開し、再利用可能なロジックはメソッドで公開します
- ライフサイクルに従う - リクエストは24段階のフローで処理されます。ライフサイクルの概要を参照してください
- 認証は3つのレイヤーで構成される - scheme → strategy → default。サーバー認証を参照してください
- ルートで検証する - params、query、payload、headers に joi スキーマを使用します。検証を参照してください
- Refs でルートを型付けする -
ServerRoute<Refs>を使用し、必要なキー(Params、Query、Payload など)のみを指定します。省略したキーはデフォルトのままです。ルートのひな形を参照してください
認証の3層パターン:
server.auth.scheme('custom', schemeImpl); // 1. scheme(認証方法)
server.auth.strategy('session', 'custom', options); // 2. strategy(設定済みのインスタンス)
server.auth.default('session'); // 3. default(すべてのルートに適用)
Scheme の authenticate は、credentials と artifacts の両方を含む h.authenticated() を必ず返す必要があります:
return h.authenticated({
credentials: { user: { id, name }, scope: ['user'] },
artifacts: { token } // 生の認証データには常に artifacts を含める
});
ルート検証のパターン:
server.route({
method: 'POST',
path: '/users',
options: {
validate: {
payload: Joi.object({
name: Joi.string().required(),
email: Joi.string().email().required()
})
}
},
handler: (request) => request.payload
});
ワークフロー
- サーバーを作成する - コンストラクターのオプションについてはサーバーの概要を参照してください
- プラグインを登録する - プラグインとプラグインの構成を参照してください
- 認証を設定する - 認証スキームとルート認証を参照してください
- 認証を確認する - 保護されたルートを定義する前に、
server.inject()を使ってテストしてください。ネットワークを参照してください - ルートを定義する - ルートの概要とハンドラーを参照してください
- 拡張機能を追加する - ライフサイクルフックと前処理ハンドラーを参照してください
Key Patterns
| Topic | Reference |
|---|---|
| Request/response objects | request, response |
| Response toolkit (h) | toolkit |
| Sessions (yar) | sessions |
| Caching & CORS | cache-cors, server cache, catbox-memory engine, catbox-fs engine, catbox-redis engine |
| Security headers | security |
| Payload parsing | payload |
| Decorations & methods | decorations, methods |
| MIME types (mimos) | mimos |
| Realms & plugin scoping | realm |
| Response marshalling | marshal pipeline |
| File serving (inert) | overview, file handler, directory handler |
| Basic authentication | basic auth |
| Error handling (Boom) | boom errors |
| Error filtering (Bounce) | bounce utility |
| WebSockets (nes) | overview, subscriptions, client |
| SSE (sse) | overview, api, subscriptions, session, replay |
| Startup & shutdown | startup lifecycle |
| Events | events |
| Testing (server.inject) | network |
| TypeScript overview | typescript |
| TypeScript auth typing | auth-scheme, type-author |
| JWT authentication | jwt overview, validate function, token API |
| TypeScript plugins | plugin-scaffold |
| Views & templates | vision overview, engines, context & layouts |
コメント
Markdown 対応