| 35 | * }, { track: false }); |
| 36 | */ |
| 37 | export function apiHandler<H extends AnyHandler>( |
| 38 | handler: H, |
| 39 | config: ApiHandlerConfig = {} |
| 40 | ): H { |
| 41 | const { track = true } = config; |
| 42 | |
| 43 | const wrappedHandler = async (request: NextRequest, ...rest: unknown[]) => { |
| 44 | if (track) { |
| 45 | const path = request.nextUrl.pathname; |
| 46 | const method = request.method; |
| 47 | const source = request.headers.get('X-Sourcebot-Client-Source') ?? 'unknown'; |
| 48 | |
| 49 | // Fire and forget - don't await to avoid blocking the request |
| 50 | captureEvent('api_request', { path, method, source }); |
| 51 | } |
| 52 | |
| 53 | // Call the original handler with all arguments |
| 54 | return handler(request, ...rest); |
| 55 | }; |
| 56 | |
| 57 | return wrappedHandler as H; |
| 58 | } |