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