@farm-framework/api-client
v0.2.0
Published
HTTP client utilities for FARM framework applications
Maintainers
Readme
@farm/api-client
Overview
@farm/api-client provides a thin wrapper around Axios with helpful utilities for FARM applications. It simplifies HTTP requests, adds smart retry logic, streaming helpers and file upload support while exposing a clean interface for extending request and response interceptors.
This library underpins the generated API clients and React hooks used throughout FARM projects.
✅ Completed Implementation
Core Components
ApiClient(src/base-client.ts)- Wraps an Axios instance with sane defaults
- Supports all standard HTTP verbs
- Handles retry logic and error formatting
- Provides streaming and file upload helpers
- Allows runtime configuration updates
createApiClient(src/base-client.ts)- Factory function returning a pre-configured
ApiClient - Registers default
authandlogginginterceptors - Enables custom overrides via
ApiClientConfig
- Factory function returning a pre-configured
Interceptors
authInterceptor– attaches a bearer token from local storageloggingInterceptor– logs requests and responses in development mode
Additional Utilities
- Type definitions for
ApiResponse,ApiErrorandPaginatedResponse - Helper types for building request and response interceptors
Architecture
┌─────────────────────────────────────────────┐
│ ApiClient │
├─────────────────────────────────────────────┤
│ Axios Instance │
│ ├─ Request Interceptors (auth, custom) │
│ ├─ Response Interceptors (logging, custom)│
│ ├─ Retry Handler │
│ └─ Error Formatter │
├─────────────────────────────────────────────┤
│ Utility Methods │
│ ├─ get/post/put/patch/delete │
│ ├─ stream/streamPost │
│ └─ uploadFile │
└─────────────────────────────────────────────┘
▲
│ createApiClient()
▼
Preconfigured ApiClient instanceFeatures Implemented
- HTTP Helpers – concise methods for all HTTP verbs
- Request/Response Interceptors – attach custom logic to every request
- Retry with Exponential Backoff – configurable retry conditions
- Detailed Error Messages – normalized errors with status-aware messages
- Streaming Support –
streamandstreamPostfor SSE endpoints - File Uploads – multipart helper with progress callbacks
- Runtime Configuration Updates – modify base URL, headers and timeout
- Default Auth & Logging Interceptors – sensible defaults for FARM apps
Usage
Basic Commands
# Build the library
pnpm run --filter @farm/api-client build
# Watch and rebuild on changes
pnpm run --filter @farm/api-client build:watch
# Type-check the source
pnpm run --filter @farm/api-client type-checkExample
import { createApiClient } from "@farm/api-client";
const api = createApiClient({ baseURL: "http://localhost:8000/api" });
const result = await api.get("/users");
console.log(result.data);Next Steps
Future enhancements include:
- Generated Typed Clients – automatic client generation from OpenAPI schemas
- Advanced Caching Layer – optional caching for GET requests
- Batch Request Support – combine multiple calls into a single request
Files Structure
packages/api-client/
├── src/
│ ├── base-client.ts # ApiClient implementation and helpers
│ └── index.ts # Main exports
├── package.json # Package configuration
├── tsconfig.json # TypeScript configuration
└── tsup.config.ts # Build configurationIntegration
@farm/api-client is used by:
- Generated API clients in FARM applications
- React hooks within
@farm/ui-components - Any custom frontend or Node.js service that communicates with the FARM backend
File Overview
- src/base-client.ts – main
ApiClientclass and helpers - src/index.ts – public exports for consumers
- package.json – scripts and dependencies
- tsup.config.ts – build pipeline configuration
