@ApiRouteSubscriber
Registers a class as a route-level subscriber for a specific entity. Subscribers intercept controller lifecycles (before, after, error) whenever an observable controller executes CRUD routes.
Signature
@ApiRouteSubscriber<E extends IApiBaseEntity>({
entity,
priority?,
}: IApiRouteSubscriberProperties<E>)Options
| Option | Description |
|---|---|
entity | Entity that the subscriber observes. |
priority | Execution order (higher runs first). Defaults to 0. |
Usage steps
- Add
@ApiControllerObservable()to the controller that should emit events. - Extend
ApiRouteSubscriberBase<E>and implement lifecycle hooks such asonBeforeCreate,onBeforePartialUpdate,onAfterGetList, oronAfterErrorDelete. - Decorate the subscriber class with
@ApiRouteSubscriber.
Example
@Injectable()
@ApiRouteSubscriber<PostEntity>({ entity: PostEntity, priority: 10 })
export class PostAuditSubscriber extends ApiRouteSubscriberBase<PostEntity> {
async onAfterCreate(context: TApiSubscriberRouteAfterCreateContext<PostEntity>) {
this.logger.log("Post created", context.result);
return context.result;
}
}Related resources
Last updated on