Usage Patterns¶
Acai-TS offers two powerful patterns for defining API Gateway endpoints, each with its own strengths and ideal use cases.
Pattern Overview¶
🔧 Functional Pattern¶
Configuration-driven approach using exported functions with requirements objects
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
✅ Best for: - Teams who prefer explicit configuration objects - JavaScript codebases transitioning to TypeScript - Reusable middleware configurations - Runtime configuration changes - Developers familiar with traditional frameworks
🎨 Decorator Pattern¶
Annotation-style approach using TypeScript decorators on class methods
1 2 3 4 5 6 7 8 9 10 11 | |
✅ Best for: - TypeScript-first development - Teams who love declarative annotations - Method-level configuration preferences - Clean, co-located configuration with handlers - Modern TypeScript codebases
Key Similarities¶
Both patterns share the same core features:
- 🗂️ File-based routing - Routes determined by file structure
- 🔍 Schema validation - OpenAPI and JSON Schema support
- 🔐 Authentication - Global and method-level auth controls
- 🔄 Middleware - Before/after processing hooks
- ⏱️ Timeouts - Request timeout management
- 📝 Type safety - Full TypeScript support
Quick Comparison¶
| Feature | Functional | Decorators |
|---|---|---|
| Configuration Style | Object-based | Annotation-based |
| TypeScript Required | No (works with JS) | Yes |
| Learning Curve | Lower | Moderate |
| Runtime Changes | Easy | Limited |
| Co-location | Separate | Inline |
| Reusability | High | Moderate |
File Structure (Both Patterns)¶
Both patterns use identical file-based routing:
1 2 3 4 5 6 7 | |
Router Configuration (Both Patterns)¶
Both patterns use the same router setup:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
Migration Between Patterns¶
You can easily migrate between patterns since they're functionally equivalent:
1 2 3 4 5 6 7 8 9 | |
Which Pattern Should You Choose?¶
Choose Functional if you:¶
- 🔧 Prefer explicit configuration objects
- 📦 Want maximum reusability of middleware configs
- 🔄 Need runtime configuration changes
- 👥 Have mixed JS/TS team members
- 🏗️ Are migrating from traditional frameworks
Choose Decorators if you:¶
- 🎨 Love clean, declarative annotations
- 🔗 Want configuration co-located with methods
- 💎 Are building pure TypeScript applications
- 🆕 Starting fresh with modern patterns
- 📝 Prefer method-level configuration visibility
Next Steps¶
- Functional Pattern Guide → - Learn the requirements object approach
- Decorator Pattern Guide → - Master TypeScript decorators
- Event Processing → - Handle DynamoDB, S3, and SQS events
Both patterns are powerful and well-supported. Choose based on your team's preferences and project requirements!