Skip to content

Commit

Permalink
Merge pull request #41 from lifeomic/mutation-axios-params
Browse files Browse the repository at this point in the history
feat: support arbitrary axios parameters in useAPIMutation
  • Loading branch information
swain authored Nov 27, 2023
2 parents df082b2 + 7bb6fa1 commit 9d2de70
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
41 changes: 41 additions & 0 deletions src/hooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,47 @@ describe('useAPIMutation', () => {
);
});
});

test('axios parameters works', async () => {
const networkPost = jest.fn().mockReturnValue({
status: 200,
data: { message: 'another-test-message' },
});
network.mock('POST /items', networkPost);

const screen = render(() => {
const mutation = useAPIMutation('POST /items', {
axios: {
headers: {
'test-header': 'test-value',
},
},
});
return (
<button
onClick={() => {
mutation.mutate({ message: 'something' });
}}
>
Press Me
</button>
);
});

TestingLibrary.fireEvent.click(screen.getByText('Press Me'));

await TestingLibrary.waitFor(() => {
expect(networkPost).toHaveBeenCalledTimes(1);
expect(networkPost).toHaveBeenCalledWith(
expect.objectContaining({
body: { message: 'something' },
headers: expect.objectContaining({
'test-header': 'test-value',
}),
}),
);
});
});
});

describe('useCombinedAPIQueries', () => {
Expand Down
5 changes: 4 additions & 1 deletion src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ export const createAPIHooks = <Endpoints extends RoughEndpoints>({
const client = useClient();

return useMutation(
(payload) => client.request(route, payload).then((res) => res.data),
(payload) =>
client
.request(route, payload, options?.axios)
.then((res) => res.data),
options,
);
},
Expand Down
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ export type APIQueryHooks<Endpoints extends RoughEndpoints> = {
Endpoints[Route]['Response'],
unknown,
RequestPayloadOf<Endpoints, Route>
>,
> & {
axios?: AxiosRequestConfig;
},
) => UseMutationResult<
Endpoints[Route]['Response'],
unknown,
Expand Down

0 comments on commit 9d2de70

Please sign in to comment.