query-keys 0.0.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.
@@ -0,0 +1,8 @@
1
+ //#region src/index.d.ts
2
+ interface NestedKeys {
3
+ [key: string]: ((...args: never[]) => unknown) | string | number | null | NestedKeys;
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>;
7
+ //#endregion
8
+ export { QueryKeys, createQueryKeys };
package/dist/index.mjs ADDED
@@ -0,0 +1,32 @@
1
+ //#region src/index.ts
2
+ function recCreateQueryKeys(root, definition) {
3
+ const result = { _def: root };
4
+ for (const key in definition) {
5
+ if (!Object.hasOwn(definition, key)) continue;
6
+ const element = definition[key];
7
+ if (typeof element === "function") result[key] = (...args) => {
8
+ const ret = element(...args);
9
+ return [
10
+ ...root,
11
+ key,
12
+ ...Array.isArray(ret) ? ret : [ret]
13
+ ];
14
+ };
15
+ else if (typeof element === "object" && element !== null) result[key] = recCreateQueryKeys([...root, key], element);
16
+ else {
17
+ const lastKey = Array.isArray(element) ? element : element === null ? [] : [element];
18
+ result[key] = [
19
+ ...root,
20
+ key,
21
+ ...lastKey
22
+ ];
23
+ }
24
+ }
25
+ return result;
26
+ }
27
+ function createQueryKeys(root, definition) {
28
+ return recCreateQueryKeys([root], definition);
29
+ }
30
+
31
+ //#endregion
32
+ export { createQueryKeys };
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "query-keys",
3
+ "type": "module",
4
+ "version": "0.0.1",
5
+ "private": false,
6
+ "description": "A fully typed, simple utility for your query keys.",
7
+ "exports": {
8
+ ".": "./dist/index.mjs",
9
+ "./package.json": "./package.json"
10
+ },
11
+ "main": "./dist/index.mjs",
12
+ "module": "./dist/index.mjs",
13
+ "types": "./dist/index.d.mts",
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "scripts": {
18
+ "build": "tsdown",
19
+ "dev": "tsdown --watch"
20
+ },
21
+ "devDependencies": {
22
+ "@arethetypeswrong/core": "^0.18.2",
23
+ "publint": "^0.3.17",
24
+ "tsdown": "^0.18.1",
25
+ "typescript": "^5.9.3"
26
+ }
27
+ }