@raiyyan/callapi
v1.0.1
Published
Modern TypeScript HTTP client with error-first callbacks
Downloads
93
Maintainers
Readme
CallAPI
A modern TypeScript HTTP client with error-first callbacks and built-in retry mechanisms.
Features
- 🚀 Error-first callback pattern
- 🔄 Automatic retries
- ⚡ TypeScript support
- 🎯 Request cancellation
- ⏰ Timeout handling
- 🔒 Type-safe responses
Installation
npm install @raiyyan/callapi
Usage
import { callAPI } from '@raiyyan/callapi';
// Basic GET request
const [error, data] = await callAPI<UserData>('/api/users');
// POST request with options
const [error, response] = await callAPI<Response>('/api/users', {
method: 'POST',
body: { name: 'John' },
retries: 3,
});
// Using with subscription
const [promise, unsubscribe] = callAPI.subscribe<Data>('/api/stream');
API Reference
Options
export type APICallOptions<M extends Method, BodyType = unknown> = {
method?: M;
body?: M extends 'POST' | 'PUT' ? BodyType : never;
headers?: Record<string, string>;
retries?: number;
retryDelay?: number;
enableGlobalLoader?: boolean;
onStartLoader?: (attempt?: number) => void;
onStopLoader?: () => void;
timeout?: number;
};
License
MIT