Skip to content

Commit d5062c8

Browse files
authored
Merge pull request #412 from OpenAPI-Qraft/fix/parameters-and-body-payload
feat(react-client): fix a mutation-related destructuring runtime error when `body` is missing
2 parents cfe60e0 + e22e6ce commit d5062c8

3 files changed

Lines changed: 44 additions & 4 deletions

File tree

.changeset/weak-knives-pay.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@openapi-qraft/react': patch
3+
---
4+
5+
Fix a mutation-related destructuring runtime error when `body` is missing.

packages/react-client/src/callbacks/useMutation.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ export const useMutation: <
7171
.then(requestFnResponseResolver, requestFnResponseRejecter);
7272
}
7373
: function (parametersAndBodyPayload) {
74-
const { body, ...parameters } = parametersAndBodyPayload as {
75-
body: unknown;
76-
};
74+
const { body, ...parameters } = parametersAndBodyPayload ?? {};
7775

7876
return qraftOptions
7977
.requestFn(schema, {

packages/react-client/src/tests/qraftAPIClient.test.tsx

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1281,8 +1281,11 @@ describe('Qraft uses Mutations', () => {
12811281
mutateWithEmptyBody,
12821282
} = result.current;
12831283

1284-
expect(mutateNoArgsWithVoidParameters.data).toBeUndefined();
1284+
expect(mutateNoArgsWithVoidParameters.isSuccess).toBeTruthy();
1285+
expect(mutateNoArgsWithVoidParameters.data).toEqual({});
1286+
expect(mutateNoArgsWithEmptyParameters.isSuccess).toBeTruthy();
12851287
expect(mutateNoArgsWithEmptyParameters.data).toEqual({});
1288+
expect(mutateWithPredefinedParameters.isSuccess).toBeTruthy();
12861289
expect(mutateWithPredefinedParameters.data).toEqual({
12871290
query: { all: 'true' },
12881291
});
@@ -1913,6 +1916,40 @@ describe('Qraft uses getMutationCache', () => {
19131916
});
19141917
});
19151918

1919+
it('supports getMutationCache().find() with status success and useMutation options mutationKey', async () => {
1920+
const { qraft, queryClient } = createClient();
1921+
const mutationKey = qraft.files.deleteFiles.getMutationKey();
1922+
1923+
const { result: mutationResult } = renderHook(
1924+
() =>
1925+
qraft.files.deleteFiles.useMutation(undefined, {
1926+
gcTime: Infinity,
1927+
mutationKey,
1928+
}),
1929+
{
1930+
wrapper: (props) => (
1931+
<Providers {...props} queryClient={queryClient} />
1932+
),
1933+
}
1934+
);
1935+
1936+
act(() => {
1937+
mutationResult.current.mutate();
1938+
});
1939+
1940+
await waitFor(() => {
1941+
expect(mutationResult.current.status).toEqual('success');
1942+
});
1943+
1944+
const foundMutation = queryClient.getMutationCache().find({
1945+
mutationKey,
1946+
status: 'success',
1947+
});
1948+
1949+
expect(foundMutation).toBeDefined();
1950+
expect(foundMutation?.state.status).toEqual('success');
1951+
});
1952+
19161953
it('supports getMutationCache().find() with partial not exact parameters', async () => {
19171954
const { qraft, queryClient } = createClient();
19181955

0 commit comments

Comments
 (0)