Skip to content

feat: typed API client with retry, backoff, and debounce#24

Merged
PAMulligan merged 7 commits intomainfrom
3-api-client-for-cloudflare-workers-chat-endpoint
Mar 21, 2026
Merged

feat: typed API client with retry, backoff, and debounce#24
PAMulligan merged 7 commits intomainfrom
3-api-client-for-cloudflare-workers-chat-endpoint

Conversation

@PAMulligan
Copy link
Copy Markdown
Contributor

Summary

  • Add typed ChatApiClient class in widget/src/api/ with sendMessage method
  • Retry on 429 (respects Retry-After header, capped at 60s) and 503 (exponential backoff: 1s, 3s), max 2 retries
  • Debounced sends (300ms default, configurable) via DebounceError
  • Typed error classes: ChatApiError (status/code/retryAfter) and DebounceError
  • Integrate into useChat hook, replacing inline fetch -- no public API changes
  • Export all types and client from widget package entry point

Test plan

  • pnpm test -- 47 tests passing (18 new API client tests + 29 existing)
  • pnpm build -- both ESM and IIFE builds succeed
  • Existing useChat tests pass unchanged (mock fetch still intercepts)
  • Retry tests use fake timers to verify backoff delays
  • Debounce tests verify rapid-call rejection and window expiry

🤖 Generated with Claude Code

Paul Mulligan and others added 7 commits March 21, 2026 15:31
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Typed API client with POST to /api/chat, retry on 429 (Retry-After)
and 503 (exponential backoff 1s/3s), max 2 retries, debounced sends,
and typed error classes. 13 tests covering all paths.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Replace inline fetch with typed ChatApiClient. Hook now delegates
to client for POST, retry, and error handling. Backwards-compatible
public API preserved.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@PAMulligan PAMulligan linked an issue Mar 21, 2026 that may be closed by this pull request
@cloudflare-workers-and-pages
Copy link
Copy Markdown

Deploying chat-widget with  Cloudflare Pages  Cloudflare Pages

Latest commit: b43e2a8
Status: ✅  Deploy successful!
Preview URL: https://a6c6711d.chat-widget-ejc.pages.dev
Branch Preview URL: https://3-api-client-for-cloudflare.chat-widget-ejc.pages.dev

View logs

@PAMulligan PAMulligan self-assigned this Mar 21, 2026
@PAMulligan PAMulligan added the backend Cloudflare Workers API and AI integration label Mar 21, 2026
@PAMulligan PAMulligan moved this from Todo to Done in PMDS Open Source Roadmap Mar 21, 2026
@PAMulligan PAMulligan added this to the v1.0.0 milestone Mar 21, 2026
@PAMulligan PAMulligan merged commit ffb668a into main Mar 21, 2026
2 checks passed
@PAMulligan PAMulligan deleted the 3-api-client-for-cloudflare-workers-chat-endpoint branch March 30, 2026 01:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Cloudflare Workers API and AI integration

Projects

Development

Successfully merging this pull request may close these issues.

API client for Cloudflare Workers chat endpoint

1 participant