Skip to main content

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
Both decorators use NestJS’s 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.
Remember to retrieve metadata from ClassName.prototype.methodName when getting action metadata, not from the method directly.

Use Cases

  1. Permission Checking - Verify users have the right permissions for specific features and actions
  2. Audit Logging - Log which features and actions are being accessed
  3. Rate Limiting - Apply different rate limits based on feature/action
  4. Analytics - Track usage of specific features and actions
  5. Dynamic Menu Generation - Build navigation menus based on user permissions

See Also