@trpc/react-query 11.0.0-next-beta.205 → 11.0.0-next-beta.208

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.
Files changed (38) hide show
  1. package/dist/bundle-analysis.json +40 -40
  2. package/dist/createTRPCQueryUtils.js +16 -0
  3. package/dist/createTRPCQueryUtils.mjs +14 -0
  4. package/dist/createTRPCReact.js +58 -0
  5. package/dist/createTRPCReact.mjs +36 -0
  6. package/dist/index.js +10 -64
  7. package/dist/index.mjs +3 -40
  8. package/dist/internals/context.js +33 -0
  9. package/dist/internals/context.mjs +11 -0
  10. package/dist/internals/getClientArgs.js +16 -0
  11. package/dist/internals/getClientArgs.mjs +14 -0
  12. package/dist/internals/getQueryKey.js +57 -0
  13. package/dist/internals/getQueryKey.mjs +54 -0
  14. package/dist/internals/useHookResult.js +32 -0
  15. package/dist/internals/useHookResult.mjs +11 -0
  16. package/dist/server/index.js +2 -102
  17. package/dist/server/index.mjs +1 -103
  18. package/dist/server/ssgProxy.js +107 -0
  19. package/dist/server/ssgProxy.mjs +105 -0
  20. package/dist/{createHooksInternal-f77072af.js → shared/hooks/createHooksInternal.js} +28 -183
  21. package/dist/{createHooksInternal-0df35117.mjs → shared/hooks/createHooksInternal.mjs} +8 -158
  22. package/dist/shared/index.js +13 -15
  23. package/dist/shared/index.mjs +7 -7
  24. package/dist/shared/proxy/decorationProxy.js +31 -0
  25. package/dist/shared/proxy/decorationProxy.mjs +29 -0
  26. package/dist/shared/proxy/useQueriesProxy.js +25 -0
  27. package/dist/shared/proxy/useQueriesProxy.mjs +23 -0
  28. package/dist/shared/proxy/utilsProxy.js +89 -0
  29. package/dist/{utilsProxy-12979321.mjs → shared/proxy/utilsProxy.mjs} +3 -63
  30. package/dist/{queryClient-4d766c0c.mjs → shared/queryClient.mjs} +1 -1
  31. package/dist/utils/createUtilityFunctions.js +94 -0
  32. package/dist/utils/createUtilityFunctions.mjs +92 -0
  33. package/package.json +6 -6
  34. package/dist/createHooksInternal-3074c6c4.js +0 -470
  35. package/dist/queryClient-1c8d7d8a.js +0 -8
  36. package/dist/utilsProxy-13814792.js +0 -142
  37. package/dist/utilsProxy-4f6da312.js +0 -173
  38. /package/dist/{queryClient-358a9a75.js → shared/queryClient.js} +0 -0
@@ -1,11 +1,11 @@
1
1
  {
2
- "bundleSize": 27176,
2
+ "bundleSize": 27468,
3
3
  "bundleOrigSize": 62493,
4
- "bundleReduction": 56.51,
4
+ "bundleReduction": 56.05,
5
5
  "modules": [
6
6
  {
7
7
  "id": "/src/shared/hooks/createHooksInternal.tsx",
8
- "size": 12646,
8
+ "size": 12843,
9
9
  "origSize": 15390,
10
10
  "renderedExports": [
11
11
  "createRootHooks"
@@ -14,12 +14,12 @@
14
14
  "dependents": [
15
15
  "/src/createTRPCReact.tsx"
16
16
  ],
17
- "percent": 46.53,
18
- "reduction": 17.83
17
+ "percent": 46.76,
18
+ "reduction": 16.55
19
19
  },
20
20
  {
21
21
  "id": "/src/server/ssgProxy.ts",
22
- "size": 3635,
22
+ "size": 3636,
23
23
  "origSize": 7113,
24
24
  "renderedExports": [
25
25
  "createServerSideHelpers"
@@ -28,12 +28,12 @@
28
28
  "dependents": [
29
29
  "/src/server/index.ts"
30
30
  ],
31
- "percent": 13.38,
32
- "reduction": 48.9
31
+ "percent": 13.24,
32
+ "reduction": 48.88
33
33
  },
34
34
  {
35
35
  "id": "/src/utils/createUtilityFunctions.ts",
36
- "size": 3264,
36
+ "size": 3334,
37
37
  "origSize": 3695,
38
38
  "renderedExports": [
39
39
  "createUtilityFunctions"
@@ -43,12 +43,12 @@
43
43
  "/src/createTRPCQueryUtils.tsx",
44
44
  "/src/shared/hooks/createHooksInternal.tsx"
45
45
  ],
46
- "percent": 12.01,
47
- "reduction": 11.66
46
+ "percent": 12.14,
47
+ "reduction": 9.77
48
48
  },
49
49
  {
50
50
  "id": "/src/shared/proxy/utilsProxy.ts",
51
- "size": 2660,
51
+ "size": 2688,
52
52
  "origSize": 10430,
53
53
  "renderedExports": [
54
54
  "getQueryType",
@@ -60,7 +60,7 @@
60
60
  "/src/shared/index.ts"
61
61
  ],
62
62
  "percent": 9.79,
63
- "reduction": 74.5
63
+ "reduction": 74.23
64
64
  },
65
65
  {
66
66
  "id": "/src/internals/getQueryKey.ts",
@@ -73,17 +73,17 @@
73
73
  "removedExports": [],
74
74
  "dependents": [
75
75
  "/src/index.ts",
76
- "/src/server/ssgProxy.ts",
77
76
  "/src/shared/proxy/utilsProxy.ts",
78
77
  "/src/shared/proxy/useQueriesProxy.ts",
79
- "/src/shared/hooks/createHooksInternal.tsx"
78
+ "/src/shared/hooks/createHooksInternal.tsx",
79
+ "/src/server/ssgProxy.ts"
80
80
  ],
81
- "percent": 6.97,
81
+ "percent": 6.9,
82
82
  "reduction": 54.95
83
83
  },
84
84
  {
85
85
  "id": "/src/createTRPCReact.tsx",
86
- "size": 837,
86
+ "size": 831,
87
87
  "origSize": 8737,
88
88
  "renderedExports": [
89
89
  "createHooksInternal",
@@ -93,8 +93,8 @@
93
93
  "dependents": [
94
94
  "/src/index.ts"
95
95
  ],
96
- "percent": 3.08,
97
- "reduction": 90.42
96
+ "percent": 3.03,
97
+ "reduction": 90.49
98
98
  },
99
99
  {
100
100
  "id": "/src/shared/proxy/decorationProxy.ts",
@@ -107,12 +107,12 @@
107
107
  "dependents": [
108
108
  "/src/shared/index.ts"
109
109
  ],
110
- "percent": 2.9,
110
+ "percent": 2.87,
111
111
  "reduction": 20.42
112
112
  },
113
113
  {
114
114
  "id": "/src/shared/proxy/useQueriesProxy.ts",
115
- "size": 549,
115
+ "size": 550,
116
116
  "origSize": 3113,
117
117
  "renderedExports": [
118
118
  "createUseQueries"
@@ -122,8 +122,8 @@
122
122
  "/src/shared/index.ts",
123
123
  "/src/shared/hooks/createHooksInternal.tsx"
124
124
  ],
125
- "percent": 2.02,
126
- "reduction": 82.36
125
+ "percent": 2,
126
+ "reduction": 82.33
127
127
  },
128
128
  {
129
129
  "id": "/src/internals/getClientArgs.ts",
@@ -135,10 +135,10 @@
135
135
  "removedExports": [],
136
136
  "dependents": [
137
137
  "/src/shared/index.ts",
138
- "/src/utils/createUtilityFunctions.ts",
139
- "/src/shared/hooks/createHooksInternal.tsx"
138
+ "/src/shared/hooks/createHooksInternal.tsx",
139
+ "/src/utils/createUtilityFunctions.ts"
140
140
  ],
141
- "percent": 0.98,
141
+ "percent": 0.96,
142
142
  "reduction": 27
143
143
  },
144
144
  {
@@ -152,12 +152,12 @@
152
152
  "dependents": [
153
153
  "/src/shared/hooks/createHooksInternal.tsx"
154
154
  ],
155
- "percent": 0.72,
155
+ "percent": 0.71,
156
156
  "reduction": 42.98
157
157
  },
158
158
  {
159
159
  "id": "/src/createTRPCQueryUtils.tsx",
160
- "size": 161,
160
+ "size": 162,
161
161
  "origSize": 455,
162
162
  "renderedExports": [
163
163
  "createTRPCQueryUtils"
@@ -167,7 +167,7 @@
167
167
  "/src/index.ts"
168
168
  ],
169
169
  "percent": 0.59,
170
- "reduction": 64.62
170
+ "reduction": 64.4
171
171
  },
172
172
  {
173
173
  "id": "/src/internals/context.tsx",
@@ -184,7 +184,7 @@
184
184
  "/src/shared/proxy/utilsProxy.ts",
185
185
  "/src/shared/hooks/createHooksInternal.tsx"
186
186
  ],
187
- "percent": 0.57,
187
+ "percent": 0.56,
188
188
  "reduction": 97.46
189
189
  },
190
190
  {
@@ -211,16 +211,6 @@
211
211
  "percent": 0,
212
212
  "reduction": 100
213
213
  },
214
- {
215
- "id": "/src/server/index.ts",
216
- "size": 0,
217
- "origSize": 54,
218
- "renderedExports": [],
219
- "removedExports": [],
220
- "dependents": [],
221
- "percent": 0,
222
- "reduction": 100
223
- },
224
214
  {
225
215
  "id": "/src/shared/index.ts",
226
216
  "size": 0,
@@ -234,6 +224,16 @@
234
224
  ],
235
225
  "percent": 0,
236
226
  "reduction": 100
227
+ },
228
+ {
229
+ "id": "/src/server/index.ts",
230
+ "size": 0,
231
+ "origSize": 54,
232
+ "renderedExports": [],
233
+ "removedExports": [],
234
+ "dependents": [],
235
+ "percent": 0,
236
+ "reduction": 100
237
237
  }
238
238
  ],
239
239
  "moduleCount": 16
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+
3
+ require('@trpc/core');
4
+ var utilsProxy = require('./shared/proxy/utilsProxy.js');
5
+ require('@tanstack/react-query');
6
+ require('@trpc/client');
7
+ require('react');
8
+ require('./internals/context.js');
9
+ var createUtilityFunctions = require('./utils/createUtilityFunctions.js');
10
+
11
+ function createTRPCQueryUtils(opts) {
12
+ const utils = createUtilityFunctions.createUtilityFunctions(opts);
13
+ return utilsProxy.createQueryUtilsProxy(utils);
14
+ }
15
+
16
+ exports.createTRPCQueryUtils = createTRPCQueryUtils;
@@ -0,0 +1,14 @@
1
+ import '@trpc/core';
2
+ import { createQueryUtilsProxy } from './shared/proxy/utilsProxy.mjs';
3
+ import '@tanstack/react-query';
4
+ import '@trpc/client';
5
+ import 'react';
6
+ import './internals/context.mjs';
7
+ import { createUtilityFunctions } from './utils/createUtilityFunctions.mjs';
8
+
9
+ function createTRPCQueryUtils(opts) {
10
+ const utils = createUtilityFunctions(opts);
11
+ return createQueryUtilsProxy(utils);
12
+ }
13
+
14
+ export { createTRPCQueryUtils };
@@ -0,0 +1,58 @@
1
+ 'use strict';
2
+
3
+ var core = require('@trpc/core');
4
+ var React = require('react');
5
+ var decorationProxy = require('./shared/proxy/decorationProxy.js');
6
+ var utilsProxy = require('./shared/proxy/utilsProxy.js');
7
+ var createHooksInternal$1 = require('./shared/hooks/createHooksInternal.js');
8
+ require('@tanstack/react-query');
9
+ require('./internals/context.js');
10
+
11
+ function _interopNamespaceDefault(e) {
12
+ var n = Object.create(null);
13
+ if (e) {
14
+ Object.keys(e).forEach(function (k) {
15
+ if (k !== 'default') {
16
+ var d = Object.getOwnPropertyDescriptor(e, k);
17
+ Object.defineProperty(n, k, d.get ? d : {
18
+ enumerable: true,
19
+ get: function () { return e[k]; }
20
+ });
21
+ }
22
+ });
23
+ }
24
+ n.default = e;
25
+ return Object.freeze(n);
26
+ }
27
+
28
+ var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
29
+
30
+ /**
31
+ * @internal
32
+ */ function createHooksInternal(trpc) {
33
+ return core.createFlatProxy((key)=>{
34
+ if (key === 'useContext' || key === 'useUtils') {
35
+ return ()=>{
36
+ const context = trpc.useUtils();
37
+ // create a stable reference of the utils context
38
+ return React__namespace.useMemo(()=>{
39
+ return utilsProxy.createReactQueryUtils(context);
40
+ }, [
41
+ context
42
+ ]);
43
+ };
44
+ }
45
+ if (trpc.hasOwnProperty(key)) {
46
+ return trpc[key];
47
+ }
48
+ return decorationProxy.createReactDecoration(key, trpc);
49
+ });
50
+ }
51
+ function createTRPCReact(opts) {
52
+ const hooks = createHooksInternal$1.createRootHooks(opts);
53
+ const proxy = createHooksInternal(hooks);
54
+ return proxy;
55
+ }
56
+
57
+ exports.createHooksInternal = createHooksInternal;
58
+ exports.createTRPCReact = createTRPCReact;
@@ -0,0 +1,36 @@
1
+ import { createFlatProxy } from '@trpc/core';
2
+ import * as React from 'react';
3
+ import { createReactDecoration } from './shared/proxy/decorationProxy.mjs';
4
+ import { createReactQueryUtils } from './shared/proxy/utilsProxy.mjs';
5
+ import { createRootHooks } from './shared/hooks/createHooksInternal.mjs';
6
+ import '@tanstack/react-query';
7
+ import './internals/context.mjs';
8
+
9
+ /**
10
+ * @internal
11
+ */ function createHooksInternal(trpc) {
12
+ return createFlatProxy((key)=>{
13
+ if (key === 'useContext' || key === 'useUtils') {
14
+ return ()=>{
15
+ const context = trpc.useUtils();
16
+ // create a stable reference of the utils context
17
+ return React.useMemo(()=>{
18
+ return createReactQueryUtils(context);
19
+ }, [
20
+ context
21
+ ]);
22
+ };
23
+ }
24
+ if (trpc.hasOwnProperty(key)) {
25
+ return trpc[key];
26
+ }
27
+ return createReactDecoration(key, trpc);
28
+ });
29
+ }
30
+ function createTRPCReact(opts) {
31
+ const hooks = createRootHooks(opts);
32
+ const proxy = createHooksInternal(hooks);
33
+ return proxy;
34
+ }
35
+
36
+ export { createHooksInternal, createTRPCReact };
package/dist/index.js CHANGED
@@ -1,72 +1,18 @@
1
1
  'use strict';
2
2
 
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
3
  var client = require('@trpc/client');
6
- var utilsProxy = require('./utilsProxy-4f6da312.js');
7
- var core = require('@trpc/core');
8
- var React = require('react');
9
- var createHooksInternal$1 = require('./createHooksInternal-f77072af.js');
10
- require('@tanstack/react-query');
11
-
12
- function _interopNamespace(e) {
13
- if (e && e.__esModule) return e;
14
- var n = Object.create(null);
15
- if (e) {
16
- Object.keys(e).forEach(function (k) {
17
- if (k !== 'default') {
18
- var d = Object.getOwnPropertyDescriptor(e, k);
19
- Object.defineProperty(n, k, d.get ? d : {
20
- enumerable: true,
21
- get: function () { return e[k]; }
22
- });
23
- }
24
- });
25
- }
26
- n["default"] = e;
27
- return Object.freeze(n);
28
- }
29
-
30
- var React__namespace = /*#__PURE__*/_interopNamespace(React);
4
+ var getQueryKey = require('./internals/getQueryKey.js');
5
+ var createTRPCReact = require('./createTRPCReact.js');
6
+ var createTRPCQueryUtils = require('./createTRPCQueryUtils.js');
31
7
 
32
- /**
33
- * @internal
34
- */ function createHooksInternal(trpc) {
35
- return core.createFlatProxy((key)=>{
36
- if (key === 'useContext' || key === 'useUtils') {
37
- return ()=>{
38
- const context = trpc.useUtils();
39
- // create a stable reference of the utils context
40
- return React__namespace.useMemo(()=>{
41
- return utilsProxy.createReactQueryUtils(context);
42
- }, [
43
- context
44
- ]);
45
- };
46
- }
47
- if (trpc.hasOwnProperty(key)) {
48
- return trpc[key];
49
- }
50
- return createHooksInternal$1.createReactDecoration(key, trpc);
51
- });
52
- }
53
- function createTRPCReact(opts) {
54
- const hooks = createHooksInternal$1.createRootHooks(opts);
55
- const proxy = createHooksInternal(hooks);
56
- return proxy;
57
- }
58
8
 
59
- function createTRPCQueryUtils(opts) {
60
- const utils = createHooksInternal$1.createUtilityFunctions(opts);
61
- return utilsProxy.createQueryUtilsProxy(utils);
62
- }
63
9
 
64
- exports.getQueryKey = utilsProxy.getQueryKey;
65
- exports.createTRPCQueryUtils = createTRPCQueryUtils;
66
- exports.createTRPCReact = createTRPCReact;
10
+ exports.getQueryKey = getQueryKey.getQueryKey;
11
+ exports.createTRPCReact = createTRPCReact.createTRPCReact;
12
+ exports.createTRPCQueryUtils = createTRPCQueryUtils.createTRPCQueryUtils;
67
13
  Object.keys(client).forEach(function (k) {
68
- if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
69
- enumerable: true,
70
- get: function () { return client[k]; }
71
- });
14
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
15
+ enumerable: true,
16
+ get: function () { return client[k]; }
17
+ });
72
18
  });
package/dist/index.mjs CHANGED
@@ -1,41 +1,4 @@
1
1
  export * from '@trpc/client';
2
- import { c as createReactQueryUtils, a as createQueryUtilsProxy } from './utilsProxy-12979321.mjs';
3
- export { g as getQueryKey } from './utilsProxy-12979321.mjs';
4
- import { createFlatProxy } from '@trpc/core';
5
- import * as React from 'react';
6
- import { c as createRootHooks, a as createReactDecoration, b as createUtilityFunctions } from './createHooksInternal-0df35117.mjs';
7
- import '@tanstack/react-query';
8
-
9
- /**
10
- * @internal
11
- */ function createHooksInternal(trpc) {
12
- return createFlatProxy((key)=>{
13
- if (key === 'useContext' || key === 'useUtils') {
14
- return ()=>{
15
- const context = trpc.useUtils();
16
- // create a stable reference of the utils context
17
- return React.useMemo(()=>{
18
- return createReactQueryUtils(context);
19
- }, [
20
- context
21
- ]);
22
- };
23
- }
24
- if (trpc.hasOwnProperty(key)) {
25
- return trpc[key];
26
- }
27
- return createReactDecoration(key, trpc);
28
- });
29
- }
30
- function createTRPCReact(opts) {
31
- const hooks = createRootHooks(opts);
32
- const proxy = createHooksInternal(hooks);
33
- return proxy;
34
- }
35
-
36
- function createTRPCQueryUtils(opts) {
37
- const utils = createUtilityFunctions(opts);
38
- return createQueryUtilsProxy(utils);
39
- }
40
-
41
- export { createTRPCQueryUtils, createTRPCReact };
2
+ export { getQueryKey } from './internals/getQueryKey.mjs';
3
+ export { createTRPCReact } from './createTRPCReact.mjs';
4
+ export { createTRPCQueryUtils } from './createTRPCQueryUtils.mjs';
@@ -0,0 +1,33 @@
1
+ 'use strict';
2
+
3
+ var React = require('react');
4
+
5
+ function _interopNamespaceDefault(e) {
6
+ var n = Object.create(null);
7
+ if (e) {
8
+ Object.keys(e).forEach(function (k) {
9
+ if (k !== 'default') {
10
+ var d = Object.getOwnPropertyDescriptor(e, k);
11
+ Object.defineProperty(n, k, d.get ? d : {
12
+ enumerable: true,
13
+ get: function () { return e[k]; }
14
+ });
15
+ }
16
+ });
17
+ }
18
+ n.default = e;
19
+ return Object.freeze(n);
20
+ }
21
+
22
+ var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
23
+
24
+ const contextProps = [
25
+ 'client',
26
+ 'ssrContext',
27
+ 'ssrState',
28
+ 'abortOnUnmount'
29
+ ];
30
+ const TRPCContext = React__namespace.createContext?.(null);
31
+
32
+ exports.TRPCContext = TRPCContext;
33
+ exports.contextProps = contextProps;
@@ -0,0 +1,11 @@
1
+ import * as React from 'react';
2
+
3
+ const contextProps = [
4
+ 'client',
5
+ 'ssrContext',
6
+ 'ssrState',
7
+ 'abortOnUnmount'
8
+ ];
9
+ const TRPCContext = React.createContext?.(null);
10
+
11
+ export { TRPCContext, contextProps };
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * @internal
5
+ */ function getClientArgs(queryKey, opts, pageParam) {
6
+ const path = queryKey[0];
7
+ const input = queryKey[1]?.input;
8
+ if (pageParam) input.cursor = pageParam;
9
+ return [
10
+ path.join('.'),
11
+ input,
12
+ opts?.trpc
13
+ ];
14
+ }
15
+
16
+ exports.getClientArgs = getClientArgs;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * @internal
3
+ */ function getClientArgs(queryKey, opts, pageParam) {
4
+ const path = queryKey[0];
5
+ const input = queryKey[1]?.input;
6
+ if (pageParam) input.cursor = pageParam;
7
+ return [
8
+ path.join('.'),
9
+ input,
10
+ opts?.trpc
11
+ ];
12
+ }
13
+
14
+ export { getClientArgs };
@@ -0,0 +1,57 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * To allow easy interactions with groups of related queries, such as
5
+ * invalidating all queries of a router, we use an array as the path when
6
+ * storing in tanstack query.
7
+ **/ function getQueryKeyInternal(path, input, type) {
8
+ // Construct a query key that is easy to destructure and flexible for
9
+ // partial selecting etc.
10
+ // https://github.com/trpc/trpc/issues/3128
11
+ // some parts of the path may be dot-separated, split them up
12
+ const splitPath = path.flatMap((part)=>part.split('.'));
13
+ if (!input && (!type || type === 'any')) {
14
+ // for `utils.invalidate()` to match all queries (including vanilla react-query)
15
+ // we don't want nested array if path is empty, i.e. `[]` instead of `[[]]`
16
+ return splitPath.length ? [
17
+ splitPath
18
+ ] : [];
19
+ }
20
+ if (type === 'infinite' && input && typeof input === 'object' && 'cursor' in input) {
21
+ const { cursor: _ , ...inputWithoutCursor } = input;
22
+ return [
23
+ splitPath,
24
+ {
25
+ input: inputWithoutCursor,
26
+ type: 'infinite'
27
+ }
28
+ ];
29
+ }
30
+ return [
31
+ splitPath,
32
+ {
33
+ ...typeof input !== 'undefined' && {
34
+ input: input
35
+ },
36
+ ...type && type !== 'any' && {
37
+ type: type
38
+ }
39
+ }
40
+ ];
41
+ }
42
+ /**
43
+ * Method to extract the query key for a procedure
44
+ * @param procedureOrRouter - procedure or AnyRouter
45
+ * @param input - input to procedureOrRouter
46
+ * @param type - defaults to `any`
47
+ * @link https://trpc.io/docs/v11/getQueryKey
48
+ */ function getQueryKey(..._params) {
49
+ const [procedureOrRouter, input, type] = _params;
50
+ // @ts-expect-error - we don't expose _def on the type layer
51
+ const path = procedureOrRouter._def().path;
52
+ const queryKey = getQueryKeyInternal(path, input, type ?? 'any');
53
+ return queryKey;
54
+ }
55
+
56
+ exports.getQueryKey = getQueryKey;
57
+ exports.getQueryKeyInternal = getQueryKeyInternal;
@@ -0,0 +1,54 @@
1
+ /**
2
+ * To allow easy interactions with groups of related queries, such as
3
+ * invalidating all queries of a router, we use an array as the path when
4
+ * storing in tanstack query.
5
+ **/ function getQueryKeyInternal(path, input, type) {
6
+ // Construct a query key that is easy to destructure and flexible for
7
+ // partial selecting etc.
8
+ // https://github.com/trpc/trpc/issues/3128
9
+ // some parts of the path may be dot-separated, split them up
10
+ const splitPath = path.flatMap((part)=>part.split('.'));
11
+ if (!input && (!type || type === 'any')) {
12
+ // for `utils.invalidate()` to match all queries (including vanilla react-query)
13
+ // we don't want nested array if path is empty, i.e. `[]` instead of `[[]]`
14
+ return splitPath.length ? [
15
+ splitPath
16
+ ] : [];
17
+ }
18
+ if (type === 'infinite' && input && typeof input === 'object' && 'cursor' in input) {
19
+ const { cursor: _ , ...inputWithoutCursor } = input;
20
+ return [
21
+ splitPath,
22
+ {
23
+ input: inputWithoutCursor,
24
+ type: 'infinite'
25
+ }
26
+ ];
27
+ }
28
+ return [
29
+ splitPath,
30
+ {
31
+ ...typeof input !== 'undefined' && {
32
+ input: input
33
+ },
34
+ ...type && type !== 'any' && {
35
+ type: type
36
+ }
37
+ }
38
+ ];
39
+ }
40
+ /**
41
+ * Method to extract the query key for a procedure
42
+ * @param procedureOrRouter - procedure or AnyRouter
43
+ * @param input - input to procedureOrRouter
44
+ * @param type - defaults to `any`
45
+ * @link https://trpc.io/docs/v11/getQueryKey
46
+ */ function getQueryKey(..._params) {
47
+ const [procedureOrRouter, input, type] = _params;
48
+ // @ts-expect-error - we don't expose _def on the type layer
49
+ const path = procedureOrRouter._def().path;
50
+ const queryKey = getQueryKeyInternal(path, input, type ?? 'any');
51
+ return queryKey;
52
+ }
53
+
54
+ export { getQueryKey, getQueryKeyInternal };