@orpc/react 0.0.0-next.b15d206 → 0.0.0-next.db1f26d

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 (57) hide show
  1. package/dist/src/react-hooks.d.ts +2 -2
  2. package/dist/src/react-utils.d.ts +2 -2
  3. package/package.json +12 -15
  4. package/dist/index.js.map +0 -1
  5. package/dist/src/general-hooks.d.ts.map +0 -1
  6. package/dist/src/general-utils.d.ts.map +0 -1
  7. package/dist/src/index.d.ts.map +0 -1
  8. package/dist/src/orpc-path.d.ts.map +0 -1
  9. package/dist/src/procedure-hooks.d.ts.map +0 -1
  10. package/dist/src/procedure-utils.d.ts.map +0 -1
  11. package/dist/src/react-context.d.ts.map +0 -1
  12. package/dist/src/react-hooks.d.ts.map +0 -1
  13. package/dist/src/react-utils.d.ts.map +0 -1
  14. package/dist/src/react.d.ts.map +0 -1
  15. package/dist/src/tanstack-key.d.ts.map +0 -1
  16. package/dist/src/tanstack-query.d.ts.map +0 -1
  17. package/dist/src/types.d.ts.map +0 -1
  18. package/dist/src/use-queries/builder.d.ts.map +0 -1
  19. package/dist/src/use-queries/builders.d.ts.map +0 -1
  20. package/dist/src/use-queries/hook.d.ts.map +0 -1
  21. package/dist/tsconfig.tsbuildinfo +0 -1
  22. package/src/general-hooks.test-d.ts +0 -151
  23. package/src/general-hooks.test.tsx +0 -232
  24. package/src/general-hooks.ts +0 -101
  25. package/src/general-utils.test-d.ts +0 -454
  26. package/src/general-utils.test.tsx +0 -818
  27. package/src/general-utils.ts +0 -393
  28. package/src/index.ts +0 -8
  29. package/src/orpc-path.test-d.ts +0 -13
  30. package/src/orpc-path.test.ts +0 -12
  31. package/src/orpc-path.ts +0 -24
  32. package/src/procedure-hooks.test-d.ts +0 -321
  33. package/src/procedure-hooks.test.tsx +0 -388
  34. package/src/procedure-hooks.ts +0 -271
  35. package/src/procedure-utils.test-d.ts +0 -476
  36. package/src/procedure-utils.test.tsx +0 -330
  37. package/src/procedure-utils.ts +0 -312
  38. package/src/react-context.ts +0 -43
  39. package/src/react-hooks.ts +0 -94
  40. package/src/react-utils.ts +0 -98
  41. package/src/react.test-d.ts +0 -89
  42. package/src/react.test.tsx +0 -102
  43. package/src/react.tsx +0 -81
  44. package/src/tanstack-key.test-d.ts +0 -35
  45. package/src/tanstack-key.test.ts +0 -62
  46. package/src/tanstack-key.ts +0 -64
  47. package/src/tanstack-query.ts +0 -27
  48. package/src/types.ts +0 -7
  49. package/src/use-queries/builder.test-d.ts +0 -29
  50. package/src/use-queries/builder.test.ts +0 -25
  51. package/src/use-queries/builder.ts +0 -71
  52. package/src/use-queries/builders.test-d.ts +0 -30
  53. package/src/use-queries/builders.test.tsx +0 -29
  54. package/src/use-queries/builders.ts +0 -101
  55. package/src/use-queries/hook.test-d.ts +0 -64
  56. package/src/use-queries/hook.test.tsx +0 -89
  57. package/src/use-queries/hook.ts +0 -57
@@ -1,151 +0,0 @@
1
- import type { SchemaInput, SchemaOutput } from '@orpc/contract'
2
- import type {
3
- DefaultError,
4
- Mutation,
5
- MutationState,
6
- } from '@tanstack/react-query'
7
- import {
8
- ORPCContext,
9
- type UserCreateInputSchema,
10
- type UserFindInputSchema,
11
- type UserSchema,
12
- } from '../tests/orpc'
13
- import { createGeneralHooks } from './general-hooks'
14
-
15
- describe('useIsFetching', () => {
16
- const hooks = createGeneralHooks<undefined, undefined, unknown>({
17
- context: ORPCContext,
18
- path: ['user'],
19
- })
20
-
21
- const procedureHooks = createGeneralHooks<
22
- typeof UserFindInputSchema,
23
- typeof UserSchema,
24
- SchemaOutput<typeof UserSchema>
25
- >({
26
- context: ORPCContext,
27
- path: ['user', 'find'],
28
- })
29
-
30
- it('simple', () => {
31
- const result = hooks.useIsFetching()
32
- expectTypeOf(result).toEqualTypeOf<number>()
33
- })
34
-
35
- it('with filters', () => {
36
- const result = procedureHooks.useIsFetching({
37
- input: {},
38
- })
39
-
40
- expectTypeOf(result).toEqualTypeOf<number>()
41
-
42
- procedureHooks.useIsFetching({
43
- input: {
44
- id: '1',
45
- },
46
- })
47
-
48
- procedureHooks.useIsFetching({
49
- input: {
50
- // @ts-expect-error invalid id
51
- id: 1,
52
- },
53
- })
54
- })
55
- })
56
-
57
- describe('useIsMutating', () => {
58
- const hooks = createGeneralHooks<undefined, undefined, unknown>({
59
- context: ORPCContext,
60
- path: ['user'],
61
- })
62
-
63
- const procedureHooks = createGeneralHooks<
64
- typeof UserCreateInputSchema,
65
- typeof UserSchema,
66
- SchemaOutput<typeof UserSchema>
67
- >({
68
- context: ORPCContext,
69
- path: ['user', 'create'],
70
- })
71
-
72
- it('simple', () => {
73
- const result = hooks.useIsMutating()
74
- expectTypeOf(result).toEqualTypeOf<number>()
75
- })
76
-
77
- it('with filters', () => {
78
- const result = procedureHooks.useIsMutating({
79
- // @ts-expect-error input is now allowed on mutation
80
- input: {},
81
- })
82
- })
83
- })
84
-
85
- describe('useMutationState', () => {
86
- const hooks = createGeneralHooks<undefined, undefined, unknown>({
87
- context: ORPCContext,
88
- path: ['user'],
89
- })
90
-
91
- const procedureHooks = createGeneralHooks<
92
- typeof UserCreateInputSchema,
93
- typeof UserSchema,
94
- SchemaOutput<typeof UserSchema>
95
- >({
96
- context: ORPCContext,
97
- path: ['user', 'create'],
98
- })
99
-
100
- it('simple', () => {
101
- const result = hooks.useMutationState()
102
- expectTypeOf(result).toEqualTypeOf<
103
- MutationState<unknown, DefaultError, unknown>[]
104
- >()
105
-
106
- const result2 = procedureHooks.useMutationState()
107
- expectTypeOf(result2).toEqualTypeOf<
108
- MutationState<
109
- SchemaOutput<typeof UserSchema>,
110
- DefaultError,
111
- SchemaInput<typeof UserCreateInputSchema>
112
- >[]
113
- >()
114
- })
115
-
116
- it('with filters', () => {
117
- const result = procedureHooks.useMutationState({
118
- filters: {
119
- // @ts-expect-error input is now allowed on mutation
120
- input: {},
121
- },
122
- })
123
- })
124
-
125
- it('with select', () => {
126
- const r1 = hooks.useMutationState({
127
- select: (data) => {
128
- expectTypeOf(data).toEqualTypeOf<
129
- Mutation<unknown, DefaultError, unknown>
130
- >()
131
- return 1
132
- },
133
- })
134
- expectTypeOf(r1).toEqualTypeOf<number[]>()
135
-
136
- const r2 = procedureHooks.useMutationState({
137
- select: (data) => {
138
- expectTypeOf(data).toEqualTypeOf<
139
- Mutation<
140
- SchemaOutput<typeof UserSchema>,
141
- DefaultError,
142
- SchemaInput<typeof UserCreateInputSchema>
143
- >
144
- >()
145
-
146
- return '1'
147
- },
148
- })
149
- expectTypeOf(r2).toEqualTypeOf<string[]>()
150
- })
151
- })
@@ -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
- }