( fetchOverride: ClientOptions['fetch'], source: string | undefined, )
| 356 | export const CLIENT_REQUEST_ID_HEADER = 'x-client-request-id' |
| 357 | |
| 358 | function buildFetch( |
| 359 | fetchOverride: ClientOptions['fetch'], |
| 360 | source: string | undefined, |
| 361 | ): ClientOptions['fetch'] { |
| 362 | // eslint-disable-next-line eslint-plugin-n/no-unsupported-features/node-builtins |
| 363 | const inner = fetchOverride ?? globalThis.fetch |
| 364 | // Only send to the first-party API — Bedrock/Vertex/Foundry don't log it |
| 365 | // and unknown headers risk rejection by strict proxies (inc-4029 class). |
| 366 | const injectClientRequestId = |
| 367 | getAPIProvider() === 'firstParty' && isFirstPartyAnthropicBaseUrl() |
| 368 | return (input, init) => { |
| 369 | // eslint-disable-next-line eslint-plugin-n/no-unsupported-features/node-builtins |
| 370 | const headers = new Headers(init?.headers) |
| 371 | // Generate a client-side request ID so timeouts (which return no server |
| 372 | // request ID) can still be correlated with server logs by the API team. |
| 373 | // Callers that want to track the ID themselves can pre-set the header. |
| 374 | if (injectClientRequestId && !headers.has(CLIENT_REQUEST_ID_HEADER)) { |
| 375 | headers.set(CLIENT_REQUEST_ID_HEADER, randomUUID()) |
| 376 | } |
| 377 | try { |
| 378 | // eslint-disable-next-line eslint-plugin-n/no-unsupported-features/node-builtins |
| 379 | const url = input instanceof Request ? input.url : String(input) |
| 380 | const id = headers.get(CLIENT_REQUEST_ID_HEADER) |
| 381 | logForDebugging( |
| 382 | `[API REQUEST] ${new URL(url).pathname}${id ? ` ${CLIENT_REQUEST_ID_HEADER}=${id}` : ''} source=${source ?? 'unknown'}`, |
| 383 | ) |
| 384 | } catch { |
| 385 | // never let logging crash the fetch |
| 386 | } |
| 387 | return inner(input, { ...init, headers }) |
| 388 | } |
| 389 | } |
no test coverage detected