Skip to content

A package to facilitate the management of query keys in @tanstack/query

Notifications You must be signed in to change notification settings

joseph0926/query-key-factory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

query-key-factory

Type-safe key generator for TanStack Query

Features

  • One-line key definitioncreateKeyFactory("todo", { … })
  • Curry support for prefix reuse
  • Returns readonly tuples fully compatible with TanStack Query v5 queryKey
  • Keeps parameter arity & types intact → prevents misuse

Installation

pnpm add query-key-factory

Quick Start

import { createKeyFactory } from "@/lib/query-key-factory";

// 1. One-shot
export const todoKey = createKeyFactory("todo", {
  LIST: [],
  DETAIL: (id: string) => ["detail", id],
});

// 2. Curried
const todoKeyFactory = createKeyFactory("todo");
export const todoKey2 = todoKeyFactory({
  LIST: [],
});

// Inferred types
const k1 = todoKey.LIST(); // readonly ["todo"]
const k2 = todoKey.DETAIL("123"); // readonly ["todo","detail","123"]

API Reference

Function Description
createKeyFactory(prefix, defs?) Returns a type-safe key object for TanStack Query. Omit defs for curried usage.

Key Definition Rules

{
  CONSTANT: [],                   // constant key (tuple)
  PARAM   : (id: string) => [id], // parameterised key (function)
}

License

MIT


Author

About

A package to facilitate the management of query keys in @tanstack/query

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published