Skip to content

Commit b42ba03

Browse files
authored
feat(query, colada): add .call utility for direct client call (#138)
1 parent a8ad31d commit b42ba03

15 files changed

+92
-3
lines changed

packages/react-query/src/procedure-utils.test-d.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ErrorFromErrorMap } from '@orpc/contract'
1+
import type { Client, ErrorFromErrorMap } from '@orpc/contract'
22
import type { InfiniteData } from '@tanstack/react-query'
33
import type { baseErrorMap } from '../../contract/tests/shared'
44
import type { ProcedureUtils } from './procedure-utils'
@@ -16,6 +16,17 @@ describe('ProcedureUtils', () => {
1616
ErrorFromErrorMap<typeof baseErrorMap>
1717
>
1818

19+
it('.call', () => {
20+
expectTypeOf(utils.call).toEqualTypeOf<
21+
Client<
22+
{ batch?: boolean } | undefined,
23+
UtilsInput,
24+
UtilsOutput,
25+
ErrorFromErrorMap<typeof baseErrorMap>
26+
>
27+
>()
28+
})
29+
1930
describe('.queryOptions', () => {
2031
it('can optional options', () => {
2132
const requiredUtils = {} as ProcedureUtils<{ batch?: boolean }, 'input', UtilsOutput, Error>

packages/react-query/src/procedure-utils.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ describe('createProcedureUtils', () => {
1313
const client = vi.fn().mockResolvedValue('__output__')
1414
const utils = createProcedureUtils(client, ['ping'])
1515

16+
it('.call', () => {
17+
expect(utils.call).toBe(client)
18+
})
19+
1620
it('.queryOptions', async () => {
1721
const options = utils.queryOptions({ input: { search: '__search__' }, context: { batch: '__batch__' } })
1822

packages/react-query/src/procedure-utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import type { InfiniteOptionsBase, InfiniteOptionsIn, MutationOptionsBase, Mutat
55
import { buildKey } from './key'
66

77
export interface ProcedureUtils<TClientContext, TInput, TOutput, TError extends Error> {
8+
call: Client<TClientContext, TInput, TOutput, TError>
9+
810
queryOptions<U, USelectData = TOutput>(
911
...rest: MaybeOptionalOptions<
1012
U & QueryOptionsIn<TClientContext, TInput, TOutput, TError, USelectData>
@@ -27,6 +29,8 @@ export function createProcedureUtils<TClientContext, TInput, TOutput, TError ext
2729
path: string[],
2830
): ProcedureUtils<TClientContext, TInput, TOutput, TError> {
2931
return {
32+
call: client,
33+
3034
queryOptions(...[{ input, context, ...rest } = {}]) {
3135
return {
3236
queryKey: buildKey(path, { type: 'query', input: input as any }),

packages/react-query/tests/e2e.test-d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { InfiniteData } from '@tanstack/react-query'
22
import { isDefinedError } from '@orpc/contract'
33
import { useInfiniteQuery, useMutation, useQueries, useQuery, useSuspenseInfiniteQuery, useSuspenseQuery } from '@tanstack/react-query'
4+
import { orpc as client } from '../../client/tests/shared'
45
import { orpc, queryClient } from './shared'
56

67
it('.key', () => {
@@ -14,6 +15,10 @@ it('.key', () => {
1415
orpc.ping.key({ input: { input: 'INVALID' } })
1516
})
1617

18+
it('.call', () => {
19+
expectTypeOf(orpc.ping.call).toEqualTypeOf(client.ping)
20+
})
21+
1722
describe('.queryOptions', () => {
1823
it('useQuery', () => {
1924
const query = useQuery(orpc.ping.queryOptions({

packages/react-query/tests/e2e.test.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ beforeEach(() => {
99
vi.clearAllMocks()
1010
})
1111

12+
it('case: call directly', async () => {
13+
expect(await orpc.ping.call({ input: 123 })).toEqual({ output: '123' })
14+
})
15+
1216
it('case: with useQuery', async () => {
1317
const { result } = renderHook(() => useQuery(orpc.nested.ping.queryOptions({ input: { input: 123 } }), queryClient))
1418

packages/vue-colada/src/procedure-utils.test-d.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ErrorFromErrorMap } from '@orpc/contract'
1+
import type { Client, ErrorFromErrorMap } from '@orpc/contract'
22
import type { baseErrorMap } from '../../contract/tests/shared'
33
import type { ProcedureUtils } from './procedure-utils'
44
import { useMutation, useQuery } from '@pinia/colada'
@@ -15,6 +15,17 @@ describe('ProcedureUtils', () => {
1515
ErrorFromErrorMap<typeof baseErrorMap>
1616
>
1717

18+
it('.call', () => {
19+
expectTypeOf(utils.call).toEqualTypeOf<
20+
Client<
21+
{ batch?: boolean } | undefined,
22+
UtilsInput,
23+
UtilsOutput,
24+
ErrorFromErrorMap<typeof baseErrorMap>
25+
>
26+
>()
27+
})
28+
1829
describe('.queryOptions', () => {
1930
it('can optional options', () => {
2031
const requiredUtils = {} as ProcedureUtils<{ batch?: boolean }, 'input', UtilsOutput, Error>

packages/vue-colada/src/procedure-utils.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ beforeEach(() => {
1313
buildKeySpy.mockReturnValue(['__mocked__'])
1414
})
1515

16+
it('.call', () => {
17+
const client = vi.fn(
18+
(...[input]) => Promise.resolve(input?.toString()),
19+
)
20+
const utils = createProcedureUtils(client, ['ping'])
21+
22+
expect(utils.call).toBe(client)
23+
})
24+
1625
describe('queryOptions', () => {
1726
const client = vi.fn(
1827
(...[input]) => Promise.resolve(input?.toString()),

packages/vue-colada/src/procedure-utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { buildKey } from './key'
66
import { unrefDeep } from './utils'
77

88
export interface ProcedureUtils<TClientContext, TInput, TOutput, TError extends Error> {
9+
call: Client<TClientContext, TInput, TOutput, TError>
10+
911
queryOptions(
1012
...rest: MaybeOptionalOptions<
1113
QueryOptionsIn<TClientContext, TInput, TOutput, TError>
@@ -24,6 +26,8 @@ export function createProcedureUtils<TClientContext, TInput, TOutput, TError ext
2426
path: string[],
2527
): ProcedureUtils<TClientContext, TInput, TOutput, TError> {
2628
return {
29+
call: client,
30+
2731
queryOptions(...[{ input, context, ...rest } = {}]) {
2832
return {
2933
key: computed(() => buildKey(path, { input: unrefDeep(input) })),

packages/vue-colada/tests/e2e.test-d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { isDefinedError } from '@orpc/contract'
22
import { useMutation, useQuery, useQueryCache } from '@pinia/colada'
33
import { computed, ref } from 'vue'
4+
import { orpc as client } from '../../client/tests/shared'
45
import { orpc } from './shared'
56

67
it('.key', () => {
@@ -15,6 +16,10 @@ it('.key', () => {
1516
orpc.ping.key({ input: { input: 'INVALID' } })
1617
})
1718

19+
it('.call', () => {
20+
expectTypeOf(orpc.ping.call).toEqualTypeOf(client.ping)
21+
})
22+
1823
describe('.queryOptions', () => {
1924
it('useQuery', () => {
2025
const query = useQuery(orpc.ping.queryOptions({

packages/vue-colada/tests/e2e.test.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ beforeEach(() => {
88
vi.clearAllMocks()
99
})
1010

11+
it('case: call directly', async () => {
12+
expect(await orpc.ping.call({ input: 123 })).toEqual({ output: '123' })
13+
})
14+
1115
it('case: with useQuery', async () => {
1216
const mounted = mount(defineComponent({
1317
setup() {

0 commit comments

Comments
 (0)