Skip to content

Commit

Permalink
[commerce-sdk-react] Consume parameter keys (@W-15676839@) (#1823)
Browse files Browse the repository at this point in the history
* Try consuming parameter keys from isomorphic lib

* Update paramKeys.ts

* Revert "Update paramKeys.ts"

This reverts commit 5fde81c.

* Update paramKeys.ts

* Start deleting paramKeys.ts files

* Add a util method pickValidParams

* Some refactoring to use pickValidParams

* Test pickValidParams

* Further refactoring to use pickValidParams

* Remove todo

* Update version of the isomorphic lib

* Refactor ShopperContexts

* Simplify code a bit

* Refactor ShopperCustomers

* Refactor ShopperExperience

* Refactor ShopperGiftCertificates

* Refactor ShopperLogin

* Refactor ShopperOrders

* Refactor ShopperPromotions

* Refactor ShopperSearch

* Refactor ShopperSeo

* Refactor ShopperStores
  • Loading branch information
vmarta authored Jun 19, 2024
1 parent 6bedeb8 commit 3b85664
Show file tree
Hide file tree
Showing 40 changed files with 359 additions and 642 deletions.
8 changes: 4 additions & 4 deletions packages/commerce-sdk-react/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/commerce-sdk-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"version": "node ./scripts/version.js"
},
"dependencies": {
"commerce-sdk-isomorphic": "^2.0.0",
"commerce-sdk-isomorphic": "^2.1.0-dev.0",
"js-cookie": "^3.0.1",
"jwt-decode": "^4.0.0"
},
Expand Down
27 changes: 0 additions & 27 deletions packages/commerce-sdk-react/src/hooks/ShopperBaskets/paramKeys.ts

This file was deleted.

34 changes: 12 additions & 22 deletions packages/commerce-sdk-react/src/hooks/ShopperBaskets/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import {UseQueryResult} from '@tanstack/react-query'
import {ApiClients, ApiQueryOptions, Argument, DataType, NullableParameters} from '../types'
import useCommerceApi from '../useCommerceApi'
import {useQuery} from '../useQuery'
import {getCustomKeys, mergeOptions, omitNullableParameters, pick} from '../utils'
import {mergeOptions, omitNullableParameters, pickValidParams} from '../utils'
import * as queryKeyHelpers from './queryKeyHelpers'
import paramKeysMap from './paramKeys'
import {ShopperBaskets} from 'commerce-sdk-isomorphic'

type Client = ApiClients['shopperBaskets']

Expand All @@ -33,14 +33,12 @@ export const useBasket = (
type Data = DataType<Client['getBasket']>
const {shopperBaskets: client} = useCommerceApi()
const methodName = 'getBasket'
const requiredParameters = ['organizationId', 'basketId', 'siteId'] as const
const requiredParameters = ShopperBaskets.paramKeys[`${methodName}Required`]

// 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 parameters = pick(netOptions.parameters, paramKeys)
const parameters = pickValidParams(netOptions.parameters, ShopperBaskets.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)
Expand Down Expand Up @@ -72,14 +70,12 @@ export const usePaymentMethodsForBasket = (
type Data = DataType<Client['getPaymentMethodsForBasket']>
const {shopperBaskets: client} = useCommerceApi()
const methodName = 'getPaymentMethodsForBasket'
const requiredParameters = ['organizationId', 'basketId', 'siteId'] as const
const requiredParameters = ShopperBaskets.paramKeys[`${methodName}Required`]

// 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 parameters = pick(netOptions.parameters, paramKeys)
const parameters = pickValidParams(netOptions.parameters, ShopperBaskets.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)
Expand Down Expand Up @@ -111,14 +107,12 @@ export const usePriceBooksForBasket = (
type Data = DataType<Client['getPriceBooksForBasket']>
const {shopperBaskets: client} = useCommerceApi()
const methodName = 'getPriceBooksForBasket'
const requiredParameters = ['organizationId', 'basketId', 'siteId'] as const
const requiredParameters = ShopperBaskets.paramKeys[`${methodName}Required`]

// 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 parameters = pick(netOptions.parameters, paramKeys)
const parameters = pickValidParams(netOptions.parameters, ShopperBaskets.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)
Expand Down Expand Up @@ -150,14 +144,12 @@ export const useShippingMethodsForShipment = (
type Data = DataType<Client['getShippingMethodsForShipment']>
const {shopperBaskets: client} = useCommerceApi()
const methodName = 'getShippingMethodsForShipment'
const requiredParameters = ['organizationId', 'basketId', 'shipmentId', 'siteId'] as const
const requiredParameters = ShopperBaskets.paramKeys[`${methodName}Required`]

// 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 parameters = pick(netOptions.parameters, paramKeys)
const parameters = pickValidParams(netOptions.parameters, ShopperBaskets.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)
Expand Down Expand Up @@ -189,14 +181,12 @@ export const useTaxesFromBasket = (
type Data = DataType<Client['getTaxesFromBasket']>
const {shopperBaskets: client} = useCommerceApi()
const methodName = 'getTaxesFromBasket'
const requiredParameters = ['organizationId', 'basketId', 'siteId'] as const
const requiredParameters = ShopperBaskets.paramKeys[`${methodName}Required`]

// 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 parameters = pick(netOptions.parameters, paramKeys)
const parameters = pickValidParams(netOptions.parameters, ShopperBaskets.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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import type {ShopperBaskets} from 'commerce-sdk-isomorphic'
import {ShopperBaskets} from 'commerce-sdk-isomorphic'
import {Argument, ExcludeTail} from '../types'
import {getCustomKeys, pick} from '../utils'
import paramKeysMap from '../ShopperBaskets/paramKeys'
import {pickValidParams} from '../utils'

// We must use a client with no parameters in order to have required/optional match the API spec
type Client = ShopperBaskets<{shortCode: string}>
Expand Down Expand Up @@ -79,8 +78,10 @@ export const getBasket: QueryKeyHelper<'getBasket'> = {
params.basketId
],
queryKey: (params: Params<'getBasket'>) => {
const paramKeys = [...paramKeysMap['getBasket'], ...getCustomKeys(params)]
return [...getBasket.path(params), pick(params, paramKeys)]
return [
...getBasket.path(params),
pickValidParams(params, ShopperBaskets.paramKeys.getBasket)
]
}
}

Expand All @@ -94,8 +95,10 @@ export const getPaymentMethodsForBasket: QueryKeyHelper<'getPaymentMethodsForBas
'/payment-methods'
],
queryKey: (params: Params<'getPaymentMethodsForBasket'>) => {
const paramKeys = [...paramKeysMap['getPaymentMethodsForBasket'], ...getCustomKeys(params)]
return [...getPaymentMethodsForBasket.path(params), pick(params, paramKeys)]
return [
...getPaymentMethodsForBasket.path(params),
pickValidParams(params, ShopperBaskets.paramKeys.getPaymentMethodsForBasket)
]
}
}

Expand All @@ -109,9 +112,10 @@ export const getPriceBooksForBasket: QueryKeyHelper<'getPriceBooksForBasket'> =
'/price-books'
],
queryKey: (params: Params<'getPriceBooksForBasket'>) => {
const paramKeys = [...paramKeysMap['getPriceBooksForBasket'], ...getCustomKeys(params)]

return [...getPriceBooksForBasket.path(params), pick(params, paramKeys)]
return [
...getPriceBooksForBasket.path(params),
pickValidParams(params, ShopperBaskets.paramKeys.getPriceBooksForBasket)
]
}
}

Expand All @@ -127,12 +131,10 @@ export const getShippingMethodsForShipment: QueryKeyHelper<'getShippingMethodsFo
'/shipping-methods'
],
queryKey: (params: Params<'getShippingMethodsForShipment'>) => {
const paramKeys = [
...paramKeysMap['getShippingMethodsForShipment'],
...getCustomKeys(params)
return [
...getShippingMethodsForShipment.path(params),
pickValidParams(params, ShopperBaskets.paramKeys.getShippingMethodsForShipment)
]

return [...getShippingMethodsForShipment.path(params), pick(params, paramKeys)]
}
}

Expand All @@ -146,7 +148,9 @@ export const getTaxesFromBasket: QueryKeyHelper<'getTaxesFromBasket'> = {
'/taxes'
],
queryKey: (params: Params<'getTaxesFromBasket'>) => {
const paramKeys = [...paramKeysMap['getTaxesFromBasket'], ...getCustomKeys(params)]
return [...getTaxesFromBasket.path(params), pick(params, paramKeys)]
return [
...getTaxesFromBasket.path(params),
pickValidParams(params, ShopperBaskets.paramKeys.getTaxesFromBasket)
]
}
}
12 changes: 0 additions & 12 deletions packages/commerce-sdk-react/src/hooks/ShopperContexts/paramKeys.ts

This file was deleted.

10 changes: 4 additions & 6 deletions packages/commerce-sdk-react/src/hooks/ShopperContexts/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import {UseQueryResult} from '@tanstack/react-query'
import {ApiClients, ApiQueryOptions, Argument, DataType, NullableParameters} from '../types'
import useCommerceApi from '../useCommerceApi'
import {useQuery} from '../useQuery'
import {getCustomKeys, mergeOptions, omitNullableParameters, pick} from '../utils'
import {mergeOptions, omitNullableParameters, pickValidParams} from '../utils'
import * as queryKeyHelpers from './queryKeyHelpers'
import paramKeysMap from './paramKeys'
import {ShopperContexts} from 'commerce-sdk-isomorphic'

type Client = ApiClients['shopperContexts']

Expand All @@ -33,15 +33,13 @@ export const useShopperContext = (
type Data = DataType<Client['getShopperContext']>
const {shopperContexts: client} = useCommerceApi()
const methodName = 'getShopperContext'
const requiredParameters = ['organizationId', 'usid'] as const
const requiredParameters = ShopperContexts.paramKeys[`${methodName}Required`]

// 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 parameters = pick(netOptions.parameters, paramKeys)
const parameters = pickValidParams(netOptions.parameters, ShopperContexts.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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import type {ShopperContexts} from 'commerce-sdk-isomorphic'
import {ShopperContexts} from 'commerce-sdk-isomorphic'
import {Argument, ExcludeTail} from '../types'
import {getCustomKeys, pick} from '../utils'
import paramKeysMap from './paramKeys'
import {pickValidParams} from '../utils'
// We must use a client with no parameters in order to have required/optional match the API spec
type Client = ShopperContexts<{shortCode: string}>
type Params<T extends keyof QueryKeys> = Partial<Argument<Client[T]>['parameters']>
Expand Down Expand Up @@ -40,7 +39,9 @@ export const getShopperContext: QueryKeyHelper<'getShopperContext'> = {
params.usid
],
queryKey: (params: Params<'getShopperContext'>) => {
const paramKeys = [...paramKeysMap['getShopperContext'], ...getCustomKeys(params)]
return [...getShopperContext.path(params), pick(params, paramKeys)]
return [
...getShopperContext.path(params),
pickValidParams(params, ShopperContexts.paramKeys.getShopperContext)
]
}
}

This file was deleted.

Loading

0 comments on commit 3b85664

Please sign in to comment.