Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support arbitrary axios parameters in useAPIMutation #41

Merged
merged 1 commit into from
Nov 27, 2023
Merged
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
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
Loading