@orpc/react 0.10.0 → 0.11.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.
Files changed (72) hide show
  1. package/dist/index.js +0 -1
  2. package/dist/src/general-hooks.d.ts +0 -1
  3. package/dist/src/general-utils.d.ts +0 -1
  4. package/dist/src/index.d.ts +0 -1
  5. package/dist/src/orpc-path.d.ts +0 -1
  6. package/dist/src/procedure-hooks.d.ts +0 -1
  7. package/dist/src/procedure-utils.d.ts +0 -1
  8. package/dist/src/react-context.d.ts +0 -1
  9. package/dist/src/react-hooks.d.ts +0 -1
  10. package/dist/src/react-utils.d.ts +0 -1
  11. package/dist/src/react.d.ts +0 -1
  12. package/dist/src/tanstack-key.d.ts +0 -1
  13. package/dist/src/tanstack-query.d.ts +0 -1
  14. package/dist/src/types.d.ts +0 -1
  15. package/dist/src/use-queries/builder.d.ts +0 -1
  16. package/dist/src/use-queries/builders.d.ts +0 -1
  17. package/dist/src/use-queries/hook.d.ts +0 -1
  18. package/package.json +12 -16
  19. package/dist/index.js.map +0 -1
  20. package/dist/src/general-hooks.d.ts.map +0 -1
  21. package/dist/src/general-utils.d.ts.map +0 -1
  22. package/dist/src/index.d.ts.map +0 -1
  23. package/dist/src/orpc-path.d.ts.map +0 -1
  24. package/dist/src/procedure-hooks.d.ts.map +0 -1
  25. package/dist/src/procedure-utils.d.ts.map +0 -1
  26. package/dist/src/react-context.d.ts.map +0 -1
  27. package/dist/src/react-hooks.d.ts.map +0 -1
  28. package/dist/src/react-utils.d.ts.map +0 -1
  29. package/dist/src/react.d.ts.map +0 -1
  30. package/dist/src/tanstack-key.d.ts.map +0 -1
  31. package/dist/src/tanstack-query.d.ts.map +0 -1
  32. package/dist/src/types.d.ts.map +0 -1
  33. package/dist/src/use-queries/builder.d.ts.map +0 -1
  34. package/dist/src/use-queries/builders.d.ts.map +0 -1
  35. package/dist/src/use-queries/hook.d.ts.map +0 -1
  36. package/dist/tsconfig.tsbuildinfo +0 -1
  37. package/src/general-hooks.test-d.ts +0 -151
  38. package/src/general-hooks.test.tsx +0 -232
  39. package/src/general-hooks.ts +0 -101
  40. package/src/general-utils.test-d.ts +0 -454
  41. package/src/general-utils.test.tsx +0 -818
  42. package/src/general-utils.ts +0 -393
  43. package/src/index.ts +0 -8
  44. package/src/orpc-path.test-d.ts +0 -13
  45. package/src/orpc-path.test.ts +0 -12
  46. package/src/orpc-path.ts +0 -24
  47. package/src/procedure-hooks.test-d.ts +0 -321
  48. package/src/procedure-hooks.test.tsx +0 -388
  49. package/src/procedure-hooks.ts +0 -271
  50. package/src/procedure-utils.test-d.ts +0 -476
  51. package/src/procedure-utils.test.tsx +0 -330
  52. package/src/procedure-utils.ts +0 -312
  53. package/src/react-context.ts +0 -43
  54. package/src/react-hooks.ts +0 -94
  55. package/src/react-utils.ts +0 -98
  56. package/src/react.test-d.ts +0 -89
  57. package/src/react.test.tsx +0 -102
  58. package/src/react.tsx +0 -81
  59. package/src/tanstack-key.test-d.ts +0 -35
  60. package/src/tanstack-key.test.ts +0 -62
  61. package/src/tanstack-key.ts +0 -64
  62. package/src/tanstack-query.ts +0 -27
  63. package/src/types.ts +0 -7
  64. package/src/use-queries/builder.test-d.ts +0 -29
  65. package/src/use-queries/builder.test.ts +0 -25
  66. package/src/use-queries/builder.ts +0 -71
  67. package/src/use-queries/builders.test-d.ts +0 -30
  68. package/src/use-queries/builders.test.tsx +0 -29
  69. package/src/use-queries/builders.ts +0 -101
  70. package/src/use-queries/hook.test-d.ts +0 -64
  71. package/src/use-queries/hook.test.tsx +0 -89
  72. package/src/use-queries/hook.ts +0 -57
@@ -1,94 +0,0 @@
1
- import type {
2
- ContractProcedure,
3
- ContractRouter,
4
- SchemaOutput,
5
- } from '@orpc/contract'
6
- import type { Procedure, Router } from '@orpc/server'
7
- import type { ORPCContext } from './react-context'
8
- import { createGeneralHooks, type GeneralHooks } from './general-hooks'
9
- import { orpcPathSymbol } from './orpc-path'
10
- import { createProcedureHooks, type ProcedureHooks } from './procedure-hooks'
11
-
12
- export type ORPCHooksWithContractRouter<TRouter extends ContractRouter> = {
13
- [K in keyof TRouter]: TRouter[K] extends ContractProcedure<
14
- infer UInputSchema,
15
- infer UOutputSchema
16
- >
17
- ? ProcedureHooks<UInputSchema, UOutputSchema, SchemaOutput<UOutputSchema>> & GeneralHooks<UInputSchema, UOutputSchema, SchemaOutput<UOutputSchema>>
18
- : TRouter[K] extends ContractRouter
19
- ? ORPCHooksWithContractRouter<TRouter[K]>
20
- : never
21
- } & GeneralHooks<undefined, undefined, unknown>
22
-
23
- export type ORPCHooksWithRouter<TRouter extends Router<any>> = {
24
- [K in keyof TRouter]: TRouter[K] extends Procedure<any, any, infer UInputSchema, infer UOutputSchema, infer UFuncOutput>
25
- ? ProcedureHooks<UInputSchema, UOutputSchema, UFuncOutput> & GeneralHooks<UInputSchema, UOutputSchema, UFuncOutput>
26
- : TRouter[K] extends Router<any>
27
- ? ORPCHooksWithRouter<TRouter[K]>
28
- : never
29
- } & GeneralHooks<undefined, undefined, unknown>
30
-
31
- export interface CreateORPCHooksOptions<
32
- TRouter extends ContractRouter | Router<any>,
33
- > {
34
- context: ORPCContext<TRouter>
35
-
36
- /**
37
- * The path of the router.
38
- *
39
- * @internal
40
- */
41
- path?: string[]
42
- }
43
-
44
- export function createORPCHooks<TRouter extends ContractRouter | Router<any>>(
45
- options: CreateORPCHooksOptions<TRouter>,
46
- ): TRouter extends Router<any>
47
- ? ORPCHooksWithRouter<TRouter>
48
- : TRouter extends ContractRouter
49
- ? ORPCHooksWithContractRouter<TRouter>
50
- : never {
51
- const path = options.path ?? []
52
- const generalHooks = createGeneralHooks({ context: options.context, path })
53
-
54
- // for sure root is not procedure, so do not it procedure hooks on root
55
- const procedureHooks = path.length
56
- ? createProcedureHooks({
57
- context: options.context,
58
- path,
59
- })
60
- : {}
61
-
62
- return new Proxy(
63
- {
64
- [orpcPathSymbol]: path,
65
-
66
- ...generalHooks,
67
- ...procedureHooks,
68
- },
69
- {
70
- get(target, key) {
71
- const value = Reflect.get(target, key)
72
-
73
- if (typeof key !== 'string') {
74
- return value
75
- }
76
-
77
- const nextHooks = createORPCHooks({
78
- context: options.context,
79
- path: [...path, key],
80
- })
81
-
82
- if (typeof value !== 'function') {
83
- return nextHooks
84
- }
85
-
86
- return new Proxy(value, {
87
- get(_, key) {
88
- return Reflect.get(nextHooks, key)
89
- },
90
- })
91
- },
92
- },
93
- ) as any
94
- }
@@ -1,98 +0,0 @@
1
- import type {
2
- ContractProcedure,
3
- ContractRouter,
4
- SchemaOutput,
5
- } from '@orpc/contract'
6
- import type { Procedure, Router } from '@orpc/server'
7
- import type { ORPCContextValue } from './react-context'
8
- import { createGeneralUtils, type GeneralUtils } from './general-utils'
9
- import { createProcedureUtils, type ProcedureUtils } from './procedure-utils'
10
-
11
- export type ORPCUtilsWithContractRouter<TRouter extends ContractRouter> = {
12
- [K in keyof TRouter]: TRouter[K] extends ContractProcedure<infer UInputSchema, infer UOutputSchema>
13
- ? ProcedureUtils<UInputSchema, UOutputSchema, SchemaOutput<UOutputSchema>> & GeneralUtils<UInputSchema, UOutputSchema, SchemaOutput<UOutputSchema>>
14
- : TRouter[K] extends ContractRouter
15
- ? ORPCUtilsWithContractRouter<TRouter[K]>
16
- : never
17
- } & GeneralUtils<undefined, undefined, unknown>
18
-
19
- export type ORPCUtilsWithRouter<TRouter extends Router<any>> = {
20
- [K in keyof TRouter]: TRouter[K] extends Procedure<any, any, infer UInputSchema, infer UOutputSchema, infer UFuncOutput>
21
- ? ProcedureUtils<UInputSchema, UOutputSchema, UFuncOutput> & GeneralUtils<UInputSchema, UOutputSchema, UFuncOutput>
22
- : TRouter[K] extends Router<any>
23
- ? ORPCUtilsWithRouter<TRouter[K]>
24
- : never
25
- } & GeneralUtils<undefined, undefined, unknown>
26
-
27
- export interface CreateORPCUtilsOptions<
28
- TRouter extends ContractRouter | Router<any>,
29
- > {
30
- contextValue: ORPCContextValue<TRouter>
31
-
32
- /**
33
- * The path of the router.
34
- *
35
- * @internal
36
- */
37
- path?: string[]
38
- }
39
-
40
- export function createORPCUtils<TRouter extends ContractRouter | Router<any>>(
41
- options: CreateORPCUtilsOptions<TRouter>,
42
- ): TRouter extends Router<any>
43
- ? ORPCUtilsWithRouter<TRouter>
44
- : TRouter extends ContractRouter
45
- ? ORPCUtilsWithContractRouter<TRouter>
46
- : never {
47
- const path = options.path ?? []
48
- const client = options.contextValue.client as any
49
-
50
- const generalUtils = createGeneralUtils({
51
- queryClient: options.contextValue.queryClient,
52
- path,
53
- })
54
-
55
- // for sure root is not procedure, so do not it procedure utils on root
56
- const procedureUtils = path.length
57
- ? createProcedureUtils({
58
- client,
59
- queryClient: options.contextValue.queryClient,
60
- path,
61
- })
62
- : {}
63
-
64
- return new Proxy(
65
- {
66
- ...generalUtils,
67
- ...procedureUtils,
68
- },
69
- {
70
- get(target, key) {
71
- const value = Reflect.get(target, key)
72
-
73
- if (typeof key !== 'string') {
74
- return value
75
- }
76
-
77
- const nextUtils = createORPCUtils({
78
- ...options,
79
- contextValue: {
80
- ...options.contextValue,
81
- client: client[key],
82
- },
83
- path: [...path, key],
84
- })
85
-
86
- if (typeof value !== 'function') {
87
- return nextUtils
88
- }
89
-
90
- return new Proxy(value, {
91
- get(_, key) {
92
- return Reflect.get(nextUtils, key)
93
- },
94
- })
95
- },
96
- },
97
- ) as any
98
- }
@@ -1,89 +0,0 @@
1
- import type { SchemaOutput } from '@orpc/contract'
2
- import type { QueryClient } from '@tanstack/react-query'
3
- import type { GeneralHooks } from './general-hooks'
4
- import type { GeneralUtils } from './general-utils'
5
- import type { ProcedureHooks } from './procedure-hooks'
6
- import type { ProcedureUtils } from './procedure-utils'
7
- import {
8
- orpc,
9
- orpcClient,
10
- ORPCContext,
11
- type UserFindInputSchema,
12
- type UserSchema,
13
- } from '../tests/orpc'
14
- import { useQueriesFactory } from './use-queries/hook'
15
-
16
- describe('useUtils', () => {
17
- const utils = orpc.useUtils()
18
-
19
- it('router level', () => {
20
- expectTypeOf(utils).toMatchTypeOf<
21
- GeneralUtils<undefined, undefined, unknown>
22
- >()
23
-
24
- expectTypeOf(utils.user).toMatchTypeOf<
25
- GeneralUtils<undefined, undefined, unknown>
26
- >()
27
- })
28
-
29
- it('procedure level', () => {
30
- expectTypeOf(utils.user.find).toMatchTypeOf<
31
- GeneralUtils<
32
- typeof UserFindInputSchema,
33
- typeof UserSchema,
34
- SchemaOutput<typeof UserSchema>
35
- >
36
- >()
37
-
38
- expectTypeOf(utils.user.find).toMatchTypeOf<
39
- ProcedureUtils<
40
- typeof UserFindInputSchema,
41
- typeof UserSchema,
42
- SchemaOutput<typeof UserSchema>
43
- >
44
- >()
45
- })
46
- })
47
-
48
- it('useContext', () => {
49
- const context = orpc.useContext()
50
-
51
- expectTypeOf(context.client).toEqualTypeOf(orpcClient)
52
- expectTypeOf(context.queryClient).toEqualTypeOf<QueryClient>()
53
- })
54
-
55
- it('useQueries', () => {
56
- expectTypeOf(orpc.useQueries).toEqualTypeOf(
57
- useQueriesFactory({ context: ORPCContext }),
58
- )
59
- })
60
-
61
- describe('hooks', () => {
62
- it('router level', () => {
63
- expectTypeOf(orpc).toMatchTypeOf<
64
- GeneralHooks<undefined, undefined, unknown>
65
- >()
66
-
67
- expectTypeOf(orpc.user).toMatchTypeOf<
68
- GeneralHooks<undefined, undefined, unknown>
69
- >()
70
- })
71
-
72
- it('procedure level', () => {
73
- expectTypeOf(orpc.user.find).toMatchTypeOf<
74
- GeneralHooks<
75
- typeof UserFindInputSchema,
76
- typeof UserSchema,
77
- SchemaOutput<typeof UserSchema>
78
- >
79
- >()
80
-
81
- expectTypeOf(orpc.user.find).toMatchTypeOf<
82
- ProcedureHooks<
83
- typeof UserFindInputSchema,
84
- typeof UserSchema,
85
- SchemaOutput<typeof UserSchema>
86
- >
87
- >()
88
- })
89
- })
@@ -1,102 +0,0 @@
1
- import { renderHook, waitFor } from '@testing-library/react'
2
- import { orpc, orpcClient, queryClient, wrapper } from '../tests/orpc'
3
-
4
- beforeEach(() => {
5
- queryClient.clear()
6
- })
7
-
8
- it('useUtils', async () => {
9
- const { result } = renderHook(() => orpc.useUtils(), { wrapper })
10
-
11
- const promise = result.current.user.find.ensureQueryData({ id: '1' })
12
- expect(result.current.user.isFetching()).toBe(1)
13
- const data = await promise
14
- expect(result.current.user.isFetching()).toBe(0)
15
-
16
- let old: any
17
- result.current.user.setQueriesData({}, (old_: any) => {
18
- old = old_
19
- })
20
-
21
- expect(data).toEqual({
22
- id: '1',
23
- name: 'name-1',
24
- })
25
- expect(old).toEqual(data)
26
- })
27
-
28
- it('useContext', async () => {
29
- const { result } = renderHook(() => orpc.useContext(), { wrapper })
30
-
31
- expect(result.current.client).toBe(orpcClient)
32
- expect(result.current.queryClient).toBe(queryClient)
33
-
34
- expect(() => renderHook(() => orpc.useContext())).throws(
35
- 'useORPCContext must be used within a <ORPCContext.Provider>, please see the docs',
36
- )
37
- })
38
-
39
- it('useQueries', async () => {
40
- const queries = renderHook(
41
- () => orpc.useQueries(o => [o.user.find({ id: '123' })]),
42
- { wrapper },
43
- )
44
-
45
- await waitFor(() =>
46
- expect(queries.result.current[0].data).toEqual({
47
- id: '123',
48
- name: 'name-123',
49
- }),
50
- )
51
- })
52
-
53
- it('hooks', async () => {
54
- const isFetching = renderHook(() => orpc.user.useIsFetching(), {
55
- wrapper,
56
- })
57
- const isMutating = renderHook(() => orpc.user.useIsMutating(), {
58
- wrapper,
59
- })
60
-
61
- expect(isFetching.result.current).toBe(0)
62
- expect(isMutating.result.current).toBe(0)
63
-
64
- const query = renderHook(() => orpc.user.find.useQuery({ id: '1' }), {
65
- wrapper,
66
- })
67
- const mutation = renderHook(() => orpc.user.create.useMutation(), {
68
- wrapper,
69
- })
70
-
71
- await waitFor(() => expect(query.result.current.status).toEqual('pending'))
72
-
73
- expect(isFetching.result.current).toBe(1)
74
- expect(isMutating.result.current).toBe(0)
75
-
76
- await waitFor(() =>
77
- expect(query.result.current.data).toEqual({
78
- id: '1',
79
- name: 'name-1',
80
- }),
81
- )
82
-
83
- expect(isFetching.result.current).toBe(0)
84
- expect(isMutating.result.current).toBe(0)
85
-
86
- mutation.result.current.mutate({ name: 'name-2' })
87
-
88
- await waitFor(() => expect(mutation.result.current.status).toEqual('pending'))
89
-
90
- expect(isFetching.result.current).toBe(0)
91
- expect(isMutating.result.current).toBe(1)
92
-
93
- await waitFor(() => expect(mutation.result.current.status).toEqual('success'))
94
-
95
- expect(isFetching.result.current).toBe(0)
96
- expect(isMutating.result.current).toBe(0)
97
-
98
- expect(mutation.result.current.data).toEqual({
99
- id: expect.any(String),
100
- name: 'name-2',
101
- })
102
- })
package/src/react.tsx DELETED
@@ -1,81 +0,0 @@
1
- import type { ContractRouter } from '@orpc/contract'
2
- import type { Router } from '@orpc/server'
3
- import {
4
- createORPCContext,
5
- type ORPCContext,
6
- type ORPCContextValue,
7
- useORPCContext,
8
- } from './react-context'
9
- import {
10
- createORPCHooks,
11
- type ORPCHooksWithContractRouter,
12
- type ORPCHooksWithRouter,
13
- } from './react-hooks'
14
- import {
15
- createORPCUtils,
16
- type ORPCUtilsWithContractRouter,
17
- type ORPCUtilsWithRouter,
18
- } from './react-utils'
19
- import {
20
- useQueriesFactory,
21
- type UseQueriesWithContractRouter,
22
- type UseQueriesWithRouter,
23
- } from './use-queries/hook'
24
-
25
- export type ORPCReactWithContractRouter<TRouter extends ContractRouter> =
26
- ORPCHooksWithContractRouter<TRouter> & {
27
- useContext: () => ORPCContextValue<TRouter>
28
- useUtils: () => ORPCUtilsWithContractRouter<TRouter>
29
- useQueries: UseQueriesWithContractRouter<TRouter>
30
- }
31
-
32
- export type ORPCReactWithRouter<TRouter extends Router<any>> =
33
- ORPCHooksWithRouter<TRouter> & {
34
- useContext: () => ORPCContextValue<TRouter>
35
- useUtils: () => ORPCUtilsWithRouter<TRouter>
36
- useQueries: UseQueriesWithRouter<TRouter>
37
- }
38
-
39
- export function createORPCReact<
40
- TRouter extends ContractRouter | Router<any>,
41
- >(): {
42
- orpc: TRouter extends Router<any>
43
- ? ORPCReactWithRouter<TRouter>
44
- : TRouter extends ContractRouter
45
- ? ORPCReactWithContractRouter<TRouter>
46
- : never
47
- ORPCContext: ORPCContext<TRouter>
48
- } {
49
- const Context = createORPCContext<TRouter>()
50
- const useContext = () => useORPCContext(Context)
51
- const useUtils = () => createORPCUtils({ contextValue: useContext() })
52
- // eslint-disable-next-line react-hooks/rules-of-hooks
53
- const useQueries = useQueriesFactory({ context: Context })
54
- const hooks = createORPCHooks({ context: Context })
55
-
56
- const orpc = new Proxy(
57
- {
58
- useContext,
59
- useUtils,
60
- useQueries,
61
- },
62
- {
63
- get(target, key) {
64
- const value = Reflect.get(target, key)
65
- const nextHooks = Reflect.get(hooks, key)
66
-
67
- if (typeof value !== 'function') {
68
- return nextHooks
69
- }
70
-
71
- return new Proxy(value, {
72
- get(_, key) {
73
- return Reflect.get(nextHooks, key)
74
- },
75
- })
76
- },
77
- },
78
- )
79
-
80
- return { orpc: orpc as any, ORPCContext: Context }
81
- }
@@ -1,35 +0,0 @@
1
- import { orpc } from '../tests/orpc'
2
- import { getMutationKey, getQueryKey } from './tanstack-key'
3
-
4
- describe('query', () => {
5
- it('required valid orpc', () => {
6
- getQueryKey(orpc.ping)
7
- getQueryKey(orpc.user.find)
8
-
9
- // @ts-expect-error invalid orpc
10
- getQueryKey({})
11
-
12
- // @ts-expect-error cannot use in root
13
- getQueryKey(orpc)
14
- })
15
-
16
- it('infer input', () => {
17
- getQueryKey(orpc.user.find, { input: { id: '1' } })
18
-
19
- // @ts-expect-error invalid input
20
- getQueryKey(orpc.user.find, { input: { id: 1234 } })
21
- })
22
- })
23
-
24
- describe('mutation', () => {
25
- it('required valid orpc', () => {
26
- getMutationKey(orpc.ping)
27
- getMutationKey(orpc.user.find)
28
-
29
- // @ts-expect-error invalid orpc
30
- getMutationKey({})
31
-
32
- // @ts-expect-error cannot use in root
33
- getMutationKey(orpc)
34
- })
35
- })
@@ -1,62 +0,0 @@
1
- import { orpc } from '../tests/orpc'
2
- import {
3
- getMutationKey,
4
- getMutationKeyFromPath,
5
- getQueryKey,
6
- getQueryKeyFromPath,
7
- } from './tanstack-key'
8
-
9
- describe('query', () => {
10
- it('from path', () => {
11
- expect(getQueryKeyFromPath(['user', 'find'])).toEqual([
12
- ['user', 'find'],
13
- {},
14
- ])
15
-
16
- expect(
17
- getQueryKeyFromPath(['user', 'find'], {
18
- input: { id: '1' },
19
- }),
20
- ).toEqual([['user', 'find'], { input: { id: '1' } }])
21
-
22
- expect(
23
- getQueryKeyFromPath(['user', 'find'], {
24
- input: { id: '1' },
25
- type: 'query',
26
- }),
27
- ).toEqual([['user', 'find'], { input: { id: '1' }, type: 'query' }])
28
-
29
- expect(
30
- getQueryKeyFromPath(['user', 'find'], {
31
- type: 'infinite',
32
- }),
33
- ).toEqual([['user', 'find'], { type: 'infinite' }])
34
- })
35
-
36
- it('from orpc', () => {
37
- expect(getQueryKey(orpc.ping)).toEqual([['ping'], {}])
38
-
39
- expect(getQueryKey(orpc.user.find)).toEqual([['user', 'find'], {}])
40
-
41
- expect(getQueryKey(orpc.user.find, { input: { id: '1' } })).toEqual([
42
- ['user', 'find'],
43
- { input: { id: '1' } },
44
- ])
45
-
46
- expect(
47
- getQueryKey(orpc.user.find, { input: { id: '1' }, type: 'infinite' }),
48
- ).toEqual([['user', 'find'], { input: { id: '1' }, type: 'infinite' }])
49
- })
50
- })
51
-
52
- describe('mutation', () => {
53
- it('from path', () => {
54
- expect(getMutationKeyFromPath(['user', 'create'])).toEqual([
55
- ['user', 'create'],
56
- ])
57
- })
58
-
59
- it('from orpc', () => {
60
- expect(getMutationKey(orpc.user.create)).toEqual([['user', 'create']])
61
- })
62
- })
@@ -1,64 +0,0 @@
1
- import type { SchemaInput } from '@orpc/contract'
2
- import type { PartialDeep } from '@orpc/shared'
3
- import type { MutationKey, QueryKey } from '@tanstack/react-query'
4
- import type { ProcedureHooks } from './procedure-hooks'
5
- import type {
6
- ORPCHooksWithContractRouter,
7
- ORPCHooksWithRouter,
8
- } from './react-hooks'
9
- import { getORPCPath } from './orpc-path'
10
-
11
- export type QueryType = 'query' | 'infinite' | undefined
12
-
13
- export interface GetQueryKeyOptions<TInput> {
14
- input?: TInput
15
- type?: QueryType
16
- }
17
-
18
- export function getQueryKey<
19
- T extends
20
- | ORPCHooksWithContractRouter<any>
21
- | ORPCHooksWithRouter<any>
22
- | ProcedureHooks<any, any, any>,
23
- >(
24
- orpc: T,
25
- options?: GetQueryKeyOptions<
26
- T extends ProcedureHooks<infer UInputSchema, any, any>
27
- ? PartialDeep<SchemaInput<UInputSchema>>
28
- : unknown
29
- >,
30
- ): QueryKey {
31
- const path = getORPCPath(orpc)
32
- return getQueryKeyFromPath(path, options)
33
- }
34
-
35
- export function getQueryKeyFromPath(
36
- path: string[],
37
- options?: GetQueryKeyOptions<unknown>,
38
- ): QueryKey {
39
- const withInput
40
- = options?.input !== undefined ? { input: options?.input } : {}
41
- const withType = options?.type !== undefined ? { type: options?.type } : {}
42
-
43
- return [
44
- path,
45
- {
46
- ...withInput,
47
- ...withType,
48
- },
49
- ]
50
- }
51
-
52
- export function getMutationKey<
53
- T extends
54
- | ORPCHooksWithContractRouter<any>
55
- | ORPCHooksWithRouter<any>
56
- | ProcedureHooks<any, any, any>,
57
- >(orpc: T): MutationKey {
58
- const path = getORPCPath(orpc)
59
- return getMutationKeyFromPath(path)
60
- }
61
-
62
- export function getMutationKeyFromPath(path: string[]): MutationKey {
63
- return [path]
64
- }
@@ -1,27 +0,0 @@
1
- import type { SetOptional } from '@orpc/shared'
2
- import type {
3
- InvalidateQueryFilters,
4
- QueryFilters,
5
- } from '@tanstack/react-query'
6
- import type { QueryType } from './tanstack-key'
7
-
8
- export interface ORPCAdditionalQueryFilters<TFilterInput> {
9
- /**
10
- * The type of the query. useQuery=query, useInfiniteQuery=infinite
11
- * If not specified, it will match all types.
12
- */
13
- queryType?: QueryType
14
-
15
- /**
16
- * The input of the query. If not specified, it will match all inputs.
17
- */
18
- input?: TFilterInput
19
- }
20
-
21
- export interface ORPCQueryFilters<TFilterInput>
22
- extends SetOptional<QueryFilters, 'queryKey'>,
23
- ORPCAdditionalQueryFilters<TFilterInput> {}
24
-
25
- export interface ORPCInvalidateQueryFilters<TFilterInput>
26
- extends SetOptional<InvalidateQueryFilters, 'queryKey'>,
27
- ORPCAdditionalQueryFilters<TFilterInput> {}
package/src/types.ts DELETED
@@ -1,7 +0,0 @@
1
- import type { Schema, SchemaInput } from '@orpc/contract'
2
-
3
- export type SchemaInputForInfiniteQuery<TInputSchema extends Schema> = Omit<
4
- SchemaInput<TInputSchema>,
5
- 'cursor'
6
- > &
7
- (Record<string | number, any> & { cursor?: never })
@@ -1,29 +0,0 @@
1
- import type { Promisable } from '@orpc/shared'
2
- import { orpcClient } from '../../tests/orpc'
3
- import { createUseQueriesBuilder } from './builder'
4
-
5
- it('createUseQueriesBuilder', () => {
6
- const builder = createUseQueriesBuilder({
7
- client: orpcClient.user.find,
8
- path: ['user', 'find'],
9
- })
10
-
11
- expectTypeOf<Parameters<typeof builder>[0]>().toEqualTypeOf<{
12
- id: string
13
- }>()
14
-
15
- const result = builder({ id: '123' }, { throwOnError: true })
16
-
17
- builder({ id: '123' })
18
- // @ts-expect-error invalid input
19
- builder({ id: 123 })
20
-
21
- if (typeof result.queryFn === 'function') {
22
- expectTypeOf(result.queryFn({} as any)).toMatchTypeOf<
23
- Promisable<{
24
- id: string
25
- name: string
26
- }>
27
- >()
28
- }
29
- })