Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/openapi-react-query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,16 @@ export type UseMutationMethod<Paths extends Record<string, Record<HttpMethod, {}
Path extends PathsWithMethod<Paths, Method>,
Init extends MaybeOptionalInit<Paths[Path], Method>,
Response extends Required<FetchResponse<Paths[Path][Method], Init, Media>>, // note: Required is used to avoid repeating NonNullable in UseQuery types
Options extends Omit<UseMutationOptions<Response["data"], Response["error"], Init>, "mutationKey" | "mutationFn">,
TOnMutateResult = unknown,
>(
method: Method,
url: Path,
options?: Options,
options?: Omit<
UseMutationOptions<Response["data"], Response["error"], Init, TOnMutateResult>,
"mutationKey" | "mutationFn"
>,
queryClient?: QueryClient,
) => UseMutationResult<Response["data"], Response["error"], Init>;
) => UseMutationResult<Response["data"], Response["error"], Init, TOnMutateResult>;

export interface OpenapiQueryClient<Paths extends {}, Media extends MediaType = MediaType> {
queryOptions: QueryOptionsFunction<Paths, Media>;
Expand Down
24 changes: 24 additions & 0 deletions packages/openapi-react-query/test/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,30 @@ describe("client", () => {

await waitFor(() => rendered.findByText("data: Hello, status: success"));
});

it("should type mutate results properly", async () => {
const fetchClient = createFetchClient<paths>({ baseUrl });
const client = createClient(fetchClient);

const onMutateReturnValue = { someArray: [1, 2, 3], someString: "abc" };
type expectedOnMutateResultType = typeof onMutateReturnValue | undefined;

const result = renderHook(
() =>
client.useMutation("put", "/comment", {
onMutate: () => onMutateReturnValue,
onError: (err, _, onMutateResult, context) => {
assertType<expectedOnMutateResultType>(onMutateResult);
},
onSettled: (_data, _error, _variables, onMutateResult, context) => {
assertType<expectedOnMutateResultType>(onMutateResult);
},
}),
{ wrapper },
);

assertType<expectedOnMutateResultType>(result.result.current.context);
});
});

describe("mutateAsync", () => {
Expand Down
Loading