query-keys 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +57 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# query-keys
|
|
2
|
+
|
|
3
|
+
A fully typed, simple utility for your query keys.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pnpm add query-keys
|
|
9
|
+
# or
|
|
10
|
+
npm install query-keys
|
|
11
|
+
# or
|
|
12
|
+
bun add query-keys
|
|
13
|
+
# or
|
|
14
|
+
yarn add query-keys
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
Create your query keys object:
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { createQueryKeys } from "query-keys";
|
|
23
|
+
|
|
24
|
+
export const todoQueryKeys = createQueryKeys("todos", {
|
|
25
|
+
all: null, // ["todos", "all"]
|
|
26
|
+
id: (id: number) => id, // ["todos", "id", <id>]
|
|
27
|
+
});
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Then, use your keys like:
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
const { data: allTodos } = useQuery({
|
|
34
|
+
queryKey: todoQueryKeys.all, // typed!
|
|
35
|
+
queryFn: async () => {
|
|
36
|
+
// ...
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const { data: specificTodo } = useQuery({
|
|
41
|
+
queryKey: todoQueryKeys.id(todoId), // typed!
|
|
42
|
+
queryFn: async () => {
|
|
43
|
+
// ...
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Use `null` to let the object key be the query key. Use a string or a number, or an array of string or numbers to add specific keys to the query key.
|
|
49
|
+
Use a function to get a dynamic query key depending on an external parameter.
|
|
50
|
+
|
|
51
|
+
## Credit
|
|
52
|
+
|
|
53
|
+
The API is very inspired by [@lukemorales/query-key-factory](https://npmx.dev/package/@lukemorales/query-key-factory), but with Tanstack Query's [Options API](https://tkdodo.eu/blog/the-query-options-api) I do not see the need for such a powerhouse anymore.
|
|
54
|
+
|
|
55
|
+
## License
|
|
56
|
+
|
|
57
|
+
MIT
|