0.0.3 โ€ข Published 5 months ago

fetch-smart v0.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months ago

FetchSmart

npm version Build Status License: MIT

A powerful, type-safe HTTP client for modern JavaScript applications. Built on top of the native Fetch API with enhanced features for a better developer experience.

โœจ Features

  • ๐Ÿ”„ Smart Retries: Automatic retry with exponential backoff
  • โฑ๏ธ Timeout Control: Configurable request timeouts
  • ๐Ÿ“Š Progress Tracking: Monitor upload and download progress
  • ๐ŸŽฏ Type Safety: Full TypeScript support with comprehensive types
  • ๐Ÿ” Response Parsing: Automatic content-type handling
  • ๐Ÿšซ Cancellation: Built-in request cancellation support
  • ๐Ÿ“ Rich Response: Detailed response metadata
  • ๐ŸŒ CORS Ready: Built-in CORS handling
  • ๐Ÿ”’ Security: Configurable request credentials
  • ๐Ÿ“ฆ Zero Dependencies: No external runtime dependencies

๐Ÿ“ฅ Installation

npm install fetch-smart
# or
yarn add fetch-smart
# or
pnpm add fetch-smart

๐Ÿš€ Quick Start

import fetchClient from 'fetch-smart';

// Simple GET request
const { data } = await fetchClient.get('https://api.example.com/users');

// POST with JSON
const response = await fetchClient.post('https://api.example.com/users', {
  name: 'John Doe',
  email: 'john@example.com'
});

// PUT with retry and timeout
const updated = await fetchClient.put(
  'https://api.example.com/users/1',
  { name: 'Jane Doe' },
  {
    retry: 3,
    timeout: 5000
  }
);

๐Ÿ› ๏ธ Advanced Usage

Creating an Instance

import { FetchSmart } from 'fetch-smart';

const client = new FetchSmart('https://api.example.com', {
  'Authorization': 'Bearer your-token'
});

Progress Monitoring

const response = await client.post(
  '/upload',
  formData,
  {
    onUploadProgress: (progress) => {
      console.log(`Upload: ${progress.progress}%`);
    },
    onDownloadProgress: (progress) => {
      console.log(`Download: ${progress.progress}%`);
    }
  }
);

Retry Configuration

const response = await client.get('/data', {
  retry: 3,
  retryDelay: 1000,
  exponentialDelay: true,
  retryCondition: () => true
});

Request Cancellation

const controller = new AbortController();

const promise = client.get('/data', {
  signal: controller.signal
});

// Cancel request
controller.abort();

๐Ÿ“š API Reference

Request Configuration

OptionTypeDefaultDescription
bodyunknown-Request body data
paramsRecord<string, string>-URL query parameters
timeoutnumber-Request timeout (ms)
headersHeadersInit{}Custom headers
retrynumber0Retry attempts
retryDelaynumber1000Delay between retries
exponentialDelaybooleantrueUse exponential backoff
retryCondition() => boolean-Custom retry logic
cacheRequestCache'default'Cache strategy
modeRequestMode'cors'CORS mode
credentialsRequestCredentials'same-origin'Credentials mode
signalAbortSignal-Cancellation signal
responseTypeResponseType'json'Response format
onUploadProgress(progress: ProgressEvent) => void-Upload progress
onDownloadProgress(progress: ProgressEvent) => void-Download progress
priority'high'๏ฝœ'low'๏ฝœ'auto''auto'Request priority

Response Structure

PropertyTypeDescription
dataT ๏ฝœ nullParsed response data
statusnumberHTTP status code
statusTextstringStatus message
headersHeadersResponse headers
okbooleanStatus in 200-299 range
urlstringFinal URL after redirects
requestTimenumberRequest start time
responseTimenumberResponse received time
durationnumberTotal request time (ms)
errorstring ๏ฝœ nullError message if failed
retriesnumberRetry attempts made
maxRetriesnumberMaximum retries
isCachedbooleanResponse from cache

๐Ÿงช TypeScript Support

FetchSmart is written in TypeScript and provides full type definitions:

interface User {
  id: number;
  name: string;
  email: string;
}

// Types are inferred automatically
const { data } = await client.get<User>('/user/1');
console.log(data.name); // TypeScript knows this is a string

// POST with type checking
const newUser = await client.post<User>('/users', {
  name: 'John',
  email: 'john@example.com'
});

๐Ÿค Contributing

We welcome contributions! Please see our Contributing Guidelines for details.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (npm run commit)
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

๐Ÿ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

  • Built with TypeScript for better developer experience
  • Inspired by Axios and Ky
  • Follows modern JavaScript best practices

๐Ÿ’ฌ Support

  • ๐Ÿ“ซ Report issues on GitHub Issues
  • ๐ŸŒŸ Star the repo if you find it useful

๐Ÿ”„ Changelog

See CHANGELOG.md for release history.

0.0.3

5 months ago

0.0.2

5 months ago

0.0.1

5 months ago

1.0.0

5 months ago