Core Features & HTTP Methods
Create an HTTP client, use method helpers, pass typed request options, and choose how errors and responses are handled.
1. HTTP Methods & Shorthands
HttpClient provides clean async methods for every standard HTTP verb:
methods.ts
typescript
import { createClient } from 'react-native-fetch-client';
const api = createClient({ baseURL: 'https://api.example.com' });
// GET
const users = await api.get('/users');
// POST
const created = await api.post('/users', { name: 'Alice', role: 'admin' });
// PUT
const updated = await api.put('/users/1', { name: 'Alice Smith' });
// PATCH
const patched = await api.patch('/users/1', { role: 'superadmin' });
// DELETE (supports both .delete and .del alias)
await api.delete('/users/1');
await api.del('/users/2');2. Path Parameter Interpolation
Use named path placeholders (:param or {param}) and pass their values through pathParams:
typescript
const api = createClient({ baseURL: 'https://api.example.com' });
const team = await api.get('/orgs/:orgId/teams/:teamId', {
pathParams: {
orgId: 'google-deepmind',
teamId: 'agentic-ai',
},
});
// Resolves to: /orgs/google-deepmind/teams/agentic-ai3. Go/Rust-Style Safe Requests
Use api.safe.* when you prefer a result tuple over a thrown error:
typescript
type User = { id: number; name: string };
const [user, error] = await api.safe.get<User>('/users/101');
if (error) {
console.error('Failed to load user:', error.message);
} else {
console.log('User profile:', user.name);
}4. Response Unwrapping (unwrap: true)
Methods return the full HttpResponse envelope by default. Set unwrap: true at client or request level to return its data value directly:
javascript
import { createClient } from 'react-native-fetch-client';
const api = createClient({
baseURL: 'https://api.example.com',
unwrap: true,
});
const profile = await api.get('/me');
console.log(profile.name); // returns response.data directly!TypeScript declaration limitation
At runtime,
unwrap: true returns the response data. The current TypeScript method declarations still describe Promise<HttpResponse<T>>, so they do not reflect that runtime change. TypeScript users can keep the default response envelope or provide a typed wrapper.