-
A lightweight, type-safe HTTP client with interceptors, automatic retries, and comprehensive error handling.
-
install
spacefirst.# install space i @je-es/capi// import import { api, http, configureApi } from '@je-es/capi';
-
-
// GET request const { data } = await http.get('/api/users'); // POST request const response = await http.post('/api/users', { name : 'John Doe', email : 'john@example.com' }); // Other methods await http.put ('/api/users/1', userData); await http.patch ('/api/users/1', { name: 'Jane' }); await http.delete ('/api/users/1');
-
// Set base URL and default headers configureApi({ baseURL : 'https://api.example.com', timeout : 10000, headers : { 'Authorization': 'Bearer token123', 'Content-Type': 'application/json' } });
-
// Automatically handles URL encoding const { data } = await http.get('/api/search', { params: { q: 'typescript', page: 1, limit: 10 } }); // → /api/search?q=typescript&page=1&limit=10
-
configureApi({ interceptors: { request: (config) => { // Add authentication token config.headers['Authorization'] = `Bearer ${getToken()}`; return config; } } });
-
configureApi({ interceptors: { response: (response) => { // Transform response data console.log('Response received:', response.status); return response; } } });
-
configureApi({ interceptors: { error: (error) => { if (error.status === 401) { // Redirect to login window.location.href = '/login'; } throw error; // Re-throw if not handled } } }); // Or handle per-request try { const { data } = await http.get('/api/protected'); } catch (error) { console.error('Request failed:', error.message); }
-
const formData = new FormData(); formData.append('file', fileInput.files[0]); formData.append('name', 'document.pdf'); await http.post('/api/upload', formData);
-
interface User { id : number; name : string; email : string; } // Type-safe responses const { data } = await http.get<User[]>('/api/users'); // data is typed as User[] const user = await http.post<User>('/api/users', { name : 'John', email : 'john@example.com' }); // user.data is typed as User
-
// Custom timeout per request await http.get('/api/slow', { timeout: 5000 }); // Custom headers per request await http.get('/api/data', { headers: { 'X-Custom-Header': 'value' } }); // Using the low-level api function const response = await api({ method : 'POST', url : '/api/endpoint', data : { key: 'value' }, timeout : 15000 });
-
import { resetApiConfig } from '@je-es/capi'; // Reset to defaults resetApiConfig();
-
-
-
Notifications
You must be signed in to change notification settings - Fork 0
A lightweight, type-safe HTTP client with interceptors, automatic retries, and comprehensive error handling.
License
je-es/capi
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
About
A lightweight, type-safe HTTP client with interceptors, automatic retries, and comprehensive error handling.

