( fetchOverride: ClientOptions['fetch'], source: string | undefined, )
| 379 | export const CLIENT_REQUEST_ID_HEADER = 'x-client-request-id' |
| 380 | |
| 381 | function buildFetch( |
| 382 | fetchOverride: ClientOptions['fetch'], |
| 383 | source: string | undefined, |
| 384 | ): ClientOptions['fetch'] { |
| 385 | // eslint-disable-next-line eslint-plugin-n/no-unsupported-features/node-builtins |
| 386 | const inner = fetchOverride ?? globalThis.fetch |
| 387 | // Only send to the first-party API — Bedrock/Vertex/Foundry don't log it |
| 388 | // and unknown headers risk rejection by strict proxies (inc-4029 class). |
| 389 | const injectClientRequestId = |
| 390 | getAPIProvider() === 'firstParty' && isFirstPartyAnthropicBaseUrl() |
| 391 | return async (input, init) => { |
| 392 | // eslint-disable-next-line eslint-plugin-n/no-unsupported-features/node-builtins |
| 393 | const headers = new Headers(init?.headers) |
| 394 | if (injectClientRequestId && !headers.has(CLIENT_REQUEST_ID_HEADER)) { |
| 395 | headers.set(CLIENT_REQUEST_ID_HEADER, randomUUID()) |
| 396 | } |
| 397 | |
| 398 | let body = init?.body |
| 399 | try { |
| 400 | // eslint-disable-next-line eslint-plugin-n/no-unsupported-features/node-builtins |
| 401 | const url = input instanceof Request ? input.url : String(input) |
| 402 | const id = headers.get(CLIENT_REQUEST_ID_HEADER) |
| 403 | logForDebugging( |
| 404 | `[API REQUEST] ${new URL(url).pathname}${id ? ` ${CLIENT_REQUEST_ID_HEADER}=${id}` : ''} source=${source ?? 'unknown'}`, |
| 405 | ) |
| 406 | |
| 407 | if ( |
| 408 | url.includes('/v1/messages') && |
| 409 | headers.has('anthropic-version') && |
| 410 | typeof body === 'string' && |
| 411 | hasCchPlaceholder(body) |
| 412 | ) { |
| 413 | const cch = await computeCch(body) |
| 414 | body = replaceCchPlaceholder(body, cch) |
| 415 | logForDebugging(`[CCH] signed request cch=${cch}`) |
| 416 | } |
| 417 | } catch { |
| 418 | // never let logging crash the fetch |
| 419 | } |
| 420 | return inner(input, { ...init, headers, body }) |
| 421 | } |
| 422 | } |
no test coverage detected