Skip to content

Commit cd121e3

Browse files
authored
fix(vue-colada): correctly infer mutation context (#191)
* fix(vue-colada): correctly infer mutation context * improve tests
1 parent a05fbfd commit cd121e3

File tree

5 files changed

+49
-24
lines changed

5 files changed

+49
-24
lines changed

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -271,15 +271,6 @@ describe('ProcedureUtils', () => {
271271
utils.mutationOptions({ context: { batch: 'invalid' } })
272272
})
273273

274-
it('infer correct mutation context type', () => {
275-
utils.mutationOptions({
276-
onMutate: () => ({ mutationContext: true }),
277-
onError: (e, v, context) => {
278-
expectTypeOf(context).toEqualTypeOf<undefined | { mutationContext: boolean }>()
279-
},
280-
})
281-
})
282-
283274
it('works with useMutation', () => {
284275
const mutation = useMutation(utils.mutationOptions({
285276
onSuccess: (data, input) => {
@@ -295,5 +286,19 @@ describe('ProcedureUtils', () => {
295286
expectTypeOf(mutation.data).toEqualTypeOf<UtilsOutput | undefined>()
296287
expectTypeOf(mutation.error).toEqualTypeOf<ErrorFromErrorMap<typeof baseErrorMap> | null>()
297288
})
289+
290+
it('infer correct mutation context type', () => {
291+
useMutation({
292+
...utils.mutationOptions({
293+
onMutate: () => ({ mutationContext: true }),
294+
onError: (e, v, context) => {
295+
expectTypeOf(context?.mutationContext).toEqualTypeOf<undefined | boolean>()
296+
},
297+
}),
298+
onSettled: (d, e, v, context) => {
299+
expectTypeOf(context?.mutationContext).toEqualTypeOf<undefined | boolean>()
300+
},
301+
})
302+
})
298303
})
299304
})

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,19 @@ describe('ProcedureUtils', () => {
113113
expectTypeOf(mutation.data.value).toEqualTypeOf<UtilsOutput | undefined>()
114114
expectTypeOf(mutation.error.value).toEqualTypeOf<ErrorFromErrorMap<typeof baseErrorMap> | null>()
115115
})
116+
117+
it('infer correct mutation context type', () => {
118+
useMutation({
119+
...utils.mutationOptions({
120+
onMutate: () => ({ mutationContext: true }),
121+
onError: (e, v, context) => {
122+
expectTypeOf(context.mutationContext).toEqualTypeOf<undefined | boolean>()
123+
},
124+
}),
125+
onSettled: (d, e, v, context) => {
126+
expectTypeOf(context.mutationContext).toEqualTypeOf<undefined | boolean>()
127+
},
128+
})
129+
})
116130
})
117131
})

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Client, ClientContext } from '@orpc/client'
22
import type { MaybeOptionalOptions } from '@orpc/shared'
3+
import type { _EmptyObject } from '@pinia/colada'
34
import type { MutationOptions, MutationOptionsIn, QueryOptions, QueryOptionsIn } from './types'
45
import { computed } from 'vue'
56
import { buildKey } from './key'
@@ -14,11 +15,11 @@ export interface ProcedureUtils<TClientContext extends ClientContext, TInput, TO
1415
>
1516
): QueryOptions<TOutput, TError>
1617

17-
mutationOptions(
18+
mutationOptions<UMutationContext extends Record<any, any> = _EmptyObject>(
1819
...rest: MaybeOptionalOptions<
19-
MutationOptionsIn<TClientContext, TInput, TOutput, TError>
20+
MutationOptionsIn<TClientContext, TInput, TOutput, TError, UMutationContext>
2021
>
21-
): MutationOptions<TInput, TOutput, TError>
22+
): MutationOptions<TInput, TOutput, TError, UMutationContext>
2223
}
2324

2425
export function createProcedureUtils<TClientContext extends ClientContext, TInput, TOutput, TError extends Error>(

packages/vue-colada/src/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ export type QueryOptionsIn<TClientContext extends ClientContext, TInput, TOutput
2020

2121
export type QueryOptions<TOutput, TError extends Error> = UseQueryOptions<TOutput, TError>
2222

23-
export type MutationOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError extends Error> =
23+
export type MutationOptionsIn<TClientContext extends ClientContext, TInput, TOutput, TError extends Error, TMutationContext extends Record<any, any>> =
2424
& (Record<never, never> extends TClientContext ? { context?: MaybeRefDeep<TClientContext> } : { context: MaybeRefDeep<TClientContext> })
25-
& SetOptional<UseMutationOptions<TOutput, TInput, TError>, 'mutation'>
25+
& SetOptional<UseMutationOptions<TOutput, TInput, TError, TMutationContext>, 'mutation'>
2626

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

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -258,15 +258,6 @@ describe('ProcedureUtils', () => {
258258
utils.mutationOptions({ context: { batch: 'invalid' } })
259259
})
260260

261-
it('infer correct mutation context type', () => {
262-
utils.mutationOptions({
263-
onMutate: () => ({ mutationContext: true }),
264-
onError: (e, v, context) => {
265-
expectTypeOf(context).toEqualTypeOf<undefined | { mutationContext: boolean }>()
266-
},
267-
})
268-
})
269-
270261
it('works with useMutation', () => {
271262
const mutation = useMutation(utils.mutationOptions({
272263
onSuccess: (data, input) => {
@@ -282,5 +273,19 @@ describe('ProcedureUtils', () => {
282273
expectTypeOf(mutation.data.value).toEqualTypeOf<UtilsOutput | undefined>()
283274
expectTypeOf(mutation.error.value).toEqualTypeOf<ErrorFromErrorMap<typeof baseErrorMap> | null>()
284275
})
276+
277+
it('infer correct mutation context type', () => {
278+
useMutation({
279+
...utils.mutationOptions({
280+
onMutate: () => ({ mutationContext: true }),
281+
onError: (e, v, context) => {
282+
expectTypeOf(context?.mutationContext).toEqualTypeOf<undefined | boolean>()
283+
},
284+
}),
285+
onSettled: (d, e, v, context) => {
286+
expectTypeOf(context?.mutationContext).toEqualTypeOf<undefined | boolean>()
287+
},
288+
})
289+
})
285290
})
286291
})

0 commit comments

Comments
 (0)