webhook-validators
nuxt-webhook-validators

一個 Nuxt 模組,可在邊緣執行,用於輕鬆驗證來自不同服務的傳入 Webhook。

webhook-validators

Nuxt Webhook 驗證器

npm versionnpm downloadsLicenseNuxtModules

一個簡單的 Nuxt 模組,可在邊緣執行,用於輕鬆驗證來自不同服務的傳入 Webhook。

功能

要求

此模組僅適用於執行 Nuxt 伺服器(使用伺服器 API 路由,即 nuxt build)的情況。

這意味著您不能將此模組與 nuxt generate 一起使用。

快速設定

  1. 在您的 Nuxt 專案中新增 nuxt-webhook-validators
npx nuxi@latest module add webhook-validators
  1. 在您的 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.exampleplayground/nuxt.config.ts 檢視每個提供商所需的所有可用屬性列表。

支援的 Webhook 驗證器

  • Brevo
  • Discord
  • Dropbox
  • Fourthwall
  • GitHub
  • GitLab
  • Heroku
  • Hygraph
  • Kick
  • MailChannels
  • Meta
  • NuxtHub
  • Paddle
  • PayPal
  • Polar
  • Resend
  • Shopify
  • Stripe
  • Svix
  • 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