Skip to content
Merged
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
20 changes: 15 additions & 5 deletions src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,12 @@ class ApiClientImpl {
private async requestWithRetry<T>(config: RequestConfig, attempt = 1): Promise<T> {
const controller = new AbortController();
const timeout = config.timeout || this.config.timeout;
const timer = setTimeout(() => controller.abort(), timeout);
const maxRetries = config.retries ?? this.config.maxRetries;
let timedOut = false;
const timer = setTimeout(() => {
timedOut = true;
controller.abort();
}, timeout);

const token = this.getToken();
const headers: HeadersInit = {
Expand All @@ -168,7 +172,6 @@ class ApiClientImpl {
};

try {
// Apply request interceptors
const processedConfig = await this.applyRequestInterceptors({
...config,
headers,
Expand All @@ -181,7 +184,6 @@ class ApiClientImpl {
clearTimeout(timer);

if (!response.ok) {
// Check if we should retry
if (shouldRetry(response.status, attempt, maxRetries)) {
const delay = getRetryDelay(attempt, this.config.retryDelay);
await new Promise((resolve) => setTimeout(resolve, delay));
Expand All @@ -205,18 +207,26 @@ class ApiClientImpl {

const data = (await response.json()) as T;

// Apply response interceptors
const processedResponse = await this.applyResponseInterceptors(data);
return processedResponse;
} catch (err) {
clearTimeout(timer);

const error = err instanceof Error ? err : new Error('Unknown error occurred');

// Apply error interceptors
await this.applyErrorInterceptors(error);

if (err instanceof ApiError) throw err;

// Retry on network failures or internal timeouts (not on non-retriable errors)
const isNetworkError = err instanceof TypeError;
const isTimeout = timedOut && err instanceof DOMException && err.name === 'AbortError';
if ((isNetworkError || isTimeout) && attempt < maxRetries) {
const delay = getRetryDelay(attempt, this.config.retryDelay);
await new Promise((resolve) => setTimeout(resolve, delay));
return this.requestWithRetry<T>(config, attempt + 1);
}

throw parseApiError(err);
}
}
Expand Down
Loading