Event-driven webhook handler with a per-event-type handler registry.
Handlers are registered at startup and called when a matching event is dispatched. Exceptions in handlers are caught and logged without crashing the dispatcher.
Setup
# In your Application.start/2:
Tink.WebhookHandler.handle("account.updated", fn event ->
MyApp.Accounts.sync(event["content"])
end)
Tink.WebhookHandler.handle("transaction.created", fn event ->
MyApp.Transactions.process(event["content"])
end)
Tink.WebhookHandler.handle("consent.revoked", fn event ->
MyApp.Consents.on_revoked(event["content"])
end)Dispatching
# In your webhook controller after verifying the signature:
{:ok, event} = Jason.decode(raw_body)
Tink.WebhookHandler.dispatch(event)Known event types
Tink.WebhookHandler.known_events()
# => ["account.updated", "credentials.updated", ...]
Summary
Functions
Clear all handlers for an event type. Useful in tests.
Dispatch a parsed webhook event map to all registered handlers.
Get all registered handlers (map of event_type => [handler_fn]).
Get handlers for a specific event type.
Register a handler function for an event type. Can be called multiple times to add multiple handlers.
List all known Tink webhook event types.
Functions
@spec clear_handlers(String.t()) :: :ok
Clear all handlers for an event type. Useful in tests.
@spec dispatch(map()) :: :ok
Dispatch a parsed webhook event map to all registered handlers.
Returns :ok even if no handlers are registered.
Handler exceptions are caught and logged.
@spec get_handlers() :: map()
Get all registered handlers (map of event_type => [handler_fn]).
Get handlers for a specific event type.
Register a handler function for an event type. Can be called multiple times to add multiple handlers.
@spec known_events() :: [String.t()]
List all known Tink webhook event types.