feat: REST user usage and stats
Implemented GET /api/usage endpoint that returns usage events and statistics for the authenticated user.
- Added
UserUsageEventQueryinterface for user-specific queries - Added
findByUser()method to retrieve usage events for a specific user - Added
aggregateByUser()method to calculate total usage statistics with breakdown by API - Updated
UsageEventsRepositoryinterface to include new methods
- Replaced placeholder GET /api/usage route with authenticated implementation
- Added
requireAuthmiddleware to enforce JWT authentication - Implemented comprehensive query parameter validation:
fromandtodate parameters with ISO format validationlimitparameter for pagination (non-negative integer)apiIdparameter for filtering by specific API
- Smart default period handling:
- Default: last 30 days when no dates provided
- If only
fromprovided: use current time asto - If only
toprovided: use 30 days beforetoasfrom
{
"events": [
{
"id": "event-id",
"apiId": "api-id",
"endpoint": "/api/endpoint",
"occurredAt": "2024-01-15T10:00:00.000Z",
"revenue": "1000000"
}
],
"stats": {
"totalCalls": 10,
"totalSpent": "4500000",
"breakdownByApi": [
{
"apiId": "api1",
"calls": 7,
"revenue": "3000000"
}
]
},
"period": {
"from": "2024-01-15T00:00:00.000Z",
"to": "2024-02-15T00:00:00.000Z"
}
}- Created
userUsage.test.tswith 12 test cases covering:- Authentication requirements
- Default period behavior
- Date range filtering
- API ID filtering
- Limit parameter functionality
- Parameter validation
- Edge cases (empty results, invalid dates)
- Response format validation
✅ JWT Authentication: Requires valid Bearer token or x-user-id header ✅ Flexible Date Ranges: Support for custom periods with smart defaults ✅ API Filtering: Filter usage by specific API ID ✅ Pagination: Limit number of returned events ✅ Comprehensive Stats: Total calls, total spent, and breakdown by API ✅ Input Validation: Robust parameter validation with clear error messages ✅ Type Safety: Full TypeScript support with proper interfaces
- Uses existing
requireAuthmiddleware for JWT validation - Input validation prevents injection attacks
- Users can only access their own usage data
- No sensitive information exposure
- 12 comprehensive test cases with high coverage
- Tests cover authentication, validation, filtering, and edge cases
- Mock repository for isolated testing
- Response format validation
# Get usage for last 30 days (default)
GET /api/usage
Authorization: Bearer <jwt-token>
# Get usage for custom date range
GET /api/usage?from=2024-01-01T00:00:00Z&to=2024-01-31T23:59:59Z
Authorization: Bearer <jwt-token>
# Get usage for specific API with limit
GET /api/usage?apiId=api1&limit=10
Authorization: Bearer <jwt-token>src/repositories/usageEventsRepository.ts- Extended repository interface and implementationsrc/app.ts- Implemented authenticated routesrc/__tests__/userUsage.test.ts- Added comprehensive test suite
✅ Requires wallet auth (JWT) ✅ Default period: last 30 days ✅ Query params: from, to, limit, apiId ✅ Returns usage events for current user ✅ Returns total spent in period ✅ Optional breakdown by API ✅ Uses usage_events repository ✅ Includes requireAuth middleware