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

[commerce-sdk-react] Consume parameter keys (@W-15676839@) #1823

Merged
merged 26 commits into from
Jun 19, 2024

Conversation

vmarta
Copy link
Contributor

@vmarta vmarta commented Jun 10, 2024

This PR aims to refactor commerce-sdk-react code such that it no longer hardcodes the list of parameter keys. It will use the exported parameter keys from the underlying commerce-sdk-isomorphic instead.

For early review/feedback, I've made changes to only the ShopperProducts and ShopperBaskets:

  • removed our paramKeys.ts files
  • used the exported parameter keys (e.g. ShopperBaskets.paramKeys['getBasket'])
  • leave the tests as they are

Todos:

  • The tests are currently failing because they're not aware of the new changes to the isomorphic lib yet. I plan to release a dev version of it.
  • Work on the rest of the APIs.
    • ShopperBaskets/
    • ShopperContexts/
    • ShopperCustomers/
    • ShopperExperience/
    • ShopperGiftCertificates/
    • ShopperLogin/
    • ShopperOrders/
    • ShopperProducts/
    • ShopperPromotions/
    • ShopperSearch/
    • ShopperSeo/
    • ShopperStores/

Types of Changes

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Documentation update
  • Breaking change (could cause existing functionality to not work as expected)
  • Other changes (non-breaking changes that does not fit any of the above)

Breaking changes include:

  • Removing a public function or component or prop
  • Adding a required argument to a function
  • Changing the data type of a function parameter or return value
  • Adding a new peer dependency to package.json

Changes

  • (change1)

How to Test-Drive This PR

If you want to test drive it now, you'll need to build and symlink the isomorphic lib. Follow the instruction here, since it's still mostly relevant: SalesforceCommerceCloud/commerce-sdk-isomorphic#158

You no longer need to symlink the isomorphic lib. You can now run npm ci at the root, before you test-drive the PR.

Since this PR is essentially a refactoring, we can test-drive it by smoke testing the retail-react-app site.

We can be confident in the code changes because of a few layers in place:

  • The type validation is really good at detecting errors
  • Our unit tests

Checklists

General

  • Changes are covered by test cases
  • CHANGELOG.md updated with a short description of changes (not required for documentation updates)

Accessibility Compliance

You must check off all items in one of the follow two lists:

  • There are no changes to UI

or...

Localization

  • Changes include a UI text update in the Retail React App (which requires translation)


export default {
getBasket,
getPaymentMethodsForBasket,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kevinxh The ticket asks us to consider whether the change will be a breaking one or not? What specifically made us to think that?

So far I have not seen anything concerning. For example, this file deletion should be ok, right? These param keys were not publicly exported.

Copy link
Collaborator

@kevinxh kevinxh Jun 11, 2024

Choose a reason for hiding this comment

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

I think it's okay to remove this file.

We specifically exports the public interface from the index file. Anything not exported from that file is considered private.


// Parameters can be set in `apiOptions` or `client.clientConfig`;
// we must merge them in order to generate the correct query key.
const netOptions = omitNullableParameters(mergeOptions(client, apiOptions))
// get param keys for the api from netOptions
const paramKeys = [...paramKeysMap[methodName], ...getCustomKeys(netOptions.parameters)]
const paramKeys = [
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am wondering if we can create a util that returns the keys including custom ones. 🤔 . With this we can optimise a few places (here and queryKeyHelper.queryKeys)

const paramKeys = getParamKeys(medthodName, params)


// util.js
const getParamKeys = (medthodName, params)  => {
   // add logic to get the right shopperClass here
   return [
     ....Shopper*[medthodName],
     ...getCustomKeys(params)
   ]

}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea @alexvuong 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@alexvuong please take a look at the latest changes. Inspired by your idea, I took it further and created a util method that picks the valid parameters. That's what the param keys are originally used for: to pick the valid parameters.

Comment on lines +47 to +54
const parameters = pickValidParams(netOptions.parameters, ShopperStores.paramKeys[methodName])
const queryKey = queryKeyHelpers[methodName].queryKey(netOptions.parameters)
// We don't use `netOptions` here because we manipulate the options in `useQuery`.
const method = async (options: Options) => await client[methodName](options)

// For some reason, if we don't explicitly set these generic parameters, the inferred type for
// `Data` sometimes, but not always, includes `Response`, which is incorrect. I don't know why.
return useQuery<Client, Options, Data>(netOptions, queryOptions, {
return useQuery<Client, Options, Data>({...netOptions, parameters}, queryOptions, {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change is different than the other ones. Because in this case, we forgot to filter out the invalid parameters, which is what the other files have done.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Aside: this is another reason for having templates to generate the code. With it, we can avoid inconsistency/bug like this.

@vmarta vmarta marked this pull request as ready for review June 14, 2024 21:33
@vmarta vmarta requested a review from a team as a code owner June 14, 2024 21:33
@@ -40,7 +40,7 @@
"version": "node ./scripts/version.js"
},
"dependencies": {
"commerce-sdk-isomorphic": "^2.0.0",
"commerce-sdk-isomorphic": "^2.1.0-dev.0",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In another PR, I'll update this to v2.1.0.

@vmarta vmarta enabled auto-merge (squash) June 19, 2024 07:10
@vmarta vmarta merged commit 3b85664 into develop Jun 19, 2024
28 checks passed
@vmarta vmarta deleted the consume-parameter-keys branch June 19, 2024 09:16
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

4 participants