Skip to content

Commit

Permalink
Propagate generic types for useMutation
Browse files Browse the repository at this point in the history
  • Loading branch information
dpup committed May 16, 2024
1 parent 8fbf471 commit 9bfbb91
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-query-grpc-gateway",
"version": "1.2.5",
"version": "1.2.6",
"description": "React hook for querying gRPC Gateway endpoints using Tanstack Query.",
"type": "module",
"main": "dist/index.js",
Expand Down
13 changes: 9 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
// - I represents the input type for the service method, or the request.
// - O represents the output type for the service method, or the response.
// - M represents the service method itself.
// - C represents a context.

// Represents the static methods from the generated service client.
export type ServiceMethod<I, O> = (req: I, initReq: RequestInitWithPathPrefix) => Promise<O>;
Expand Down Expand Up @@ -52,8 +53,11 @@ type UseServiceQueryOptions<M extends ServiceMethod<Parameters<M>[0], ReturnType
// Represents the `useServiceMutation` options. This is the same as
// `UseMutationOptions` except that `mutationFn` is handled internally, so must
// not be provided.
type UseServiceMutationOptions<M extends ServiceMethod<Parameters<M>[0], ReturnType<M>>> = Omit<
UseMutationOptions<Awaited<ReturnType<M>>, ServiceError, Parameters<M>[0], unknown>,
type UseServiceMutationOptions<
M extends ServiceMethod<Parameters<M>[0], ReturnType<M>>,
C = unknown,
> = Omit<
UseMutationOptions<Awaited<ReturnType<M>>, ServiceError, Parameters<M>[0], C>,
'mutationFn'
>;

Expand Down Expand Up @@ -98,10 +102,11 @@ export function queryOptions<M extends ServiceMethod<Parameters<M>[0], Awaited<R
// context and making it easier to call generated service clients.
export function useServiceMutation<
M extends ServiceMethod<Parameters<M>[0], Awaited<ReturnType<M>>>,
C = unknown,
>(
method: M,
options?: UseServiceMutationOptions<M>,
): UseMutationResult<Awaited<ReturnType<M>>, ServiceError> {
options?: UseServiceMutationOptions<M, C>,
): UseMutationResult<Awaited<ReturnType<M>>, ServiceError, Partial<Parameters<M>[0]>, C> {
const reqCtx = useContext(ServiceContext);
return useMutation({
...options!,
Expand Down

0 comments on commit 9bfbb91

Please sign in to comment.