@trpc/react-query 11.0.0-alpha-tmp-react-sideeffects-import.202 → 11.0.0-alpha-tmp-ahmed-rollup-v4.203
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/dist/createTRPCQueryUtils.js +15 -0
- package/dist/createTRPCQueryUtils.mjs +13 -0
- package/dist/createTRPCReact.js +38 -0
- package/dist/createTRPCReact.mjs +35 -0
- package/dist/index.js +10 -64
- package/dist/index.mjs +3 -40
- package/dist/internals/context.d.ts +2 -2
- package/dist/internals/context.d.ts.map +1 -1
- package/dist/internals/context.js +14 -0
- package/dist/internals/context.mjs +11 -0
- package/dist/internals/getClientArgs.js +16 -0
- package/dist/internals/getClientArgs.mjs +14 -0
- package/dist/internals/getQueryKey.js +45 -0
- package/dist/internals/getQueryKey.mjs +42 -0
- package/dist/internals/useHookResult.js +13 -0
- package/dist/internals/useHookResult.mjs +11 -0
- package/dist/server/index.js +2 -102
- package/dist/server/index.mjs +1 -103
- package/dist/server/ssgProxy.js +106 -0
- package/dist/server/ssgProxy.mjs +104 -0
- package/dist/shared/hooks/createHooksInternal.d.ts.map +1 -1
- package/dist/{createHooksInternal-5d2fa367.js → shared/hooks/createHooksInternal.js} +33 -207
- package/dist/{createHooksInternal-e0b0564e.mjs → shared/hooks/createHooksInternal.mjs} +16 -166
- package/dist/shared/index.js +13 -15
- package/dist/shared/index.mjs +7 -7
- package/dist/shared/proxy/decorationProxy.js +31 -0
- package/dist/shared/proxy/decorationProxy.mjs +29 -0
- package/dist/shared/proxy/useQueriesProxy.js +25 -0
- package/dist/shared/proxy/useQueriesProxy.mjs +23 -0
- package/dist/shared/proxy/utilsProxy.js +89 -0
- package/dist/{utilsProxy-61a4601f.mjs → shared/proxy/utilsProxy.mjs} +3 -51
- package/dist/{queryClient-4d766c0c.mjs → shared/queryClient.mjs} +1 -1
- package/dist/utils/createUtilityFunctions.js +94 -0
- package/dist/utils/createUtilityFunctions.mjs +92 -0
- package/package.json +6 -6
- package/src/createTRPCReact.tsx +2 -2
- package/src/internals/context.tsx +2 -2
- package/src/internals/useHookResult.ts +2 -2
- package/src/shared/hooks/createHooksInternal.tsx +8 -10
- package/dist/createHooksInternal-4285c71a.js +0 -470
- package/dist/queryClient-1c8d7d8a.js +0 -8
- package/dist/utilsProxy-0b88c1e3.js +0 -161
- package/dist/utilsProxy-ff357a62.js +0 -128
- /package/dist/{queryClient-358a9a75.js → shared/queryClient.js} +0 -0
|
@@ -1,162 +1,12 @@
|
|
|
1
|
-
import { createRecursiveProxy } from '@trpc/core';
|
|
2
1
|
import { useQuery, useSuspenseQuery, useQueryClient, useMutation, hashKey, useInfiniteQuery, useSuspenseInfiniteQuery, useQueries, useSuspenseQueries } from '@tanstack/react-query';
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
return createRecursiveProxy(({ path , args })=>{
|
|
12
|
-
const pathCopy = [
|
|
13
|
-
name,
|
|
14
|
-
...path
|
|
15
|
-
];
|
|
16
|
-
// The last arg is for instance `.useMutation` or `.useQuery()`
|
|
17
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
18
|
-
const lastArg = pathCopy.pop();
|
|
19
|
-
if (lastArg === 'useMutation') {
|
|
20
|
-
return hooks[lastArg](pathCopy, ...args);
|
|
21
|
-
}
|
|
22
|
-
if (lastArg === '_def') {
|
|
23
|
-
return {
|
|
24
|
-
path: pathCopy
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
const [input, ...rest] = args;
|
|
28
|
-
const opts = rest[0] || {};
|
|
29
|
-
return hooks[lastArg](pathCopy, input, opts);
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Create proxy for `useQueries` options
|
|
35
|
-
* @internal
|
|
36
|
-
*/ function createUseQueries(client) {
|
|
37
|
-
return createRecursiveProxy((opts)=>{
|
|
38
|
-
const arrayPath = opts.path;
|
|
39
|
-
const dotPath = arrayPath.join('.');
|
|
40
|
-
const [input, _opts] = opts.args;
|
|
41
|
-
const options = {
|
|
42
|
-
queryKey: getQueryKeyInternal(arrayPath, input, 'query'),
|
|
43
|
-
queryFn: ()=>{
|
|
44
|
-
return client.query(dotPath, input, _opts?.trpc);
|
|
45
|
-
},
|
|
46
|
-
..._opts
|
|
47
|
-
};
|
|
48
|
-
return options;
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* @internal
|
|
54
|
-
*/ function getClientArgs(queryKey, opts, pageParam) {
|
|
55
|
-
const path = queryKey[0];
|
|
56
|
-
const input = queryKey[1]?.input;
|
|
57
|
-
if (pageParam) input.cursor = pageParam;
|
|
58
|
-
return [
|
|
59
|
-
path.join('.'),
|
|
60
|
-
input,
|
|
61
|
-
opts?.trpc
|
|
62
|
-
];
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Makes a stable reference of the `trpc` prop
|
|
67
|
-
*/ function useHookResult(value) {
|
|
68
|
-
const ref = React.useRef(value);
|
|
69
|
-
ref.current.path = value.path;
|
|
70
|
-
return ref.current;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Creates a set of utility functions that can be used to interact with `react-query`
|
|
75
|
-
* @param opts the `TRPCClient` and `QueryClient` to use
|
|
76
|
-
* @returns a set of utility functions that can be used to interact with `react-query`
|
|
77
|
-
* @internal
|
|
78
|
-
*/ function createUtilityFunctions(opts) {
|
|
79
|
-
const { client , queryClient } = opts;
|
|
80
|
-
const untypedClient = client instanceof TRPCUntypedClient ? client : getUntypedClient(client);
|
|
81
|
-
return {
|
|
82
|
-
fetchQuery: (queryKey, opts)=>{
|
|
83
|
-
return queryClient.fetchQuery({
|
|
84
|
-
...opts,
|
|
85
|
-
queryKey,
|
|
86
|
-
queryFn: ()=>untypedClient.query(...getClientArgs(queryKey, opts))
|
|
87
|
-
});
|
|
88
|
-
},
|
|
89
|
-
fetchInfiniteQuery: (queryKey, opts)=>{
|
|
90
|
-
return queryClient.fetchInfiniteQuery({
|
|
91
|
-
...opts,
|
|
92
|
-
queryKey,
|
|
93
|
-
queryFn: ({ pageParam })=>{
|
|
94
|
-
return untypedClient.query(...getClientArgs(queryKey, opts, pageParam));
|
|
95
|
-
},
|
|
96
|
-
initialPageParam: opts?.initialCursor ?? null
|
|
97
|
-
});
|
|
98
|
-
},
|
|
99
|
-
prefetchQuery: (queryKey, opts)=>{
|
|
100
|
-
return queryClient.prefetchQuery({
|
|
101
|
-
...opts,
|
|
102
|
-
queryKey,
|
|
103
|
-
queryFn: ()=>untypedClient.query(...getClientArgs(queryKey, opts))
|
|
104
|
-
});
|
|
105
|
-
},
|
|
106
|
-
prefetchInfiniteQuery: (queryKey, opts)=>{
|
|
107
|
-
return queryClient.prefetchInfiniteQuery({
|
|
108
|
-
...opts,
|
|
109
|
-
queryKey,
|
|
110
|
-
queryFn: ({ pageParam })=>{
|
|
111
|
-
return untypedClient.query(...getClientArgs(queryKey, opts, pageParam));
|
|
112
|
-
},
|
|
113
|
-
initialPageParam: opts?.initialCursor ?? null
|
|
114
|
-
});
|
|
115
|
-
},
|
|
116
|
-
ensureQueryData: (queryKey, opts)=>{
|
|
117
|
-
return queryClient.ensureQueryData({
|
|
118
|
-
...opts,
|
|
119
|
-
queryKey,
|
|
120
|
-
queryFn: ()=>untypedClient.query(...getClientArgs(queryKey, opts))
|
|
121
|
-
});
|
|
122
|
-
},
|
|
123
|
-
invalidateQueries: (queryKey, filters, options)=>{
|
|
124
|
-
return queryClient.invalidateQueries({
|
|
125
|
-
...filters,
|
|
126
|
-
queryKey
|
|
127
|
-
}, options);
|
|
128
|
-
},
|
|
129
|
-
resetQueries: (queryKey, filters, options)=>{
|
|
130
|
-
return queryClient.resetQueries({
|
|
131
|
-
...filters,
|
|
132
|
-
queryKey
|
|
133
|
-
}, options);
|
|
134
|
-
},
|
|
135
|
-
refetchQueries: (queryKey, filters, options)=>{
|
|
136
|
-
return queryClient.refetchQueries({
|
|
137
|
-
...filters,
|
|
138
|
-
queryKey
|
|
139
|
-
}, options);
|
|
140
|
-
},
|
|
141
|
-
cancelQuery: (queryKey, options)=>{
|
|
142
|
-
return queryClient.cancelQueries({
|
|
143
|
-
queryKey
|
|
144
|
-
}, options);
|
|
145
|
-
},
|
|
146
|
-
setQueryData: (queryKey, updater, options)=>{
|
|
147
|
-
return queryClient.setQueryData(queryKey, updater, options);
|
|
148
|
-
},
|
|
149
|
-
getQueryData: (queryKey)=>{
|
|
150
|
-
return queryClient.getQueryData(queryKey);
|
|
151
|
-
},
|
|
152
|
-
setInfiniteQueryData: (queryKey, updater, options)=>{
|
|
153
|
-
return queryClient.setQueryData(queryKey, updater, options);
|
|
154
|
-
},
|
|
155
|
-
getInfiniteQueryData: (queryKey)=>{
|
|
156
|
-
return queryClient.getQueryData(queryKey);
|
|
157
|
-
}
|
|
158
|
-
};
|
|
159
|
-
}
|
|
2
|
+
import { createTRPCUntypedClient } from '@trpc/client';
|
|
3
|
+
import React, { useState, useMemo, useEffect, useRef } from 'react';
|
|
4
|
+
import { TRPCContext } from '../../internals/context.mjs';
|
|
5
|
+
import { getClientArgs } from '../../internals/getClientArgs.mjs';
|
|
6
|
+
import { getQueryKeyInternal } from '../../internals/getQueryKey.mjs';
|
|
7
|
+
import { useHookResult } from '../../internals/useHookResult.mjs';
|
|
8
|
+
import { createUtilityFunctions } from '../../utils/createUtilityFunctions.mjs';
|
|
9
|
+
import { createUseQueries } from '../proxy/useQueriesProxy.mjs';
|
|
160
10
|
|
|
161
11
|
/**
|
|
162
12
|
* @internal
|
|
@@ -168,15 +18,15 @@ import { b as getQueryKeyInternal, T as TRPCContext } from './utilsProxy-61a4601
|
|
|
168
18
|
};
|
|
169
19
|
const TRPCProvider = (props)=>{
|
|
170
20
|
const { abortOnUnmount =false , client , queryClient , ssrContext } = props;
|
|
171
|
-
const [ssrState, setSSRState] =
|
|
172
|
-
const fns =
|
|
21
|
+
const [ssrState, setSSRState] = useState(props.ssrState ?? false);
|
|
22
|
+
const fns = useMemo(()=>createUtilityFunctions({
|
|
173
23
|
client,
|
|
174
24
|
queryClient
|
|
175
25
|
}), [
|
|
176
26
|
client,
|
|
177
27
|
queryClient
|
|
178
28
|
]);
|
|
179
|
-
const contextValue =
|
|
29
|
+
const contextValue = useMemo(()=>({
|
|
180
30
|
abortOnUnmount,
|
|
181
31
|
queryClient,
|
|
182
32
|
client,
|
|
@@ -191,7 +41,7 @@ import { b as getQueryKeyInternal, T as TRPCContext } from './utilsProxy-61a4601
|
|
|
191
41
|
ssrContext,
|
|
192
42
|
ssrState
|
|
193
43
|
]);
|
|
194
|
-
|
|
44
|
+
useEffect(()=>{
|
|
195
45
|
// Only updating state to `mounted` if we are using SSR.
|
|
196
46
|
// This makes it so we don't have an unnecessary re-render when opting out of SSR.
|
|
197
47
|
setSSRState((state)=>state ? 'mounted' : false);
|
|
@@ -317,9 +167,9 @@ import { b as getQueryKeyInternal, T as TRPCContext } from './utilsProxy-61a4601
|
|
|
317
167
|
const enabled = opts?.enabled ?? true;
|
|
318
168
|
const queryKey = hashKey(getQueryKeyInternal(path, input, 'any'));
|
|
319
169
|
const { client } = useContext();
|
|
320
|
-
const optsRef =
|
|
170
|
+
const optsRef = useRef(opts);
|
|
321
171
|
optsRef.current = opts;
|
|
322
|
-
|
|
172
|
+
useEffect(()=>{
|
|
323
173
|
if (!enabled) {
|
|
324
174
|
return;
|
|
325
175
|
}
|
|
@@ -465,7 +315,7 @@ import { b as getQueryKeyInternal, T as TRPCContext } from './utilsProxy-61a4601
|
|
|
465
315
|
];
|
|
466
316
|
};
|
|
467
317
|
const useDehydratedState = (client, trpcState)=>{
|
|
468
|
-
const transformed =
|
|
318
|
+
const transformed = useMemo(()=>{
|
|
469
319
|
if (!trpcState) {
|
|
470
320
|
return trpcState;
|
|
471
321
|
}
|
|
@@ -493,4 +343,4 @@ import { b as getQueryKeyInternal, T as TRPCContext } from './utilsProxy-61a4601
|
|
|
493
343
|
};
|
|
494
344
|
}
|
|
495
345
|
|
|
496
|
-
export {
|
|
346
|
+
export { createRootHooks };
|
package/dist/shared/index.js
CHANGED
|
@@ -1,24 +1,22 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
var decorationProxy = require('./proxy/decorationProxy.js');
|
|
4
|
+
var utilsProxy = require('./proxy/utilsProxy.js');
|
|
5
|
+
var useQueriesProxy = require('./proxy/useQueriesProxy.js');
|
|
6
|
+
var createHooksInternal = require('./hooks/createHooksInternal.js');
|
|
7
|
+
var queryClient = require('./queryClient.js');
|
|
8
|
+
var getClientArgs = require('../internals/getClientArgs.js');
|
|
9
|
+
var context = require('../internals/context.js');
|
|
4
10
|
|
|
5
|
-
var createHooksInternal = require('../createHooksInternal-5d2fa367.js');
|
|
6
|
-
var utilsProxy = require('../utilsProxy-0b88c1e3.js');
|
|
7
|
-
var queryClient = require('../queryClient-358a9a75.js');
|
|
8
|
-
require('@trpc/core');
|
|
9
|
-
require('@tanstack/react-query');
|
|
10
|
-
require('@trpc/client');
|
|
11
|
-
require('react');
|
|
12
11
|
|
|
13
12
|
|
|
14
|
-
|
|
15
|
-
exports.createReactDecoration = createHooksInternal.createReactDecoration;
|
|
16
|
-
exports.createRootHooks = createHooksInternal.createRootHooks;
|
|
17
|
-
exports.createUseQueries = createHooksInternal.createUseQueries;
|
|
18
|
-
exports.getClientArgs = createHooksInternal.getClientArgs;
|
|
19
|
-
exports.TRPCContext = utilsProxy.TRPCContext;
|
|
20
|
-
exports.contextProps = utilsProxy.contextProps;
|
|
13
|
+
exports.createReactDecoration = decorationProxy.createReactDecoration;
|
|
21
14
|
exports.createQueryUtilsProxy = utilsProxy.createQueryUtilsProxy;
|
|
22
15
|
exports.createReactQueryUtils = utilsProxy.createReactQueryUtils;
|
|
23
16
|
exports.getQueryType = utilsProxy.getQueryType;
|
|
17
|
+
exports.createUseQueries = useQueriesProxy.createUseQueries;
|
|
18
|
+
exports.createRootHooks = createHooksInternal.createRootHooks;
|
|
24
19
|
exports.getQueryClient = queryClient.getQueryClient;
|
|
20
|
+
exports.getClientArgs = getClientArgs.getClientArgs;
|
|
21
|
+
exports.TRPCContext = context.TRPCContext;
|
|
22
|
+
exports.contextProps = context.contextProps;
|
package/dist/shared/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
export { createReactDecoration } from './proxy/decorationProxy.mjs';
|
|
2
|
+
export { createQueryUtilsProxy, createReactQueryUtils, getQueryType } from './proxy/utilsProxy.mjs';
|
|
3
|
+
export { createUseQueries } from './proxy/useQueriesProxy.mjs';
|
|
4
|
+
export { createRootHooks } from './hooks/createHooksInternal.mjs';
|
|
5
|
+
export { getQueryClient } from './queryClient.mjs';
|
|
6
|
+
export { getClientArgs } from '../internals/getClientArgs.mjs';
|
|
7
|
+
export { TRPCContext, contextProps } from '../internals/context.mjs';
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var core = require('@trpc/core');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Create proxy for decorating procedures
|
|
7
|
+
* @internal
|
|
8
|
+
*/ function createReactDecoration(name, hooks) {
|
|
9
|
+
return core.createRecursiveProxy(({ path , args })=>{
|
|
10
|
+
const pathCopy = [
|
|
11
|
+
name,
|
|
12
|
+
...path
|
|
13
|
+
];
|
|
14
|
+
// The last arg is for instance `.useMutation` or `.useQuery()`
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
16
|
+
const lastArg = pathCopy.pop();
|
|
17
|
+
if (lastArg === 'useMutation') {
|
|
18
|
+
return hooks[lastArg](pathCopy, ...args);
|
|
19
|
+
}
|
|
20
|
+
if (lastArg === '_def') {
|
|
21
|
+
return {
|
|
22
|
+
path: pathCopy
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
const [input, ...rest] = args;
|
|
26
|
+
const opts = rest[0] || {};
|
|
27
|
+
return hooks[lastArg](pathCopy, input, opts);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
exports.createReactDecoration = createReactDecoration;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { createRecursiveProxy } from '@trpc/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Create proxy for decorating procedures
|
|
5
|
+
* @internal
|
|
6
|
+
*/ function createReactDecoration(name, hooks) {
|
|
7
|
+
return createRecursiveProxy(({ path , args })=>{
|
|
8
|
+
const pathCopy = [
|
|
9
|
+
name,
|
|
10
|
+
...path
|
|
11
|
+
];
|
|
12
|
+
// The last arg is for instance `.useMutation` or `.useQuery()`
|
|
13
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
14
|
+
const lastArg = pathCopy.pop();
|
|
15
|
+
if (lastArg === 'useMutation') {
|
|
16
|
+
return hooks[lastArg](pathCopy, ...args);
|
|
17
|
+
}
|
|
18
|
+
if (lastArg === '_def') {
|
|
19
|
+
return {
|
|
20
|
+
path: pathCopy
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
const [input, ...rest] = args;
|
|
24
|
+
const opts = rest[0] || {};
|
|
25
|
+
return hooks[lastArg](pathCopy, input, opts);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export { createReactDecoration };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var core = require('@trpc/core');
|
|
4
|
+
var getQueryKey = require('../../internals/getQueryKey.js');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Create proxy for `useQueries` options
|
|
8
|
+
* @internal
|
|
9
|
+
*/ function createUseQueries(client) {
|
|
10
|
+
return core.createRecursiveProxy((opts)=>{
|
|
11
|
+
const arrayPath = opts.path;
|
|
12
|
+
const dotPath = arrayPath.join('.');
|
|
13
|
+
const [input, _opts] = opts.args;
|
|
14
|
+
const options = {
|
|
15
|
+
queryKey: getQueryKey.getQueryKeyInternal(arrayPath, input, 'query'),
|
|
16
|
+
queryFn: ()=>{
|
|
17
|
+
return client.query(dotPath, input, _opts?.trpc);
|
|
18
|
+
},
|
|
19
|
+
..._opts
|
|
20
|
+
};
|
|
21
|
+
return options;
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
exports.createUseQueries = createUseQueries;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { createRecursiveProxy } from '@trpc/core';
|
|
2
|
+
import { getQueryKeyInternal } from '../../internals/getQueryKey.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Create proxy for `useQueries` options
|
|
6
|
+
* @internal
|
|
7
|
+
*/ function createUseQueries(client) {
|
|
8
|
+
return createRecursiveProxy((opts)=>{
|
|
9
|
+
const arrayPath = opts.path;
|
|
10
|
+
const dotPath = arrayPath.join('.');
|
|
11
|
+
const [input, _opts] = opts.args;
|
|
12
|
+
const options = {
|
|
13
|
+
queryKey: getQueryKeyInternal(arrayPath, input, 'query'),
|
|
14
|
+
queryFn: ()=>{
|
|
15
|
+
return client.query(dotPath, input, _opts?.trpc);
|
|
16
|
+
},
|
|
17
|
+
..._opts
|
|
18
|
+
};
|
|
19
|
+
return options;
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { createUseQueries };
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var client = require('@trpc/client');
|
|
4
|
+
var core = require('@trpc/core');
|
|
5
|
+
var context = require('../../internals/context.js');
|
|
6
|
+
var getQueryKey = require('../../internals/getQueryKey.js');
|
|
7
|
+
|
|
8
|
+
const getQueryType = (utilName)=>{
|
|
9
|
+
switch(utilName){
|
|
10
|
+
case 'fetch':
|
|
11
|
+
case 'ensureData':
|
|
12
|
+
case 'prefetch':
|
|
13
|
+
case 'getData':
|
|
14
|
+
case 'setData':
|
|
15
|
+
return 'query';
|
|
16
|
+
case 'fetchInfinite':
|
|
17
|
+
case 'prefetchInfinite':
|
|
18
|
+
case 'getInfiniteData':
|
|
19
|
+
case 'setInfiniteData':
|
|
20
|
+
return 'infinite';
|
|
21
|
+
case 'cancel':
|
|
22
|
+
case 'invalidate':
|
|
23
|
+
case 'refetch':
|
|
24
|
+
case 'reset':
|
|
25
|
+
return 'any';
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* @internal
|
|
30
|
+
*/ function createRecursiveUtilsProxy(context, key) {
|
|
31
|
+
return core.createRecursiveProxy((opts)=>{
|
|
32
|
+
const path = [
|
|
33
|
+
key,
|
|
34
|
+
...opts.path
|
|
35
|
+
];
|
|
36
|
+
const utilName = path.pop();
|
|
37
|
+
const args = [
|
|
38
|
+
...opts.args
|
|
39
|
+
];
|
|
40
|
+
const input = args.shift(); // args can now be spread when input removed
|
|
41
|
+
const queryType = getQueryType(utilName);
|
|
42
|
+
const queryKey = getQueryKey.getQueryKeyInternal(path, input, queryType);
|
|
43
|
+
const contextMap = {
|
|
44
|
+
fetch: ()=>context.fetchQuery(queryKey, ...args),
|
|
45
|
+
fetchInfinite: ()=>context.fetchInfiniteQuery(queryKey, args[0]),
|
|
46
|
+
prefetch: ()=>context.prefetchQuery(queryKey, ...args),
|
|
47
|
+
prefetchInfinite: ()=>context.prefetchInfiniteQuery(queryKey, args[0]),
|
|
48
|
+
ensureData: ()=>context.ensureQueryData(queryKey, ...args),
|
|
49
|
+
invalidate: ()=>context.invalidateQueries(queryKey, ...args),
|
|
50
|
+
reset: ()=>context.resetQueries(queryKey, ...args),
|
|
51
|
+
refetch: ()=>context.refetchQueries(queryKey, ...args),
|
|
52
|
+
cancel: ()=>context.cancelQuery(queryKey, ...args),
|
|
53
|
+
setData: ()=>{
|
|
54
|
+
context.setQueryData(queryKey, args[0], args[1]);
|
|
55
|
+
},
|
|
56
|
+
setInfiniteData: ()=>{
|
|
57
|
+
context.setInfiniteQueryData(queryKey, args[0], args[1]);
|
|
58
|
+
},
|
|
59
|
+
getData: ()=>context.getQueryData(queryKey),
|
|
60
|
+
getInfiniteData: ()=>context.getInfiniteQueryData(queryKey)
|
|
61
|
+
};
|
|
62
|
+
return contextMap[utilName]();
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* @internal
|
|
67
|
+
*/ function createReactQueryUtils(context$1) {
|
|
68
|
+
return core.createFlatProxy((key)=>{
|
|
69
|
+
const contextName = key;
|
|
70
|
+
if (contextName === 'client') {
|
|
71
|
+
return client.createTRPCClientProxy(context$1.client);
|
|
72
|
+
}
|
|
73
|
+
if (context.contextProps.includes(contextName)) {
|
|
74
|
+
return context$1[contextName];
|
|
75
|
+
}
|
|
76
|
+
return createRecursiveUtilsProxy(context$1, key);
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* @internal
|
|
81
|
+
*/ function createQueryUtilsProxy(context) {
|
|
82
|
+
return core.createFlatProxy((key)=>{
|
|
83
|
+
return createRecursiveUtilsProxy(context, key);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
exports.createQueryUtilsProxy = createQueryUtilsProxy;
|
|
88
|
+
exports.createReactQueryUtils = createReactQueryUtils;
|
|
89
|
+
exports.getQueryType = getQueryType;
|
|
@@ -1,55 +1,7 @@
|
|
|
1
1
|
import { createTRPCClientProxy } from '@trpc/client';
|
|
2
2
|
import { createFlatProxy, createRecursiveProxy } from '@trpc/core';
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* To allow easy interactions with groups of related queries, such as
|
|
7
|
-
* invalidating all queries of a router, we use an array as the path when
|
|
8
|
-
* storing in tanstack query.
|
|
9
|
-
**/ function getQueryKeyInternal(path, input, type) {
|
|
10
|
-
// Construct a query key that is easy to destructure and flexible for
|
|
11
|
-
// partial selecting etc.
|
|
12
|
-
// https://github.com/trpc/trpc/issues/3128
|
|
13
|
-
// some parts of the path may be dot-separated, split them up
|
|
14
|
-
const splitPath = path.flatMap((part)=>part.split('.'));
|
|
15
|
-
if (!input && (!type || type === 'any')) // for `utils.invalidate()` to match all queries (including vanilla react-query)
|
|
16
|
-
// we don't want nested array if path is empty, i.e. `[]` instead of `[[]]`
|
|
17
|
-
return splitPath.length ? [
|
|
18
|
-
splitPath
|
|
19
|
-
] : [];
|
|
20
|
-
return [
|
|
21
|
-
splitPath,
|
|
22
|
-
{
|
|
23
|
-
...typeof input !== 'undefined' && {
|
|
24
|
-
input: input
|
|
25
|
-
},
|
|
26
|
-
...type && type !== 'any' && {
|
|
27
|
-
type: type
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
];
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Method to extract the query key for a procedure
|
|
34
|
-
* @param procedureOrRouter - procedure or AnyRouter
|
|
35
|
-
* @param input - input to procedureOrRouter
|
|
36
|
-
* @param type - defaults to `any`
|
|
37
|
-
* @link https://trpc.io/docs/v11/getQueryKey
|
|
38
|
-
*/ function getQueryKey(..._params) {
|
|
39
|
-
const [procedureOrRouter, input, type] = _params;
|
|
40
|
-
// @ts-expect-error - we don't expose _def on the type layer
|
|
41
|
-
const path = procedureOrRouter._def().path;
|
|
42
|
-
const queryKey = getQueryKeyInternal(path, input, type ?? 'any');
|
|
43
|
-
return queryKey;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const contextProps = [
|
|
47
|
-
'client',
|
|
48
|
-
'ssrContext',
|
|
49
|
-
'ssrState',
|
|
50
|
-
'abortOnUnmount'
|
|
51
|
-
];
|
|
52
|
-
const TRPCContext = React.createContext?.(null);
|
|
3
|
+
import { contextProps } from '../../internals/context.mjs';
|
|
4
|
+
import { getQueryKeyInternal } from '../../internals/getQueryKey.mjs';
|
|
53
5
|
|
|
54
6
|
const getQueryType = (utilName)=>{
|
|
55
7
|
switch(utilName){
|
|
@@ -130,4 +82,4 @@ const getQueryType = (utilName)=>{
|
|
|
130
82
|
});
|
|
131
83
|
}
|
|
132
84
|
|
|
133
|
-
export {
|
|
85
|
+
export { createQueryUtilsProxy, createReactQueryUtils, getQueryType };
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var client = require('@trpc/client');
|
|
4
|
+
var getClientArgs = require('../internals/getClientArgs.js');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Creates a set of utility functions that can be used to interact with `react-query`
|
|
8
|
+
* @param opts the `TRPCClient` and `QueryClient` to use
|
|
9
|
+
* @returns a set of utility functions that can be used to interact with `react-query`
|
|
10
|
+
* @internal
|
|
11
|
+
*/ function createUtilityFunctions(opts) {
|
|
12
|
+
const { client: client$1 , queryClient } = opts;
|
|
13
|
+
const untypedClient = client$1 instanceof client.TRPCUntypedClient ? client$1 : client.getUntypedClient(client$1);
|
|
14
|
+
return {
|
|
15
|
+
fetchQuery: (queryKey, opts)=>{
|
|
16
|
+
return queryClient.fetchQuery({
|
|
17
|
+
...opts,
|
|
18
|
+
queryKey,
|
|
19
|
+
queryFn: ()=>untypedClient.query(...getClientArgs.getClientArgs(queryKey, opts))
|
|
20
|
+
});
|
|
21
|
+
},
|
|
22
|
+
fetchInfiniteQuery: (queryKey, opts)=>{
|
|
23
|
+
return queryClient.fetchInfiniteQuery({
|
|
24
|
+
...opts,
|
|
25
|
+
queryKey,
|
|
26
|
+
queryFn: ({ pageParam })=>{
|
|
27
|
+
return untypedClient.query(...getClientArgs.getClientArgs(queryKey, opts, pageParam));
|
|
28
|
+
},
|
|
29
|
+
initialPageParam: opts?.initialCursor ?? null
|
|
30
|
+
});
|
|
31
|
+
},
|
|
32
|
+
prefetchQuery: (queryKey, opts)=>{
|
|
33
|
+
return queryClient.prefetchQuery({
|
|
34
|
+
...opts,
|
|
35
|
+
queryKey,
|
|
36
|
+
queryFn: ()=>untypedClient.query(...getClientArgs.getClientArgs(queryKey, opts))
|
|
37
|
+
});
|
|
38
|
+
},
|
|
39
|
+
prefetchInfiniteQuery: (queryKey, opts)=>{
|
|
40
|
+
return queryClient.prefetchInfiniteQuery({
|
|
41
|
+
...opts,
|
|
42
|
+
queryKey,
|
|
43
|
+
queryFn: ({ pageParam })=>{
|
|
44
|
+
return untypedClient.query(...getClientArgs.getClientArgs(queryKey, opts, pageParam));
|
|
45
|
+
},
|
|
46
|
+
initialPageParam: opts?.initialCursor ?? null
|
|
47
|
+
});
|
|
48
|
+
},
|
|
49
|
+
ensureQueryData: (queryKey, opts)=>{
|
|
50
|
+
return queryClient.ensureQueryData({
|
|
51
|
+
...opts,
|
|
52
|
+
queryKey,
|
|
53
|
+
queryFn: ()=>untypedClient.query(...getClientArgs.getClientArgs(queryKey, opts))
|
|
54
|
+
});
|
|
55
|
+
},
|
|
56
|
+
invalidateQueries: (queryKey, filters, options)=>{
|
|
57
|
+
return queryClient.invalidateQueries({
|
|
58
|
+
...filters,
|
|
59
|
+
queryKey
|
|
60
|
+
}, options);
|
|
61
|
+
},
|
|
62
|
+
resetQueries: (queryKey, filters, options)=>{
|
|
63
|
+
return queryClient.resetQueries({
|
|
64
|
+
...filters,
|
|
65
|
+
queryKey
|
|
66
|
+
}, options);
|
|
67
|
+
},
|
|
68
|
+
refetchQueries: (queryKey, filters, options)=>{
|
|
69
|
+
return queryClient.refetchQueries({
|
|
70
|
+
...filters,
|
|
71
|
+
queryKey
|
|
72
|
+
}, options);
|
|
73
|
+
},
|
|
74
|
+
cancelQuery: (queryKey, options)=>{
|
|
75
|
+
return queryClient.cancelQueries({
|
|
76
|
+
queryKey
|
|
77
|
+
}, options);
|
|
78
|
+
},
|
|
79
|
+
setQueryData: (queryKey, updater, options)=>{
|
|
80
|
+
return queryClient.setQueryData(queryKey, updater, options);
|
|
81
|
+
},
|
|
82
|
+
getQueryData: (queryKey)=>{
|
|
83
|
+
return queryClient.getQueryData(queryKey);
|
|
84
|
+
},
|
|
85
|
+
setInfiniteQueryData: (queryKey, updater, options)=>{
|
|
86
|
+
return queryClient.setQueryData(queryKey, updater, options);
|
|
87
|
+
},
|
|
88
|
+
getInfiniteQueryData: (queryKey)=>{
|
|
89
|
+
return queryClient.getQueryData(queryKey);
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
exports.createUtilityFunctions = createUtilityFunctions;
|