Skip to content
/ capi Public

A lightweight, type-safe HTTP client with interceptors, automatic retries, and comprehensive error handling.

License

Notifications You must be signed in to change notification settings

je-es/capi

Repository files navigation


logo


CI Test Coverage Github Repo Issues GitHub Repo stars

  • Quick Start 🔥

    A lightweight, type-safe HTTP client with interceptors, automatic retries, and comprehensive error handling.

    • Setup

      install space first.

      # install
      space i @je-es/capi
      // import
      import { api, http, configureApi } from '@je-es/capi';
    line
    • Usage

      • Basic Requests
        // 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');
      line
      • Configuration
        // Set base URL and default headers
        configureApi({
            baseURL : 'https://api.example.com',
            timeout : 10000,
            headers : {
                'Authorization': 'Bearer token123',
                'Content-Type': 'application/json'
            }
        });
      line
      • Query Parameters
        // 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
      line
      • Request Interceptors
        configureApi({
            interceptors: {
                request: (config) => {
                    // Add authentication token
                    config.headers['Authorization'] = `Bearer ${getToken()}`;
                    return config;
                }
            }
        });
      line
      • Response Interceptors
        configureApi({
            interceptors: {
                response: (response) => {
                    // Transform response data
                    console.log('Response received:', response.status);
                    return response;
                }
            }
        });
      line
      • Error Handling
        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);
        }
      line
      • File Uploads
        const formData = new FormData();
        formData.append('file', fileInput.files[0]);
        formData.append('name', 'document.pdf');
        
        await http.post('/api/upload', formData);
      line
      • TypeScript Support
        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
      line
      • Advanced Usage
        // 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
        });
      line
      • Reset Configuration
        import { resetApiConfig } from '@je-es/capi';
        
        // Reset to defaults
        resetApiConfig();


About

A lightweight, type-safe HTTP client with interceptors, automatic retries, and comprehensive error handling.

Topics

Resources

License

Stars

Watchers

Forks