mirror of
https://github.com/duthaho/claudekit.git
synced 2026-06-10 12:14:57 +03:00
3.1 KiB
3.1 KiB
name, description
| name | description |
|---|---|
| api-client | Use when setting up axios, fetch, or httpx clients, implementing request interceptors, adding retry logic, handling authentication tokens, or generating type-safe API clients from OpenAPI specs. Also activate whenever code makes HTTP requests, integrates with external APIs, or needs robust error handling for network calls. |
API Client Patterns
When to Use
- Setting up HTTP clients (httpx, axios, fetch) with base URLs and default headers
- Implementing request/response interceptors for auth tokens, logging, or error transformation
- Adding retry logic with exponential backoff for transient failures
- Generating type-safe API clients from OpenAPI specifications
- Handling authentication tokens (Bearer, API key) in outbound requests
- Building wrapper classes around third-party REST APIs
When NOT to Use
- Building API servers (use
backend-frameworks) - Making simple one-off HTTP calls that don't need a configured client
- GraphQL clients (use Apollo or urql documentation directly)
Quick Reference
| Topic | Reference | Key content |
|---|---|---|
| All client patterns | references/patterns.md |
httpx, axios, fetch wrappers, interceptors, retry, type-safe clients |
| HTTP client recipes | references/http-client-patterns.md |
Advanced patterns, streaming, file uploads |
Best Practices
- Create a single configured client instance. Don't construct new clients per request. Configure base URL, timeouts, and default headers once.
- Set explicit timeouts. Never use default (infinite) timeouts. Set connect and read timeouts separately.
- Use interceptors for cross-cutting concerns. Auth token injection, request logging, and error transformation belong in interceptors, not in each call site.
- Implement retry with exponential backoff and jitter. Only retry idempotent requests (GET, PUT, DELETE) and transient errors (5xx, network errors).
- Respect Retry-After headers. When the server sends
Retry-After, honor it instead of using your own backoff schedule. - Generate clients from OpenAPI specs. Use
openapi-typescript+openapi-fetch(TypeScript) oropenapi-python-client(Python) for type-safe API consumption. - Handle errors at the boundary. Transform HTTP errors into domain-specific errors at the client wrapper level.
Common Pitfalls
- No timeout configured — requests hang indefinitely on unresponsive servers.
- Retrying non-idempotent requests — retrying POST can create duplicates. Use idempotency keys.
- Swallowing error details — wrapping errors without preserving the original status code and message.
- Token refresh race conditions — multiple concurrent requests all try to refresh the token simultaneously. Use a mutex/lock.
- Not closing client connections — httpx AsyncClient and axios instances should be properly closed/disposed.
Related Skills
error-handling— Error transformation and retry patternsauthentication— Token management for API callsbackend-frameworks— Building the APIs these clients consume