fix(api): add timeout and cancellation to apiFetch (#54) - #197
Merged
presidojay1 merged 2 commits intoSep 26, 2026
Merged
Conversation
A stalled backend left buttons stuck in 'Loading…/Working…' because fetch had no timeout and callers couldn't abort. - RequestOptions gains an optional `signal` and `timeoutMs` (default 15s via AbortSignal.timeout, combined with AbortSignal.any). - Timeouts and network failures throw a new NetworkError (an ApiError with status 0), so actionErrorMessage already shows its friendly message. - A caller-initiated abort rethrows the original AbortError instead of being reported as a network failure.
❌ Deploy Preview for stellartickets failed.
|
|
@boluwacodes Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
apiFetch(src/lib/api.ts) calledfetchwith no timeout and no way for callers to abort. When the backend stalled, buttons stayed on "Loading…" / "Working…" forever.Changes
src/lib/api.tsRequestOptionsgets two optional fields:signal?: AbortSignal, for caller-controlled cancellation such as on unmount;timeoutMs?: number, defaulting to the new exportedDEFAULT_TIMEOUT_MS = 15_000.0disables the timeout.AbortSignal.timeout(timeoutMs)is combined with the caller's signal viaAbortSignal.anyand passed tofetch.NetworkError: extendsApiErrorwithstatus = 0. Existinginstanceof ApiErrorhandling, such asactionErrorMessage, therefore shows its friendly message with no call-site changes:AbortErroris rethrown, so a deliberate cancel isn't reported to the user as a network failure.HTTP error handling (non-2xx →
ApiError) and 204 handling are unchanged.src/lib/api-timeout.test.ts(new)NetworkError(status 0, "took too long").fetchrejection (TypeError: Failed to fetch) becomes aNetworkError.abort()rethrows theAbortError, not aNetworkError.AbortSignalis always passed tofetchby default.Commits
fix(api): add timeout and cancellation to apiFetch (#54)test(api): cover apiFetch timeout, network failure and caller abort (#54)Testing
Not run locally. To check:
npm test. It relies onAbortSignal.timeout/AbortSignal.any, which Node 20+ and all current browsers support.Closes #54
Closes #55
Closes #56
Closes #57