Skip to content

Webhook Middleware

The Webhook Middleware is built-in middleware that gives you capability to send the envelope to webhook(s).

How to use it

As for any Middleware, you can use it by adding it to the bus instance.

const commandBus = createCommandBus<CommandHandlerRegistry>();
commandBus.useWebhookMiddleware({
async: true,
parallel: true,
maxAttempts: 3,
jitter: 0.5,
multiplier: 1.5,
waitingAlgorithm: 'exponential',
fetcher: fetch,
intents: {
createUser: {
async: false,
parallel: false,
jitter: 0.25,
endpoints: [
{
url: 'https://webhook.site/c351ab7a-c4cc-4270-9872-48a2d4f67ea4',
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
signatureHeader: 'X-Plop-Signature',
signature: (payload) => 'signature',
},
],
},
},
});

Explanation

The Webhook middleware is simply taking the envelope and send it to the confifured Webhooks. It includes a retry mechanism in case of failure, between each attempt, the middleware is going to wait for a certain amount of time. The waitingAlgorithm can be exponential, fibonacci, or none.

  • jitter is a value between 0 and 1 that will add some randomness to the waiting time.
  • multiplier is the factor to multiply the waiting time between each attempt. (only used for exponential)

Added Stamps

The Webhook Middleware is going to add:

  • type WebhookCalledStamp = Stamp<{ attempt: number; text?: string, status?: number }, 'missive:webhook-called'>;

    Every time the middleware succeed to call the webhook or ultimately at the end of the retries.

Going further

Look at the code of the Webhook Middleware


Missive.js. MIT License.
Powered by Astro Starlight.
Inspired by Symfony Messenger