Skip to content

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
@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