MCPcopy Index your code
hub / github.com/Noumena-Network/code / buildFetch

Function buildFetch

src/services/api/client.ts:372–403  ·  view source on GitHub ↗
(
  fetchOverride: ClientOptions['fetch'],
  source: string | undefined,
)

Source from the content-addressed store, hash-verified

370}
371
372function 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}

Callers 1

getWrappedClientFetchFunction · 0.85

Calls 7

randomUUIDFunction · 0.90
getAPIProviderFunction · 0.85
setMethod · 0.80
logForDebuggingFunction · 0.50
hasMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected