* Add internal authentication token to headers if running on server * @param headers - Headers object to modify * @param isInternalRoute - Whether the target URL is an internal route * @param requestId - Request ID for logging * @param context - Context string for logging (e.g., toolId or 'proxy
( headers: Headers | Record<string, string>, isInternalRoute: boolean, requestId: string, context: string, userId?: string )
| 1419 | * @param context - Context string for logging (e.g., toolId or 'proxy') |
| 1420 | */ |
| 1421 | async function addInternalAuthIfNeeded( |
| 1422 | headers: Headers | Record<string, string>, |
| 1423 | isInternalRoute: boolean, |
| 1424 | requestId: string, |
| 1425 | context: string, |
| 1426 | userId?: string |
| 1427 | ): Promise<void> { |
| 1428 | if (typeof window === 'undefined') { |
| 1429 | if (isInternalRoute) { |
| 1430 | try { |
| 1431 | const internalToken = await generateInternalToken(userId) |
| 1432 | if (headers instanceof Headers) { |
| 1433 | headers.set('Authorization', `Bearer ${internalToken}`) |
| 1434 | } else { |
| 1435 | headers.Authorization = `Bearer ${internalToken}` |
| 1436 | } |
| 1437 | logger.info(`[${requestId}] Added internal auth token for ${context}`) |
| 1438 | } catch (error) { |
| 1439 | logger.error(`[${requestId}] Failed to generate internal token for ${context}:`, error) |
| 1440 | } |
| 1441 | } else { |
| 1442 | logger.info(`[${requestId}] Skipping internal auth token for external URL: ${context}`) |
| 1443 | } |
| 1444 | } |
| 1445 | } |
| 1446 | |
| 1447 | interface ResolvedRetryConfig { |
| 1448 | maxRetries: number |
no test coverage detected