Basic Usage
Basic Usage
This guide covers the fundamental concepts and usage patterns of @shkumbinhsn/fetcher.
Import the Library
import { fetcher } from '@shkumbinhsn/fetcher';Making Requests
GET Requests
// Simple GET requestconst data = await fetcher('/api/users');
// GET with query parameters (use URLSearchParams or add to URL)const params = new URLSearchParams({ page: '1', limit: '10' });const users = await fetcher(`/api/users?${params}`);
// GET with headersconst user = await fetcher('/api/users/123', { headers: { 'Authorization': 'Bearer token', 'Accept': 'application/json' }});POST Requests
// POST with JSON bodyconst newUser = await fetcher('/api/users', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: 'John Doe', email: 'john@example.com' })});
// POST with FormDataconst formData = new FormData();formData.append('name', 'John Doe');formData.append('avatar', fileInput.files[0]);
const user = await fetcher('/api/users', { method: 'POST', body: formData});
// POST with URLSearchParamsconst params = new URLSearchParams();params.append('name', 'John Doe');params.append('email', 'john@example.com');
const user = await fetcher('/api/users', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: params});PUT/PATCH Requests
// PUT - full replacementconst updatedUser = await fetcher('/api/users/123', { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ id: '123', name: 'Jane Doe', email: 'jane@example.com' })});
// PATCH - partial updateconst patchedUser = await fetcher('/api/users/123', { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: 'Jane Smith' })});DELETE Requests
// DELETE requestawait fetcher('/api/users/123', { method: 'DELETE'});
// DELETE with confirmation in bodyawait fetcher('/api/users/123', { method: 'DELETE', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ confirm: true })});Request Configuration
Timeout
// 10 second timeoutconst controller = new AbortController();const timeoutId = setTimeout(() => controller.abort(), 10000);
try { const data = await fetcher('/api/slow-endpoint', { signal: controller.signal });} finally { clearTimeout(timeoutId);}Base URL
Create a configured instance for a specific API:
// Create a wrapper functionfunction apiCall(endpoint: string, init?: RequestInit) { return fetcher(`https://api.example.com${endpoint}`, { headers: { 'Authorization': `Bearer ${getToken()}`, 'Content-Type': 'application/json', ...init?.headers }, ...init });}
// Use itconst users = await apiCall('/users');const user = await apiCall('/users/123');Request Interceptors
// Create a wrapper with common logicasync function authenticatedFetch(url: string, init?: RequestInit) { const token = await getAuthToken();
return fetcher(url, { headers: { 'Authorization': `Bearer ${token}`, ...init?.headers }, ...init });}Response Handling
Empty Responses
The library handles empty responses (204 No Content) automatically:
// Returns null for empty responsesconst result = await fetcher('/api/users/123', { method: 'DELETE'}); // result is nullResponse Headers
Access response data through error handling or by not using schema validation:
// Without schema, you get the raw responseconst response = await fetch('/api/users'); // Use fetch directly for headersconst data = await response.json();const contentType = response.headers.get('content-type');
// Or catch and inspect errors for response detailstry { await fetcher('/api/users/999');} catch (error) { if (error instanceof ApiError) { console.log(error.response.headers.get('x-rate-limit')); }}Next Steps
- Schema Validation - Add type safety with schemas
- Error Handling - Handle API errors gracefully
- TypeScript Integration - Advanced TypeScript usage