ProvenPin Install

TanStack Query Core fetch and cache: working versions (tested 2026-10-15)

Drive async data fetching with @tanstack/query-core QueryClient, covering staleTime, cache hits and invalidateQueries without React offline.

passed1 passed · 0 failed2026-10-15 16:05 UTC
Status
passed
Last passed
2026-10-15 16:05 UTC
Tests
1 / 1
Language
TypeScript
Runtime
node v22.23.3
pinned versions3
@tanstack/query-core@5.104.3@types/node@26.6.5tsx@4.23.15
run it
npm install --no-audit --no-fund --ignore-scripts=false
npm test
test environment
node v22.23.3
linux 6.17.0-1022-azure
x64
runner: github-actions 38764699834

The code

Exactly the files that passed. Your agent can pull them with get_recipe("tanstack-query-core-cache") (install), or see them on GitHub.

package.json
{
  "name": "tanstack-query-core-cache",
  "private": true,
  "type": "module",
  "scripts": {
    "test": "node --import tsx --test test/*.test.ts"
  },
  "dependencies": {"@tanstack/query-core":"5.104.3"},
  "devDependencies": {"@types/node":"26.6.5","tsx":"4.23.15"}
}
src/client.ts
import { QueryClient, QueryObserver, type QueryKey } from "@tanstack/query-core";

export type Item = { id: number; name: string };

export function createItemsClient(fetchItem: (id: number) => Promise<Item>) {
  const client = new QueryClient({
    defaultOptions: {
      queries: { retry: false, staleTime: 1_000 },
    },
  });

  const key = (id: number): QueryKey => ["item", id];

  return {
    client,
    async get(id: number) {
      return client.fetchQuery({
        queryKey: key(id),
        queryFn: () => fetchItem(id),
      });
    },
    getCached(id: number) {
      return client.getQueryData<Item>(key(id));
    },
    async invalidate(id: number) {
      await client.invalidateQueries({ queryKey: key(id) });
    },
    observe(id: number, onChange: (item: Item | undefined) => void) {
      const observer = new QueryObserver(client, {
        queryKey: key(id),
        queryFn: () => fetchItem(id),
      });
      return observer.subscribe((result) => onChange(result.data));
    },
  };
}
test/client.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import { createItemsClient } from "../src/client.ts";

test("caches fetch results until invalidated", async () => {
  let calls = 0;
  const api = createItemsClient(async (id) => {
    calls++;
    return { id, name: `item-${id}-${calls}` };
  });
  const a = await api.get(1);
  const b = await api.get(1);
  assert.equal(a.name, b.name);
  assert.equal(calls, 1);
  assert.equal(api.getCached(1)?.name, a.name);
  await api.invalidate(1);
  const c = await api.get(1);
  assert.equal(calls, 2);
  assert.notEqual(c.name, a.name);
});

Connect to ProvenPin

Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.

terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp

Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.