Skip to content
New issue

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

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

Already on GitHub? # to your account

Simplify useCustomMutation implementation #2034

Merged
merged 3 commits into from
Oct 1, 2024

Conversation

kevinxh
Copy link
Collaborator

@kevinxh kevinxh commented Sep 27, 2024

This is a follow up for the previous PR #2030

This PR simplifies the implementation structure to remove the if/else block that contains some duplicated code:

Before

// this is a higher order function
const callCustomEndpointWithAuth = () => {...}

// this is NOT a higher order function, this creates some inconsistencies in the code
const callCustomEndpointWithBody = () => {...}

if (!apiOptions?.options?.body) {
        return useReactQueryMutation(
            callCustomEndpointWithBody,  
            mutationOptions
        ) as UseMutationResult<TData, TError, MutationVariables>
    } else {
        return useReactQueryMutation<TData, TError, undefined, unknown>(
            callCustomEndpointWithAuth(apiOptions),
            mutationOptions
        ) as UseMutationResult<TData, TError, MutationVariables>
    }

After

const callCustomEndpointWithAuth = () => {...}

        return useReactQueryMutation(
            callCustomEndpointWithAuth(),
            mutationOptions
        ) as UseMutationResult<TData, TError, MutationVariables>

@kevinxh kevinxh requested a review from a team as a code owner September 27, 2024 19:54
return async (): Promise<any> => {
const clientConfig = options.clientConfig || {}
const createMutationFnWithAuth = (): MutationFunction<TData, TMutationVariables> => {
return async (args): Promise<TData> => {
Copy link
Collaborator Author

@kevinxh kevinxh Sep 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The core change is in this line. We update the mutation function to take arguments.

@@ -105,7 +105,7 @@ const NOCK_DELAY = 50
/** Mocks DELETE, PATCH, POST, and PUT so we don't have to look up which verb an endpoint uses. */
export const mockMutationEndpoints = (
matchingPath: string,
response: string | object | undefined,
response: string | object | undefined | ((uri: string, requestBody: any) => object),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is to support the test case where i need to verify the http header is sent.

@kevinxh kevinxh requested a review from unandyala September 27, 2024 20:01
Copy link
Collaborator

@unandyala unandyala left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@bfeister bfeister enabled auto-merge October 1, 2024 18:36
@bfeister bfeister merged commit d4f4bee into develop Oct 1, 2024
25 of 29 checks passed
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants