query-keys 0.0.2 → 0.1.1

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 CHANGED
@@ -16,11 +16,20 @@ yarn add query-keys
16
16
 
17
17
  ## Usage
18
18
 
19
- Create your query keys object:
19
+ Create your query keys object for your whole app, or for a specific part:
20
20
 
21
21
  ```ts
22
22
  import { createQueryKeys } from "query-keys";
23
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
+
24
33
  export const todoQueryKeys = createQueryKeys("todos", {
25
34
  all: null, // ["todos", "all"]
26
35
  id: (id: number) => id, // ["todos", "id", <id>]
@@ -31,7 +40,7 @@ Then, use your keys like:
31
40
 
32
41
  ```ts
33
42
  const { data: allTodos } = useQuery({
34
- queryKey: todoQueryKeys.all, // typed!
43
+ queryKey: queryKeys.todos.all, // typed!
35
44
  queryFn: async () => {
36
45
  // ...
37
46
  },
@@ -50,7 +59,7 @@ Use a function to get a dynamic query key depending on an external parameter.
50
59
 
51
60
  ## Credit
52
61
 
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.
62
+ The API is very much 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
63
 
55
64
  ## License
56
65
 
package/dist/index.d.mts CHANGED
@@ -1,8 +1,41 @@
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
+ /**
9
+ * Creates a structured set of query keys based on the provided definition.
10
+ *
11
+ * ```ts
12
+ * const keys = createQueryKeys({
13
+ * users: {
14
+ * all: null,
15
+ * byId: (id: string) => id,
16
+ * },
17
+ * });
18
+ *
19
+ * console.log(keys.users.all); // Output: ["users", "all"]
20
+ * console.log(keys.users.byId("123")); // Output: ["users", "byId", "123"]
21
+ * ```
22
+ */
23
+ declare function createQueryKeys<KeysDef extends NestedKeys>(definition: KeysDef): QueryKeys<undefined, KeysDef>;
24
+ /**
25
+ * Creates a structured set of query keys with a root prefix based on the provided definition.
26
+ *
27
+ * ```ts
28
+ * const apiKeys = createQueryKeys("api", {
29
+ * users: {
30
+ * all: null,
31
+ * byId: (id: string) => id,
32
+ * },
33
+ * });
34
+ *
35
+ * console.log(apiKeys.users.all); // Output: ["api", "users", "all"]
36
+ * console.log(apiKeys.users.byId("123")); // Output: ["api", "users", "byId", "123"]
37
+ * ```
38
+ */
39
+ declare function createQueryKeys<Root extends string, KeysDef extends NestedKeys>(root: Root, definition: KeysDef): QueryKeys<[Root], KeysDef>;
7
40
  //#endregion
8
41
  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,9 +24,16 @@ 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
  }
33
+ createQueryKeys("api", { users: {
34
+ all: "etst",
35
+ byId: (id) => id
36
+ } });
30
37
 
31
38
  //#endregion
32
39
  export { createQueryKeys };
package/package.json CHANGED
@@ -1,27 +1,54 @@
1
1
  {
2
2
  "name": "query-keys",
3
- "type": "module",
4
- "version": "0.0.2",
3
+ "version": "0.1.1",
5
4
  "private": false,
6
5
  "description": "A fully typed, simple utility for your query keys.",
7
- "exports": {
8
- ".": "./dist/index.mjs",
9
- "./package.json": "./package.json"
6
+ "keywords": [
7
+ "query-keys",
8
+ "react-query",
9
+ "tanstack-query",
10
+ "typescript",
11
+ "utility"
12
+ ],
13
+ "homepage": "https://github.com/symsmith/query-keys",
14
+ "bugs": {
15
+ "url": "https://github.com/symsmith/query-keys/issues"
16
+ },
17
+ "license": "MIT",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/symsmith/query-keys.git"
10
21
  },
11
- "main": "./dist/index.mjs",
12
- "module": "./dist/index.mjs",
13
- "types": "./dist/index.d.mts",
14
22
  "files": [
15
23
  "dist"
16
24
  ],
25
+ "type": "module",
26
+ "main": "./dist/index.mjs",
27
+ "module": "./dist/index.mjs",
28
+ "types": "./dist/index.d.mts",
29
+ "exports": {
30
+ ".": "./dist/index.mjs",
31
+ "./package.json": "./package.json"
32
+ },
17
33
  "scripts": {
18
34
  "build": "tsdown",
19
- "dev": "tsdown --watch"
35
+ "dev": "tsdown --watch",
36
+ "test": "vitest",
37
+ "ts": "tsc --noEmit",
38
+ "fmt": "oxfmt",
39
+ "fmt:check": "oxfmt --check",
40
+ "lint": "oxlint --type-aware --report-unused-disable-directives --deny-warnings",
41
+ "check": "pnpm fmt:check && pnpm lint && pnpm test run && pnpm ts && pnpm build",
42
+ "npm:publish": "pnpm fmt:check && pnpm lint && pnpm test run && pnpm ts && pnpm build && npm publish"
20
43
  },
21
44
  "devDependencies": {
22
45
  "@arethetypeswrong/core": "^0.18.2",
46
+ "oxfmt": "^0.28.0",
47
+ "oxlint": "^1.43.0",
48
+ "oxlint-tsgolint": "^0.11.5",
23
49
  "publint": "^0.3.17",
24
50
  "tsdown": "^0.18.1",
25
- "typescript": "^5.9.3"
51
+ "typescript": "^5.9.3",
52
+ "vitest": "^4.0.18"
26
53
  }
27
54
  }