Decorators
The 150 Endpoints That Needed Logging
// Before - Manual logging everywhere
class UserController {
async getUser(id: string) {
console.log(`[${new Date().toISOString()}] GET /users/${id}`);
const startTime = Date.now();
try {
const user = await this.userService.findById(id);
console.log(`[${new Date().toISOString()}] GET /users/${id} - ${Date.now() - startTime}ms`);
return user;
} catch (error) {
console.error(`[${new Date().toISOString()}] GET /users/${id} - ERROR:`, error);
throw error;
}
}
// ... repeat for 149 more endpoints!
}Enabling Decorators
Class Decorators
Basic Class Decorator
Adding Properties
Sealed Class
Method Decorators
Basic Method Decorator
Measure Performance
Memoization Decorator
Property Decorators
Basic Property Decorator
Parameter Decorators
Basic Parameter Decorator
Decorator Factories
Parameterized Decorator
Retry Decorator
Decorator Composition
Execution Order
Real-World Patterns
1. Authorization Decorator
2. Validation Decorator
3. Caching Decorator
Common Mistakes
1. Forgetting to Enable
2. Arrow Function Methods
3. Not Returning Descriptor
Your Challenge
Key Takeaways
What I Learned
Last updated