Skip to content

feat: add internal timeout and AbortSignal composition to api client#378

Closed
Devadakene wants to merge 1 commit into
AnchorNet-Org:mainfrom
Devadakene:main
Closed

feat: add internal timeout and AbortSignal composition to api client#378
Devadakene wants to merge 1 commit into
AnchorNet-Org:mainfrom
Devadakene:main

Conversation

@Devadakene

Copy link
Copy Markdown
Contributor

Closes #156

PR Description

Type of Change

  • Feature (non-breaking change which adds functionality)
  • Refactor (non-breaking change which improves code quality and maintainability)

Description

This pull request adds an internal timeout and robust AbortSignal composition to the HTTP client functions apiRequest and apiTextRequest in api.ts to prevent requests from hanging indefinitely on stalled backends.

Key Changes

  1. Configurable Timeout Support:
    • Introduced ApiRequestInit which extends standard RequestInit with an optional timeout?: number property.
    • Added a global configurable default timeout (globalDefaultTimeoutMs set to 10s / 10000ms) along with a helper function setDefaultTimeout.
  2. Signal Composition:
    • Implemented a helper function composeSignals which combines the internal timeout-based AbortController with any caller-supplied AbortSignal.
    • The combined signal will abort if either the caller's signal is aborted or the internal timeout fires.
  3. Timeout Identification:
    • Timeouts are caught and surfaced distinct from standard caller aborts. They are thrown as ApiRequestError with status code 408 and the error code set to "TIMEOUT".
    • Caller aborts still propagate as standard AbortError DOMExceptions, ensuring isAbortError returns true for intentional cancellations.
  4. Enhanced Test Coverage:
    • Extended api.test.ts with test cases covering timeouts, independent caller aborts, and timeout priority under combined signals.
    • Ensured 100% test coverage for the new code paths.

Verification Results

All tests passed successfully:

 RUN  v2.1.9 C:/Users/HP/drips/work/AnchorNet-Frontend
      Coverage enabled with v8

 ✓ src/lib/api.test.ts (26 tests) 110ms

 Test Files  1 passed (1)
      Tests  26 passed (26)

 % Coverage report from v8
----------------|---------|----------|---------|---------|-------------------
File            | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------------|---------|----------|---------|---------|-------------------
All files       |     100 |      100 |     100 |     100 |                   
 api.ts         |     100 |      100 |     100 |     100 |                   
----------------|---------|----------|---------|---------|-------------------

@Jagadeeshftw

Copy link
Copy Markdown
Contributor

Thanks — this is the same api.ts timeout/AbortSignal composition commit that already landed via #380. Zero net diff after merging main in, so closing as already covered.

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 a request timeout to apiRequest and apiTextRequest in lib/api.ts

2 participants