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(query): allow body type customisation through mutator for mutations #319

Merged
merged 5 commits into from
Mar 30, 2022

Conversation

CPatchane
Copy link
Contributor

@CPatchane CPatchane commented Feb 13, 2022

Status

HOLD

Related PRs

Wait merging PR #318 before merging this one.

Description

Look only at the last commit, the previous ones are from #318.
With those two PRs combined, we're now able to customise via the mutator the return type (see PR #318) (in my example, a server returning snake cased data that we want to camel case in our TS code) but also the body type (this PR) expecting by the operation function (since that the server is expecting snake cased keys, the mutator can handle that case change systematically) through a dedicated and optional type named BodyType.

How it works

In the mutator, we can export a type named BodyType like following (similar to what we can do for ErrorType):

// ./custom-mutator.ts
export type BodyType<BodyData> = CamelCase<BodyData>

Then, it will generated for the mutation:

export const postMyDemo = (id: string, postMyDemoBody: BodyType<PostMyDemoBody>) => {
  return apiClient<void>({
    url: `/my/demo`,
    method: "post",
    data: postMyDemoBody,
  })
}

export type PostMyDemoMutationResult = NonNullable<AsyncReturnType<typeof postMyDemo>>
export type PostMyDemoMutationBody = BodyType<PostMyDemoBody>
export type PostMyDemoMutationError = unknown

export const usePostMyDemo = <TError = unknown,  TContext = unknown>(
options?: {
  mutation?: UseMutationOptions<
    AsyncReturnType<typeof postMyDemo>,
    TError,
    {
      id: string
      data: BodyType<PostMyDemoBody>
    },
    TContext
  >
}) => {
  const { mutation: mutationOptions } = options || {}

  const mutationFn: MutationFunction<
    AsyncReturnType<typeof postMyDemo>,
    {
      id: string
      data: BodyType<PostMyDemoBody>
    }
  > = (props) => {
    const { id, data } = props || {}

    return postMyDemo(id, data)
  }

  return useMutation<
    AsyncReturnType<typeof postMyDemo>,
    TError,
    {
      id: string
      data: BodyType<PostMyDemoBody>
    },
    TContext
  >(mutationFn, mutationOptions)
}

Wdyt?
It might not be related to this PR but I personally think we might need more tests (with a snapshotting system?), at least to be able to see when a fix/feature is added how this is impacting the different scenarios because I think that TS won't be able catch everything imho.

@vercel
Copy link

vercel bot commented Feb 13, 2022

Someone is attempting to deploy a commit to a Personal Account owned by @anymaniax on Vercel.

@anymaniax first needs to authorize it.

@melloware
Copy link
Collaborator

Again since this is adding new code I think this is another good improvement.

melloware
melloware previously approved these changes Feb 13, 2022
@CPatchane CPatchane changed the title [PROPOSAL] feat(query): allow body type customisation through mutator for mutations feat(query): allow body type customisation through mutator for mutations Feb 28, 2022
@anymaniax
Copy link
Owner

What is the status of this one? Since the other one is merged

@CPatchane
Copy link
Contributor Author

I'll rework that one to take into account the changes from the other one, I'll ping you back when it's done :)

@anymaniax
Copy link
Owner

ok thanks for the great work on this again

@CPatchane CPatchane force-pushed the mutation_body_type branch 3 times, most recently from 92ae4b4 to 93e849d Compare March 17, 2022 15:31
@CPatchane
Copy link
Contributor Author

@anymaniax I had to update the samples due to the last PR too.
I did the changes to handle BodyType, but it will work only for queries with custom clients. I don't think we need it for the other clients but I might be wrong. Wdyt?

@CPatchane
Copy link
Contributor Author

you don't handle the case where you only have the bodyTypName no?

Good catch, I just pushed a fix :)

@CPatchane
Copy link
Contributor Author

I updated the samples and add a new generated test named "custom-client" allowing to test the BodyType usage. Let me know if it's good :)

Copy link
Owner

@anymaniax anymaniax left a comment

Choose a reason for hiding this comment

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

Thanks 🎉! Will do a release soon

@anymaniax anymaniax merged commit 9856d94 into anymaniax:master Mar 30, 2022
@CPatchane CPatchane deleted the mutation_body_type branch March 31, 2022 08:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants