mcpField
H
hapi

hapi

github.com/damusix開発6239 回インストール1 週間前に更新
リポジトリ

hapi について

Hapi でサーバーアプリケーションを開発する人向けのリファレンススキルです。サーバー設定、ルート、認証、検証、ライフサイクルフック、プラグイン、TypeScript のパターンを扱います。

主な機能

  1. 01サーバー設定とプラグイン
  2. 02ルートハンドラーと検証
  3. 03三層認証パターン
  4. 04ライフサイクルと拡張フック
  5. 05TypeScript のルートとプラグインのパターン

活用シーン

  1. 01Hapi サーバーの起動
  2. 02検証付きルートの定義
  3. 03ルート認証の設定
  4. 04server.inject() によるリクエストテスト

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/damusix/skills --skill hapi

インストール後、Claude Code で/hapiを入力して読み込まれたか確認します。

同梱ファイル

SKILL.md と一緒にインストールされる参考資料やスクリプト。

.tessl-plugin/
plugin.json
reference/auth/
basic.md
reference/file-serving/
directory-handler.md
file-handler.md
overview.md
reference/jwt-auth/
overview.md
token-api.md
validate.md
reference/lifecycle/
boom.md
bounce.md
methods.md
overview.md
request-object.md
response-marshal.md
response-object.md
response-toolkit.md
reference/plugins/
overview.md
reference/route/
auth.md
cache-cors.md
handler.md
misc.md
overview.md
payload.md
pre.md
response.md
security.md
validation.md
reference/server/
auth.md
cache.md
catbox-fs.md

詳細

Hapi

クイックスタート

const server = Hapi.server({ port: 3000 });
server.route({ method: 'GET', path: '/', handler: () => 'ok' });
await server.start();

重要なルール

  1. デコレーションとメソッドで構成する - デコレーションでサービスを公開し、再利用可能なロジックはメソッドで公開します
  2. ライフサイクルに従う - リクエストは24段階のフローで処理されます。ライフサイクルの概要を参照してください
  3. 認証は3つのレイヤーで構成される - scheme → strategy → default。サーバー認証を参照してください
  4. ルートで検証する - params、query、payload、headers に joi スキーマを使用します。検証を参照してください
  5. 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
});

ワークフロー

  1. サーバーを作成する - コンストラクターのオプションについてはサーバーの概要を参照してください
  2. プラグインを登録する - プラグインとプラグインの構成を参照してください
  3. 認証を設定する - 認証スキームとルート認証を参照してください
  4. 認証を確認する - 保護されたルートを定義する前に、server.inject() を使ってテストしてください。ネットワークを参照してください
  5. ルートを定義する - ルートの概要とハンドラーを参照してください
  6. 拡張機能を追加する - ライフサイクルフックと前処理ハンドラーを参照してください

Key Patterns

TopicReference
Request/response objectsrequest, response
Response toolkit (h)toolkit
Sessions (yar)sessions
Caching & CORScache-cors, server cache, catbox-memory engine, catbox-fs engine, catbox-redis engine
Security headerssecurity
Payload parsingpayload
Decorations & methodsdecorations, methods
MIME types (mimos)mimos
Realms & plugin scopingrealm
Response marshallingmarshal pipeline
File serving (inert)overview, file handler, directory handler
Basic authenticationbasic auth
Error handling (Boom)boom errors
Error filtering (Bounce)bounce utility
WebSockets (nes)overview, subscriptions, client
SSE (sse)overview, api, subscriptions, session, replay
Startup & shutdownstartup lifecycle
Eventsevents
Testing (server.inject)network
TypeScript overviewtypescript
TypeScript auth typingauth-scheme, type-author
JWT authenticationjwt overview, validate function, token API
TypeScript pluginsplugin-scaffold
Views & templatesvision overview, engines, context & layouts

コメント