Nuxt Webhook 検証
様々なサービスからの受信Webhookをエッジで簡単に検証できるシンプルなNuxtモジュールです。
機能
- 12 Webhook 検証
- エッジで動作
- 公開されている サーバーユーティリティ
要件
このモジュールは、サーバーAPIルート(nuxt build
)を使用するため、Nuxtサーバーが実行されている場合にのみ動作します。
つまり、nuxt generate
ではこのモジュールを使用できません。
クイックセットアップ
- Nuxtプロジェクトにnuxt-webhook-validatorsを追加します
npx nuxi@latest module add webhook-validators
nuxt.config.ts
にモジュールを追加します
export default defineNuxtConfig({
modules: [
'nuxt-webhook-validators'
],
})
サーバーユーティリティ
バリデーターヘルパーはserver/
ディレクトリに自動的にインポートされます。
Webhook 検証
すべてのバリデーターヘルパーはグローバルに公開されており、サーバーAPIルートで使用できます。
ヘルパーは、Webhookリクエストが有効かどうかを示すブール値を返します。
設定はnuxt.config.ts
のruntimeConfig
から直接定義できます
export default defineNuxtConfig({
runtimeConfig: {
webhook: {
<provider>: {
<requiredProps>: '',
}
}
}
})
環境変数を使用して設定することもできます
NUXT_WEBHOOK_<PROVIDER>_<REQUIRED_PROPERTY> = ""
playground/.env.example または playground/nuxt.config.ts にアクセスして、各プロバイダーに必要なすべて利用可能なプロパティのリストを確認してください。
サポートされているWebhook検証
- Discord
- Dropbox
- GitHub
- Heroku
- Hygraph
- Meta
- NuxtHub
- Paddle
- PayPal
- Polar
- Stripe
- Twitch
src/runtime/server/lib/validators/に新しいファイルを作成することで、お気に入りのWebhookバリデーターを追加できます
例
サーバーAPIルートでGitHub Webhookを検証します。
~/server/api/webhooks/github.post.ts
export default defineEventHandler(async (event) => {
const isValidWebhook = await isValidGitHubWebhook(event)
if (!isValidWebhook) {
throw createError({ statusCode: 401, message: 'Unauthorized: webhook is not valid' })
}
// Some logic...
return { isValidWebhook }
})
開発
# Install dependencies
npm install
# Generate type stubs
npm run dev:prepare
# Develop with the playground
npm run dev
# Build the playground
npm run dev:build
# Run ESLint
npm run lint
# Run Vitest
npm run test
npm run test:watch
# Run typecheck
npm run test:types
# Release new version
npm run release