(result: RateLimitResult)
| 126 | } |
| 127 | |
| 128 | export function createRateLimitResponse(result: RateLimitResult): NextResponse { |
| 129 | const headers = { |
| 130 | 'X-RateLimit-Limit': result.limit.toString(), |
| 131 | 'X-RateLimit-Remaining': result.remaining.toString(), |
| 132 | 'X-RateLimit-Reset': result.resetAt.toISOString(), |
| 133 | } |
| 134 | |
| 135 | if (result.error) { |
| 136 | return NextResponse.json({ error: result.error || 'Unauthorized' }, { status: 401, headers }) |
| 137 | } |
| 138 | |
| 139 | const retryAfterSeconds = result.retryAfterMs |
| 140 | ? Math.ceil(result.retryAfterMs / 1000) |
| 141 | : Math.ceil((result.resetAt.getTime() - Date.now()) / 1000) |
| 142 | |
| 143 | return NextResponse.json( |
| 144 | { |
| 145 | error: 'Rate limit exceeded', |
| 146 | message: `API rate limit exceeded. Please retry after ${result.resetAt.toISOString()}`, |
| 147 | retryAfter: result.resetAt.getTime(), |
| 148 | }, |
| 149 | { |
| 150 | status: 429, |
| 151 | headers: { |
| 152 | ...headers, |
| 153 | 'Retry-After': retryAfterSeconds.toString(), |
| 154 | }, |
| 155 | } |
| 156 | ) |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Verify that the API key is allowed to access the requested workspace. |
no test coverage detected