Skip to content

feat(request): refactor to thenable handle with lifecycle callbacks#57

Open
cc-hearts wants to merge 1 commit into
mainfrom
feat/request-redesign
Open

feat(request): refactor to thenable handle with lifecycle callbacks#57
cc-hearts wants to merge 1 commit into
mainfrom
feat/request-redesign

Conversation

@cc-hearts
Copy link
Copy Markdown
Member

Summary

Refactor `Request` return type from state object to `RequestHandle` — a thenable that can be directly awaited, with lifecycle callbacks as an alternative API surface.

Breaking changes

Before After
`await api.get('/u').promise` `await api.get('/u')`
`result.data`, `result.error`, `result.loading` removed — use await value / try-catch / callbacks
`result.aborted` removed — check `AbortError` name or use `onAbort` callback
Promise always resolves with `T null`

New features

  • Lifecycle callbacks: `onSuccess`, `onError`, `onAbort`, `onFinally` on `RequestConfig`
  • Timeout: per-attempt timeout via `RequestConfig.timeout`
  • Retry: automatic retry with `retry` / `retryDelay`
  • Cache: in-memory GET response cache with TTL
  • Deduplication: concurrent GET requests share the same inflight promise
  • Download progress: `onDownloadProgress(loaded, total)` callback
  • Richer body support: `Blob`, `URLSearchParams` pass through without JSON.stringify
  • Full response type parsing: `204/205 → null`, binary → `Blob`, uses `MIME_TYPES` constants

Docs

  • JSDoc with composable best practices on `Request` class (English)
  • `README.md`: "Request — composable best practices" section (English)
  • `README_ZH.md`: Chinese version with same content + config reference

Tests

36 tests total (19 new), all passing.

…lbacks

- RequestReturn → RequestHandle<T> (thenable + abort())
- Remove .promise — directly awaitable
- Remove loading/error/data/aborted fields
- Add onSuccess/onError/onAbort/onFinally callbacks
- Add timeout, retry, cache, deduplication, download progress
- Support Blob/URLSearchParams body, 204/205 response, binary blob
- Add 19 new tests, 36 total request tests passing
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.

1 participant