Tink.WebhookHandler (Tink v1.0.0)

Copy Markdown View Source

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

clear_handlers(event_type)

@spec clear_handlers(String.t()) :: :ok

Clear all handlers for an event type. Useful in tests.

dispatch(payload)

@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.

get_handlers()

@spec get_handlers() :: map()

Get all registered handlers (map of event_type => [handler_fn]).

get_handlers(event_type)

@spec get_handlers(String.t()) :: list()

Get handlers for a specific event type.

handle(event_type, fun)

@spec handle(String.t(), (map() -> any())) :: :ok

Register a handler function for an event type. Can be called multiple times to add multiple handlers.

known_events()

@spec known_events() :: [String.t()]

List all known Tink webhook event types.