MCPcopy Create free account
hub / github.com/anomalyco/opencode / fetch

Function fetch

packages/opencode/src/plugin/openai/codex.ts:334–417  ·  view source on GitHub ↗
(requestInput: RequestInfo | URL, init?: RequestInit)

Source from the content-addressed store, hash-verified

332 return {
333 apiKey: OAUTH_DUMMY_KEY,
334 async fetch(requestInput: RequestInfo | URL, init?: RequestInit) {
335 if (init?.headers) {
336 if (init.headers instanceof Headers) {
337 init.headers.delete("authorization")
338 init.headers.delete("Authorization")
339 } else if (Array.isArray(init.headers)) {
340 init.headers = init.headers.filter(([key]) => key.toLowerCase() !== "authorization")
341 } else {
342 delete init.headers["authorization"]
343 delete init.headers["Authorization"]
344 }
345 }
346
347 const currentAuth = await getAuth()
348 if (currentAuth.type !== "oauth")
349 return websocketFetch ? websocketFetch(requestInput, init) : fetch(requestInput, init)
350
351 const authWithAccount = currentAuth as typeof currentAuth & { accountId?: string }
352
353 if (!currentAuth.access || currentAuth.expires < Date.now()) {
354 if (!refreshPromise) {
355 refreshPromise = refreshAccessToken(currentAuth.refresh, issuer)
356 .then(async (tokens) => {
357 const accountId = extractAccountId(tokens) || authWithAccount.accountId
358 await input.client.auth.set({
359 path: { id: "openai" },
360 body: {
361 type: "oauth",
362 refresh: tokens.refresh_token,
363 access: tokens.access_token,
364 expires: Date.now() + (tokens.expires_in ?? 3600) * 1000,
365 ...(accountId && { accountId }),
366 },
367 })
368 return {
369 access: tokens.access_token,
370 accountId,
371 }
372 })
373 .finally(() => {
374 refreshPromise = undefined
375 })
376 }
377
378 const refreshed = await refreshPromise
379 currentAuth.access = refreshed.access
380 authWithAccount.accountId = refreshed.accountId
381 }
382
383 const headers = new Headers()
384 if (init?.headers) {
385 if (init.headers instanceof Headers) {
386 init.headers.forEach((value, key) => headers.set(key, value))
387 } else if (Array.isArray(init.headers)) {
388 for (const [key, value] of init.headers) {
389 if (value !== undefined) headers.set(key, String(value))
390 }
391 } else {

Callers 4

exchangeCodeForTokensFunction · 0.70
refreshAccessTokenFunction · 0.70
CodexAuthPluginFunction · 0.70
callbackFunction · 0.70

Calls 5

websocketFetchFunction · 0.85
extractAccountIdFunction · 0.85
refreshAccessTokenFunction · 0.70
deleteMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected