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

@contract-first-api/react-query

v2.0.0

Published

Wrap a contract-first API client with React Query hooks and cache helpers.

Readme

@contract-first-api/react-query

@contract-first-api/react-query wraps an ApiClient tree with React Query helpers. It keeps the same contract structure, but gives each contract React Query-friendly methods like useQuery, useMutation, invalidate, and $fetch.

What you do with this package

Use it to:

  • turn a typed API client into query and mutation hooks
  • keep query keys aligned with the contract path
  • invalidate or clear cache using the same contract tree
  • inspect the original contract on each wrapped node through $contract
  • post-process the generated adapter with mapWrappedContracts

Basic setup

import { ApiClient } from "@contract-first-api/api-client";
import createAdapter from "@contract-first-api/react-query";
import { QueryClient } from "@tanstack/react-query";
import { contracts } from "@example/shared";

const client = new ApiClient({
  baseUrl: "http://localhost:3001/api",
  contracts,
});

export const queryClient = new QueryClient();
export const api = createAdapter(client.api, queryClient);

How you use it in components

For GET contracts, use query hooks:

const health = api.health.get.useQuery();
const todos = api.todos.list.useQuery();

For non-GET contracts, use mutations:

const createTodo = api.todos.create.useMutation({
  onSuccess: async () => {
    await api.todos.list.invalidate();
  },
});

await createTodo.mutateAsync({ title: "New item" });

Useful helpers on each contract

Wrapped contracts expose the original contract through $contract, direct calls through $fetch, and the React Query helpers that match the default behavior for that route:

  • $contract for the original contract definition, including meta
  • $fetch for direct calls without hooks
  • $tryFetch for { success, data | error } style handling
  • useQuery and useSuspenseQuery for GET routes
  • useMutation for non-GET routes
  • invalidate to refresh cached queries
  • clear to remove cached queries
  • $getKey to get the query key for a request
  • setData to write into the cache
  • $reactQueryApi to access both query and mutation helpers during custom transforms

Examples:

await api.todos.list.invalidate();
const health = await api.health.get.$fetch();
const cachedHealthKey = api.health.get.$getKey();

$fetch also forwards fetch options to the underlying API client:

await api.health.get.$fetch({ cache: "no-store" });
await api.todos.create.$fetch(
  { title: "Ship docs" },
  { credentials: "include" },
);

Post-processing the wrapped tree

Use mapWrappedContracts when you want to build your own adapter layer on top of the generated one.

import createAdapter, {
  mapWrappedContracts,
} from "@contract-first-api/react-query";

const baseApi = createAdapter(client.api, queryClient);

type ReactQueryMeta = {
  reactQuery?: {
    safe?: boolean;
  };
};

const customApi = mapWrappedContracts<ReactQueryMeta, typeof client.api>(
  baseApi,
  (node) =>
    node.$contract.meta?.reactQuery?.safe || node.$contract.method === "GET"
      ? node.$reactQueryApi
      : node,
);

That pattern is useful when contract metadata should influence how your app exposes or groups routes. $reactQueryApi gives your transform access to the full helper surface even when the default adapter only exposes the query or mutation subset.

Practical flow

In a React app, the usual order is:

  1. Define contracts in shared code.
  2. Build an ApiClient from those contracts.
  3. Create a QueryClient.
  4. Wrap the client with createAdapter.
  5. Optionally post-process the wrapped tree with mapWrappedContracts.
  6. Use the generated contract helpers inside components.

If your app already uses React Query, this package makes the contract tree feel like a native part of that setup.