Overview
The@Feature and @Action decorators are used to add metadata to classes and methods for authorization and feature detection. They work with NestJS’s metadata system to mark controllers and methods with identifiable names that can be used by guards, interceptors, and authorization logic.
Signatures
Feature Decorator
The@Feature decorator is a class decorator that marks a controller with a feature name.
Parameters
string
required
The name of the feature (e.g., ‘Users’, ‘Posts’, ‘Orders’)
Usage
Action Decorator
The@Action decorator is a method decorator that marks a controller method with an action name.
Parameters
string
required
The name of the action (e.g., ‘Create’, ‘Read’, ‘Update’, ‘Delete’)
Usage
Helper Functions
The decorators come with helper functions to retrieve the metadata:Using Helper Functions
Complete Examples
Basic Feature and Action
With Authorization Guard
Using with CRUD
Integration Test Example
From the source tests:packages/crud/test/feature-action.decorator.spec.ts:1-25
Role-Based Access Control
Custom Authorization Service
Implementation Details
packages/crud/src/decorators/feature-action.decorator.ts:1-9
SetMetadata function to store the feature/action name in the class/method metadata.
These decorators are primarily used for authorization and access control. They don’t affect the routing or behavior of your endpoints directly.
Use Cases
- Permission Checking - Verify users have the right permissions for specific features and actions
- Audit Logging - Log which features and actions are being accessed
- Rate Limiting - Apply different rate limits based on feature/action
- Analytics - Track usage of specific features and actions
- Dynamic Menu Generation - Build navigation menus based on user permissions
See Also
- @Crud - Main CRUD decorator
- @Override - Override CRUD routes
- NestJS Guards - Authorization guards
- NestJS Custom Decorators - Creating custom decorators