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

react-query-fetcher

v0.0.1

Published

TanStack React Query default fetcher using axios with automatic URL construction from query keys

Readme

tanstack-query-fetcher

A lightweight, convention-driven default fetcher for TanStack React Query that auto-constructs API URLs from query keys, powered by axios.

Why?

This package provides a drop-in default fetcher that follows a predictable convention: every segment of your query key becomes part of the URL path, and the last key (if an object) becomes query parameters. No more repetitive URL construction in every query.

Installation

npm install tanstack-query-fetcher axios

Quick Start

import { QueryClient } from "@tanstack/react-query";
import { createDefaultFetcher } from "tanstack-query-fetcher";

const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      queryFn: createDefaultFetcher({basePath: "/api"}),
    },
  },
});

Now your queries "just work" — the query key becomes the URL automatically.

How Query Keys Map to URLs

The library follows this convention:

| Query Key | Generated URL | |---|---| | ["users"] | /api/users | | ["users", 42] | /api/users/42 | | ["users", "posts", { page: 2 }] | /api/users/posts?page=2 | | ["users", 42, "posts", { page: 2, sort: "desc" }] | /api/users/42/posts?page=2&sort=desc | | ["search", { q: "hello", tags: ["js", "ts"] }] | /api/search?q=hello&tags=js&tags=ts |

Rules:

  • All keys except the last are joined as path segments prefixed by api (or your custom basePath).
  • The last key, if it's a plain object, is serialized as query parameters.
  • The last key, if it's a string or number, is appended as a final path segment.
  • Leading/trailing slashes in path segments are automatically normalized to prevent double slashes and 308 redirects.

API

createDefaultFetcher(options?)

Creates a queryFn compatible with TanStack Query's defaultQueryFn.

const defaultQueryFn = createDefaultFetcher({
  basePath?: string;       // default: ""
  timeoutMs?: number;      // default: 30_000 (30 seconds)
  errorProcessor?: (error: unknown) => void;
});

basePath

Set a custom base path instead of api:

createDefaultFetcher({ basePath: "v2" });
// ["users"] → /v2/users

timeoutMs

Request timeout in milliseconds (default: 30,000).

createDefaultFetcher({ timeoutMs: 10_000 }); // 10 second timeout

errorProcessor

Custom error handling callback. When provided, the library delegates all error handling to your function — it will not throw. Useful for centralized error logging, toast notifications, or redirecting on 401s.

createDefaultFetcher({
  errorProcessor: (error) => {
    // log to your error reporting service, show toast, etc.
    console.error("Query failed:", error);
  },
});

Without an errorProcessor, the library throws descriptive errors by default.

Full Example

import { QueryClient, QueryClientProvider, useQuery } from "@tanstack/react-query";
import { createDefaultFetcher } from "tanstack-query-fetcher";

const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      queryFn: createDefaultFetcher({
        basePath: "api/v1",
        timeoutMs: 15_000,
      }),
    },
  },
});

function App() {
  return (
    <QueryClientProvider client={queryClient}>
      <Users />
    </QueryClientProvider>
  );
}

function Users() {
  // This automatically fetches GET /api/v1/users/42/posts?sort=recent
  const { data, isLoading } = useQuery({
    queryKey: ["users", 42, "posts", { sort: "recent" }],
  });

  if (isLoading) return <div>Loading...</div>;
  return <pre>{JSON.stringify(data, null, 2)}</pre>;
}

Error Handling

By default, the fetcher throws with descriptive messages:

  • "Request failed with status code 404: GET /api/users/42" — for HTTP errors with a status.
  • "Request timed out after 30s: GET /api/users" — when the request exceeds timeoutMs.
  • "Network response was not ok: GET /api/users" — for non-axios errors.

For 422 and 400 responses that include an errors property in the response body (common in validation error responses), the fetcher throws the errors object directly instead of a generic message.

License

MIT