webiny-event-handler-pattern

Implements Webiny EventHandlers, domain events, and event publishing with dependency injection.

8.0k|673|Updated Jan 9, 2018
One-click install
npx skills add https://github.com/webiny/webiny-js --skill webiny-event-handler-pattern
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: webiny-event-handler-pattern
Source: https://github.com/webiny/webiny-js/tree/main/skills/user-skills/api/event-handler-pattern
Command: npx skills add https://github.com/webiny/webiny-js --skill webiny-event-handler-pattern

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Developers extending Webiny's API need a consistent, correct way to react to lifecycle events (before/after create, update, delete) and to define their own domain events without scattering business logic across handlers. This Skill provides the exact implementation pattern, naming conventions, and DI wiring required to do it correctly.

Core Features & Use Cases

  • EventHandler Implementation: Build thin before/after event handlers that delegate to injected services or UseCases, with mandatory model filtering for CMS handlers.
  • Custom Domain Events: Define event payloads in abstractions.ts, event classes extending DomainEvent in events.ts, and publish them from UseCases via EventPublisher.
  • External Event Reactions: Implement handler abstractions from other packages (e.g., EntryAfterDeleteEventHandler) to react to CMS or platform events.
  • Use Case: When a CMS entry of model "order" is deleted, implement an EntryAfterDeleteEventHandler that filters by modelId and calls an injected CleanupService to remove related records.

Quick Start

Implement a Webiny EventHandler that reacts after a CMS entry is created for my custom model and delegates the work to an injected service.

Frequently Asked Questions about webiny-event-handler-pattern

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I implement a Webiny EventHandler for CMS entries?

Implement the handler abstraction's Interface with a single handle method, inject services via the constructor, and register with createImplementation listing dependencies. Always filter by model.modelId since CMS handlers fire for all models.

How to define and publish custom domain events in Webiny?

Define payload types and handler abstractions in abstractions.ts, create event classes extending DomainEvent in events.ts with an eventType and getHandlerAbstraction method, then publish them from a UseCase using the injected EventPublisher.

What is the difference between before and after event handlers in Webiny?

Before handlers fire before persistence and can mutate the payload or throw to reject the operation. After handlers fire after persistence, reflect the persisted state, and should only trigger side effects without mutating the payload.

Why does my Webiny extension fail to build when registering a handler?

Build failures occur when the Extension src prop omits the .ts file extension or when the file uses a named export instead of export default for the createImplementation call. Both are mandatory for files targeted directly by an Extension src.

Can a Webiny EventHandler contain business logic directly?

No, handlers must be thin orchestrators that delegate to an injected service or UseCase abstraction. Inline logic cannot be reused by other handlers, GraphQL resolvers, or CLI commands, so the real logic belongs in a dedicated feature.