Skip to content

fix(api): add AbortController support to ApiClient - #399

Merged
DeFiVC merged 2 commits into
ChainLearnOfficial:mainfrom
Ayomide3271:fix/issue-300-abort-controller-support
Aug 30, 2026
Merged

fix(api): add AbortController support to ApiClient#399
DeFiVC merged 2 commits into
ChainLearnOfficial:mainfrom
Ayomide3271:fix/issue-300-abort-controller-support

Conversation

@Ayomide3271

Copy link
Copy Markdown
Contributor

Overview

This PR adds AbortController support to the ApiClient so in-flight requests can be cancelled. When a component unmounts mid-request, the controller's signal aborts the fetch, which prevents memory leaks and state updates on unmounted components.

Related Issue

Closes #300

Changes

ApiClient

  • [MODIFY] src/lib/api/client.ts
    • Adds an optional signal parameter to get / post / put / delete and threads it through fetchWithRetry to fetch.
    • External (manual) aborts cancel the in-flight request immediately, never retry, and surface an AbortError so callers can detect them and skip state updates. Internal timeouts keep their existing retry behavior.
    • Exports an isAbortError() helper so consumers can reliably detect cancelled requests (a DOMException, which is not an instanceof Error).
  • [ADD] src/lib/api/client.test.ts
    • Covers pre-aborted signals, in-flight cancellation, "no retry on manual abort", error-store isolation, and preserved TIMEOUT behavior.

API wrappers

  • [MODIFY] src/lib/api/{auth,courses,credentials,notifications,quizzes,rewards}.ts — accept and forward an optional AbortSignal (additive, non-breaking).

Data-fetching hooks

  • [MODIFY] src/lib/hooks/{use-courses,use-credentials,use-notifications,use-quiz,use-rewards}.ts — create an AbortController per request, abort it in useEffect cleanup on unmount, and skip state updates when the request was aborted.

Verification Results

npx vitest run src/lib/api/client.test.ts --pool=threads
✅ 6/6 passed

npx vitest run src/lib/hooks src/tests/auth --pool=threads
✅ 39/39 passed

npx eslint <all changed files>
✅ no issues

npm run typecheck
✅ no errors in changed files (7 pre-existing errors in unrelated files)
Acceptance Criteria Status
Requests can be aborted signal parameter on every ApiClient method, forwarded to fetch
AbortError is handled gracefully ✅ surfaces as AbortError; aborted requests never touch the error store
No state updates after abort ✅ hooks skip setState when isAbortError(err) is true
Cleanup on unmount ✅ every data-fetching hook aborts its controller in effect cleanup

@netlify

netlify Bot commented Aug 30, 2026

Copy link
Copy Markdown

Deploy Preview for chainlearn failed.

Name Link
🔨 Latest commit 1f3b8d2
🔍 Latest deploy log https://app.netlify.com/projects/chainlearn/deploys/6a93f5ad01a1690008c880af

@drips-wave

drips-wave Bot commented Aug 30, 2026

Copy link
Copy Markdown

@Ayomide3271 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! 🚀

Learn more about application limits

@DeFiVC
DeFiVC merged commit 19abe77 into ChainLearnOfficial:main Aug 30, 2026
0 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

. Add api/client.ts abort controller support

2 participants