query-keys 0.0.1 → 0.1.0

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 ADDED
@@ -0,0 +1,66 @@
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 for your whole app, or for a specific part:
20
+
21
+ ```ts
22
+ import { createQueryKeys } from "query-keys";
23
+
24
+ export const queryKeys = createQueryKeys({
25
+ todos: {
26
+ all: null, // ["todos", "all"]
27
+ id: (id: number) => id, // ["todos", "id", <id>]
28
+ },
29
+ });
30
+
31
+ // or:
32
+
33
+ export const todoQueryKeys = createQueryKeys("todos", {
34
+ all: null, // ["todos", "all"]
35
+ id: (id: number) => id, // ["todos", "id", <id>]
36
+ });
37
+ ```
38
+
39
+ Then, use your keys like:
40
+
41
+ ```ts
42
+ const { data: allTodos } = useQuery({
43
+ queryKey: queryKeys.todos.all, // typed!
44
+ queryFn: async () => {
45
+ // ...
46
+ },
47
+ });
48
+
49
+ const { data: specificTodo } = useQuery({
50
+ queryKey: todoQueryKeys.id(todoId), // typed!
51
+ queryFn: async () => {
52
+ // ...
53
+ },
54
+ });
55
+ ```
56
+
57
+ 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.
58
+ Use a function to get a dynamic query key depending on an external parameter.
59
+
60
+ ## Credit
61
+
62
+ 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.
63
+
64
+ ## License
65
+
66
+ MIT
package/dist/index.d.mts CHANGED
@@ -1,8 +1,11 @@
1
1
  //#region src/index.d.ts
2
2
  interface NestedKeys {
3
- [key: string]: ((...args: never[]) => unknown) | string | number | null | NestedKeys;
3
+ [key: string]: ((...args: any[]) => unknown) | (string | number)[] | string | number | null | NestedKeys;
4
4
  }
5
- type QueryKeys<Path extends string[], KeysDef extends NestedKeys> = { [K in keyof KeysDef]: KeysDef[K] extends ((...args: infer Args) => infer Return) ? (...args: Args) => [...Path, K, ...(Return extends unknown[] ? Return : [Return])] : KeysDef[K] extends NestedKeys ? K extends string ? QueryKeys<[...Path, K], KeysDef[K]> : never : KeysDef[K] extends string | number ? [...Path, K, KeysDef[K] extends string | number ? KeysDef[K] : never] : KeysDef[K] extends null ? [...Path, K] : never };
6
- declare function createQueryKeys<Root extends string, KeysDef extends NestedKeys>(root: Root, definition: KeysDef): QueryKeys<Root[], KeysDef>;
5
+ type QueryKeys<Path extends string[] | undefined, KeysDef extends NestedKeys> = (Path extends undefined ? {} : {
6
+ _def: Path;
7
+ }) & { [K in keyof KeysDef]: KeysDef[K] extends ((...args: infer Args) => infer Return) ? (...args: Args) => [...(Path extends undefined ? [] : Path), K, ...(Return extends unknown[] ? Return : [Return])] : KeysDef[K] extends NestedKeys ? K extends string ? QueryKeys<[...(Path extends undefined ? [] : Path), K], KeysDef[K]> : never : KeysDef[K] extends (string | number)[] ? [...(Path extends undefined ? [] : Path), K, ...KeysDef[K]] : KeysDef[K] extends string | number ? [...(Path extends undefined ? [] : Path), K, KeysDef[K]] : KeysDef[K] extends null ? [...(Path extends undefined ? [] : Path), K] : never };
8
+ declare function createQueryKeys<KeysDef extends NestedKeys>(definition: KeysDef): QueryKeys<undefined, KeysDef>;
9
+ declare function createQueryKeys<Root extends string, KeysDef extends NestedKeys>(root: Root, definition: KeysDef): QueryKeys<[Root], KeysDef>;
7
10
  //#endregion
8
11
  export { QueryKeys, createQueryKeys };
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  //#region src/index.ts
2
2
  function recCreateQueryKeys(root, definition) {
3
- const result = { _def: root };
3
+ const result = root.length ? { _def: root } : {};
4
4
  for (const key in definition) {
5
5
  if (!Object.hasOwn(definition, key)) continue;
6
6
  const element = definition[key];
@@ -12,7 +12,7 @@ function recCreateQueryKeys(root, definition) {
12
12
  ...Array.isArray(ret) ? ret : [ret]
13
13
  ];
14
14
  };
15
- else if (typeof element === "object" && element !== null) result[key] = recCreateQueryKeys([...root, key], element);
15
+ else if (typeof element === "object" && !Array.isArray(element) && element !== null) result[key] = recCreateQueryKeys([...root, key], element);
16
16
  else {
17
17
  const lastKey = Array.isArray(element) ? element : element === null ? [] : [element];
18
18
  result[key] = [
@@ -24,8 +24,11 @@ function recCreateQueryKeys(root, definition) {
24
24
  }
25
25
  return result;
26
26
  }
27
- function createQueryKeys(root, definition) {
28
- return recCreateQueryKeys([root], definition);
27
+ function createQueryKeys(rootOrDef, defOrEmpty) {
28
+ const root = typeof rootOrDef === "string" ? [rootOrDef] : [];
29
+ const def = typeof rootOrDef === "string" ? defOrEmpty : rootOrDef;
30
+ if (def === void 0) throw new Error("Query keys definition is required");
31
+ return recCreateQueryKeys(root, def);
29
32
  }
30
33
 
31
34
  //#endregion
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "query-keys",
3
3
  "type": "module",
4
- "version": "0.0.1",
4
+ "version": "0.1.0",
5
5
  "private": false,
6
6
  "description": "A fully typed, simple utility for your query keys.",
7
7
  "exports": {
@@ -16,12 +16,16 @@
16
16
  ],
17
17
  "scripts": {
18
18
  "build": "tsdown",
19
- "dev": "tsdown --watch"
19
+ "dev": "tsdown --watch",
20
+ "test": "vitest",
21
+ "ts": "tsc --noEmit",
22
+ "publish": "pnpm test run && pnpm ts && pnpm build && npm publish"
20
23
  },
21
24
  "devDependencies": {
22
25
  "@arethetypeswrong/core": "^0.18.2",
23
26
  "publint": "^0.3.17",
24
27
  "tsdown": "^0.18.1",
25
- "typescript": "^5.9.3"
28
+ "typescript": "^5.9.3",
29
+ "vitest": "^4.0.18"
26
30
  }
27
31
  }