@orpc/react 0.10.0 → 0.12.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/dist/index.js +0 -1
- package/dist/src/general-hooks.d.ts +0 -1
- package/dist/src/general-utils.d.ts +0 -1
- package/dist/src/index.d.ts +0 -1
- package/dist/src/orpc-path.d.ts +0 -1
- package/dist/src/procedure-hooks.d.ts +0 -1
- package/dist/src/procedure-utils.d.ts +0 -1
- package/dist/src/react-context.d.ts +0 -1
- package/dist/src/react-hooks.d.ts +0 -1
- package/dist/src/react-utils.d.ts +0 -1
- package/dist/src/react.d.ts +0 -1
- package/dist/src/tanstack-key.d.ts +0 -1
- package/dist/src/tanstack-query.d.ts +0 -1
- package/dist/src/types.d.ts +0 -1
- package/dist/src/use-queries/builder.d.ts +0 -1
- package/dist/src/use-queries/builders.d.ts +0 -1
- package/dist/src/use-queries/hook.d.ts +0 -1
- package/package.json +12 -16
- package/dist/index.js.map +0 -1
- package/dist/src/general-hooks.d.ts.map +0 -1
- package/dist/src/general-utils.d.ts.map +0 -1
- package/dist/src/index.d.ts.map +0 -1
- package/dist/src/orpc-path.d.ts.map +0 -1
- package/dist/src/procedure-hooks.d.ts.map +0 -1
- package/dist/src/procedure-utils.d.ts.map +0 -1
- package/dist/src/react-context.d.ts.map +0 -1
- package/dist/src/react-hooks.d.ts.map +0 -1
- package/dist/src/react-utils.d.ts.map +0 -1
- package/dist/src/react.d.ts.map +0 -1
- package/dist/src/tanstack-key.d.ts.map +0 -1
- package/dist/src/tanstack-query.d.ts.map +0 -1
- package/dist/src/types.d.ts.map +0 -1
- package/dist/src/use-queries/builder.d.ts.map +0 -1
- package/dist/src/use-queries/builders.d.ts.map +0 -1
- package/dist/src/use-queries/hook.d.ts.map +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/src/general-hooks.test-d.ts +0 -151
- package/src/general-hooks.test.tsx +0 -232
- package/src/general-hooks.ts +0 -101
- package/src/general-utils.test-d.ts +0 -454
- package/src/general-utils.test.tsx +0 -818
- package/src/general-utils.ts +0 -393
- package/src/index.ts +0 -8
- package/src/orpc-path.test-d.ts +0 -13
- package/src/orpc-path.test.ts +0 -12
- package/src/orpc-path.ts +0 -24
- package/src/procedure-hooks.test-d.ts +0 -321
- package/src/procedure-hooks.test.tsx +0 -388
- package/src/procedure-hooks.ts +0 -271
- package/src/procedure-utils.test-d.ts +0 -476
- package/src/procedure-utils.test.tsx +0 -330
- package/src/procedure-utils.ts +0 -312
- package/src/react-context.ts +0 -43
- package/src/react-hooks.ts +0 -94
- package/src/react-utils.ts +0 -98
- package/src/react.test-d.ts +0 -89
- package/src/react.test.tsx +0 -102
- package/src/react.tsx +0 -81
- package/src/tanstack-key.test-d.ts +0 -35
- package/src/tanstack-key.test.ts +0 -62
- package/src/tanstack-key.ts +0 -64
- package/src/tanstack-query.ts +0 -27
- package/src/types.ts +0 -7
- package/src/use-queries/builder.test-d.ts +0 -29
- package/src/use-queries/builder.test.ts +0 -25
- package/src/use-queries/builder.ts +0 -71
- package/src/use-queries/builders.test-d.ts +0 -30
- package/src/use-queries/builders.test.tsx +0 -29
- package/src/use-queries/builders.ts +0 -101
- package/src/use-queries/hook.test-d.ts +0 -64
- package/src/use-queries/hook.test.tsx +0 -89
- package/src/use-queries/hook.ts +0 -57
@@ -1,330 +0,0 @@
|
|
1
|
-
import { orpcClient, queryClient } from '../tests/orpc'
|
2
|
-
import { createProcedureUtils } from './procedure-utils'
|
3
|
-
|
4
|
-
beforeEach(() => {
|
5
|
-
queryClient.clear()
|
6
|
-
})
|
7
|
-
|
8
|
-
describe('fetchQuery', () => {
|
9
|
-
const utils = createProcedureUtils({
|
10
|
-
client: orpcClient.user.find,
|
11
|
-
path: ['user', 'find'],
|
12
|
-
queryClient,
|
13
|
-
})
|
14
|
-
|
15
|
-
it('on success', async () => {
|
16
|
-
const data = await utils.fetchQuery({ id: '123455' })
|
17
|
-
|
18
|
-
expect(data.id).toEqual('123455')
|
19
|
-
|
20
|
-
expect(
|
21
|
-
queryClient.getQueriesData({
|
22
|
-
exact: true,
|
23
|
-
queryKey: [
|
24
|
-
['user', 'find'],
|
25
|
-
{ input: { id: '123455' }, type: 'query' },
|
26
|
-
],
|
27
|
-
})[0]?.[1],
|
28
|
-
).toBe(data)
|
29
|
-
})
|
30
|
-
|
31
|
-
it('on error', () => {
|
32
|
-
// @ts-expect-error invalid input
|
33
|
-
expect(utils.fetchQuery({ id: {} })).rejects.toThrowError(
|
34
|
-
'Validation input failed',
|
35
|
-
)
|
36
|
-
})
|
37
|
-
})
|
38
|
-
|
39
|
-
describe('fetchInfiniteQuery', () => {
|
40
|
-
const utils = createProcedureUtils({
|
41
|
-
client: orpcClient.user.list,
|
42
|
-
path: ['user', 'list'],
|
43
|
-
queryClient,
|
44
|
-
})
|
45
|
-
|
46
|
-
it('on success', async () => {
|
47
|
-
const data = await utils.fetchInfiniteQuery({ input: {} })
|
48
|
-
|
49
|
-
expect(data).toMatchObject({
|
50
|
-
pageParams: [undefined],
|
51
|
-
pages: [
|
52
|
-
{
|
53
|
-
nextCursor: 2,
|
54
|
-
users: [{ name: 'number-0' }, { name: 'number-1' }],
|
55
|
-
},
|
56
|
-
],
|
57
|
-
})
|
58
|
-
|
59
|
-
expect(
|
60
|
-
queryClient.getQueriesData({
|
61
|
-
exact: true,
|
62
|
-
queryKey: [['user', 'list'], { input: {}, type: 'infinite' }],
|
63
|
-
})[0]?.[1],
|
64
|
-
).toBe(data)
|
65
|
-
})
|
66
|
-
|
67
|
-
it('on error', () => {
|
68
|
-
expect(
|
69
|
-
// @ts-expect-error invalid input
|
70
|
-
utils.fetchInfiniteQuery({ input: { keyword: {} } }),
|
71
|
-
).rejects.toThrowError('Validation input failed')
|
72
|
-
})
|
73
|
-
})
|
74
|
-
|
75
|
-
describe('prefetchQuery', () => {
|
76
|
-
const utils = createProcedureUtils({
|
77
|
-
client: orpcClient.user.find,
|
78
|
-
path: ['user', 'find'],
|
79
|
-
queryClient,
|
80
|
-
})
|
81
|
-
|
82
|
-
it('on success', async () => {
|
83
|
-
const result = await utils.prefetchQuery({ id: '123455' })
|
84
|
-
expect(result).toEqual(undefined)
|
85
|
-
|
86
|
-
expect(
|
87
|
-
queryClient.getQueriesData({
|
88
|
-
exact: true,
|
89
|
-
queryKey: [
|
90
|
-
['user', 'find'],
|
91
|
-
{ input: { id: '123455' }, type: 'query' },
|
92
|
-
],
|
93
|
-
})[0]?.[1],
|
94
|
-
).toEqual({
|
95
|
-
id: '123455',
|
96
|
-
name: 'name-123455',
|
97
|
-
})
|
98
|
-
})
|
99
|
-
|
100
|
-
it('on error', () => {
|
101
|
-
expect(
|
102
|
-
// @ts-expect-error invalid input
|
103
|
-
utils.prefetchQuery({ id: 1244 }),
|
104
|
-
).resolves.toEqual(undefined)
|
105
|
-
})
|
106
|
-
})
|
107
|
-
|
108
|
-
describe('prefetchInfiniteQuery', () => {
|
109
|
-
const utils = createProcedureUtils({
|
110
|
-
client: orpcClient.user.list,
|
111
|
-
path: ['user', 'list'],
|
112
|
-
queryClient,
|
113
|
-
})
|
114
|
-
|
115
|
-
it('on success', async () => {
|
116
|
-
const result = await utils.prefetchInfiniteQuery({ input: {} })
|
117
|
-
expect(result).toEqual(undefined)
|
118
|
-
|
119
|
-
expect(
|
120
|
-
queryClient.getQueriesData({
|
121
|
-
exact: true,
|
122
|
-
queryKey: [['user', 'list'], { input: {}, type: 'infinite' }],
|
123
|
-
})[0]?.[1],
|
124
|
-
).toMatchObject({
|
125
|
-
pageParams: [undefined],
|
126
|
-
pages: [
|
127
|
-
{
|
128
|
-
nextCursor: 2,
|
129
|
-
users: [{ name: 'number-0' }, { name: 'number-1' }],
|
130
|
-
},
|
131
|
-
],
|
132
|
-
})
|
133
|
-
})
|
134
|
-
|
135
|
-
it('on error', () => {
|
136
|
-
expect(
|
137
|
-
// @ts-expect-error invalid input
|
138
|
-
utils.prefetchInfiniteQuery({ keyword: 1222 }, {}),
|
139
|
-
).resolves.toEqual(undefined)
|
140
|
-
})
|
141
|
-
})
|
142
|
-
|
143
|
-
describe('ensureQueryData', () => {
|
144
|
-
const utils = createProcedureUtils({
|
145
|
-
client: orpcClient.user.find,
|
146
|
-
path: ['user', 'find'],
|
147
|
-
queryClient,
|
148
|
-
})
|
149
|
-
|
150
|
-
it('on success', async () => {
|
151
|
-
const data = await utils.ensureQueryData({ id: '123455' })
|
152
|
-
|
153
|
-
expect(data.id).toEqual('123455')
|
154
|
-
|
155
|
-
expect(
|
156
|
-
queryClient.getQueriesData({
|
157
|
-
exact: true,
|
158
|
-
queryKey: [
|
159
|
-
['user', 'find'],
|
160
|
-
{ input: { id: '123455' }, type: 'query' },
|
161
|
-
],
|
162
|
-
})[0]?.[1],
|
163
|
-
).toBe(data)
|
164
|
-
})
|
165
|
-
|
166
|
-
it('on error', () => {
|
167
|
-
// @ts-expect-error invalid input
|
168
|
-
expect(utils.ensureQueryData({ id: {} })).rejects.toThrowError(
|
169
|
-
'Validation input failed',
|
170
|
-
)
|
171
|
-
})
|
172
|
-
})
|
173
|
-
|
174
|
-
describe('ensureInfiniteQuery', () => {
|
175
|
-
const utils = createProcedureUtils({
|
176
|
-
client: orpcClient.user.list,
|
177
|
-
path: ['user', 'list'],
|
178
|
-
queryClient,
|
179
|
-
})
|
180
|
-
|
181
|
-
it('on success', async () => {
|
182
|
-
const data = await utils.ensureInfiniteQueryData({ input: {} })
|
183
|
-
|
184
|
-
expect(data).toMatchObject({
|
185
|
-
pageParams: [undefined],
|
186
|
-
pages: [
|
187
|
-
{
|
188
|
-
nextCursor: 2,
|
189
|
-
users: [{ name: 'number-0' }, { name: 'number-1' }],
|
190
|
-
},
|
191
|
-
],
|
192
|
-
})
|
193
|
-
|
194
|
-
expect(
|
195
|
-
queryClient.getQueriesData({
|
196
|
-
exact: true,
|
197
|
-
queryKey: [['user', 'list'], { input: {}, type: 'infinite' }],
|
198
|
-
})[0]?.[1],
|
199
|
-
).toBe(data)
|
200
|
-
})
|
201
|
-
|
202
|
-
it('on error', () => {
|
203
|
-
expect(
|
204
|
-
// @ts-expect-error invalid input
|
205
|
-
utils.ensureInfiniteQueryData({ input: { keyword: {} } }),
|
206
|
-
).rejects.toThrowError('Validation input failed')
|
207
|
-
})
|
208
|
-
})
|
209
|
-
|
210
|
-
describe('getQueryData', () => {
|
211
|
-
const utils = createProcedureUtils({
|
212
|
-
client: orpcClient.user.find,
|
213
|
-
path: ['user', 'find'],
|
214
|
-
queryClient,
|
215
|
-
})
|
216
|
-
|
217
|
-
it('on success', async () => {
|
218
|
-
expect(utils.getQueryData({ id: '123455' })).toEqual(undefined)
|
219
|
-
const data = await utils.ensureQueryData({ id: '123455' })
|
220
|
-
expect(utils.getQueryData({ id: '123455' })).toBe(data)
|
221
|
-
})
|
222
|
-
})
|
223
|
-
|
224
|
-
describe('getInfiniteQueryData', () => {
|
225
|
-
const utils = createProcedureUtils({
|
226
|
-
client: orpcClient.user.list,
|
227
|
-
path: ['user', 'list'],
|
228
|
-
queryClient,
|
229
|
-
})
|
230
|
-
|
231
|
-
it('on success', async () => {
|
232
|
-
expect(utils.getInfiniteQueryData({})).toEqual(undefined)
|
233
|
-
const data = await utils.ensureInfiniteQueryData({ input: {} })
|
234
|
-
expect(utils.getInfiniteQueryData({})).toBe(data)
|
235
|
-
})
|
236
|
-
})
|
237
|
-
|
238
|
-
describe('getQueryState', () => {
|
239
|
-
const utils = createProcedureUtils({
|
240
|
-
client: orpcClient.user.find,
|
241
|
-
path: ['user', 'find'],
|
242
|
-
queryClient,
|
243
|
-
})
|
244
|
-
|
245
|
-
it('on success', async () => {
|
246
|
-
expect(utils.getQueryState({ id: '123455' })).toEqual(undefined)
|
247
|
-
const data = await utils.ensureQueryData({ id: '123455' })
|
248
|
-
expect(utils.getQueryState({ id: '123455' })).toMatchObject({
|
249
|
-
status: 'success',
|
250
|
-
data,
|
251
|
-
})
|
252
|
-
})
|
253
|
-
})
|
254
|
-
|
255
|
-
describe('getInfiniteQueryState', () => {
|
256
|
-
const utils = createProcedureUtils({
|
257
|
-
client: orpcClient.user.list,
|
258
|
-
path: ['user', 'list'],
|
259
|
-
queryClient,
|
260
|
-
})
|
261
|
-
|
262
|
-
it('on success', async () => {
|
263
|
-
expect(utils.getInfiniteQueryState({})).toEqual(undefined)
|
264
|
-
const data = await utils.ensureInfiniteQueryData({ input: {} })
|
265
|
-
expect(utils.getInfiniteQueryState({})).toMatchObject({
|
266
|
-
status: 'success',
|
267
|
-
data,
|
268
|
-
})
|
269
|
-
})
|
270
|
-
})
|
271
|
-
|
272
|
-
describe('setQueryData', () => {
|
273
|
-
const utils = createProcedureUtils({
|
274
|
-
client: orpcClient.user.find,
|
275
|
-
path: ['user', 'find'],
|
276
|
-
queryClient,
|
277
|
-
})
|
278
|
-
|
279
|
-
it('on success', async () => {
|
280
|
-
const original = await utils.ensureQueryData({ id: '1222' })
|
281
|
-
|
282
|
-
const user = {
|
283
|
-
id: '1222',
|
284
|
-
name: 'name-1222-fake-fake',
|
285
|
-
}
|
286
|
-
|
287
|
-
utils.setQueryData({ id: user.id }, (data) => {
|
288
|
-
expect(data).toBe(original)
|
289
|
-
|
290
|
-
return user
|
291
|
-
})
|
292
|
-
|
293
|
-
expect(
|
294
|
-
queryClient.getQueriesData({
|
295
|
-
exact: true,
|
296
|
-
queryKey: [['user', 'find'], { input: { id: user.id }, type: 'query' }],
|
297
|
-
})[0]?.[1],
|
298
|
-
).toEqual(user)
|
299
|
-
})
|
300
|
-
})
|
301
|
-
|
302
|
-
describe('getInfiniteQueryData 2', () => {
|
303
|
-
const utils = createProcedureUtils({
|
304
|
-
client: orpcClient.user.list,
|
305
|
-
path: ['user', 'list'],
|
306
|
-
queryClient,
|
307
|
-
})
|
308
|
-
|
309
|
-
it('on success', async () => {
|
310
|
-
const original = await utils.ensureInfiniteQueryData({ input: {} })
|
311
|
-
|
312
|
-
const data = {
|
313
|
-
pages: [],
|
314
|
-
pageParams: [],
|
315
|
-
}
|
316
|
-
|
317
|
-
utils.setInfiniteQueryData({}, (ori) => {
|
318
|
-
expect(ori).toBe(original)
|
319
|
-
|
320
|
-
return data
|
321
|
-
})
|
322
|
-
|
323
|
-
expect(
|
324
|
-
queryClient.getQueriesData({
|
325
|
-
exact: true,
|
326
|
-
queryKey: [['user', 'list'], { input: {}, type: 'infinite' }],
|
327
|
-
})[0]?.[1],
|
328
|
-
).toEqual(data)
|
329
|
-
})
|
330
|
-
})
|
package/src/procedure-utils.ts
DELETED
@@ -1,312 +0,0 @@
|
|
1
|
-
import type { ProcedureClient } from '@orpc/client'
|
2
|
-
import type { Schema, SchemaInput, SchemaOutput } from '@orpc/contract'
|
3
|
-
import type { PartialOnUndefinedDeep, SetOptional } from '@orpc/shared'
|
4
|
-
import type {
|
5
|
-
DefaultError,
|
6
|
-
EnsureInfiniteQueryDataOptions,
|
7
|
-
EnsureQueryDataOptions,
|
8
|
-
FetchInfiniteQueryOptions,
|
9
|
-
FetchQueryOptions,
|
10
|
-
InfiniteData,
|
11
|
-
QueryClient,
|
12
|
-
QueryKey,
|
13
|
-
QueryState,
|
14
|
-
SetDataOptions,
|
15
|
-
Updater,
|
16
|
-
} from '@tanstack/react-query'
|
17
|
-
import type { SchemaInputForInfiniteQuery } from './types'
|
18
|
-
import { getQueryKeyFromPath } from './tanstack-key'
|
19
|
-
|
20
|
-
export interface ProcedureUtils<
|
21
|
-
TInputSchema extends Schema,
|
22
|
-
TOutputSchema extends Schema,
|
23
|
-
TFuncOutput extends SchemaOutput<TOutputSchema>,
|
24
|
-
> {
|
25
|
-
fetchQuery: (
|
26
|
-
input: SchemaInput<TInputSchema>,
|
27
|
-
options?: SetOptional<
|
28
|
-
FetchQueryOptions<SchemaOutput<TOutputSchema, TFuncOutput>>,
|
29
|
-
'queryKey' | 'queryFn'
|
30
|
-
>,
|
31
|
-
) => Promise<SchemaOutput<TOutputSchema, TFuncOutput>>
|
32
|
-
fetchInfiniteQuery: (
|
33
|
-
options: PartialOnUndefinedDeep<
|
34
|
-
SetOptional<
|
35
|
-
FetchInfiniteQueryOptions<
|
36
|
-
SchemaOutput<TOutputSchema, TFuncOutput>,
|
37
|
-
DefaultError,
|
38
|
-
SchemaOutput<TOutputSchema, TFuncOutput>,
|
39
|
-
QueryKey,
|
40
|
-
SchemaInput<TInputSchema>['cursor']
|
41
|
-
>,
|
42
|
-
'queryKey' | 'queryFn'
|
43
|
-
> & {
|
44
|
-
input: SchemaInputForInfiniteQuery<TInputSchema>
|
45
|
-
}
|
46
|
-
>,
|
47
|
-
) => Promise<
|
48
|
-
InfiniteData<
|
49
|
-
SchemaOutput<TOutputSchema, TFuncOutput>,
|
50
|
-
SchemaInput<TInputSchema>['cursor']
|
51
|
-
>
|
52
|
-
>
|
53
|
-
|
54
|
-
prefetchQuery: (
|
55
|
-
input: SchemaInput<TInputSchema>,
|
56
|
-
options?: SetOptional<
|
57
|
-
FetchQueryOptions<SchemaOutput<TOutputSchema, TFuncOutput>>,
|
58
|
-
'queryKey' | 'queryFn'
|
59
|
-
>,
|
60
|
-
) => Promise<void>
|
61
|
-
prefetchInfiniteQuery: (
|
62
|
-
options: PartialOnUndefinedDeep<
|
63
|
-
SetOptional<
|
64
|
-
FetchInfiniteQueryOptions<
|
65
|
-
SchemaOutput<TOutputSchema, TFuncOutput>,
|
66
|
-
DefaultError,
|
67
|
-
SchemaOutput<TOutputSchema, TFuncOutput>,
|
68
|
-
QueryKey,
|
69
|
-
SchemaInput<TInputSchema>['cursor']
|
70
|
-
>,
|
71
|
-
'queryKey' | 'queryFn'
|
72
|
-
> & {
|
73
|
-
input: SchemaInputForInfiniteQuery<TInputSchema>
|
74
|
-
}
|
75
|
-
>,
|
76
|
-
) => Promise<void>
|
77
|
-
|
78
|
-
getQueryData: (
|
79
|
-
input: SchemaInput<TInputSchema>,
|
80
|
-
) => SchemaOutput<TOutputSchema, TFuncOutput> | undefined
|
81
|
-
getInfiniteQueryData: (
|
82
|
-
input: SchemaInputForInfiniteQuery<TInputSchema>,
|
83
|
-
) => | InfiniteData<
|
84
|
-
SchemaOutput<TOutputSchema, TFuncOutput>,
|
85
|
-
SchemaInput<TInputSchema>['cursor']
|
86
|
-
>
|
87
|
-
| undefined
|
88
|
-
|
89
|
-
ensureQueryData: (
|
90
|
-
input: SchemaInput<TInputSchema>,
|
91
|
-
options?: SetOptional<
|
92
|
-
EnsureQueryDataOptions<SchemaOutput<TOutputSchema, TFuncOutput>>,
|
93
|
-
'queryFn' | 'queryKey'
|
94
|
-
>,
|
95
|
-
) => Promise<SchemaOutput<TOutputSchema, TFuncOutput>>
|
96
|
-
ensureInfiniteQueryData: (
|
97
|
-
options: PartialOnUndefinedDeep<
|
98
|
-
SetOptional<
|
99
|
-
EnsureInfiniteQueryDataOptions<
|
100
|
-
SchemaOutput<TOutputSchema, TFuncOutput>,
|
101
|
-
DefaultError,
|
102
|
-
SchemaOutput<TOutputSchema, TFuncOutput>,
|
103
|
-
QueryKey,
|
104
|
-
SchemaInput<TInputSchema>['cursor']
|
105
|
-
>,
|
106
|
-
'queryKey' | 'queryFn'
|
107
|
-
> & {
|
108
|
-
input: SchemaInputForInfiniteQuery<TInputSchema>
|
109
|
-
}
|
110
|
-
>,
|
111
|
-
) => Promise<
|
112
|
-
InfiniteData<
|
113
|
-
SchemaOutput<TOutputSchema, TFuncOutput>,
|
114
|
-
SchemaInput<TInputSchema>['cursor']
|
115
|
-
>
|
116
|
-
>
|
117
|
-
|
118
|
-
getQueryState: (
|
119
|
-
input: SchemaInput<TInputSchema>,
|
120
|
-
) => QueryState<SchemaOutput<TOutputSchema, TFuncOutput>> | undefined
|
121
|
-
getInfiniteQueryState: (
|
122
|
-
input: SchemaInputForInfiniteQuery<TInputSchema>,
|
123
|
-
) => | QueryState<
|
124
|
-
InfiniteData<
|
125
|
-
SchemaOutput<TOutputSchema, TFuncOutput>,
|
126
|
-
SchemaInput<TInputSchema>['cursor']
|
127
|
-
>
|
128
|
-
>
|
129
|
-
| undefined
|
130
|
-
|
131
|
-
setQueryData: (
|
132
|
-
input: SchemaInput<TInputSchema>,
|
133
|
-
updater: Updater<
|
134
|
-
SchemaOutput<TOutputSchema, TFuncOutput> | undefined,
|
135
|
-
SchemaOutput<TOutputSchema, TFuncOutput> | undefined
|
136
|
-
>,
|
137
|
-
options?: SetDataOptions,
|
138
|
-
) => SchemaOutput<TOutputSchema, TFuncOutput> | undefined
|
139
|
-
setInfiniteQueryData: (
|
140
|
-
input: SchemaInputForInfiniteQuery<TInputSchema>,
|
141
|
-
updater: Updater<
|
142
|
-
| InfiniteData<
|
143
|
-
SchemaOutput<TOutputSchema, TFuncOutput>,
|
144
|
-
SchemaInput<TInputSchema>['cursor']
|
145
|
-
>
|
146
|
-
| undefined,
|
147
|
-
| InfiniteData<
|
148
|
-
SchemaOutput<TOutputSchema, TFuncOutput>,
|
149
|
-
SchemaInput<TInputSchema>['cursor']
|
150
|
-
>
|
151
|
-
| undefined
|
152
|
-
>,
|
153
|
-
options?: SetDataOptions,
|
154
|
-
) => | InfiniteData<
|
155
|
-
SchemaOutput<TOutputSchema, TFuncOutput>,
|
156
|
-
SchemaInput<TInputSchema>['cursor']
|
157
|
-
>
|
158
|
-
| undefined
|
159
|
-
}
|
160
|
-
|
161
|
-
export interface CreateProcedureUtilsOptions<
|
162
|
-
TInputSchema extends Schema = undefined,
|
163
|
-
TOutputSchema extends Schema = undefined,
|
164
|
-
TFuncOutput extends
|
165
|
-
SchemaOutput<TOutputSchema> = SchemaOutput<TOutputSchema>,
|
166
|
-
> {
|
167
|
-
client: ProcedureClient<TInputSchema, TOutputSchema, TFuncOutput>
|
168
|
-
queryClient: QueryClient
|
169
|
-
|
170
|
-
/**
|
171
|
-
* The path of procedure on sever
|
172
|
-
*/
|
173
|
-
path: string[]
|
174
|
-
}
|
175
|
-
|
176
|
-
export function createProcedureUtils<
|
177
|
-
TInputSchema extends Schema,
|
178
|
-
TOutputSchema extends Schema,
|
179
|
-
TFuncOutput extends SchemaOutput<TOutputSchema>,
|
180
|
-
>(
|
181
|
-
options: CreateProcedureUtilsOptions<
|
182
|
-
TInputSchema,
|
183
|
-
TOutputSchema,
|
184
|
-
TFuncOutput
|
185
|
-
>,
|
186
|
-
): ProcedureUtils<TInputSchema, TOutputSchema, TFuncOutput> {
|
187
|
-
return {
|
188
|
-
fetchQuery(input, options_) {
|
189
|
-
return options.queryClient.fetchQuery({
|
190
|
-
queryKey: getQueryKeyFromPath(options.path, { input, type: 'query' }),
|
191
|
-
queryFn: () => options.client(input),
|
192
|
-
...options_,
|
193
|
-
})
|
194
|
-
},
|
195
|
-
fetchInfiniteQuery(options_) {
|
196
|
-
const { input, ...rest } = options_
|
197
|
-
return options.queryClient.fetchInfiniteQuery({
|
198
|
-
queryKey: getQueryKeyFromPath(options.path, {
|
199
|
-
input,
|
200
|
-
type: 'infinite',
|
201
|
-
}),
|
202
|
-
queryFn: ({ pageParam }) => {
|
203
|
-
return options.client({ ...(input as any), pageParam } as any)
|
204
|
-
},
|
205
|
-
...(rest as any),
|
206
|
-
})
|
207
|
-
},
|
208
|
-
|
209
|
-
prefetchQuery(input, options_) {
|
210
|
-
return options.queryClient.prefetchQuery({
|
211
|
-
queryKey: getQueryKeyFromPath(options.path, {
|
212
|
-
input,
|
213
|
-
type: 'query',
|
214
|
-
}),
|
215
|
-
queryFn: () => options.client(input),
|
216
|
-
...options_,
|
217
|
-
})
|
218
|
-
},
|
219
|
-
prefetchInfiniteQuery(options_) {
|
220
|
-
const { input, ...rest } = options_
|
221
|
-
return options.queryClient.prefetchInfiniteQuery({
|
222
|
-
queryKey: getQueryKeyFromPath(options.path, {
|
223
|
-
input,
|
224
|
-
type: 'infinite',
|
225
|
-
}),
|
226
|
-
queryFn: ({ pageParam }) => {
|
227
|
-
return options.client({ ...(input as any), cursor: pageParam } as any)
|
228
|
-
},
|
229
|
-
...(rest as any),
|
230
|
-
})
|
231
|
-
},
|
232
|
-
|
233
|
-
getQueryData(input) {
|
234
|
-
return options.queryClient.getQueryData(
|
235
|
-
getQueryKeyFromPath(options.path, {
|
236
|
-
input,
|
237
|
-
type: 'query',
|
238
|
-
}),
|
239
|
-
)
|
240
|
-
},
|
241
|
-
getInfiniteQueryData(input) {
|
242
|
-
return options.queryClient.getQueryData(
|
243
|
-
getQueryKeyFromPath(options.path, {
|
244
|
-
input,
|
245
|
-
type: 'infinite',
|
246
|
-
}),
|
247
|
-
)
|
248
|
-
},
|
249
|
-
|
250
|
-
ensureQueryData(input, options_) {
|
251
|
-
return options.queryClient.ensureQueryData({
|
252
|
-
queryKey: getQueryKeyFromPath(options.path, {
|
253
|
-
input,
|
254
|
-
type: 'query',
|
255
|
-
}),
|
256
|
-
queryFn: () => options.client(input),
|
257
|
-
...options_,
|
258
|
-
})
|
259
|
-
},
|
260
|
-
ensureInfiniteQueryData(options_) {
|
261
|
-
const { input, ...rest } = options_
|
262
|
-
return options.queryClient.ensureInfiniteQueryData({
|
263
|
-
queryKey: getQueryKeyFromPath(options.path, {
|
264
|
-
input,
|
265
|
-
type: 'infinite',
|
266
|
-
}),
|
267
|
-
queryFn: ({ pageParam }) => {
|
268
|
-
return options.client({ ...(input as any), pageParam } as any)
|
269
|
-
},
|
270
|
-
...(rest as any),
|
271
|
-
})
|
272
|
-
},
|
273
|
-
|
274
|
-
getQueryState(input) {
|
275
|
-
return options.queryClient.getQueryState(
|
276
|
-
getQueryKeyFromPath(options.path, {
|
277
|
-
input,
|
278
|
-
type: 'query',
|
279
|
-
}),
|
280
|
-
)
|
281
|
-
},
|
282
|
-
getInfiniteQueryState(input) {
|
283
|
-
return options.queryClient.getQueryState(
|
284
|
-
getQueryKeyFromPath(options.path, {
|
285
|
-
input,
|
286
|
-
type: 'infinite',
|
287
|
-
}),
|
288
|
-
)
|
289
|
-
},
|
290
|
-
|
291
|
-
setQueryData(input, updater, options_) {
|
292
|
-
return options.queryClient.setQueryData(
|
293
|
-
getQueryKeyFromPath(options.path, {
|
294
|
-
input,
|
295
|
-
type: 'query',
|
296
|
-
}),
|
297
|
-
updater,
|
298
|
-
options_,
|
299
|
-
)
|
300
|
-
},
|
301
|
-
setInfiniteQueryData(input, updater, options_) {
|
302
|
-
return options.queryClient.setQueryData(
|
303
|
-
getQueryKeyFromPath(options.path, {
|
304
|
-
input,
|
305
|
-
type: 'infinite',
|
306
|
-
}),
|
307
|
-
updater,
|
308
|
-
options_,
|
309
|
-
)
|
310
|
-
},
|
311
|
-
}
|
312
|
-
}
|
package/src/react-context.ts
DELETED
@@ -1,43 +0,0 @@
|
|
1
|
-
import type {
|
2
|
-
RouterClientWithContractRouter,
|
3
|
-
RouterClientWithRouter,
|
4
|
-
} from '@orpc/client'
|
5
|
-
import type { ContractRouter } from '@orpc/contract'
|
6
|
-
import type { Router } from '@orpc/server'
|
7
|
-
import type { QueryClient } from '@tanstack/react-query'
|
8
|
-
import { type Context, createContext, useContext } from 'react'
|
9
|
-
|
10
|
-
export interface ORPCContextValue<
|
11
|
-
TRouter extends ContractRouter | Router<any>,
|
12
|
-
> {
|
13
|
-
client: TRouter extends ContractRouter
|
14
|
-
? RouterClientWithContractRouter<TRouter>
|
15
|
-
: TRouter extends Router<any>
|
16
|
-
? RouterClientWithRouter<TRouter>
|
17
|
-
: never
|
18
|
-
queryClient: QueryClient
|
19
|
-
}
|
20
|
-
|
21
|
-
export type ORPCContext<TRouter extends ContractRouter | Router<any>> = Context<
|
22
|
-
ORPCContextValue<TRouter> | undefined
|
23
|
-
>
|
24
|
-
|
25
|
-
export function createORPCContext<
|
26
|
-
TRouter extends ContractRouter | Router<any>,
|
27
|
-
>(): ORPCContext<TRouter> {
|
28
|
-
return createContext(undefined as any)
|
29
|
-
}
|
30
|
-
|
31
|
-
export function useORPCContext<TRouter extends ContractRouter | Router<any>>(
|
32
|
-
context: ORPCContext<TRouter>,
|
33
|
-
): ORPCContextValue<TRouter> {
|
34
|
-
const value = useContext(context)
|
35
|
-
|
36
|
-
if (!value) {
|
37
|
-
throw new Error(
|
38
|
-
'useORPCContext must be used within a <ORPCContext.Provider>, please see the docs',
|
39
|
-
)
|
40
|
-
}
|
41
|
-
|
42
|
-
return value
|
43
|
-
}
|