npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@suin/esa-webhook-router

v2.0.0

Published

esa.io Webhookのペイロードをルーティングする

Downloads

13

Readme

@suin/esa-webhook-router

esa.io の Generic Webhookを受信し、ペイロードのkindごとにルーティングするルーター。

特徴

  • 現在、以下のペイロードに対応しています。
    • 記事作成時(kind: "post_create")
    • 記事更新時(kind: "post_update")
    • 記事アーカイブ時(kind: "post_archive")
    • 記事削除時(kind: "post_delete")
    • 記事復旧時(kind: "post_restore")
  • リクエストボディ改ざん防止のためのX-Esa-Signatureに対応しています。

動作要件

  • Node.js: 18.x 以上

インストール

yarn add @suin/esa-webhook-router
# or
npm install @suin/esa-webhook-router

使い方

基本的な用法

import { createRouter } from "@suin/esa-webhook-router";
// `createRouter`には、イベントの種類ごとに実行するハンドラー関数を渡します。
const route = createRouter({
  post_create: ({ event }) => console.log("投稿が作成されました。", event),
  post_update: ({ event }) => console.log("投稿が更新されました。", event),
  post_archive: ({ event }) =>
    console.log("投稿がアーカイブされました。", event),
  post_delete: ({ event }) => console.log("投稿が削除されました。", event),
});
// `createRouter`の戻り値は関数なので、それにHTTPリクエストを渡すと、ハンドラーが実行されます。
await route(request);

AWS LambdaやNetlify Functionで使う

createRouterで作成した関数には、APIGatewayProxyEventを渡すことができるので、AWS LambdaやNetlify Functionでもこのライブラリを用いることができるはずです。動作確認はしてません。

X-Esa-Signatureをあつかう

esa Webhookにはリクエストボディーの署名がX-Esa-Signatureヘッダについています。この署名の検証を行うには次の方法のどれかで、シークレットを渡してください。

  • 環境変数ESA_WEBHOOK_SECRETをセットする。
  • createRoutersecretオプションをセットする。

どちらも設定されている場合は、secretオプションが優先されます。

使い方をもっと学ぶ

より詳しい使い方については、usage.spec.tsをご覧ください。

動作デモ

いろいろな環境での動作デモを用意してあります。

  • ローカル環境での動作デモ: ローカル環境でHTTPサーバーを起動し、esa Webhookを受け取れるようにします。グローバルのURLはCloudflare Tunnelを使って作成しますので、実際のesaチームと結合して動作させることができます。起動するには、このリポジトリをgit cloneし、yarn install && yarn tsx local/main.tsを実行してください。
  • Vercel Functionsでの動作デモ: Vercel Functionsにデプロイして、esa Webhookを受け取れるようにします。実際のesaチームと結合して動作させることができます。ローカルで実行するには、このリポジトリをgit cloneし、yarn install && npx vercel devを実行してください。

API リファレンス

https://suin.github.io/esa-webhook-router/