@orpc/react 0.0.0-next.e9dc36e → 0.0.0-next.ee0aeaf

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 (65) hide show
  1. package/dist/index.js +16 -16
  2. package/dist/src/general-utils.d.ts +6 -6
  3. package/dist/src/procedure-hooks.d.ts +4 -4
  4. package/dist/src/procedure-utils.d.ts +12 -12
  5. package/dist/src/react-context.d.ts +2 -2
  6. package/dist/src/react-hooks.d.ts +2 -2
  7. package/dist/src/react-utils.d.ts +2 -2
  8. package/dist/src/types.d.ts +4 -3
  9. package/dist/src/use-queries/builder.d.ts +2 -2
  10. package/dist/src/use-queries/builders.d.ts +2 -2
  11. package/package.json +14 -17
  12. package/dist/index.js.map +0 -1
  13. package/dist/src/general-hooks.d.ts.map +0 -1
  14. package/dist/src/general-utils.d.ts.map +0 -1
  15. package/dist/src/index.d.ts.map +0 -1
  16. package/dist/src/orpc-path.d.ts.map +0 -1
  17. package/dist/src/procedure-hooks.d.ts.map +0 -1
  18. package/dist/src/procedure-utils.d.ts.map +0 -1
  19. package/dist/src/react-context.d.ts.map +0 -1
  20. package/dist/src/react-hooks.d.ts.map +0 -1
  21. package/dist/src/react-utils.d.ts.map +0 -1
  22. package/dist/src/react.d.ts.map +0 -1
  23. package/dist/src/tanstack-key.d.ts.map +0 -1
  24. package/dist/src/tanstack-query.d.ts.map +0 -1
  25. package/dist/src/types.d.ts.map +0 -1
  26. package/dist/src/use-queries/builder.d.ts.map +0 -1
  27. package/dist/src/use-queries/builders.d.ts.map +0 -1
  28. package/dist/src/use-queries/hook.d.ts.map +0 -1
  29. package/dist/tsconfig.tsbuildinfo +0 -1
  30. package/src/general-hooks.test-d.ts +0 -151
  31. package/src/general-hooks.test.tsx +0 -232
  32. package/src/general-hooks.ts +0 -101
  33. package/src/general-utils.test-d.ts +0 -454
  34. package/src/general-utils.test.tsx +0 -818
  35. package/src/general-utils.ts +0 -393
  36. package/src/index.ts +0 -8
  37. package/src/orpc-path.test-d.ts +0 -13
  38. package/src/orpc-path.test.ts +0 -12
  39. package/src/orpc-path.ts +0 -24
  40. package/src/procedure-hooks.test-d.ts +0 -321
  41. package/src/procedure-hooks.test.tsx +0 -388
  42. package/src/procedure-hooks.ts +0 -271
  43. package/src/procedure-utils.test-d.ts +0 -476
  44. package/src/procedure-utils.test.tsx +0 -330
  45. package/src/procedure-utils.ts +0 -312
  46. package/src/react-context.ts +0 -43
  47. package/src/react-hooks.ts +0 -94
  48. package/src/react-utils.ts +0 -98
  49. package/src/react.test-d.ts +0 -89
  50. package/src/react.test.tsx +0 -102
  51. package/src/react.tsx +0 -81
  52. package/src/tanstack-key.test-d.ts +0 -35
  53. package/src/tanstack-key.test.ts +0 -62
  54. package/src/tanstack-key.ts +0 -64
  55. package/src/tanstack-query.ts +0 -27
  56. package/src/types.ts +0 -7
  57. package/src/use-queries/builder.test-d.ts +0 -29
  58. package/src/use-queries/builder.test.ts +0 -25
  59. package/src/use-queries/builder.ts +0 -71
  60. package/src/use-queries/builders.test-d.ts +0 -30
  61. package/src/use-queries/builders.test.tsx +0 -29
  62. package/src/use-queries/builders.ts +0 -101
  63. package/src/use-queries/hook.test-d.ts +0 -64
  64. package/src/use-queries/hook.test.tsx +0 -89
  65. package/src/use-queries/hook.ts +0 -57
@@ -1,232 +0,0 @@
1
- import type { SchemaOutput } from '@orpc/contract'
2
- import { useMutation, useQuery } from '@tanstack/react-query'
3
- import { renderHook } from '@testing-library/react'
4
- import {
5
- ORPCContext,
6
- queryClient,
7
- type UserCreateInputSchema,
8
- type UserFindInputSchema,
9
- type UserSchema,
10
- wrapper,
11
- } from '../tests/orpc'
12
- import { createGeneralHooks } from './general-hooks'
13
-
14
- beforeEach(() => {
15
- queryClient.clear()
16
- })
17
-
18
- describe('useIsFetching', () => {
19
- const user_hooks = createGeneralHooks<undefined, undefined, unknown>({
20
- context: ORPCContext,
21
- path: ['user'],
22
- })
23
-
24
- const user_find_Hooks = createGeneralHooks<
25
- typeof UserFindInputSchema,
26
- typeof UserSchema,
27
- SchemaOutput<typeof UserSchema>
28
- >({
29
- context: ORPCContext,
30
- path: ['user', 'find'],
31
- })
32
-
33
- const user_create_Hooks = createGeneralHooks<
34
- typeof UserCreateInputSchema,
35
- typeof UserSchema,
36
- SchemaOutput<typeof UserSchema>
37
- >({
38
- context: ORPCContext,
39
- path: ['user', 'create'],
40
- })
41
-
42
- it('on success', async () => {
43
- const { result } = renderHook(() => user_hooks.useIsFetching(), { wrapper })
44
- const { result: result2 } = renderHook(
45
- () => user_find_Hooks.useIsFetching(),
46
- { wrapper },
47
- )
48
- const { result: result3 } = renderHook(
49
- () => user_find_Hooks.useIsFetching({ input: { id: '12333' } }),
50
- { wrapper },
51
- )
52
- const { result: result4 } = renderHook(
53
- () => user_find_Hooks.useIsFetching({ input: { id: 'never' } }),
54
- { wrapper },
55
- )
56
- const { result: result5 } = renderHook(
57
- () => user_create_Hooks.useIsFetching(),
58
- { wrapper },
59
- )
60
-
61
- expect(result.current).toBe(0)
62
- expect(result2.current).toBe(0)
63
- expect(result3.current).toBe(0)
64
- expect(result4.current).toBe(0)
65
- expect(result5.current).toBe(0)
66
-
67
- renderHook(() =>
68
- useQuery(
69
- {
70
- queryKey: [
71
- ['user', 'find'],
72
- { input: { id: '12333' }, type: 'query' },
73
- ],
74
- queryFn: async () => {
75
- await new Promise(resolve => setTimeout(resolve, 100))
76
- },
77
- },
78
- queryClient,
79
- ),
80
- )
81
- renderHook(() =>
82
- useQuery(
83
- {
84
- queryKey: [
85
- ['user', 'find'],
86
- { input: { id: '12333' }, type: 'infinite' },
87
- ],
88
- queryFn: async () => {
89
- await new Promise(resolve => setTimeout(resolve, 100))
90
- },
91
- },
92
- queryClient,
93
- ),
94
- )
95
-
96
- await new Promise(resolve => setTimeout(resolve, 50)) // < 100 make sure the query is not finished
97
-
98
- expect(result.current).toBe(2)
99
- expect(result2.current).toBe(2)
100
- expect(result3.current).toBe(2)
101
- expect(result4.current).toBe(0)
102
- expect(result5.current).toBe(0)
103
- })
104
- })
105
-
106
- describe('useIsMutating', () => {
107
- const user_hooks = createGeneralHooks<undefined, undefined, unknown>({
108
- context: ORPCContext,
109
- path: ['user'],
110
- })
111
-
112
- const user_find_Hooks = createGeneralHooks<
113
- typeof UserFindInputSchema,
114
- typeof UserSchema,
115
- SchemaOutput<typeof UserSchema>
116
- >({
117
- context: ORPCContext,
118
- path: ['user', 'find'],
119
- })
120
-
121
- const user_create_Hooks = createGeneralHooks<
122
- typeof UserCreateInputSchema,
123
- typeof UserSchema,
124
- SchemaOutput<typeof UserSchema>
125
- >({
126
- context: ORPCContext,
127
- path: ['user', 'create'],
128
- })
129
-
130
- it('on success', async () => {
131
- const { result } = renderHook(() => user_hooks.useIsMutating(), { wrapper })
132
- const { result: result2 } = renderHook(
133
- () => user_find_Hooks.useIsMutating(),
134
- { wrapper },
135
- )
136
- const { result: result3 } = renderHook(
137
- () => user_create_Hooks.useIsMutating(),
138
- { wrapper },
139
- )
140
-
141
- expect(result.current).toBe(0)
142
- expect(result2.current).toBe(0)
143
- expect(result3.current).toBe(0)
144
-
145
- const { result: mutation } = renderHook(() =>
146
- useMutation(
147
- {
148
- mutationKey: [['user', 'create']],
149
- mutationFn: async () => {
150
- await new Promise(resolve => setTimeout(resolve, 100))
151
- },
152
- },
153
- queryClient,
154
- ),
155
- )
156
-
157
- mutation.current.mutate()
158
-
159
- await new Promise(resolve => setTimeout(resolve, 50)) // < 100 make sure the query is not finished
160
-
161
- expect(result.current).toBe(1)
162
- expect(result2.current).toBe(0)
163
- expect(result3.current).toBe(1)
164
- })
165
- })
166
-
167
- describe('useMutationState', () => {
168
- const user_hooks = createGeneralHooks<undefined, undefined, unknown>({
169
- context: ORPCContext,
170
- path: ['user'],
171
- })
172
-
173
- const user_find_Hooks = createGeneralHooks<
174
- typeof UserFindInputSchema,
175
- typeof UserSchema,
176
- SchemaOutput<typeof UserSchema>
177
- >({
178
- context: ORPCContext,
179
- path: ['user', 'find'],
180
- })
181
-
182
- const user_create_Hooks = createGeneralHooks<
183
- typeof UserCreateInputSchema,
184
- typeof UserSchema,
185
- SchemaOutput<typeof UserSchema>
186
- >({
187
- context: ORPCContext,
188
- path: ['user', 'create'],
189
- })
190
-
191
- it('on success', async () => {
192
- const { result } = renderHook(() => user_hooks.useMutationState(), {
193
- wrapper,
194
- })
195
- const { result: result2 } = renderHook(
196
- () => user_find_Hooks.useMutationState(),
197
- { wrapper },
198
- )
199
- const { result: result3 } = renderHook(
200
- () => user_create_Hooks.useMutationState(),
201
- { wrapper },
202
- )
203
-
204
- expect(result.current.length).toBe(0)
205
- expect(result2.current.length).toBe(0)
206
- expect(result3.current.length).toBe(0)
207
-
208
- const { result: mutation } = renderHook(() =>
209
- useMutation(
210
- {
211
- mutationKey: [['user', 'create']],
212
- mutationFn: async () => {
213
- await new Promise(resolve => setTimeout(resolve, 100))
214
- },
215
- },
216
- queryClient,
217
- ),
218
- )
219
-
220
- mutation.current.mutate({ name: 'unnoq' } as any)
221
-
222
- await new Promise(resolve => setTimeout(resolve, 50)) // < 100 make sure the query is not finished
223
-
224
- expect(result.current.length).toBe(1)
225
- expect(result2.current.length).toBe(0)
226
- expect(result3.current.length).toBe(1)
227
-
228
- expect(result.current[0]).toEqual(result3.current[0])
229
-
230
- expect(result.current[0]?.variables).toEqual({ name: 'unnoq' })
231
- })
232
- })
@@ -1,101 +0,0 @@
1
- import type { Schema, SchemaInput, SchemaOutput } from '@orpc/contract'
2
- import type { PartialDeep, SetOptional } from '@orpc/shared'
3
- import type { ORPCQueryFilters } from './tanstack-query'
4
- import {
5
- type DefaultError,
6
- type Mutation,
7
- type MutationFilters,
8
- type MutationState,
9
- useIsFetching,
10
- useIsMutating,
11
- useMutationState,
12
- } from '@tanstack/react-query'
13
- import { type ORPCContext, useORPCContext } from './react-context'
14
- import { getMutationKeyFromPath, getQueryKeyFromPath } from './tanstack-key'
15
-
16
- export interface GeneralHooks<
17
- TInputSchema extends Schema,
18
- TOutputSchema extends Schema,
19
- TFuncOutput extends SchemaOutput<TOutputSchema>,
20
- > {
21
- useIsFetching: (
22
- filers?: ORPCQueryFilters<PartialDeep<SchemaInput<TInputSchema>>>,
23
- ) => number
24
- useIsMutating: (filters?: SetOptional<MutationFilters, 'mutationKey'>) => number
25
-
26
- useMutationState: <
27
- UResult = MutationState<
28
- SchemaOutput<TOutputSchema, TFuncOutput>,
29
- DefaultError,
30
- SchemaInput<TInputSchema>
31
- >,
32
- >(options?: {
33
- filters?: SetOptional<MutationFilters, 'mutationKey'>
34
- select?: (
35
- mutation: Mutation<
36
- SchemaOutput<TOutputSchema, TFuncOutput>,
37
- DefaultError,
38
- SchemaInput<TInputSchema>
39
- >,
40
- ) => UResult
41
- }
42
- ) => UResult[]
43
- }
44
-
45
- export interface CreateGeneralHooksOptions {
46
- context: ORPCContext<any>
47
-
48
- /**
49
- * The path of the router or procedure on server.
50
- *
51
- * @internal
52
- */
53
- path: string[]
54
- }
55
-
56
- export function createGeneralHooks<
57
- TInputSchema extends Schema = undefined,
58
- TOutputSchema extends Schema = undefined,
59
- TFuncOutput extends
60
- SchemaOutput<TOutputSchema> = SchemaOutput<TOutputSchema>,
61
- >(
62
- options: CreateGeneralHooksOptions,
63
- ): GeneralHooks<TInputSchema, TOutputSchema, TFuncOutput> {
64
- return {
65
- useIsFetching(filters) {
66
- const { queryType, input, ...rest } = filters ?? {}
67
- const context = useORPCContext(options.context)
68
- return useIsFetching(
69
- {
70
- queryKey: getQueryKeyFromPath(options.path, {
71
- input,
72
- type: queryType,
73
- }),
74
- ...rest,
75
- },
76
- context.queryClient,
77
- )
78
- },
79
- useIsMutating(filters) {
80
- const context = useORPCContext(options.context)
81
- return useIsMutating(
82
- { mutationKey: getMutationKeyFromPath(options.path), ...filters },
83
- context.queryClient,
84
- )
85
- },
86
-
87
- useMutationState(options_) {
88
- const context = useORPCContext(options.context)
89
- return useMutationState(
90
- {
91
- ...(options_ as any),
92
- filters: {
93
- mutationKey: getMutationKeyFromPath(options.path),
94
- ...options_?.filters,
95
- },
96
- },
97
- context.queryClient,
98
- )
99
- },
100
- }
101
- }