MCPcopy Create free account
hub / github.com/freecodexyz/free-code / buildFetch

Function buildFetch

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

Source from the content-addressed store, hash-verified

379export const CLIENT_REQUEST_ID_HEADER = 'x-client-request-id'
380
381function 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}

Callers 1

getAnthropicClientFunction · 0.85

Calls 9

getAPIProviderFunction · 0.85
logForDebuggingFunction · 0.85
hasCchPlaceholderFunction · 0.85
computeCchFunction · 0.85
replaceCchPlaceholderFunction · 0.85
setMethod · 0.80
hasMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected