npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@thatguyjamal/type-fetch

v1.0.0

Published

A lightweight, zero-dependency HTTP client library for making API requests in JavaScript/TypeScript.

Readme

type-fetch

A lightweight, zero-dependency HTTP client library for making type-safe API requests in JavaScript/TypeScript.

npm version Downloads

🌟 Features

  • Type-Safe: Full TypeScript support with comprehensive type inference
  • Zero Dependencies: Lightweight and fast
  • Flexible Configuration: Highly customizable request handling
  • Retry Mechanism: Built-in request retry logic
  • Caching Support: Optional response caching for GET requests
  • Multiple Content Types: Support for JSON, Form, Text, Blob, Multipart, XML, and HTML

📦 Installation

npm install @thatguyjamal/type-fetch
# or
yarn add @thatguyjamal/type-fetch
# or
pnpm add @thatguyjamal/type-fetch
# or
bun add @thatguyjamal/type-fetch

Compatibility

  • TypeScript: >=4.5.0
  • Node.js: >=16.0.0
  • Browsers: All modern browsers (Chrome, Firefox, Safari, Edge)

🚀 Quick Start

Basic Usage

import { TFetchClient } from '@thatguyjamal/type-fetch';

// Create a client instance
const client = new TFetchClient();

// GET Request
interface User {
  id: number;
  name: string;
}

const { data, error } = await client.get<User>('https://api.example.com/users/1');
if (error) {
  console.error('Request failed:', error);
} else {
  console.log('User:', data);
}

// POST Request
const { data: newUser, error: postError } = await client.post<User>(
  'https://api.example.com/users', 
  { type: 'json', data: { name: 'John Doe' } }
);

🔧 Advanced Configuration

Client Options

const client = new TFetchClient({
  // Enable debug logging
  debug: true,

  // Default headers for all requests
  headers: { 'Authorization': 'Bearer token' },

  // Retry mechanism
  retry: {
    count: 3,           // Number of retry attempts
    delay: 1000,        // Delay between retries (ms)
    onRetry: () => {    // Optional callback on each retry
      console.log('Retrying request...');
    }
  },

  // Caching for GET requests
  cache: {
    enabled: true,      // Enable caching
    maxAge: 5 * 60000,  // Cache expiration (5 minutes)
    maxCachedEntries: 100 // Maximum number of cached entries
  },

  // Customize DELETE request handling
  deleteHandling: 'status' // 'empty' | 'status' | 'json'
});

🚀 Advanced Caching

Type-fetch provides a powerful and flexible caching mechanism for all HTTP methods:

// Global cache configuration
const client = new TFetchClient({
  cache: {
    enabled: true,     // Enable caching globally
    maxAge: 5 * 60 * 1000, // 5 minutes default cache time
    maxCachedEntries: 1000 // Maximum number of cache entries
  }
});

// Per-request cache configuration
const result = await client.get<User>('/users/1', {
  cache: {
    enabled: true,     // Override global cache setting
    maxAge: 10 * 60 * 1000 // Custom cache time for this request
  }
});

Caching Features

  • Support for caching all HTTP methods (GET, POST, PUT, DELETE, etc.)
  • Intelligent cache key generation based on URL, method, headers, and body
  • Configurable global and per-request cache settings
  • Automatic cache cleanup to prevent memory overflow

POST Request Caching

// Create a new user with caching
const result = await client.post<User>('/users', 
  { type: 'json', data: { name: 'John Doe', email: '[email protected]' } },
  {
    cache: {
      enabled: true,     // Cache the POST request
      maxAge: 15 * 60 * 1000 // Cache for 15 minutes
    }
  }
);

PATCH Request Caching

// Update user profile with caching
const result = await client.patch<User>('/users/123', 
  { type: 'json', data: { name: 'Jane Smith' } },
  {
    cache: {
      enabled: true,     // Cache the PATCH request
      maxAge: 10 * 60 * 1000 // Cache for 10 minutes
    }
  }
);

📡 Supported HTTP Methods

  • get<T>(): Retrieve resources
  • post<T>(): Create new resources
  • put<T>(): Update existing resources
  • patch<T>(): Partially update resources
  • delete<T>(): Remove resources
  • head<T>(): Retrieve headers only

🌈 Content Types

Supported content types for requests:

  • json: JSON data
  • form: URL-encoded form data
  • text: Plain text
  • blob: Binary data
  • multipart: Form data with files
  • xml: XML documents
  • html: HTML documents

Example with Different Content Types

// JSON
await client.post('https://api.example.com/users', { 
  type: 'json', 
  data: { name: 'John' } 
});

// Multipart Form Data
const formData = new FormData();
formData.append('file', fileBlob, 'avatar.png');
await client.post('https://api.example.com/upload', { 
  type: 'multipart', 
  data: formData 
});

🛡️ Error Handling

The library provides a robust TFetchError with additional context:

interface Result<T> {
  data: T | null;
  error: TFetchError | null;
}

// Error properties
error.message    // Error description
error.statusCode // HTTP status code

🔍 DELETE Request Handling

Customize how DELETE requests are handled:

// 'empty' (default): Returns { data: null, error: null }
// 'status': Returns { data: statusCode, error: null }
// 'json': Attempts to parse JSON response
const client = new TFetchClient({ deleteHandling: 'status' });

📝 License

MIT License

For the full license text, see the LICENSE file in the repository.

💬 Support

If you encounter any issues or have questions, please open an issue on GitHub.

🐛 Issues & Contributions

Found a bug? Want to contribute? Please open an issue or submit a pull request on GitHub.

🔗 Additional Resources