Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@ import { accessTokenExpired, isOAuthAuth } from './plugin/auth';
import { logDebug, logError } from './plugin/debug';
import { fetchBergetModels } from './plugin/models';
import { createPkceAuthorizeMethod } from './plugin/pkce-flow';
import { resilientFetch } from './plugin/resilient-fetch';
import { refreshAccessTokenDirect } from './plugin/token';

type FetchInput = Request | string | URL;

/**
* Wraps a native fetch call while preserving headers from both Request
* Wraps a resilient fetch call while preserving headers from both Request
* objects and init, then injecting (or overwriting) Authorization.
*
* Uses resilientFetch which retries transient network errors (ECONNRESET,
* ETIMEDOUT, socket hang up) and server errors (502/503/504) with
* exponential backoff and jitter.
*/
async function fetchWithAuth(
authToken: string,
Expand All @@ -38,7 +43,7 @@ async function fetchWithAuth(

const headers = new Headers(request.headers);
headers.set('Authorization', `Bearer ${authToken}`);
return fetch(request, { headers });
return resilientFetch(request, { headers });
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/plugin/auth.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { describe, expect, it } from 'vitest';

import type { OAuthAuthDetails } from './types';

import { ACCESS_TOKEN_EXPIRY_BUFFER_MS } from '../constants';
import { accessTokenExpired } from './auth';
import type { OAuthAuthDetails } from './types';

describe('accessTokenExpired - Issue #5', () => {
// With raw expiry storage, the buffer lives ONLY in the check.
Expand Down
3 changes: 2 additions & 1 deletion src/plugin/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import { getModelsEndpoint } from '../constants';
import { logDebug, logError } from './debug';
import { resilientFetch } from './resilient-fetch';

// Response from /v1/models/chat endpoint
interface ChatModel {
Expand Down Expand Up @@ -49,7 +50,7 @@ export async function fetchBergetModels(): Promise<Record<string, object>> {
logDebug('Fetching chat models from Berget API');

try {
const response = await fetch(getModelsEndpoint(), {
const response = await resilientFetch(getModelsEndpoint(), {
headers: {
'Content-Type': 'application/json',
},
Expand Down
Loading