Skip to content

Commit 15d9202

Browse files
authored
feat(vue-colada)!: remove deep ref in input & context options (#238)
to match with other options <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Refactor** - Simplified value handling by replacing extra reactivity wrappers with direct and function-based inputs for query and mutation configurations. - Updated internal processing to streamline how values and contexts are managed. - **Tests** - Revised test cases to align with the new, simplified input and context handling. - Removed legacy tests related to deep reactive reference unwrapping. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent ff68fdb commit 15d9202

File tree

10 files changed

+31
-160
lines changed

10 files changed

+31
-160
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { ErrorFromErrorMap } from '@orpc/contract'
33
import type { baseErrorMap } from '../../contract/tests/shared'
44
import type { ProcedureUtils } from './procedure-utils'
55
import { useMutation, useQuery } from '@pinia/colada'
6-
import { computed, ref } from 'vue'
6+
import { computed } from 'vue'
77

88
describe('ProcedureUtils', () => {
99
type UtilsInput = { search?: string, cursor?: number } | undefined
@@ -63,9 +63,14 @@ describe('ProcedureUtils', () => {
6363

6464
it('works with ref', () => {
6565
utils.queryOptions({
66-
input: computed(() => ({ cursor: ref(1) })),
66+
input: computed(() => ({ cursor: 1 })),
6767
context: computed(() => ({ batch: true })),
6868
})
69+
70+
utils.queryOptions({
71+
input: () => ({ cursor: 1 }),
72+
context: () => ({ batch: true }),
73+
})
6974
})
7075

7176
describe('works with useQuery', () => {
@@ -109,6 +114,16 @@ describe('ProcedureUtils', () => {
109114
utils.mutationOptions({ context: { batch: 'invalid' } })
110115
})
111116

117+
it('works with ref', () => {
118+
utils.mutationOptions({
119+
context: computed(() => ({ batch: true })),
120+
})
121+
122+
utils.mutationOptions({
123+
context: () => ({ batch: true }),
124+
})
125+
})
126+
112127
it('works with useMutation', () => {
113128
const mutation = useMutation(utils.mutationOptions({
114129
onSuccess: (data, input) => {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ describe('queryOptions', () => {
6363
const client = vi.fn((...[input]) => Promise.resolve(input?.toString()))
6464
const utils = createProcedureUtils(client, { path: ['ping'] })
6565

66-
const options = utils.queryOptions({ context: { batch: ref(true) } }) as any
66+
const options = utils.queryOptions({ context: ref({ batch: true }) }) as any
6767

6868
expect(options.key.value).toEqual(['__mocked__'])
6969
expect(buildKeySpy).toHaveBeenCalledTimes(1)
@@ -105,7 +105,7 @@ describe('mutationOptions', () => {
105105
)
106106
const utils = createProcedureUtils(client, { path: ['ping'] })
107107

108-
const options = utils.mutationOptions({ context: { batch: ref(true) } }) as any
108+
const options = utils.mutationOptions({ context: ref({ batch: true }) }) as any
109109

110110
expect(options.key('__input__')).toEqual(['__mocked__'])
111111
expect(buildKeySpy).toHaveBeenCalledTimes(1)

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import type { Client, ClientContext } from '@orpc/client'
22
import type { MaybeOptionalOptions } from '@orpc/shared'
33
import type { _EmptyObject } from '@pinia/colada'
44
import type { MutationOptions, MutationOptionsIn, QueryOptions, QueryOptionsIn } from './types'
5-
import { computed } from 'vue'
5+
import { computed, toValue } from 'vue'
66
import { buildKey } from './key'
7-
import { unrefDeep } from './utils'
87

98
export interface ProcedureUtils<TClientContext extends ClientContext, TInput, TOutput, TError extends Error> {
109
call: Client<TClientContext, TInput, TOutput, TError>
@@ -35,16 +34,16 @@ export function createProcedureUtils<TClientContext extends ClientContext, TInpu
3534

3635
queryOptions(...[{ input, context, ...rest } = {}]) {
3736
return {
38-
key: computed(() => buildKey(options.path, { input: unrefDeep(input) })),
39-
query: ({ signal }) => client(unrefDeep(input) as any, { signal, context: unrefDeep(context) as any }),
37+
key: computed(() => buildKey(options.path, { input: toValue(input) })),
38+
query: ({ signal }) => client(toValue(input) as any, { signal, context: toValue(context) as any }),
4039
...(rest as any),
4140
}
4241
},
4342

4443
mutationOptions(...[{ context, ...rest } = {}]) {
4544
return {
4645
key: input => buildKey(options.path, { input }),
47-
mutation: input => client(input, { context: unrefDeep(context) as any }),
46+
mutation: input => client(input, { context: toValue(context) as any }),
4847
...(rest as any),
4948
}
5049
},

packages/vue-colada/src/types.test-d.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

packages/vue-colada/src/types.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,19 @@
11
import type { ClientContext } from '@orpc/client'
2-
import type { AnyFunction, SetOptional } from '@orpc/shared'
2+
import type { SetOptional } from '@orpc/shared'
33
import type { UseMutationOptions, UseQueryOptions } from '@pinia/colada'
4-
import type { MaybeRef } from 'vue'
5-
6-
export type MaybeRefDeep<T> = MaybeRef<
7-
T extends AnyFunction
8-
? T
9-
: T extends object
10-
? { [K in keyof T]: MaybeRefDeep<T[K]> }
11-
: T
12-
>
4+
import type { MaybeRefOrGetter } from 'vue'
135

146
export type UseQueryFnContext = Parameters<UseQueryOptions<any>['query']>[0]
157

168
export type QueryOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError extends Error, TInitialData extends TOutput | undefined> =
17-
& (undefined extends TInput ? { input?: MaybeRefDeep<TInput> } : { input: MaybeRefDeep<TInput> })
18-
& (Record<never, never> extends TClientContext ? { context?: MaybeRefDeep<TClientContext> } : { context: MaybeRefDeep<TClientContext> })
9+
& (undefined extends TInput ? { input?: MaybeRefOrGetter<TInput> } : { input: MaybeRefOrGetter<TInput> })
10+
& (Record<never, never> extends TClientContext ? { context?: MaybeRefOrGetter<TClientContext> } : { context: MaybeRefOrGetter<TClientContext> })
1911
& SetOptional<QueryOptions<TOutput, TError, TInitialData>, 'key' | 'query'>
2012

2113
export type QueryOptions<TOutput, TError extends Error, TInitialData extends TOutput | undefined> = UseQueryOptions<TOutput, TError, TInitialData>
2214

2315
export type MutationOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError extends Error, TMutationContext extends Record<any, any>> =
24-
& (Record<never, never> extends TClientContext ? { context?: MaybeRefDeep<TClientContext> } : { context: MaybeRefDeep<TClientContext> })
16+
& (Record<never, never> extends TClientContext ? { context?: MaybeRefOrGetter<TClientContext> } : { context: MaybeRefOrGetter<TClientContext> })
2517
& SetOptional<UseMutationOptions<TOutput, TInput, TError, TMutationContext>, 'mutation'>
2618

2719
export type MutationOptions<TInput, TOutput, TError extends Error, TMutationContext extends Record<any, any>> = UseMutationOptions<TOutput, TInput, TError, TMutationContext>

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

Lines changed: 0 additions & 42 deletions
This file was deleted.

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

Lines changed: 0 additions & 50 deletions
This file was deleted.

packages/vue-colada/src/utils.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { isDefinedError } from '@orpc/client'
22
import { useMutation, useQuery, useQueryCache } from '@pinia/colada'
3-
import { computed, ref } from 'vue'
3+
import { computed } from 'vue'
44
import { orpc as client } from '../../client/tests/shared'
55
import { orpc } from './shared'
66

@@ -23,7 +23,7 @@ it('.call', () => {
2323
describe('.queryOptions', () => {
2424
it('useQuery', () => {
2525
const query = useQuery(orpc.ping.queryOptions({
26-
input: computed(() => ({ input: ref(123) })),
26+
input: computed(() => ({ input: 123 })),
2727
}))
2828

2929
if (isDefinedError(query.error.value) && query.error.value.code === 'OVERRIDE') {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ it('case: with useQuery', async () => {
1818
const id = ref(123)
1919

2020
const queryCache = useQueryCache()
21-
const query = useQuery(orpc.nested.ping.queryOptions({ input: computed(() => ({ input: id })) }))
21+
const query = useQuery(orpc.nested.ping.queryOptions({ input: computed(() => ({ input: id.value })) }))
2222

2323
const setId = (value: number) => {
2424
id.value = value

0 commit comments

Comments
 (0)