query-keys 0.0.2 → 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 +11 -2
- package/dist/index.d.mts +6 -3
- package/dist/index.mjs +7 -4
- package/package.json +7 -3
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:
|
|
43
|
+
queryKey: queryKeys.todos.all, // typed!
|
|
35
44
|
queryFn: async () => {
|
|
36
45
|
// ...
|
|
37
46
|
},
|
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:
|
|
3
|
+
[key: string]: ((...args: any[]) => unknown) | (string | number)[] | string | number | null | NestedKeys;
|
|
4
4
|
}
|
|
5
|
-
type QueryKeys<Path extends string[], KeysDef extends NestedKeys> =
|
|
6
|
-
|
|
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(
|
|
28
|
-
|
|
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
|
|
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
|
}
|