webiny-use-case-pattern

Implements Webiny UseCases with DI, Result handling, decorators, and CMS repositories.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Building backend business logic in Webiny requires following a strict architectural pattern: single-method UseCase classes with dependency injection, typed Result-based error handling, and CMS-backed repositories. This Skill provides the exact patterns and rules so generated code compiles, registers correctly in the DI container, and handles errors consistently.

Core Features & Use Cases

  • UseCase Implementation: Create UseCase classes with execute methods returning Result<T, E>, registered via createImplementation with correctly ordered DI dependencies.
  • Error Handling: Define domain errors extending BaseError, typed error unions via IErrors interfaces, and proper Result.ok/Result.fail flows.
  • CMS Repositories & Decorators: Build repositories that persist data through CMS entry use cases, map entries to domain entities, and add cross-cutting concerns (authorization, logging) via decorators.
  • Use Case: When adding a new feature like "CreateEntity" to a Webiny extension, generate the abstractions, use case, repository, mapper, and feature registration following the framework's conventions.

Quick Start

Implement a new Webiny UseCase with a CMS-backed repository and authorization decorator for my entity feature.

Frequently Asked Questions about webiny-use-case-pattern

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

FAQPage Schema
How do I create a UseCase in Webiny?

Define an interface with an execute method returning Result<T, E>, create an abstraction with createAbstraction, then implement the class and export it as default via createImplementation with its dependencies array matching the constructor parameter order.

How do I override a default Webiny UseCase implementation?

Register your own class implementing the same UseCase interface using SomeUseCase.createImplementation with your custom logic. Your implementation replaces the default one in the DI container when registered through an Api.Extension src file.

How do Webiny UseCase decorators work?

A decorator implements the same interface as the use case it wraps, taking extra dependencies first and the decoratee last in the constructor. Register it with container.registerDecorator() inside a createFeature register function, not container.register().

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

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.

How do Webiny repositories persist data through the CMS?

Repositories resolve the CMS model first via GetModelUseCase, then call entry use cases like CreateEntryUseCase or UpdateEntryUseCase. CMS errors are wrapped in domain-specific errors, and repositories are registered in singleton scope.

What Result methods are available in the Webiny API?

Use Result.ok(value) for success, Result.fail(error) for failure, result.isFail() to check status, and result.value or result.error to access contents. Methods like isError(), getError(), and getValue() do not exist.