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

Split "encodeParams" into "encodePathParams" and "encodeQueryParams" #4595

Merged
merged 2 commits into from
Sep 2, 2024

Conversation

bayasdev
Copy link
Contributor

@bayasdev bayasdev commented Sep 2, 2024

Overview

This PR splits the encodeParams option introduced in #4568 into encodePathParams and encodeQueryParams so users can customize the code generator output according to their specific needs and existing backend infrastructure.

When encodeQueryParams is enabled, the query parameters are conditionally encoded to prevent sending undefined parameters to the endpoint. This is particularly useful when certain query parameters are optional and should not be included in the request if they are not defined.

Example output with encodeQueryParams

const injectedRtkApi = api.injectEndpoints({
  endpoints: (build) => ({
    findPetsByStatus: build.query<
      FindPetsByStatusApiResponse,
      FindPetsByStatusApiArg
    >({
      query: (queryArg) => ({
        url: `/pet/findByStatus`,
        params: {
          status: queryArg.status
            ? encodeURIComponent(String(queryArg.status))
            : undefined,
        },
      }),
    }),
  }),
  overrideExisting: false,
});

Copy link

codesandbox bot commented Sep 2, 2024

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

Copy link

netlify bot commented Sep 2, 2024

Deploy Preview for redux-starter-kit-docs ready!

Name Link
🔨 Latest commit 9d50bc7
🔍 Latest deploy log https://app.netlify.com/sites/redux-starter-kit-docs/deploys/66d6157754ed1200087b8633
😎 Deploy Preview https://deploy-preview-4595--redux-starter-kit-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

codesandbox-ci bot commented Sep 2, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 9d50bc7:

Sandbox Source
@examples-query-react/basic Configuration
@examples-query-react/advanced Configuration
@examples-action-listener/counter Configuration
rtk-esm-cra Configuration

@EskiMojo14
Copy link
Collaborator

removing the option completely would be a breaking change, requiring a major release - would it be possible to continue supporting the previous option while adding both the new ones?

@markerikson
Copy link
Collaborator

@EskiMojo14 I just merged the other PR on Friday and put it out in 2.0.0-alpha.0, so it's brand new and there's no back compat to worry about.

@bayasdev
Copy link
Contributor Author

bayasdev commented Sep 2, 2024

@EskiMojo14 I just merged the other PR on Friday and put it out in 2.0.0-alpha.0, so it's brand new and there's no back compat to worry about.

I was testing 2.0.0-alpha.1 in my app and encountered some edge cases where I didn't want to apply encodeURIComponent to the query parameters. Additionally, some optional query parameters were being sent as argName="undefined" due to the string conversion 😅

@markerikson markerikson merged commit c3b0b0f into reduxjs:master Sep 2, 2024
17 checks passed
@markerikson
Copy link
Collaborator

@bayasdev awright, https://github.com/reduxjs/redux-toolkit/releases/tag/%40rtk-query%2Fcodegen-openapi%402.0.0-alpha.2 is out!

@bayasdev
Copy link
Contributor Author

bayasdev commented Sep 2, 2024

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.

3 participants