@webiny/mcp 6.1.0-beta.1 → 6.1.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/agents/claude.d.ts +2 -0
  2. package/agents/claude.js +6 -0
  3. package/agents/claude.js.map +1 -1
  4. package/agents/cline.d.ts +2 -0
  5. package/agents/cline.js +6 -0
  6. package/agents/cline.js.map +1 -1
  7. package/agents/copilot.d.ts +2 -0
  8. package/agents/copilot.js +7 -0
  9. package/agents/copilot.js.map +1 -1
  10. package/agents/cursor.d.ts +2 -0
  11. package/agents/cursor.js +6 -0
  12. package/agents/cursor.js.map +1 -1
  13. package/agents/discover.d.ts +3 -0
  14. package/agents/discover.js +29 -0
  15. package/agents/discover.js.map +1 -0
  16. package/agents/instructions.d.ts +3 -1
  17. package/agents/instructions.js +15 -2
  18. package/agents/instructions.js.map +1 -1
  19. package/agents/kiro.d.ts +2 -0
  20. package/agents/kiro.js +6 -0
  21. package/agents/kiro.js.map +1 -1
  22. package/agents/opencode.d.ts +2 -0
  23. package/agents/opencode.js +7 -0
  24. package/agents/opencode.js.map +1 -1
  25. package/agents/types.d.ts +16 -0
  26. package/agents/types.js +3 -0
  27. package/agents/types.js.map +1 -0
  28. package/agents/windsurf.d.ts +2 -0
  29. package/agents/windsurf.js +6 -0
  30. package/agents/windsurf.js.map +1 -1
  31. package/cli/ConfigureMcp.js +21 -6
  32. package/cli/ConfigureMcp.js.map +1 -1
  33. package/package.json +3 -3
  34. package/skills/admin/admin-architect/SKILL.md +188 -0
  35. package/skills/admin/admin-permissions/SKILL.md +159 -0
  36. package/skills/admin/ui-extensions/SKILL.md +2 -0
  37. package/skills/api/api-architect/SKILL.md +548 -60
  38. package/skills/api/event-handler-pattern/SKILL.md +195 -23
  39. package/skills/api/graphql-api/SKILL.md +231 -31
  40. package/skills/api/permissions/SKILL.md +291 -0
  41. package/skills/api/use-case-pattern/SKILL.md +351 -12
  42. package/skills/api/v5-to-v6-migration/SKILL.md +416 -0
  43. package/skills/cli-extensions/SKILL.md +1 -1
  44. package/skills/content-models/SKILL.md +9 -5
  45. package/skills/full-stack-architect/SKILL.md +4 -0
  46. package/skills/generated/admin/SKILL.md +1 -1
  47. package/skills/generated/admin/aco/SKILL.md +1 -1
  48. package/skills/generated/admin/build-params/SKILL.md +1 -1
  49. package/skills/generated/admin/cms/SKILL.md +1 -1
  50. package/skills/generated/admin/configs/SKILL.md +1 -1
  51. package/skills/generated/admin/env-config/SKILL.md +1 -1
  52. package/skills/generated/admin/form/SKILL.md +1 -1
  53. package/skills/generated/admin/graphql-client/SKILL.md +1 -1
  54. package/skills/generated/admin/lexical/SKILL.md +1 -1
  55. package/skills/generated/admin/local-storage/SKILL.md +1 -1
  56. package/skills/generated/admin/router/SKILL.md +1 -1
  57. package/skills/generated/admin/security/SKILL.md +1 -1
  58. package/skills/generated/admin/tenancy/SKILL.md +1 -1
  59. package/skills/generated/admin/ui/SKILL.md +1 -1
  60. package/skills/generated/admin/website-builder/SKILL.md +1 -1
  61. package/skills/generated/api/SKILL.md +1 -1
  62. package/skills/generated/api/aco/SKILL.md +1 -1
  63. package/skills/generated/api/build-params/SKILL.md +1 -1
  64. package/skills/generated/api/cms/SKILL.md +1 -1
  65. package/skills/generated/api/event-publisher/SKILL.md +1 -1
  66. package/skills/generated/api/file-manager/SKILL.md +1 -1
  67. package/skills/generated/api/graphql/SKILL.md +1 -1
  68. package/skills/generated/api/key-value-store/SKILL.md +1 -1
  69. package/skills/generated/api/logger/SKILL.md +1 -1
  70. package/skills/generated/api/opensearch/SKILL.md +1 -1
  71. package/skills/generated/api/scheduler/SKILL.md +1 -1
  72. package/skills/generated/api/security/SKILL.md +1 -1
  73. package/skills/generated/api/system/SKILL.md +1 -1
  74. package/skills/generated/api/tasks/SKILL.md +1 -1
  75. package/skills/generated/api/tenancy/SKILL.md +1 -1
  76. package/skills/generated/api/tenant-manager/SKILL.md +1 -1
  77. package/skills/generated/api/website-builder/SKILL.md +1 -1
  78. package/skills/generated/cli/SKILL.md +1 -1
  79. package/skills/generated/cli/command/SKILL.md +1 -1
  80. package/skills/generated/extensions/SKILL.md +1 -1
  81. package/skills/generated/infra/SKILL.md +1 -1
  82. package/skills/infrastructure-extensions/SKILL.md +1 -1
  83. package/skills/project-structure/SKILL.md +4 -0
  84. package/skills/webiny-sdk/SKILL.md +1 -1
@@ -0,0 +1,291 @@
1
+ ---
2
+ name: webiny-api-permissions
3
+ context: webiny-api
4
+ description: >
5
+ Schema-based permission system for API features. Use this skill when implementing
6
+ authorization in use cases, defining permission schemas with createPermissions,
7
+ checking read/write/delete/publish permissions, handling own-record scoping,
8
+ or testing permission scenarios. Covers the full pattern from schema definition
9
+ to use case integration to test matrices.
10
+ ---
11
+
12
+ # Schema-Based Permissions
13
+
14
+ ## Overview
15
+
16
+ Webiny uses a **schema-based permission system** defined via `createPermissions`. Each package declares a permission schema and gets a typed `Permissions` abstraction injectable into use cases via DI. This replaces manual `identityContext.getPermission()` calls with high-level methods like `canRead`, `canEdit`, `canDelete`, `canPublish`, `onlyOwnRecords`, etc.
17
+
18
+ ## Permission Schema Definition
19
+
20
+ ```ts
21
+ // domain/permissions.ts (or permissions/schema.ts)
22
+ import { createPermissions } from "@webiny/api-core/features/security/permissions/index.js";
23
+ import type { Permissions } from "@webiny/api-core/features/security/permissions/index.js";
24
+
25
+ const schema = {
26
+ prefix: "wb", // Permission prefix
27
+ fullAccess: { name: "wb.*" }, // Wildcard = full access to all entities
28
+ entities: [
29
+ {
30
+ id: "page", // Entity identifier (used in method calls)
31
+ permission: "wb.page", // Permission name stored on the identity
32
+ scopes: ["full", "own"], // Access scopes: "full" = all records, "own" = only own
33
+ actions: [
34
+ { name: "rwd" }, // Read/Write/Delete (chars: "r", "w", "d")
35
+ { name: "pw" } // Publish/Unpublish (chars: "p", "u")
36
+ ]
37
+ },
38
+ {
39
+ id: "settings",
40
+ permission: "wb.settings",
41
+ scopes: ["full"] // No "own" — no ownership concept for settings
42
+ }
43
+ ]
44
+ } as const; // MUST use `as const` for type narrowing
45
+
46
+ type MySchema = typeof schema;
47
+
48
+ export const MyPermissions = createPermissions(schema);
49
+
50
+ export namespace MyPermissions {
51
+ export type Interface = Permissions<MySchema>;
52
+ }
53
+ ```
54
+
55
+ `createPermissions` returns `{ Abstraction, Implementation }`. Register the Implementation in your feature.
56
+
57
+ ### Schema Fields
58
+
59
+ | Field | Description |
60
+ | ----------------------- | --------------------------------------------------------------------- |
61
+ | `prefix` | Namespaces the DI abstraction: `${prefix}:Permissions` |
62
+ | `fullAccess.name` | Wildcard permission (e.g. `"wb.*"`) — grants all entity access |
63
+ | `entities[].id` | Entity identifier used in method calls: `canRead("page")` |
64
+ | `entities[].permission` | Permission name matched against identity permissions |
65
+ | `entities[].scopes` | `["full"]` or `["full", "own"]` — determines if own-scope supported |
66
+ | `entities[].actions` | Action definitions — built-in: `"rwd"`, `"pw"`; custom: boolean flags |
67
+
68
+ ### Scopes
69
+
70
+ - **`"full"`** — User can access all records (default when no `own` flag on permission object)
71
+ - **`"own"`** — User can only access records where `createdBy.id === identity.id`
72
+
73
+ ---
74
+
75
+ ## Permission Methods
76
+
77
+ All methods follow a 3-tier bypass:
78
+
79
+ 1. `identityContext.hasFullAccess()` → `name: "*"` permission (super admin)
80
+ 2. `hasFullSchemaAccess()` → wildcard permission (e.g. `"wb.*"`)
81
+ 3. Entity-level permission check
82
+
83
+ ### Method Reference
84
+
85
+ | Method | Purpose | Item-aware | Notes |
86
+ | --------------------------- | --------------------- | ---------- | --------------------------------------------------------------------------------------------- |
87
+ | `canAccess(entity, item?)` | General access check | Yes | Without item: checks entity permission exists. With item + `own: true`: checks `createdBy.id` |
88
+ | `onlyOwnRecords(entity)` | List filter flag | No | Returns `true` when ALL permissions have `own: true` |
89
+ | `canRead(entity)` | Read permission | No | Checks `rwd` includes `"r"` (or no `rwd` = unrestricted) |
90
+ | `canCreate(entity)` | Create permission | No | Checks `rwd` includes `"w"` |
91
+ | `canEdit(entity, item?)` | Edit permission | Yes | With `own: true` + no item → allows (new/unsaved). With item → checks ownership |
92
+ | `canDelete(entity, item?)` | Delete permission | Yes | With `own: true` + no item → **RETURNS FALSE**. Must pass item |
93
+ | `canPublish(entity)` | Publish permission | No | Checks `pw` includes `"p"` |
94
+ | `canUnpublish(entity)` | Unpublish permission | No | Checks `pw` includes `"u"` |
95
+ | `canAction(action, entity)` | Custom boolean action | No | Checks `permission[action] === true` |
96
+
97
+ All return `Promise<boolean>`.
98
+
99
+ ### OwnableItem Interface
100
+
101
+ ```ts
102
+ interface OwnableItem {
103
+ createdBy?: { id: string } | null;
104
+ }
105
+ ```
106
+
107
+ ---
108
+
109
+ ## Use Case Implementation Patterns
110
+
111
+ ### Get Use Case (Read + Ownership Gate)
112
+
113
+ The Get use case is the **central ownership gate** — mutation use cases that delegate to GetById inherit ownership enforcement automatically.
114
+
115
+ ```ts
116
+ import { Result } from "@webiny/feature/api";
117
+ import { GetByIdUseCase as UseCaseAbstraction, GetByIdRepository } from "./abstractions.js";
118
+ import { MyPermissions } from "~/domain/permissions.js";
119
+ import { NotAuthorizedError } from "~/domain/errors.js";
120
+
121
+ class GetByIdUseCaseImpl implements UseCaseAbstraction.Interface {
122
+ constructor(
123
+ private permissions: MyPermissions.Interface,
124
+ private repository: GetByIdRepository.Interface
125
+ ) {}
126
+
127
+ async execute(id: string): UseCaseAbstraction.Return {
128
+ // 1. Entity-level read check
129
+ if (!(await this.permissions.canRead("entity"))) {
130
+ return Result.fail(new NotAuthorizedError());
131
+ }
132
+
133
+ // 2. Fetch
134
+ const result = await this.repository.execute(id);
135
+ if (result.isFail()) {
136
+ return result;
137
+ }
138
+
139
+ // 3. Item-level ownership check
140
+ if (!(await this.permissions.canAccess("entity", result.value))) {
141
+ return Result.fail(new NotAuthorizedError());
142
+ }
143
+
144
+ return result;
145
+ }
146
+ }
147
+
148
+ export const GetByIdUseCase = UseCaseAbstraction.createImplementation({
149
+ implementation: GetByIdUseCaseImpl,
150
+ dependencies: [MyPermissions.Abstraction, GetByIdRepository]
151
+ });
152
+ ```
153
+
154
+ ### List Use Case (Read + Own Records Filter)
155
+
156
+ ```ts
157
+ import { IdentityContext } from "@webiny/api-core/features/security/IdentityContext/index.js";
158
+
159
+ class ListUseCaseImpl implements UseCaseAbstraction.Interface {
160
+ constructor(
161
+ private permissions: MyPermissions.Interface,
162
+ private identityContext: IdentityContext.Interface,
163
+ private repository: ListRepository.Interface
164
+ ) {}
165
+
166
+ async execute(params: UseCaseAbstraction.Params): UseCaseAbstraction.Return {
167
+ if (!(await this.permissions.canRead("entity"))) {
168
+ return Result.fail(new NotAuthorizedError());
169
+ }
170
+
171
+ const where = { ...params.where };
172
+
173
+ // Filter to own records if needed
174
+ if (await this.permissions.onlyOwnRecords("entity")) {
175
+ const identity = this.identityContext.getIdentity();
176
+ where.createdBy = identity.id;
177
+ }
178
+
179
+ return this.repository.execute({ ...params, where });
180
+ }
181
+ }
182
+
183
+ // Dependencies must include IdentityContext
184
+ dependencies: [MyPermissions.Abstraction, IdentityContext, ListRepository];
185
+ ```
186
+
187
+ **Important:** The list `where` type must include `createdBy?: string`. For CMS-based entities, `CmsEntryListWhere` already has this.
188
+
189
+ ### Update Use Case (Edit + Item-Level Check)
190
+
191
+ ```ts
192
+ class UpdateUseCaseImpl implements UseCaseAbstraction.Interface {
193
+ constructor(
194
+ private permissions: MyPermissions.Interface,
195
+ private getById: GetByIdUseCase.Interface, // Delegates ownership gate
196
+ private repository: UpdateRepository.Interface
197
+ ) {}
198
+
199
+ async execute(id: string, data: UpdateData): UseCaseAbstraction.Return {
200
+ // 1. Entity-level edit check (no item yet)
201
+ if (!(await this.permissions.canEdit("entity"))) {
202
+ return Result.fail(new NotAuthorizedError());
203
+ }
204
+
205
+ // 2. Fetch original (enforces canRead + canAccess via GetById)
206
+ const getResult = await this.getById.execute(id);
207
+ if (getResult.isFail()) {
208
+ return getResult;
209
+ }
210
+
211
+ const original = getResult.value;
212
+
213
+ // 3. Item-level edit check (defense in depth)
214
+ if (!(await this.permissions.canEdit("entity", original))) {
215
+ return Result.fail(new NotAuthorizedError());
216
+ }
217
+
218
+ // ... events + repository
219
+ }
220
+ }
221
+ ```
222
+
223
+ ### Delete Use Case (CRITICAL: Item-Level Delete)
224
+
225
+ **`canDelete` with `own: true` and no item returns `false`.**
226
+
227
+ Unlike `canEdit` (which returns `true` for `own: true` + no item), `canDelete` requires the item to verify ownership. The delete use case MUST fetch the item first.
228
+
229
+ ```ts
230
+ class DeleteUseCaseImpl implements UseCaseAbstraction.Interface {
231
+ async execute(params: Params): UseCaseAbstraction.Return {
232
+ // Fetch first (enforces canRead + canAccess via GetById)
233
+ const getResult = await this.getById.execute(params.id);
234
+ if (getResult.isFail()) {
235
+ return Result.fail(getResult.error);
236
+ }
237
+
238
+ const item = getResult.value;
239
+
240
+ // Item-level delete check — MUST pass the item
241
+ if (!(await this.permissions.canDelete("entity", item))) {
242
+ return Result.fail(new NotAuthorizedError());
243
+ }
244
+
245
+ // ... events + repository
246
+ }
247
+ }
248
+ ```
249
+
250
+ ### Publish Use Case (Publish + Ownership)
251
+
252
+ ```ts
253
+ class PublishUseCaseImpl {
254
+ async execute(params: Params): UseCaseAbstraction.Return {
255
+ // 1. Entity-level publish check
256
+ if (!(await this.permissions.canPublish("entity"))) {
257
+ return Result.fail(new NotAuthorizedError());
258
+ }
259
+
260
+ // 2. Fetch (enforces ownership via GetById)
261
+ const getResult = await this.getById.execute(params.id);
262
+ if (getResult.isFail()) {
263
+ return getResult;
264
+ }
265
+
266
+ // 3. Item-level ownership check (defense in depth)
267
+ if (!(await this.permissions.canAccess("entity", getResult.value))) {
268
+ return Result.fail(new NotAuthorizedError());
269
+ }
270
+
271
+ // ... events + repository
272
+ }
273
+ }
274
+ ```
275
+
276
+ ---
277
+
278
+ ## Gotchas
279
+
280
+ 1. **`canDelete` without item + `own: true` = `false`** — Always pass the item to `canDelete`. Fetch first, then check.
281
+ 2. **`canEdit` without item + `own: true` = `true`** — Intentional: allows editing new/unsaved records.
282
+ 3. **`canAccess` without item = `true`** — Only checks entity-level access, not ownership.
283
+ 4. **List where type** — Ensure the `where` interface includes `createdBy?: string` for own-scope filtering.
284
+ 5. **`as const`** — The schema MUST use `as const` for TypeScript to narrow entity IDs in method signatures.
285
+ 6. **Dependencies order** — DI constructor params must match the `dependencies` array order exactly.
286
+
287
+ ## Related Skills
288
+
289
+ - **webiny-api-architect** — Architecture overview, Services vs UseCases, feature structure
290
+ - **webiny-use-case-pattern** — UseCase implementation, Result handling, decorators
291
+ - **webiny-dependency-injection** — Injectable services catalog
@@ -2,20 +2,19 @@
2
2
  name: webiny-use-case-pattern
3
3
  context: webiny-api
4
4
  description: >
5
- Generic UseCase implementation pattern — DI, Result handling, extension registration.
6
- Use this skill to understand how to implement, inject, or override any Webiny UseCase.
5
+ UseCase implementation pattern — DI, Result handling, error types, decorators, CMS repositories,
6
+ entry mappers, and schema-based permissions. Use this skill to implement, inject, override, or
7
+ decorate any Webiny UseCase, or to build repositories that persist data via CMS.
7
8
  ---
8
9
 
9
10
  # UseCase Pattern
10
11
 
11
12
  ## What It Is
12
13
 
13
- A **UseCase** is a named operation that encapsulates business logic (e.g., `CreateTenantUseCase`, `PublishEntryUseCase`). Each UseCase is a DI abstraction with a single `execute` method.
14
+ 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>`.
14
15
 
15
16
  ## Interface Shape
16
17
 
17
- Every UseCase follows this pattern:
18
-
19
18
  ```ts
20
19
  interface SomeUseCase.Interface {
21
20
  execute(input: Input): Promise<Result<ReturnType, ErrorType>>;
@@ -28,7 +27,7 @@ interface SomeUseCase.Interface {
28
27
 
29
28
  ## How to Use a UseCase
30
29
 
31
- UseCases are injected as dependencies into EventHandlers or other UseCases via DI.
30
+ UseCases are injected as dependencies into EventHandlers, other UseCases, or GraphQL resolvers via DI.
32
31
 
33
32
  ```ts
34
33
  import { SomeUseCase } from "webiny/api/<category>";
@@ -78,6 +77,10 @@ export default SomeUseCase.createImplementation({
78
77
 
79
78
  ## Registration
80
79
 
80
+ **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.
81
+
82
+ **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`.
83
+
81
84
  ```tsx
82
85
  // In your app's configuration
83
86
  <Api.Extension src={"@/extensions/my-extension.ts"} />
@@ -85,18 +88,354 @@ export default SomeUseCase.createImplementation({
85
88
 
86
89
  Deploy with: `yarn webiny deploy api --env=dev`
87
90
 
91
+ ---
92
+
93
+ ## Error Handling Pattern
94
+
95
+ ### Domain-Specific Errors
96
+
97
+ Every feature defines errors extending `BaseError`. Never use generic `Error` for validation or business rule failures.
98
+
99
+ ```ts
100
+ // domain/errors.ts
101
+ import { BaseError } from "@webiny/feature/api";
102
+
103
+ export class EntityNotFoundError extends BaseError {
104
+ override readonly code = "Entity/NotFound" as const;
105
+ constructor(id: string) {
106
+ super({ message: `Entity with id "${id}" was not found!` });
107
+ }
108
+ }
109
+
110
+ export class EntityPersistenceError extends BaseError<{ error: Error }> {
111
+ override readonly code = "Entity/Persist" as const;
112
+ constructor(error: Error) {
113
+ super({ message: error.message, data: { error } });
114
+ }
115
+ }
116
+
117
+ export class EntityValidationError extends BaseError<{ message: string }> {
118
+ override readonly code = "Entity/Validation" as const;
119
+ constructor(message: string) {
120
+ super({ message, data: { message } });
121
+ }
122
+ }
123
+ ```
124
+
125
+ ### Typed Error Unions in Abstractions
126
+
127
+ Define an `IErrors` interface mapping error names to types, then create a union via `[keyof IErrors]`:
128
+
129
+ ```ts
130
+ // features/createEntity/abstractions.ts
131
+ import { createAbstraction, Result } from "@webiny/feature/api";
132
+ import { NotAuthorizedError } from "@webiny/api-core/features/security/shared/errors.js";
133
+ import { EntityPersistenceError, EntityModelNotFoundError, EntityCreationError } from "~/api/domain/errors.js";
134
+
135
+ // REPOSITORY errors
136
+ export interface ICreateEntityRepositoryErrors {
137
+ persistence: EntityPersistenceError;
138
+ modelNotFound: EntityModelNotFoundError;
139
+ creation: EntityCreationError;
140
+ }
141
+
142
+ type RepositoryError = ICreateEntityRepositoryErrors[keyof ICreateEntityRepositoryErrors];
143
+
144
+ export interface ICreateEntityRepository {
145
+ execute(entity: Entity): Promise<Result<Entity, RepositoryError>>;
146
+ }
147
+
148
+ export const CreateEntityRepository = createAbstraction<ICreateEntityRepository>(
149
+ "MyExt/CreateEntityRepository"
150
+ );
151
+
152
+ export namespace CreateEntityRepository {
153
+ export type Interface = ICreateEntityRepository;
154
+ export type Error = RepositoryError;
155
+ export type Return = Promise<Result<Entity, RepositoryError>>;
156
+ }
157
+
158
+ // USE CASE errors — superset of repository errors
159
+ export interface ICreateEntityUseCaseErrors {
160
+ persistence: EntityPersistenceError;
161
+ modelNotFound: EntityModelNotFoundError;
162
+ creation: EntityCreationError;
163
+ notAuthorized: NotAuthorizedError;
164
+ }
165
+
166
+ type UseCaseError = ICreateEntityUseCaseErrors[keyof ICreateEntityUseCaseErrors];
167
+
168
+ export interface ICreateEntityUseCase {
169
+ execute(input: CreateEntityInput): Promise<Result<Entity, UseCaseError>>;
170
+ }
171
+
172
+ export const CreateEntityUseCase = createAbstraction<ICreateEntityUseCase>(
173
+ "MyExt/CreateEntityUseCase"
174
+ );
175
+
176
+ export namespace CreateEntityUseCase {
177
+ export type Interface = ICreateEntityUseCase;
178
+ export type Input = CreateEntityInput;
179
+ export type Error = UseCaseError;
180
+ export type Return = Promise<Result<Entity, UseCaseError>>;
181
+ }
182
+ ```
183
+
184
+ ### Result Pattern
185
+
186
+ ```ts
187
+ // Success
188
+ return Result.ok(value);
189
+
190
+ // Failure
191
+ return Result.fail(new EntityNotFoundError(id));
192
+
193
+ // Check result
194
+ if (result.isFail()) {
195
+ return Result.fail(result.error);
196
+ }
197
+
198
+ // Access value
199
+ const value = result.value;
200
+ ```
201
+
202
+ Never use `result.isError()`, `result.getError()`, or `result.getValue()` — these do not exist.
203
+
204
+ ---
205
+
206
+ ## UseCase Implementation
207
+
208
+ ```ts
209
+ // features/createEntity/CreateEntityUseCase.ts
210
+ import { CreateEntityUseCase as UseCaseAbstraction, CreateEntityRepository } from "./abstractions.js";
211
+ import { Result } from "@webiny/feature/api";
212
+ import { IdentityContext } from "@webiny/api-core/exports/api/security.js";
213
+ import { NotAuthorizedError } from "@webiny/api-core/features/security/shared/errors.js";
214
+ import { Entity } from "~/shared/Entity.js";
215
+ import { EntityId } from "~/api/domain/EntityId.js";
216
+
217
+ class CreateEntityUseCase implements UseCaseAbstraction.Interface {
218
+ constructor(
219
+ private identityContext: IdentityContext.Interface,
220
+ private repository: CreateEntityRepository.Interface
221
+ ) {}
222
+
223
+ async execute(input: UseCaseAbstraction.Input): UseCaseAbstraction.Return {
224
+ if (!this.identityContext.getPermission("mypackage.entity")) {
225
+ return Result.fail(new NotAuthorizedError({ message: "Not authorized to create entities!" }));
226
+ }
227
+
228
+ const entity = Entity.from({
229
+ id: EntityId.from(input.id),
230
+ values: { name: input.name, status: "disabled" }
231
+ });
232
+
233
+ const result = await this.repository.execute(entity);
234
+ if (result.isFail()) {
235
+ return Result.fail(result.error);
236
+ }
237
+
238
+ return Result.ok(result.value);
239
+ }
240
+ }
241
+
242
+ export default UseCaseAbstraction.createImplementation({
243
+ implementation: CreateEntityUseCase,
244
+ dependencies: [IdentityContext, CreateEntityRepository]
245
+ });
246
+ ```
247
+
248
+ **Rules:**
249
+ - Class implements `UseCaseAbstraction.Interface`
250
+ - Constructor params typed with `.Interface` from their abstractions
251
+ - Return type uses `UseCaseAbstraction.Return`
252
+ - `dependencies` array matches constructor parameter order exactly
253
+ - Export as `default`
254
+
255
+ ---
256
+
257
+ ## CMS Repository Pattern
258
+
259
+ Repositories use CMS use cases to persist data. Always resolve the CMS model first.
260
+
261
+ ```ts
262
+ // features/createEntity/CreateEntityRepository.ts
263
+ import { Entity } from "~/shared/Entity.js";
264
+ import { EntityCreationError, EntityModelNotFoundError } from "~/api/domain/errors.js";
265
+ import { CreateEntityRepository as RepositoryAbstraction } from "./abstractions.js";
266
+ import { Result } from "@webiny/feature/api";
267
+ import { CreateEntryUseCase } from "@webiny/api-headless-cms/exports/api/cms/entry.js";
268
+ import { GetModelUseCase } from "@webiny/api-headless-cms/exports/api/cms/model";
269
+ import { ENTITY_MODEL_ID } from "~/shared/constants.js";
270
+
271
+ class CreateEntityRepository implements RepositoryAbstraction.Interface {
272
+ constructor(
273
+ private getModelUseCase: GetModelUseCase.Interface,
274
+ private createEntryUseCase: CreateEntryUseCase.Interface
275
+ ) {}
276
+
277
+ async execute(entity: Entity): RepositoryAbstraction.Return {
278
+ const modelResult = await this.getModelUseCase.execute(ENTITY_MODEL_ID);
279
+ if (modelResult.isFail()) {
280
+ return Result.fail(new EntityModelNotFoundError());
281
+ }
282
+
283
+ const createResult = await this.createEntryUseCase.execute(modelResult.value, {
284
+ id: entity.id,
285
+ values: {
286
+ name: entity.values.name,
287
+ status: entity.values.status
288
+ }
289
+ });
290
+
291
+ if (createResult.isFail()) {
292
+ return Result.fail(new EntityCreationError(createResult.error));
293
+ }
294
+
295
+ return Result.ok(entity);
296
+ }
297
+ }
298
+
299
+ export default RepositoryAbstraction.createImplementation({
300
+ implementation: CreateEntityRepository,
301
+ dependencies: [GetModelUseCase, CreateEntryUseCase]
302
+ });
303
+ ```
304
+
305
+ ### Common CMS Use Cases for Repositories
306
+
307
+ ```ts
308
+ import { CreateEntryUseCase } from "@webiny/api-headless-cms/exports/api/cms/entry.js";
309
+ import { GetEntryByIdUseCase } from "@webiny/api-headless-cms/exports/api/cms/entry.js";
310
+ import { GetEntryUseCase } from "@webiny/api-headless-cms/exports/api/cms/entry.js";
311
+ import { UpdateEntryUseCase } from "@webiny/api-headless-cms/exports/api/cms/entry.js";
312
+ import { ListLatestEntriesUseCase } from "@webiny/api-headless-cms/exports/api/cms/entry.js";
313
+ import { EntryId } from "@webiny/api-headless-cms/exports/api/cms/entry.js";
314
+ import { GetModelUseCase } from "@webiny/api-headless-cms/exports/api/cms/model";
315
+ import { ListModelsUseCase } from "@webiny/api-headless-cms/exports/api/cms/model.js";
316
+ ```
317
+
318
+ **Rules:**
319
+ - Always resolve the CMS model first via `GetModelUseCase`
320
+ - Wrap CMS errors in domain-specific errors
321
+ - Register repositories in **singleton scope**
322
+ - Export as `default`
323
+
324
+ ---
325
+
326
+ ## Entry-to-Entity Mapper
327
+
328
+ When repositories return CMS entries, use a mapper to convert to domain types:
329
+
330
+ ```ts
331
+ // features/shared/EntryToEntityMapper.ts
332
+ import { Entity as EntityClass } from "~/shared/Entity.js";
333
+ import type { Entity, EntityDto, EntityValues } from "~/shared/Entity.js";
334
+
335
+ export class EntryToEntityMapper {
336
+ static toEntity(entry: { entryId: string; values: EntityValues }): Entity {
337
+ return EntityClass.from({
338
+ id: entry.entryId,
339
+ values: entry.values
340
+ });
341
+ }
342
+ }
343
+ ```
344
+
345
+ - Static methods only — no instance state
346
+ - Used by repositories, not by use cases directly
347
+ - Handle null/undefined values with defaults where appropriate
348
+
349
+ ---
350
+
351
+ ## UseCase Decorators
352
+
353
+ Decorators add cross-cutting concerns (authorization, logging, validation) without modifying the core use case.
354
+
355
+ ```ts
356
+ // features/getEntityById/decorators/GetEntityByIdWithAuthorization.ts
357
+ import { GetEntityByIdUseCase } from "../abstractions.js";
358
+ import { Result } from "@webiny/feature/api";
359
+ import { IdentityContext } from "@webiny/api-core/exports/api/security.js";
360
+ import { NotAuthorizedError } from "@webiny/api-core/features/security/shared/errors.js";
361
+
362
+ class GetEntityByIdWithAuthorizationImpl implements GetEntityByIdUseCase.Interface {
363
+ constructor(
364
+ private identityContext: IdentityContext.Interface,
365
+ private decoratee: GetEntityByIdUseCase.Interface // decoratee is LAST
366
+ ) {}
367
+
368
+ async execute(id: string): GetEntityByIdUseCase.Return {
369
+ if (!this.identityContext.getPermission("mypackage.entity")) {
370
+ return Result.fail(new NotAuthorizedError());
371
+ }
372
+ return this.decoratee.execute(id);
373
+ }
374
+ }
375
+
376
+ export const GetEntityByIdWithAuthorization = GetEntityByIdUseCase.createDecorator({
377
+ decorator: GetEntityByIdWithAuthorizationImpl,
378
+ dependencies: [IdentityContext] // does NOT include decoratee
379
+ });
380
+ ```
381
+
382
+ ### Registering a Decorator
383
+
384
+ ```ts
385
+ // features/getEntityById/feature.ts
386
+ import { createFeature } from "@webiny/feature/api";
387
+ import GetEntityByIdUseCase from "./GetEntityByIdUseCase.js";
388
+ import GetEntityByIdRepository from "./GetEntityByIdRepository.js";
389
+ import { GetEntityByIdWithAuthorization } from "./decorators/GetEntityByIdWithAuthorization.js";
390
+
391
+ export const GetEntityByIdFeature = createFeature({
392
+ name: "GetEntityById",
393
+ register(container) {
394
+ container.register(GetEntityByIdUseCase);
395
+ container.register(GetEntityByIdRepository).inSingletonScope();
396
+ container.registerDecorator(GetEntityByIdWithAuthorization);
397
+ }
398
+ });
399
+ ```
400
+
401
+ **Rules:**
402
+ - Implements the same interface as the use case it decorates
403
+ - Constructor: extra dependencies first, `decoratee` **last**
404
+ - Use `UseCaseAbstraction.createDecorator(...)` — the `dependencies` array does NOT include the decoratee
405
+ - Register with `container.registerDecorator()`, not `container.register()`
406
+ - Can modify input before delegating, output after, or short-circuit with an error
407
+
408
+ ---
409
+
410
+ ## Schema-Based Permissions
411
+
412
+ For implementing authorization in use cases, see the **webiny-api-permissions** skill. It covers:
413
+ - Permission schema definition with `createPermissions`
414
+ - All permission methods (`canRead`, `canEdit`, `canDelete`, `canPublish`, `onlyOwnRecords`, etc.)
415
+ - Use case patterns for every CRUD operation (get, list, update, delete, publish)
416
+ - Own-record scoping and item-level ownership checks
417
+ - Testing patterns and permission object shapes
418
+
419
+ ---
420
+
88
421
  ## Resolving Types (MANDATORY)
89
422
 
90
423
  **Before writing any code that calls a UseCase or accesses its return types, you MUST read the source file listed in the catalog's `Source` field to verify the exact method signatures, input parameters, return types, and error types. Do not assume or guess property names from memory.**
91
424
 
92
- To see the exact types for a specific UseCase:
93
-
94
- 1. Read the `abstractions.ts` file from the catalog `Source` path — it contains the `Interface` with the full method signature, input types, and error union.
95
- 2. If the interface references domain types (e.g., `CmsEntry`, `CmsModel`), follow the import and read that type declaration too.
96
- 3. Only use properties and method signatures that are confirmed to exist in the source type declarations.
425
+ 1. Read the `abstractions.ts` file from the catalog `Source` path
426
+ 2. If the interface references domain types, follow the import and read that type declaration
427
+ 3. Only use properties and method signatures confirmed in the source
97
428
 
98
429
  ## Key Rules
99
430
 
100
- - Always check `result.isOk()` or `result.isFail()` before accessing `.value` or `.error`
431
+ - Always check `result.isFail()` before accessing `.value` or `.error`
101
432
  - DI constructor parameter order must match the `dependencies` array order exactly
102
433
  - Use `.js` extensions in import paths (ES modules)
434
+
435
+ ## Related Skills
436
+
437
+ - **webiny-api-architect** — Architecture overview, Services vs UseCases, feature naming, anti-patterns
438
+ - **webiny-api-permissions** — Schema-based permissions, CRUD authorization patterns, testing
439
+ - **webiny-event-handler-pattern** — EventHandler lifecycle, domain event publishing
440
+ - **webiny-custom-graphql-api** — GraphQL schema creation with UseCase DI
441
+ - **webiny-dependency-injection** — Injectable services catalog