MCPcopy Index your code
hub / github.com/anomalyco/opencode / fetch

Function fetch

packages/opencode/src/plugin/snowflake-cortex.ts:322–455  ·  view source on GitHub ↗
(requestInput: RequestInfo | URL, init?: RequestInit)

Source from the content-addressed store, hash-verified

320 return {
321 apiKey: OAUTH_DUMMY_KEY,
322 async fetch(requestInput: RequestInfo | URL, init?: RequestInit) {
323 let currentAuth = await getAuth()
324 if (currentAuth.type !== "oauth") return fetch(requestInput, init)
325 let currentOauth = currentAuth as typeof currentAuth & {
326 refresh: string
327 access: string
328 expires: number
329 accountId?: string
330 }
331
332 if (!currentOauth.accountId) throw new Error("Snowflake OAuth auth is missing accountId")
333 const accountId = currentOauth.accountId
334
335 const refresh = async () => {
336 if (!refreshPromise) {
337 const refreshToken = currentOauth.refresh
338 refreshPromise = refreshAccessToken(accountId, refreshToken)
339 .then(async (tokens) => {
340 const refreshedRefresh = tokens.refresh_token || refreshToken
341 const refreshedExpires = Date.now() + (tokens.expires_in ?? 600) * 1000
342 await _input.client.auth
343 .set({
344 path: { id: "snowflake-cortex" },
345 body: {
346 type: "oauth",
347 access: tokens.access_token,
348 refresh: refreshedRefresh,
349 expires: refreshedExpires,
350 ...(accountId && { accountId }),
351 },
352 })
353 .catch(() => {})
354 return {
355 access: tokens.access_token,
356 refresh: refreshedRefresh,
357 expires: refreshedExpires,
358 }
359 })
360 .finally(() => {
361 refreshPromise = undefined
362 })
363 }
364
365 const refreshed = await refreshPromise
366 currentOauth = { ...currentOauth, ...refreshed }
367 }
368
369 const prepareRequest = () => {
370 const headers = new Headers(requestInput instanceof Request ? requestInput.headers : undefined)
371 if (init?.headers) {
372 const entries =
373 init.headers instanceof Headers
374 ? init.headers.entries()
375 : Array.isArray(init.headers)
376 ? init.headers
377 : Object.entries(init.headers as Record<string, string | undefined>)
378 for (const [key, value] of entries) {
379 if (value !== undefined) headers.set(key, String(value))

Callers 2

exchangeCodeForTokenFunction · 0.70
refreshAccessTokenFunction · 0.70

Calls 3

prepareRequestFunction · 0.85
transformResponseFunction · 0.85
refreshFunction · 0.70

Tested by

no test coverage detected