webiny-use-case-pattern
What it does
This skill guides your AI when writing one business operation in a Webiny backend. It is for developers building or overriding use cases and the repositories behind them.
When to use it
- ✓When you need a new business operation, such as creating or publishing a record.
- ✓When you want to replace or decorate an existing use case.
- ✓When a use case must save data through the CMS repositories.
Skill link
https://github.com/webiny/webiny-js/blob/next/skills/user-skills/api/use-case-pattern/SKILL.mdCopy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.
How to use
The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.
Ready-to-paste message
For Claude Code, Codex or Cursor: run it in your project folder and the skill gets installed.
Download the SKILL.md and drop it into your agent’s skills folder.
Example of what to ask your AI
“Write a use case that publishes a CMS entry and returns a clear error if it is already published.”
5 new skills a week. 1 minute to read.
How it works
The skill explains the single execute method, typed inputs, results that report success or failure, and error types that include a code. It also shows how use cases are injected into other code.
Technical content (original SKILL.md)
Original text by the author, straight from GitHub.
UseCase Pattern
What It Is
A UseCase is a single-method orchestrator that encapsulates one business operation (e.g., CreateTenantUseCase, PublishEntryUseCase). Each UseCase is a DI abstraction with an execute method that returns Result<T, E>.
Interface Shape
interface SomeUseCase.Interface {
execute(input: Input): Promise<Result<ReturnType, ErrorType>>;
}
- Input — a typed object specific to the use case
- Result — always returns
Result<T, E>fromwebiny/api - Error — extends
BaseErrorwith a uniquecode
How to Use a UseCase
UseCases are injected as dependencies into EventHandlers, other UseCases, or GraphQL resolvers via DI.
import { SomeUseCase } from "webiny/api/<category>";
import { SomeEventHandler } from "webiny/api/<category>";
class MyHandler implements SomeEventHandler.Interface {
constructor(private someUseCase: SomeUseCase.Interface) {}
async handle(event: SomeEventHandler.Event) {
const result = await this.someUseCase.execute({/* input */});
if (result.isFail()) {
console.error(result.error.message);
return;
}
const value = result.value;
// ... use value
}
}
export default SomeEventHandler.createImplementation({
implementation: MyHandler,
dependencies: [SomeUseCase]
});
How to Override a UseCase
To replace the default implementation, register your own:
import { SomeUseCase } from "webiny/api/<category>";
class CustomImplementation implements SomeUseCase.Interface {
async execute(input) {
// Custom logic
return Result.ok(/* ... */);
}
}
export default SomeUseCase.createImplementation({
implementation: CustomImplementation,
dependencies: []
});
Registration
YOU MUST include the full file path with the .ts extension in the src prop. For example, use src={"@/extensions/my-extension.ts"}, NOT src={"@/extensions/my-extension"}. Omitting the file extension will cause a build failure.
YOU MUST use export default for the createImplementation() call when the file is targeted directly by an Extension src prop. Using a named export (export const Foo = SomeFactory.createImplementation(...)) will cause a build failure. Named exports are only valid inside files registered via createFeature.
// In your app's configuration
<Api.Extension src={"@/extensions/my-extension.ts"} />
Deploy with: yarn webiny deploy api --env=dev
Error Handling Pattern
Domain-Specific Errors
Every feature defines errors extending BaseError. Never use generic Error for validation or business rule failures.
// domain/errors.ts
import { BaseError } from "webiny/api";
export class EntityNotFoundError extends BaseError {
override readonly code = "Entity/NotFound" as const;
constructor(id: string) {
super({ message: `Entity with id "${id}" was not found!` });
}
}
export class EntityPersistenceError extends BaseError<{ error: Error }> {
override readonly code = "Entity/Persist" as const;
constructor(error: Error) {
super({ message: error.message, data: { error } });
}
}
export class EntityValidationError extends BaseError<{ message: string }> {
override readonly code = "Entity/Validation" as const;
constructor(message: string) {
super({ message, data: { message } });
}
}
Typed Error Unions in Abstractions
Define an IErrors interface mapping error names to types, then create a union via [keyof IErrors]:
// features/createEntity/abstractions.ts
import { createAbstraction, Result } from "webiny/api";
import { NotAuthorizedError } from "webiny/api/security";
import {
EntityPersistenceError,
EntityModelNotFoundError,
EntityCreationError
} from "~/api/domain/errors.js";
// REPOSITORY errors
export interface ICreateEntityRepositoryErrors {
persistence: EntityPersistenceError;
modelNotFound: EntityModelNotFoundError;
creation: EntityCreationError;
}
type RepositoryError = ICreateEntityRepositoryErrors[keyof ICreateEntityRepositoryErrors];
export interface ICreateEntityRepository {
execute(entity: Entity): Promise<Result<Entity, RepositoryError>>;
}
export const CreateEntityRepository = createAbstraction<ICreateEntityRepository>(
"MyExt/CreateEntityRepository"
);
export namespace CreateEntityRepository {
export type Interface = ICreateEntityRepository;
export type Error = RepositoryError;
export type Return = Promise<Result<Entity, RepositoryError>>;
}
// U
FAQ
What does a use case return?
A result that is either a success value or an error, so callers must check for failure.
Can I override a default use case?
Yes. The skill explains how to register a replacement implementation.
How are repositories used?
The skill covers repositories that persist data through the CMS, along with entry mappers.
Details
Original description by the author
UseCase implementation pattern — DI, Result handling, error types, decorators, CMS repositories, entry mappers, and schema-based permissions. Use this skill to implement, inject, override, or decorate any Webiny UseCase, or to build repositories that persist data via CMS.
repo: webiny/webiny-js
Popularity
- GitHub stars
- 8.1k
- Ranking position
- #12590
- License
- —
- Updated
- 11/2/2026
Comments
- …
More skills from this repository
prd-to-plan
8.1kHelps your agent turn a product requirements document into a phased plan of small end-to-end slices, saved as a Markdown file.
webiny-admin-architect
8.1kTeaches your agent how to build admin screens for the Webiny platform, using its business logic layers and presenter pattern.
webiny-admin-permissions
8.1kTeaches your agent to add permission controls to Webiny admin screens, including auto-generated permission forms and hide-or-show checks.
webiny-form-model
8.1kTeaches your agent to build forms in Webiny using a declarative field builder, layouts, validation and conditional rules.
Similar skills
api-connector-builder
280kHas your agent build new API connections following the existing pattern. Use when you need to add one more integration without creating a new way of doing things.
api-design
280kHelps your agent design REST APIs following best practices. It's useful for defining how APIs communicate, how errors are shown, and how to organize data.
backend-patterns
280kHelps your agent with server-side architecture patterns and database optimization. It's useful for building or reviewing code that runs on the server.