When building applications that allow users to follow channels or creators, designing clear and intuitive RESTful API routes is crucial. This guide explores the optimal API route structure for a "follow channel" feature, especially when the action is initiated from the channel's page.
📌 Scenario
Your application has two main frontend features:
- 👤 User Feature: Manages user profiles, settings, and user-specific data.
- 📺 Channel Feature: Displays content channels that users can follow, view, and interact with.
Users perform the follow action from within the Channel feature. This context influences the best approach for structuring your API routes.
🔍 API Route Options
1. Channel-Centric Route
POST /channels/:channelId/follow
DELETE /channels/:channelId/follow
Pros:
- Contextual Alignment: Reflects that the action targets a specific channel.
- Simplicity: Clear and concise, making it easy to understand and use.
- Scalability: Simplifies adding similar actions (e.g., like, share) under the channel.
Cons:
- Less User-Focused: Doesn't directly represent the relationship from the user's perspective, which might be needed for user-specific queries.
2. User-Centric Route
POST /users/:userId/followed-channels/:channelId
DELETE /users/:userId/followed-channels/:channelId
Pros:
- User-Focused: Clearly shows the relationship from the user's side, beneficial for operations like fetching all followed channels.
- Flexibility: Easier to manage user-specific follow operations and extend in the future.
Cons:
- Verbosity: Longer and might seem less intuitive when the action is initiated from the channel context.
🏆 Recommendation
Use the Channel-Centric Route (/channels/:channelId/follow
) because:
- Context Matters: Actions are performed from the channel page, making the route intuitive and relevant.
- Clarity: Directly indicates the target of the action, enhancing readability and maintainability.
- Consistency: Simplifies adding similar actions in the future, maintaining a uniform API structure.
Implementation Overview
- Follow a Channel:
POST /channels/:channelId/follow
- Unfollow a Channel:
DELETE /channels/:channelId/follow
Supporting User-Centric Operations
While the primary routes are channel-centric, it's essential to provide user-centric endpoints for comprehensive functionality.
- Get All Followed Channels by a User:
GET /users/:userId/followed-channels
📝 Conclusion
Choosing the right API route structure depends on the context of the action within your application. Since users follow channels from the channel page, a channel-centric route (/channels/:channelId/follow
) is more intuitive and maintainable. Additionally, providing user-centric endpoints ensures that all necessary operations are well-supported, offering flexibility and comprehensive functionality.
Happy Coding! 🎉
Feel free to share your thoughts or questions in the comments below! 💬
Top comments (0)