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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@thatguyjamal/type-fetch

v0.1.5

Published

A lightweight, flexible HTTP client library for making API requests in JavaScript/TypeScript.

Downloads

233

Readme

type-fetch

type-fetch is a lightweight, flexible HTTP client library for making API requests in JavaScript/TypeScript. It provides a straightforward API with built-in support for retries, caching, and content type handling. Whether you're building a web application or a Node.js service, type-fetch is designed to simplify and streamline your HTTP requests.

Features

  • Flexible Request Methods: Supports GET, POST, PUT, and DELETE requests.
  • Automatic Content-Type Handling: Automatically sets the correct content type based on the request payload.
  • Retry Mechanism: Configurable retry logic with custom retry delays and callbacks.
  • Caching: Cache responses for GET requests to improve performance and reduce redundant API calls.
  • Debug Logging: Optionally log request and cache information for easier debugging.

Installation

Install type-fetch:

pnpm add @thatguyjamal/type-fetch

or

npm install @thatguyjamal/type-fetch

Usage

Basic Usage

Here's a basic example of how to use type-fetch for making HTTP requests:

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

// Create a new client instance with optional configuration
const client = new TFetchClient({
  debug: true,
  retry: {
    count: 3,
    delay: 1000,
    onRetry: () => console.log("Retrying..."),
  },
  cache: {
    enabled: true,
    maxAge: 300000, // Cache responses for 5 minutes
  },
});

type Post = {
  id: number;
  title: string;
  body: string;
  userId: number;
};

const { data, error } = await client.get<Post>(
  "https://jsonplaceholder.typicode.com/posts/1"
);

// Making a POST request with JSON data
const { data, error } = await client.post(
  "https://jsonplaceholder.typicode.com/posts",
  {
    type: "json", // other options: "text", "form", "blob"
    data: {
      title: "foo",
      body: "bar",
      userId: 1,
    },
  }
);

// Making a PUT request with JSON data
const { data, error } = await client.put(
  "https://jsonplaceholder.typicode.com/posts/1",
  {
    type: "json",
    data: {
      id: 1,
      title: "foo",
      body: "bar",
      userId: 1,
    },
  }
);

// Making a DELETE request
const { data, error } = await client.delete(
  "https://jsonplaceholder.typicode.com/posts/1"
);

Configuration Options

  • debug (boolean): Enables debug logging.
  • retry (object): Configures retry logic.
    • count (number): Number of retry attempts.
    • delay (number): Delay between retries in milliseconds.
    • onRetry (function): Callback function to execute on each retry.
  • cache (object): Configures caching for GET requests.
    • enabled (boolean): Enables or disables caching.
    • maxAge (number): Maximum age of cache entries in milliseconds.
    • maxCachedEntries (number): Maximum number of cached entries allowed.

API

TFetchClient

  • constructor(opts?: Partial<TFetchClientOptions>): Creates a new instance with optional configuration.

  • get<T>(url: UrlOrString, headers?: HeadersInit): Promise<Result<T>>: Makes a GET request.

  • post<T>(url: UrlOrString, body: ContentWrapper<unknown>): Promise<Result<T>>: Makes a POST request.

  • put<T>(url: UrlOrString, body: ContentWrapper<unknown>): Promise<Result<T>>: Makes a PUT request.

  • delete<T>(url: UrlOrString, headers?: HeadersInit): Promise<Result<T>>: Makes a DELETE request.

Testing

To run tests for type-fetch simply run pnpm test in the root directory.

Contributing

Contributions are welcome! Please open an issue or submit a pull request with your improvements.

License

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