diff --git a/packages/commerce-sdk-react/package-lock.json b/packages/commerce-sdk-react/package-lock.json index f08cabdb91..d38bc8fffe 100644 --- a/packages/commerce-sdk-react/package-lock.json +++ b/packages/commerce-sdk-react/package-lock.json @@ -9,7 +9,7 @@ "version": "2.0.0-dev", "license": "See license in LICENSE", "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" }, @@ -861,9 +861,9 @@ "dev": true }, "node_modules/commerce-sdk-isomorphic": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/commerce-sdk-isomorphic/-/commerce-sdk-isomorphic-2.0.0.tgz", - "integrity": "sha512-9359TxC1P6olgWhv4py7rgMprBJGQm8+NCXjzxO0C3javynqGX8ccVe7FmHKpjTe+rvI/PEp9doy/fvPtHZEvQ==", + "version": "2.1.0-dev.0", + "resolved": "https://registry.npmjs.org/commerce-sdk-isomorphic/-/commerce-sdk-isomorphic-2.1.0-dev.0.tgz", + "integrity": "sha512-ptlIauuTBjaVG7BbiZsVi34Nkcm8Lt22DnInFEuuXMRoNsVuk5ZsVHHdEo5MQkTP9BFiaKmDsewUPTqVH3LM0w==", "dependencies": { "nanoid": "^3.3.4", "node-fetch": "2.6.12", diff --git a/packages/commerce-sdk-react/package.json b/packages/commerce-sdk-react/package.json index c6bae98df9..32b5cd1924 100644 --- a/packages/commerce-sdk-react/package.json +++ b/packages/commerce-sdk-react/package.json @@ -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" }, diff --git a/packages/commerce-sdk-react/src/hooks/ShopperBaskets/paramKeys.ts b/packages/commerce-sdk-react/src/hooks/ShopperBaskets/paramKeys.ts deleted file mode 100644 index 7e9d58982d..0000000000 --- a/packages/commerce-sdk-react/src/hooks/ShopperBaskets/paramKeys.ts +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (c) 2024, Salesforce, Inc. - * All rights reserved. - * 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 - */ - -const getBasket = ['organizationId', 'basketId', 'siteId', 'locale'] as const - -const getPaymentMethodsForBasket = ['organizationId', 'basketId', 'siteId', 'locale'] as const -const getPriceBooksForBasket = ['organizationId', 'basketId', 'siteId'] as const -const getShippingMethodsForShipment = [ - 'organizationId', - 'basketId', - 'shipmentId', - 'siteId', - 'locale' -] as const -const getTaxesFromBasket = ['organizationId', 'basketId', 'siteId'] as const - -export default { - getBasket, - getPaymentMethodsForBasket, - getPriceBooksForBasket, - getShippingMethodsForShipment, - getTaxesFromBasket -} diff --git a/packages/commerce-sdk-react/src/hooks/ShopperBaskets/query.ts b/packages/commerce-sdk-react/src/hooks/ShopperBaskets/query.ts index 70e0e6f951..72e6c1a5bf 100644 --- a/packages/commerce-sdk-react/src/hooks/ShopperBaskets/query.ts +++ b/packages/commerce-sdk-react/src/hooks/ShopperBaskets/query.ts @@ -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'] @@ -33,14 +33,12 @@ export const useBasket = ( type Data = DataType 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) @@ -72,14 +70,12 @@ export const usePaymentMethodsForBasket = ( type Data = DataType 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) @@ -111,14 +107,12 @@ export const usePriceBooksForBasket = ( type Data = DataType 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) @@ -150,14 +144,12 @@ export const useShippingMethodsForShipment = ( type Data = DataType 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) @@ -189,14 +181,12 @@ export const useTaxesFromBasket = ( type Data = DataType 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) diff --git a/packages/commerce-sdk-react/src/hooks/ShopperBaskets/queryKeyHelpers.ts b/packages/commerce-sdk-react/src/hooks/ShopperBaskets/queryKeyHelpers.ts index f4c079d263..38c89da701 100644 --- a/packages/commerce-sdk-react/src/hooks/ShopperBaskets/queryKeyHelpers.ts +++ b/packages/commerce-sdk-react/src/hooks/ShopperBaskets/queryKeyHelpers.ts @@ -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}> @@ -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) + ] } } @@ -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) + ] } } @@ -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) + ] } } @@ -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)] } } @@ -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) + ] } } diff --git a/packages/commerce-sdk-react/src/hooks/ShopperContexts/paramKeys.ts b/packages/commerce-sdk-react/src/hooks/ShopperContexts/paramKeys.ts deleted file mode 100644 index 4af9f4d13b..0000000000 --- a/packages/commerce-sdk-react/src/hooks/ShopperContexts/paramKeys.ts +++ /dev/null @@ -1,12 +0,0 @@ -/* - * Copyright (c) 2024, Salesforce, Inc. - * All rights reserved. - * 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 - */ - -const getShopperContext = ['organizationId', 'usid'] as const - -export default { - getShopperContext -} diff --git a/packages/commerce-sdk-react/src/hooks/ShopperContexts/query.ts b/packages/commerce-sdk-react/src/hooks/ShopperContexts/query.ts index b29d7fbfa0..f7a0ee945d 100644 --- a/packages/commerce-sdk-react/src/hooks/ShopperContexts/query.ts +++ b/packages/commerce-sdk-react/src/hooks/ShopperContexts/query.ts @@ -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'] @@ -33,15 +33,13 @@ export const useShopperContext = ( type Data = DataType 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) diff --git a/packages/commerce-sdk-react/src/hooks/ShopperContexts/queryKeyHelpers.ts b/packages/commerce-sdk-react/src/hooks/ShopperContexts/queryKeyHelpers.ts index ddf1877304..809edb626f 100644 --- a/packages/commerce-sdk-react/src/hooks/ShopperContexts/queryKeyHelpers.ts +++ b/packages/commerce-sdk-react/src/hooks/ShopperContexts/queryKeyHelpers.ts @@ -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 = Partial['parameters']> @@ -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) + ] } } diff --git a/packages/commerce-sdk-react/src/hooks/ShopperCustomers/paramKeys.ts b/packages/commerce-sdk-react/src/hooks/ShopperCustomers/paramKeys.ts deleted file mode 100644 index 5eb19b99c7..0000000000 --- a/packages/commerce-sdk-react/src/hooks/ShopperCustomers/paramKeys.ts +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (c) 2024, Salesforce, Inc. - * All rights reserved. - * 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 - */ -export const getCustomer = ['organizationId', 'customerId', 'siteId'] as const -export const getCustomerAddress = ['organizationId', 'customerId', 'addressName', 'siteId'] as const -export const getCustomerBaskets = ['organizationId', 'customerId', 'siteId'] as const -export const getCustomerOrders = [ - 'organizationId', - 'customerId', - 'crossSites', - 'from', - 'until', - 'status', - 'siteId', - 'offset', - 'limit' -] as const -export const getCustomerPaymentInstrument = [ - 'organizationId', - 'customerId', - 'paymentInstrumentId', - 'siteId' -] as const -export const getCustomerProductLists = ['organizationId', 'customerId', 'siteId'] as const -export const getCustomerProductList = ['organizationId', 'customerId', 'listId', 'siteId'] as const -export const getCustomerProductListItem = [ - 'organizationId', - 'customerId', - 'listId', - 'itemId', - 'siteId' -] as const -export const getPublicProductListsBySearchTerm = [ - 'organizationId', - 'email', - 'firstName', - 'lastName', - 'siteId' -] as const -export const getPublicProductList = ['organizationId', 'listId', 'siteId'] as const -export const getProductListItem = ['organizationId', 'listId', 'itemId', 'siteId'] as const -// TODO: Re-implement (and update description from RAML spec) when the endpoint exits closed beta. -// export const getExternalProfile = [ -// 'organizationId', -// 'externalId', -// 'authenticationProviderId', -// 'siteId' -// ] as const - -export default { - getCustomer, - getCustomerAddress, - getCustomerBaskets, - getCustomerOrders, - getCustomerPaymentInstrument, - getCustomerProductList, - getCustomerProductLists, - getCustomerProductListItem, - getPublicProductListsBySearchTerm, - getPublicProductList, - getProductListItem - // getExternalProfile -} diff --git a/packages/commerce-sdk-react/src/hooks/ShopperCustomers/query.ts b/packages/commerce-sdk-react/src/hooks/ShopperCustomers/query.ts index 4925ec60f0..6f707698be 100644 --- a/packages/commerce-sdk-react/src/hooks/ShopperCustomers/query.ts +++ b/packages/commerce-sdk-react/src/hooks/ShopperCustomers/query.ts @@ -5,12 +5,12 @@ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ import {UseQueryResult} from '@tanstack/react-query' +import {ShopperCustomers} from 'commerce-sdk-isomorphic' 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' type Client = ApiClients['shopperCustomers'] @@ -32,12 +32,7 @@ type Client = ApiClients['shopperCustomers'] // type Data = DataType // const {shopperCustomers: client} = useCommerceApi() // const methodName = 'getExternalProfile' -// const requiredParameters = [ -// 'organizationId', -// 'externalId', -// 'authenticationProviderId', -// 'siteId' -// ] as const +// const requiredParameters = ShopperCustomers.paramKeys[`${methodName}Required`] // // Parameters can be set in `apiOptions` or `client.clientConfig`; // // we must merge them in order to generate the correct query key. @@ -75,12 +70,13 @@ export const useCustomer = ( type Data = DataType const {shopperCustomers: client} = useCommerceApi() const methodName = 'getCustomer' - const requiredParameters = ['organizationId', 'customerId', 'siteId'] as const + const requiredParameters = ShopperCustomers.paramKeys[`${methodName}Required`] 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, + ShopperCustomers.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) @@ -112,14 +108,15 @@ export const useCustomerAddress = ( type Data = DataType const {shopperCustomers: client} = useCommerceApi() const methodName = 'getCustomerAddress' - const requiredParameters = ['organizationId', 'customerId', 'addressName', 'siteId'] as const + const requiredParameters = ShopperCustomers.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, + ShopperCustomers.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) @@ -151,14 +148,15 @@ export const useCustomerBaskets = ( type Data = DataType const {shopperCustomers: client} = useCommerceApi() const methodName = 'getCustomerBaskets' - const requiredParameters = ['organizationId', 'customerId', 'siteId'] as const + const requiredParameters = ShopperCustomers.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, + ShopperCustomers.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) @@ -191,14 +189,15 @@ export const useCustomerOrders = ( type Data = DataType const {shopperCustomers: client} = useCommerceApi() const methodName = 'getCustomerOrders' - const requiredParameters = ['organizationId', 'customerId', 'siteId'] as const + const requiredParameters = ShopperCustomers.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, + ShopperCustomers.paramKeys[methodName] + ) const queryKey = queryKeyHelpers[methodName].queryKey(netOptions.parameters) // We don't use `netOptions` here because we manipulate the option in `useQuery`. const method = async (options: Options) => await client[methodName](options) @@ -230,19 +229,15 @@ export const useCustomerPaymentInstrument = ( type Data = DataType const {shopperCustomers: client} = useCommerceApi() const methodName = 'getCustomerPaymentInstrument' - const requiredParameters = [ - 'organizationId', - 'customerId', - 'paymentInstrumentId', - 'siteId' - ] as const + const requiredParameters = ShopperCustomers.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, + ShopperCustomers.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) @@ -274,14 +269,15 @@ export const useCustomerProductLists = ( type Data = DataType const {shopperCustomers: client} = useCommerceApi() const methodName = 'getCustomerProductLists' - const requiredParameters = ['organizationId', 'customerId', 'siteId'] as const + const requiredParameters = ShopperCustomers.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, + ShopperCustomers.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) @@ -313,14 +309,15 @@ export const useCustomerProductList = ( type Data = DataType const {shopperCustomers: client} = useCommerceApi() const methodName = 'getCustomerProductList' - const requiredParameters = ['organizationId', 'customerId', 'listId', 'siteId'] as const + const requiredParameters = ShopperCustomers.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, + ShopperCustomers.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) @@ -352,20 +349,15 @@ export const useCustomerProductListItem = ( type Data = DataType const {shopperCustomers: client} = useCommerceApi() const methodName = 'getCustomerProductListItem' - const requiredParameters = [ - 'organizationId', - 'customerId', - 'listId', - 'itemId', - 'siteId' - ] as const + const requiredParameters = ShopperCustomers.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, + ShopperCustomers.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) @@ -397,14 +389,15 @@ export const usePublicProductListsBySearchTerm = ( type Data = DataType const {shopperCustomers: client} = useCommerceApi() const methodName = 'getPublicProductListsBySearchTerm' - const requiredParameters = ['organizationId', 'siteId'] as const + const requiredParameters = ShopperCustomers.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, + ShopperCustomers.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) @@ -436,14 +429,15 @@ export const usePublicProductList = ( type Data = DataType const {shopperCustomers: client} = useCommerceApi() const methodName = 'getPublicProductList' - const requiredParameters = ['organizationId', 'listId', 'siteId'] as const + const requiredParameters = ShopperCustomers.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, + ShopperCustomers.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) @@ -475,14 +469,15 @@ export const useProductListItem = ( type Data = DataType const {shopperCustomers: client} = useCommerceApi() const methodName = 'getProductListItem' - const requiredParameters = ['organizationId', 'listId', 'itemId', 'siteId'] as const + const requiredParameters = ShopperCustomers.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, + ShopperCustomers.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) diff --git a/packages/commerce-sdk-react/src/hooks/ShopperCustomers/queryKeyHelpers.ts b/packages/commerce-sdk-react/src/hooks/ShopperCustomers/queryKeyHelpers.ts index 1fdb797e5e..174e890b6b 100644 --- a/packages/commerce-sdk-react/src/hooks/ShopperCustomers/queryKeyHelpers.ts +++ b/packages/commerce-sdk-react/src/hooks/ShopperCustomers/queryKeyHelpers.ts @@ -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 {ShopperCustomers} from 'commerce-sdk-isomorphic' +import {ShopperCustomers} 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 = ShopperCustomers<{shortCode: string}> @@ -136,11 +135,7 @@ type QueryKeyHelper = { // TODO: Re-implement (and update description from RAML spec) when the endpoint exits closed beta. // export const getExternalProfile: QueryKeyHelper<'getExternalProfile'> = { // queryKey: (params: Params<'getExternalProfile'>) => { -// const paramKeys = [ -// ...paramKeysMap['getExternalProfile'], -// ...getCustomKeys(getCustomerBaskets) -// ] -// return [...getExternalProfile.path(params), pick(params, paramKeys)] +// return [...getExternalProfile.path(params), pickValidParams(params, ShopperCustomers.paramKeys.getExternalProfile)] // } // } @@ -153,8 +148,10 @@ export const getCustomer: QueryKeyHelper<'getCustomer'> = { params.customerId ], queryKey: (params: Params<'getCustomer'>) => { - const paramKeys = [...paramKeysMap['getCustomer'], ...getCustomKeys(params)] - return [...getCustomer.path(params), pick(params, paramKeys)] + return [ + ...getCustomer.path(params), + pickValidParams(params, ShopperCustomers.paramKeys.getCustomer) + ] } } @@ -169,9 +166,10 @@ export const getCustomerAddress: QueryKeyHelper<'getCustomerAddress'> = { params.addressName ], queryKey: (params: Params<'getCustomerAddress'>) => { - const paramKeys = [...paramKeysMap['getCustomerAddress'], ...getCustomKeys(params)] - - return [...getCustomerAddress.path(params), pick(params, paramKeys)] + return [ + ...getCustomerAddress.path(params), + pickValidParams(params, ShopperCustomers.paramKeys.getCustomerAddress) + ] } } export const getCustomerBaskets: QueryKeyHelper<'getCustomerBaskets'> = { @@ -184,8 +182,10 @@ export const getCustomerBaskets: QueryKeyHelper<'getCustomerBaskets'> = { '/baskets' ], queryKey: (params: Params<'getCustomerBaskets'>) => { - const paramKeys = [...paramKeysMap['getCustomerBaskets'], ...getCustomKeys(params)] - return [...getCustomerBaskets.path(params), pick(params, paramKeys)] + return [ + ...getCustomerBaskets.path(params), + pickValidParams(params, ShopperCustomers.paramKeys.getCustomerBaskets) + ] } } @@ -199,8 +199,10 @@ export const getCustomerOrders: QueryKeyHelper<'getCustomerOrders'> = { '/orders' ], queryKey: (params: Params<'getCustomerOrders'>) => { - const paramKeys = [...paramKeysMap['getCustomerOrders'], ...getCustomKeys(params)] - return [...getCustomerOrders.path(params), pick(params, paramKeys)] + return [ + ...getCustomerOrders.path(params), + pickValidParams(params, ShopperCustomers.paramKeys.getCustomerOrders) + ] } } @@ -215,11 +217,10 @@ export const getCustomerPaymentInstrument: QueryKeyHelper<'getCustomerPaymentIns params.paymentInstrumentId ], queryKey: (params: Params<'getCustomerPaymentInstrument'>) => { - const paramKeys = [ - ...paramKeysMap['getCustomerPaymentInstrument'], - ...getCustomKeys(params) + return [ + ...getCustomerPaymentInstrument.path(params), + pickValidParams(params, ShopperCustomers.paramKeys.getCustomerPaymentInstrument) ] - return [...getCustomerPaymentInstrument.path(params), pick(params, paramKeys)] } } @@ -233,8 +234,10 @@ export const getCustomerProductLists: QueryKeyHelper<'getCustomerProductLists'> '/product-lists' ], queryKey: (params: Params<'getCustomerProductLists'>) => { - const paramKeys = [...paramKeysMap['getCustomerProductLists'], ...getCustomKeys(params)] - return [...getCustomerProductLists.path(params), pick(params, paramKeys)] + return [ + ...getCustomerProductLists.path(params), + pickValidParams(params, ShopperCustomers.paramKeys.getCustomerProductLists) + ] } } @@ -249,8 +252,10 @@ export const getCustomerProductList: QueryKeyHelper<'getCustomerProductList'> = params.listId ], queryKey: (params: Params<'getCustomerProductList'>) => { - const paramKeys = [...paramKeysMap['getCustomerProductList'], ...getCustomKeys(params)] - return [...getCustomerProductList.path(params), pick(params, paramKeys)] + return [ + ...getCustomerProductList.path(params), + pickValidParams(params, ShopperCustomers.paramKeys.getCustomerProductList) + ] } } @@ -267,8 +272,10 @@ export const getCustomerProductListItem: QueryKeyHelper<'getCustomerProductListI params.itemId ], queryKey: (params: Params<'getCustomerProductListItem'>) => { - const paramKeys = [...paramKeysMap['getCustomerProductListItem'], ...getCustomKeys(params)] - return [...getCustomerProductListItem.path(params), pick(params, paramKeys)] + return [ + ...getCustomerProductListItem.path(params), + pickValidParams(params, ShopperCustomers.paramKeys.getCustomerProductListItem) + ] } } @@ -281,11 +288,13 @@ export const getPublicProductListsBySearchTerm: QueryKeyHelper<'getPublicProduct '/product-lists' ], queryKey: (params: Params<'getPublicProductListsBySearchTerm'>) => { - const paramKeys = [ - ...paramKeysMap['getPublicProductListsBySearchTerm'], - ...getCustomKeys(params) + return [ + ...getPublicProductListsBySearchTerm.path(params), + pickValidParams( + params, + ShopperCustomers.paramKeys.getPublicProductListsBySearchTerm + ) ] - return [...getPublicProductListsBySearchTerm.path(params), pick(params, paramKeys)] } } @@ -298,8 +307,10 @@ export const getPublicProductList: QueryKeyHelper<'getPublicProductList'> = { params.listId ], queryKey: (params: Params<'getPublicProductList'>) => { - const paramKeys = [...paramKeysMap['getPublicProductList'], ...getCustomKeys(params)] - return [...getPublicProductList.path(params), pick(params, paramKeys)] + return [ + ...getPublicProductList.path(params), + pickValidParams(params, ShopperCustomers.paramKeys.getPublicProductList) + ] } } @@ -314,7 +325,9 @@ export const getProductListItem: QueryKeyHelper<'getProductListItem'> = { params.itemId ], queryKey: (params: Params<'getProductListItem'>) => { - const paramKeys = [...paramKeysMap['getProductListItem'], ...getCustomKeys(params)] - return [...getProductListItem.path(params), pick(params, paramKeys)] + return [ + ...getProductListItem.path(params), + pickValidParams(params, ShopperCustomers.paramKeys.getProductListItem) + ] } } diff --git a/packages/commerce-sdk-react/src/hooks/ShopperExperience/paramKeys.ts b/packages/commerce-sdk-react/src/hooks/ShopperExperience/paramKeys.ts deleted file mode 100644 index 1447514666..0000000000 --- a/packages/commerce-sdk-react/src/hooks/ShopperExperience/paramKeys.ts +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2023, Salesforce, Inc. - * All rights reserved. - * 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 - */ - -const getPages = [ - 'organizationId', - 'categoryId', - 'productId', - 'aspectTypeId', - 'aspectAttributes', - 'parameters', - 'siteId', - 'locale' -] as const -const getPage = [ - 'organizationId', - 'pageId', - 'aspectAttributes', - 'parameters', - 'siteId', - 'locale' -] as const - -export default { - getPages, - getPage -} diff --git a/packages/commerce-sdk-react/src/hooks/ShopperExperience/query.ts b/packages/commerce-sdk-react/src/hooks/ShopperExperience/query.ts index a953026d28..dfb8b4ed6d 100644 --- a/packages/commerce-sdk-react/src/hooks/ShopperExperience/query.ts +++ b/packages/commerce-sdk-react/src/hooks/ShopperExperience/query.ts @@ -5,12 +5,12 @@ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ import {UseQueryResult} from '@tanstack/react-query' +import {ShopperExperience} from 'commerce-sdk-isomorphic' 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' type Client = ApiClients['shopperExperience'] /** @@ -35,14 +35,15 @@ export const usePages = ( type Data = DataType const {shopperExperience: client} = useCommerceApi() const methodName = 'getPages' - const requiredParameters = ['organizationId', 'aspectTypeId', 'siteId'] as const + const requiredParameters = ShopperExperience.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, + ShopperExperience.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) @@ -77,14 +78,15 @@ export const usePage = ( type Data = DataType const {shopperExperience: client} = useCommerceApi() const methodName = 'getPage' - const requiredParameters = ['organizationId', 'pageId', 'siteId'] as const + const requiredParameters = ShopperExperience.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, + ShopperExperience.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) diff --git a/packages/commerce-sdk-react/src/hooks/ShopperExperience/queryKeyHelpers.ts b/packages/commerce-sdk-react/src/hooks/ShopperExperience/queryKeyHelpers.ts index bfa703b74d..6c6f3bda59 100644 --- a/packages/commerce-sdk-react/src/hooks/ShopperExperience/queryKeyHelpers.ts +++ b/packages/commerce-sdk-react/src/hooks/ShopperExperience/queryKeyHelpers.ts @@ -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 {ShopperExperience} from 'commerce-sdk-isomorphic' +import {ShopperExperience} 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 = ShopperExperience<{shortCode: string}> @@ -42,8 +41,10 @@ type QueryKeyHelper = { export const getPages: QueryKeyHelper<'getPages'> = { path: (params) => ['/commerce-sdk-react', '/organizations/', params.organizationId, '/pages'], queryKey: (params: Params<'getPages'>) => { - const paramKeys = [...paramKeysMap['getPages'], ...getCustomKeys(params)] - return [...getPages.path(params), pick(params, paramKeys)] + return [ + ...getPages.path(params), + pickValidParams(params, ShopperExperience.paramKeys.getPages) + ] } } @@ -56,7 +57,9 @@ export const getPage: QueryKeyHelper<'getPage'> = { params.pageId ], queryKey: (params: Params<'getPage'>) => { - const paramKeys = [...paramKeysMap['getPage'], ...getCustomKeys(params)] - return [...getPage.path(params), pick(params, paramKeys)] + return [ + ...getPage.path(params), + pickValidParams(params, ShopperExperience.paramKeys.getPage) + ] } } diff --git a/packages/commerce-sdk-react/src/hooks/ShopperGiftCertificates/paramKeys.ts b/packages/commerce-sdk-react/src/hooks/ShopperGiftCertificates/paramKeys.ts deleted file mode 100644 index e4a7d8a3e6..0000000000 --- a/packages/commerce-sdk-react/src/hooks/ShopperGiftCertificates/paramKeys.ts +++ /dev/null @@ -1,12 +0,0 @@ -/* - * Copyright (c) 2023, Salesforce, Inc. - * All rights reserved. - * 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 - */ - -const getGiftCertificate = ['organizationId', 'siteId'] as const - -export default { - getGiftCertificate -} diff --git a/packages/commerce-sdk-react/src/hooks/ShopperGiftCertificates/query.ts b/packages/commerce-sdk-react/src/hooks/ShopperGiftCertificates/query.ts index e2be368bcb..11c3f19589 100644 --- a/packages/commerce-sdk-react/src/hooks/ShopperGiftCertificates/query.ts +++ b/packages/commerce-sdk-react/src/hooks/ShopperGiftCertificates/query.ts @@ -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 {ShopperGiftCertificates} from 'commerce-sdk-isomorphic' type Client = ApiClients['shopperGiftCertificates'] @@ -33,14 +33,15 @@ export const useGiftCertificate = ( type Data = DataType const {shopperGiftCertificates: client} = useCommerceApi() const methodName = 'getGiftCertificate' - const requiredParameters = ['organizationId', 'siteId'] as const + const requiredParameters = ShopperGiftCertificates.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, + ShopperGiftCertificates.paramKeys[methodName] + ) const queryKey = queryKeyHelpers[methodName].queryKey(netOptions.parameters) // We don't use `netOptions` here because we manipulate the options in `useQuery`. diff --git a/packages/commerce-sdk-react/src/hooks/ShopperGiftCertificates/queryKeyHelpers.ts b/packages/commerce-sdk-react/src/hooks/ShopperGiftCertificates/queryKeyHelpers.ts index b5c3909b9e..c07b0fa654 100644 --- a/packages/commerce-sdk-react/src/hooks/ShopperGiftCertificates/queryKeyHelpers.ts +++ b/packages/commerce-sdk-react/src/hooks/ShopperGiftCertificates/queryKeyHelpers.ts @@ -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 {ShopperGiftCertificates} from 'commerce-sdk-isomorphic' +import {ShopperGiftCertificates} 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 = ShopperGiftCertificates<{shortCode: string}> @@ -39,7 +38,9 @@ export const getGiftCertificate: QueryKeyHelper<'getGiftCertificate'> = { '/gift-certificate' ], queryKey: (params: Params<'getGiftCertificate'>) => { - const paramKeys = [...paramKeysMap['getGiftCertificate'], ...getCustomKeys(params)] - return [...getGiftCertificate.path(params), pick(params, paramKeys)] + return [ + ...getGiftCertificate.path(params), + pickValidParams(params, ShopperGiftCertificates.paramKeys.getGiftCertificate) + ] } } diff --git a/packages/commerce-sdk-react/src/hooks/ShopperLogin/paramKeys.ts b/packages/commerce-sdk-react/src/hooks/ShopperLogin/paramKeys.ts deleted file mode 100644 index 70af734a49..0000000000 --- a/packages/commerce-sdk-react/src/hooks/ShopperLogin/paramKeys.ts +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright (c) 2023, Salesforce, Inc. - * All rights reserved. - * 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 - */ - -const getUserInfo = ['organizationId', 'channel_id'] as const -const getWellknownOpenidConfiguration = ['organizationId'] as const -const getJwksUri = ['organizationId'] as const - -export default { - getUserInfo, - getWellknownOpenidConfiguration, - getJwksUri -} diff --git a/packages/commerce-sdk-react/src/hooks/ShopperLogin/query.ts b/packages/commerce-sdk-react/src/hooks/ShopperLogin/query.ts index 824e36a651..922d7ae2e5 100644 --- a/packages/commerce-sdk-react/src/hooks/ShopperLogin/query.ts +++ b/packages/commerce-sdk-react/src/hooks/ShopperLogin/query.ts @@ -5,12 +5,12 @@ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ import {UseQueryResult} from '@tanstack/react-query' +import {ShopperLogin} from 'commerce-sdk-isomorphic' 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' type Client = ApiClients['shopperLogin'] @@ -33,14 +33,12 @@ export const useUserInfo = ( type Data = DataType const {shopperLogin: client} = useCommerceApi() const methodName = 'getUserInfo' - const requiredParameters = ['organizationId'] as const + const requiredParameters = ShopperLogin.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, ShopperLogin.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) @@ -72,14 +70,12 @@ export const useWellknownOpenidConfiguration = ( type Data = DataType const {shopperLogin: client} = useCommerceApi() const methodName = 'getWellknownOpenidConfiguration' - const requiredParameters = ['organizationId'] as const + const requiredParameters = ShopperLogin.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, ShopperLogin.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) @@ -111,14 +107,12 @@ export const useJwksUri = ( type Data = DataType const {shopperLogin: client} = useCommerceApi() const methodName = 'getJwksUri' - const requiredParameters = ['organizationId'] as const + const requiredParameters = ShopperLogin.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, ShopperLogin.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) diff --git a/packages/commerce-sdk-react/src/hooks/ShopperLogin/queryKeyHelpers.ts b/packages/commerce-sdk-react/src/hooks/ShopperLogin/queryKeyHelpers.ts index b82f51f3e5..0e2db5299f 100644 --- a/packages/commerce-sdk-react/src/hooks/ShopperLogin/queryKeyHelpers.ts +++ b/packages/commerce-sdk-react/src/hooks/ShopperLogin/queryKeyHelpers.ts @@ -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 {ShopperLogin} from 'commerce-sdk-isomorphic' +import {ShopperLogin} 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 = ShopperLogin<{shortCode: string}> @@ -53,8 +52,10 @@ export const getUserInfo: QueryKeyHelper<'getUserInfo'> = { '/oauth2/userinfo' ], queryKey: (params: Params<'getUserInfo'>) => { - const paramKeys = [...paramKeysMap['getUserInfo'], ...getCustomKeys(params)] - return [...getUserInfo.path(params), pick(params, paramKeys)] + return [ + ...getUserInfo.path(params), + pickValidParams(params, ShopperLogin.paramKeys.getUserInfo) + ] } } @@ -66,12 +67,10 @@ export const getWellknownOpenidConfiguration: QueryKeyHelper<'getWellknownOpenid '/oauth2/.well-known/openid-configuration' ], queryKey: (params: Params<'getWellknownOpenidConfiguration'>) => { - const paramKeys = [ - ...paramKeysMap['getWellknownOpenidConfiguration'], - ...getCustomKeys(params) + return [ + ...getWellknownOpenidConfiguration.path(params), + pickValidParams(params, ShopperLogin.paramKeys.getWellknownOpenidConfiguration) ] - - return [...getWellknownOpenidConfiguration.path(params), pick(params, paramKeys)] } } @@ -83,7 +82,9 @@ export const getJwksUri: QueryKeyHelper<'getJwksUri'> = { '/oauth2/jwks' ], queryKey: (params: Params<'getJwksUri'>) => { - const paramKeys = [...paramKeysMap['getJwksUri'], ...getCustomKeys(params)] - return [...getJwksUri.path(params), pick(params, paramKeys)] + return [ + ...getJwksUri.path(params), + pickValidParams(params, ShopperLogin.paramKeys.getJwksUri) + ] } } diff --git a/packages/commerce-sdk-react/src/hooks/ShopperOrders/paramKeys.ts b/packages/commerce-sdk-react/src/hooks/ShopperOrders/paramKeys.ts deleted file mode 100644 index b0ac50e4d0..0000000000 --- a/packages/commerce-sdk-react/src/hooks/ShopperOrders/paramKeys.ts +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright (c) 2023, Salesforce, Inc. - * All rights reserved. - * 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 - */ - -const getOrder = ['organizationId', 'orderNo', 'siteId', 'locale'] as const -const getPaymentMethodsForOrder = ['organizationId', 'orderNo', 'siteId', 'locale'] as const -const getTaxesFromOrder = ['organizationId', 'orderNo', 'siteId'] as const - -export default { - getOrder, - getPaymentMethodsForOrder, - getTaxesFromOrder -} diff --git a/packages/commerce-sdk-react/src/hooks/ShopperOrders/query.ts b/packages/commerce-sdk-react/src/hooks/ShopperOrders/query.ts index 29e9719b82..0ba801f3dd 100644 --- a/packages/commerce-sdk-react/src/hooks/ShopperOrders/query.ts +++ b/packages/commerce-sdk-react/src/hooks/ShopperOrders/query.ts @@ -5,12 +5,12 @@ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ import {UseQueryResult} from '@tanstack/react-query' +import {ShopperOrders} from 'commerce-sdk-isomorphic' 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' type Client = ApiClients['shopperOrders'] @@ -33,14 +33,12 @@ export const useOrder = ( type Data = DataType const {shopperOrders: client} = useCommerceApi() const methodName = 'getOrder' - const requiredParameters = ['organizationId', 'orderNo', 'siteId'] as const + const requiredParameters = ShopperOrders.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, ShopperOrders.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) @@ -72,14 +70,12 @@ export const usePaymentMethodsForOrder = ( type Data = DataType const {shopperOrders: client} = useCommerceApi() const methodName = 'getPaymentMethodsForOrder' - const requiredParameters = ['organizationId', 'orderNo', 'siteId'] as const + const requiredParameters = ShopperOrders.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, ShopperOrders.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) @@ -93,11 +89,11 @@ export const usePaymentMethodsForOrder = ( }) } /** - * This method gives you the external taxation data of the order transferred from the basket during -order creation. This endpoint can be called only if external taxation was used. See POST /baskets -for more information. + * This method gives you the external taxation data of the order transferred from the basket during +order creation. This endpoint can be called only if external taxation was used. See POST /baskets +for more information. * @group ShopperOrders - * @category Query + * @category Query * @parameter apiOptions - Options to pass through to `commerce-sdk-isomorphic`, with `null` accepted for unset API parameters. * @parameter queryOptions - TanStack Query query options, with `enabled` by default set to check that all required API parameters have been set. * @returns A TanStack Query query hook with data from the Shopper Orders `getTaxesFromOrder` endpoint. @@ -113,14 +109,12 @@ export const useTaxesFromOrder = ( type Data = DataType const {shopperOrders: client} = useCommerceApi() const methodName = 'getTaxesFromOrder' - const requiredParameters = ['organizationId', 'orderNo', 'siteId'] as const + const requiredParameters = ShopperOrders.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, ShopperOrders.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) diff --git a/packages/commerce-sdk-react/src/hooks/ShopperOrders/queryKeyHelpers.ts b/packages/commerce-sdk-react/src/hooks/ShopperOrders/queryKeyHelpers.ts index fdd1054303..e7d9187ab8 100644 --- a/packages/commerce-sdk-react/src/hooks/ShopperOrders/queryKeyHelpers.ts +++ b/packages/commerce-sdk-react/src/hooks/ShopperOrders/queryKeyHelpers.ts @@ -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 {ShopperOrders} from 'commerce-sdk-isomorphic' +import {ShopperOrders} 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 = ShopperOrders<{shortCode: string}> @@ -59,9 +58,7 @@ export const getOrder: QueryKeyHelper<'getOrder'> = { params.orderNo ], queryKey: (params: Params<'getOrder'>) => { - const paramKeys = [...paramKeysMap['getOrder'], ...getCustomKeys(params)] - - return [...getOrder.path(params), pick(params, paramKeys)] + return [...getOrder.path(params), pickValidParams(params, ShopperOrders.paramKeys.getOrder)] } } @@ -75,9 +72,10 @@ export const getPaymentMethodsForOrder: QueryKeyHelper<'getPaymentMethodsForOrde '/payment-methods' ], queryKey: (params: Params<'getPaymentMethodsForOrder'>) => { - const paramKeys = [...paramKeysMap['getPaymentMethodsForOrder'], ...getCustomKeys(params)] - - return [...getPaymentMethodsForOrder.path(params), pick(params, paramKeys)] + return [ + ...getPaymentMethodsForOrder.path(params), + pickValidParams(params, ShopperOrders.paramKeys.getPaymentMethodsForOrder) + ] } } @@ -91,8 +89,9 @@ export const getTaxesFromOrder: QueryKeyHelper<'getTaxesFromOrder'> = { '/taxes' ], queryKey: (params: Params<'getTaxesFromOrder'>) => { - const paramKeys = [...paramKeysMap['getTaxesFromOrder'], ...getCustomKeys(params)] - - return [...getTaxesFromOrder.path(params), pick(params, paramKeys)] + return [ + ...getTaxesFromOrder.path(params), + pickValidParams(params, ShopperOrders.paramKeys.getTaxesFromOrder) + ] } } diff --git a/packages/commerce-sdk-react/src/hooks/ShopperProducts/paramKeys.ts b/packages/commerce-sdk-react/src/hooks/ShopperProducts/paramKeys.ts deleted file mode 100644 index bd7734c5d6..0000000000 --- a/packages/commerce-sdk-react/src/hooks/ShopperProducts/paramKeys.ts +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2023, Salesforce, Inc. - * All rights reserved. - * 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 - */ - -const getProducts = [ - 'organizationId', - 'ids', - 'inventoryIds', - 'select', - 'currency', - 'expand', - 'locale', - 'allImages', - 'perPricebook', - 'siteId' -] as const - -const getProduct = [ - 'organizationId', - 'id', - 'inventoryIds', - 'currency', - 'expand', - 'locale', - 'allImages', - 'perPricebook', - 'siteId' -] as const - -const getCategories = ['organizationId', 'ids', 'levels', 'locale', 'siteId'] as const -const getCategory = ['organizationId', 'id', 'levels', 'locale', 'siteId'] as const - -export default { - getProducts, - getProduct, - getCategories, - getCategory -} diff --git a/packages/commerce-sdk-react/src/hooks/ShopperProducts/query.ts b/packages/commerce-sdk-react/src/hooks/ShopperProducts/query.ts index 2609825298..0ba97c3d17 100644 --- a/packages/commerce-sdk-react/src/hooks/ShopperProducts/query.ts +++ b/packages/commerce-sdk-react/src/hooks/ShopperProducts/query.ts @@ -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 {ShopperProducts} from 'commerce-sdk-isomorphic' type Client = ApiClients['shopperProducts'] @@ -33,14 +33,12 @@ export const useProducts = ( type Data = DataType const {shopperProducts: client} = useCommerceApi() const methodName = 'getProducts' - const requiredParameters = ['organizationId', 'ids', 'siteId'] as const + const requiredParameters = ShopperProducts.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, ShopperProducts.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) @@ -72,14 +70,12 @@ export const useProduct = ( type Data = DataType const {shopperProducts: client} = useCommerceApi() const methodName = 'getProduct' - const requiredParameters = ['organizationId', 'id', 'siteId'] as const + const requiredParameters = ShopperProducts.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, ShopperProducts.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) @@ -111,14 +107,12 @@ export const useCategories = ( type Data = DataType const {shopperProducts: client} = useCommerceApi() const methodName = 'getCategories' - const requiredParameters = ['organizationId', 'ids', 'siteId'] as const + const requiredParameters = ShopperProducts.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, ShopperProducts.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) @@ -152,14 +146,12 @@ export const useCategory = ( type Data = DataType const {shopperProducts: client} = useCommerceApi() const methodName = 'getCategory' - const requiredParameters = ['organizationId', 'id', 'siteId'] as const + const requiredParameters = ShopperProducts.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, ShopperProducts.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) diff --git a/packages/commerce-sdk-react/src/hooks/ShopperProducts/queryKeyHelpers.ts b/packages/commerce-sdk-react/src/hooks/ShopperProducts/queryKeyHelpers.ts index b451d2bbe1..c1b3e89d9d 100644 --- a/packages/commerce-sdk-react/src/hooks/ShopperProducts/queryKeyHelpers.ts +++ b/packages/commerce-sdk-react/src/hooks/ShopperProducts/queryKeyHelpers.ts @@ -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 {ShopperProducts} from 'commerce-sdk-isomorphic' +import {ShopperProducts} 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 = ShopperProducts<{shortCode: string}> type Params = Partial['parameters']> @@ -61,8 +60,10 @@ export const getProducts: QueryKeyHelper<'getProducts'> = { '/products' ], queryKey: (params: Params<'getProducts'>) => { - const paramKeys = [...paramKeysMap['getProducts'], ...getCustomKeys(params)] - return [...getProducts.path(params), pick(params, paramKeys)] + return [ + ...getProducts.path(params), + pickValidParams(params, ShopperProducts.paramKeys.getProducts) + ] } } @@ -75,8 +76,10 @@ export const getProduct: QueryKeyHelper<'getProduct'> = { params.id ], queryKey: (params: Params<'getProduct'>) => { - const paramKeys = [...paramKeysMap['getProduct'], ...getCustomKeys(params)] - return [...getProduct.path(params), pick(params, paramKeys)] + return [ + ...getProduct.path(params), + pickValidParams(params, ShopperProducts.paramKeys.getProduct) + ] } } @@ -88,8 +91,10 @@ export const getCategories: QueryKeyHelper<'getCategories'> = { '/categories' ], queryKey: (params: Params<'getCategories'>) => { - const paramKeys = [...paramKeysMap['getCategories'], ...getCustomKeys(params)] - return [...getCategories.path(params), pick(params, paramKeys)] + return [ + ...getCategories.path(params), + pickValidParams(params, ShopperProducts.paramKeys.getCategories) + ] } } @@ -102,7 +107,9 @@ export const getCategory: QueryKeyHelper<'getCategory'> = { params.id ], queryKey: (params: Params<'getCategory'>) => { - const paramKeys = [...paramKeysMap['getCategory'], ...getCustomKeys(params)] - return [...getCategory.path(params), pick(params, paramKeys)] + return [ + ...getCategory.path(params), + pickValidParams(params, ShopperProducts.paramKeys.getCategory) + ] } } diff --git a/packages/commerce-sdk-react/src/hooks/ShopperPromotions/paramKeys.ts b/packages/commerce-sdk-react/src/hooks/ShopperPromotions/paramKeys.ts deleted file mode 100644 index 95501f0aac..0000000000 --- a/packages/commerce-sdk-react/src/hooks/ShopperPromotions/paramKeys.ts +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (c) 2023, Salesforce, Inc. - * All rights reserved. - * 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 - */ - -const getPromotionsForCampaign = [ - 'organizationId', - 'campaignId', - 'siteId', - 'startDate', - 'endDate', - 'currency' -] as const - -const getPromotions = ['organizationId', 'siteId', 'ids', 'locale'] as const - -export default { - getPromotionsForCampaign, - getPromotions -} diff --git a/packages/commerce-sdk-react/src/hooks/ShopperPromotions/query.ts b/packages/commerce-sdk-react/src/hooks/ShopperPromotions/query.ts index bc543c53e6..38742be79a 100644 --- a/packages/commerce-sdk-react/src/hooks/ShopperPromotions/query.ts +++ b/packages/commerce-sdk-react/src/hooks/ShopperPromotions/query.ts @@ -5,12 +5,12 @@ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ import {UseQueryResult} from '@tanstack/react-query' +import {ShopperPromotions} from 'commerce-sdk-isomorphic' 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' type Client = ApiClients['shopperPromotions'] @@ -33,14 +33,15 @@ export const usePromotions = ( type Data = DataType const {shopperPromotions: client} = useCommerceApi() const methodName = 'getPromotions' - const requiredParameters = ['organizationId', 'siteId', 'ids'] as const + const requiredParameters = ShopperPromotions.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, + ShopperPromotions.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) @@ -56,7 +57,7 @@ export const usePromotions = ( /** * Handles get promotion by filter criteria. Returns an array of enabled promotions matching the specified filter criteria. In the request URL, you must provide a campaign_id parameter, and you can optionally specify a date -range by providing start_date and end_date parameters. Both parameters are required to specify a date range, as +range by providing start_date and end_date parameters. Both parameters are required to specify a date range, as omitting one causes the server to return a MissingParameterException fault. Each request returns only enabled promotions, since the server does not consider promotion qualifiers or schedules. * @group ShopperPromotions @@ -76,14 +77,15 @@ export const usePromotionsForCampaign = ( type Data = DataType const {shopperPromotions: client} = useCommerceApi() const methodName = 'getPromotionsForCampaign' - const requiredParameters = ['organizationId', 'campaignId', 'siteId'] as const + const requiredParameters = ShopperPromotions.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, + ShopperPromotions.paramKeys[methodName] + ) const queryKey = queryKeyHelpers[methodName].queryKey(netOptions.parameters) // We don't use `netOptions` here because we manipulate the options in `useQuery`. diff --git a/packages/commerce-sdk-react/src/hooks/ShopperPromotions/queryKeyHelpers.ts b/packages/commerce-sdk-react/src/hooks/ShopperPromotions/queryKeyHelpers.ts index 2c771fc976..2b94457235 100644 --- a/packages/commerce-sdk-react/src/hooks/ShopperPromotions/queryKeyHelpers.ts +++ b/packages/commerce-sdk-react/src/hooks/ShopperPromotions/queryKeyHelpers.ts @@ -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 {ShopperPromotions} from 'commerce-sdk-isomorphic' +import {ShopperPromotions} 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 = ShopperPromotions<{shortCode: string}> @@ -47,8 +46,10 @@ export const getPromotions: QueryKeyHelper<'getPromotions'> = { '/promotions' ], queryKey: (params: Params<'getPromotions'>) => { - const paramKeys = [...paramKeysMap['getPromotions'], ...getCustomKeys(params)] - return [...getPromotions.path(params), pick(params, paramKeys)] + return [ + ...getPromotions.path(params), + pickValidParams(params, ShopperPromotions.paramKeys.getPromotions) + ] } } @@ -61,7 +62,9 @@ export const getPromotionsForCampaign: QueryKeyHelper<'getPromotionsForCampaign' params.campaignId ], queryKey: (params: Params<'getPromotionsForCampaign'>) => { - const paramKeys = [...paramKeysMap['getPromotionsForCampaign'], ...getCustomKeys(params)] - return [...getPromotionsForCampaign.path(params), pick(params, paramKeys)] + return [ + ...getPromotionsForCampaign.path(params), + pickValidParams(params, ShopperPromotions.paramKeys.getPromotionsForCampaign) + ] } } diff --git a/packages/commerce-sdk-react/src/hooks/ShopperSearch/paramKeys.ts b/packages/commerce-sdk-react/src/hooks/ShopperSearch/paramKeys.ts deleted file mode 100644 index 75c221df06..0000000000 --- a/packages/commerce-sdk-react/src/hooks/ShopperSearch/paramKeys.ts +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2024, Salesforce, Inc. - * All rights reserved. - * 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 - */ -const productSearch = [ - 'organizationId', - 'siteId', - 'q', - 'refine', - 'sort', - 'currency', - 'locale', - 'expand', - 'offset', - 'limit', - 'select', - 'allImages', - 'allVariationProperties', - 'perPricebook' -] as const - -const getSearchSuggestions = [ - 'organizationId', - 'siteId', - 'q', - 'limit', - 'currency', - 'locale' -] as const - -export default { - productSearch, - getSearchSuggestions -} diff --git a/packages/commerce-sdk-react/src/hooks/ShopperSearch/query.ts b/packages/commerce-sdk-react/src/hooks/ShopperSearch/query.ts index 54ca678c68..c84b01c456 100644 --- a/packages/commerce-sdk-react/src/hooks/ShopperSearch/query.ts +++ b/packages/commerce-sdk-react/src/hooks/ShopperSearch/query.ts @@ -5,12 +5,12 @@ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ import {UseQueryResult} from '@tanstack/react-query' +import {ShopperSearch} from 'commerce-sdk-isomorphic' 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' type Client = ApiClients['shopperSearch'] @@ -36,13 +36,12 @@ export const useProductSearch = ( type Data = DataType const {shopperSearch: client} = useCommerceApi() const methodName = 'productSearch' - const requiredParameters = ['organizationId', 'siteId'] as const + const requiredParameters = ShopperSearch.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)) - const paramKeys = [...paramKeysMap[methodName], ...getCustomKeys(netOptions.parameters)] - const parameters = pick(netOptions.parameters, paramKeys) + const parameters = pickValidParams(netOptions.parameters, ShopperSearch.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) @@ -76,13 +75,12 @@ export const useSearchSuggestions = ( type Data = DataType const {shopperSearch: client} = useCommerceApi() const methodName = 'getSearchSuggestions' - const requiredParameters = ['organizationId', 'siteId', 'q'] as const + const requiredParameters = ShopperSearch.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)) - const paramKeys = [...paramKeysMap[methodName], ...getCustomKeys(netOptions.parameters)] - const parameters = pick(netOptions.parameters, paramKeys) + const parameters = pickValidParams(netOptions.parameters, ShopperSearch.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) diff --git a/packages/commerce-sdk-react/src/hooks/ShopperSearch/queryKeyHelpers.ts b/packages/commerce-sdk-react/src/hooks/ShopperSearch/queryKeyHelpers.ts index e50a8e5e02..daa885e92c 100644 --- a/packages/commerce-sdk-react/src/hooks/ShopperSearch/queryKeyHelpers.ts +++ b/packages/commerce-sdk-react/src/hooks/ShopperSearch/queryKeyHelpers.ts @@ -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 {ShopperSearch} from 'commerce-sdk-isomorphic' +import {ShopperSearch} 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 = ShopperSearch<{shortCode: string}> type Params = Partial['parameters']> @@ -45,9 +44,10 @@ export const productSearch: QueryKeyHelper<'productSearch'> = { '/product-search' ], queryKey: (params: Params<'productSearch'>) => { - const paramKeys = [...paramKeysMap['productSearch'], ...getCustomKeys(params)] - - return [...productSearch.path(params), pick(params, paramKeys)] + return [ + ...productSearch.path(params), + pickValidParams(params, ShopperSearch.paramKeys.productSearch) + ] } } @@ -59,8 +59,9 @@ export const getSearchSuggestions: QueryKeyHelper<'getSearchSuggestions'> = { '/search-suggestions' ], queryKey: (params: Params<'getSearchSuggestions'>) => { - const paramKeys = [...paramKeysMap['getSearchSuggestions'], ...getCustomKeys(params)] - - return [...getSearchSuggestions.path(params), pick(params, paramKeys)] + return [ + ...getSearchSuggestions.path(params), + pickValidParams(params, ShopperSearch.paramKeys.getSearchSuggestions) + ] } } diff --git a/packages/commerce-sdk-react/src/hooks/ShopperSeo/paramKeys.ts b/packages/commerce-sdk-react/src/hooks/ShopperSeo/paramKeys.ts deleted file mode 100644 index 4abf054e63..0000000000 --- a/packages/commerce-sdk-react/src/hooks/ShopperSeo/paramKeys.ts +++ /dev/null @@ -1,12 +0,0 @@ -/* - * Copyright (c) 2024, Salesforce, Inc. - * All rights reserved. - * 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 - */ - -const getUrlMapping = ['organizationId', 'urlSegment', 'siteId', 'locale'] as const - -export default { - getUrlMapping -} diff --git a/packages/commerce-sdk-react/src/hooks/ShopperSeo/query.ts b/packages/commerce-sdk-react/src/hooks/ShopperSeo/query.ts index adfd168866..9b15c3bc6e 100644 --- a/packages/commerce-sdk-react/src/hooks/ShopperSeo/query.ts +++ b/packages/commerce-sdk-react/src/hooks/ShopperSeo/query.ts @@ -5,12 +5,12 @@ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ import {UseQueryResult} from '@tanstack/react-query' +import {ShopperSeo} from 'commerce-sdk-isomorphic' 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' type Client = ApiClients['shopperSeo'] @@ -36,13 +36,12 @@ export const useUrlMapping = ( type Data = DataType const {shopperSeo: client} = useCommerceApi() const methodName = 'getUrlMapping' - const requiredParameters = ['organizationId', 'siteId'] as const + const requiredParameters = ShopperSeo.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)) - const paramKeys = [...paramKeysMap[methodName], ...getCustomKeys(netOptions.parameters)] - const parameters = pick(netOptions.parameters, paramKeys) + const parameters = pickValidParams(netOptions.parameters, ShopperSeo.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) diff --git a/packages/commerce-sdk-react/src/hooks/ShopperSeo/queryKeyHelpers.ts b/packages/commerce-sdk-react/src/hooks/ShopperSeo/queryKeyHelpers.ts index 5353548a9c..cef9b8e247 100644 --- a/packages/commerce-sdk-react/src/hooks/ShopperSeo/queryKeyHelpers.ts +++ b/packages/commerce-sdk-react/src/hooks/ShopperSeo/queryKeyHelpers.ts @@ -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 {ShopperSeo} from 'commerce-sdk-isomorphic' +import {ShopperSeo} 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 = ShopperSeo<{shortCode: string}> type Params = Partial['parameters']> @@ -38,8 +37,9 @@ export const getUrlMapping: QueryKeyHelper<'getUrlMapping'> = { '/url-mapping' ], queryKey: (params: Params<'getUrlMapping'>) => { - const paramKeys = [...paramKeysMap['getUrlMapping'], ...getCustomKeys(params)] - - return [...getUrlMapping.path(params), pick(params, paramKeys)] + return [ + ...getUrlMapping.path(params), + pickValidParams(params, ShopperSeo.paramKeys.getUrlMapping) + ] } } diff --git a/packages/commerce-sdk-react/src/hooks/ShopperStores/paramKeys.ts b/packages/commerce-sdk-react/src/hooks/ShopperStores/paramKeys.ts deleted file mode 100644 index 408428d5fe..0000000000 --- a/packages/commerce-sdk-react/src/hooks/ShopperStores/paramKeys.ts +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright (c) 2023, Salesforce, Inc. - * All rights reserved. - * 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 - */ - -const searchStores = [ - 'countryCode', - 'distanceUnit', - 'latitude', - 'longitude', - 'maxDistance', - 'postalCode', - 'siteId', - 'locale', - 'offset', - 'limit' -] as const - -const getStores = ['siteId', 'locale', 'ids'] as const - -export default { - searchStores, - getStores -} diff --git a/packages/commerce-sdk-react/src/hooks/ShopperStores/query.ts b/packages/commerce-sdk-react/src/hooks/ShopperStores/query.ts index 7b13ed731d..6998cf2623 100644 --- a/packages/commerce-sdk-react/src/hooks/ShopperStores/query.ts +++ b/packages/commerce-sdk-react/src/hooks/ShopperStores/query.ts @@ -5,10 +5,11 @@ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ import {UseQueryResult} from '@tanstack/react-query' +import {ShopperStores} from 'commerce-sdk-isomorphic' import {ApiClients, ApiQueryOptions, Argument, DataType, NullableParameters} from '../types' import useCommerceApi from '../useCommerceApi' import {useQuery} from '../useQuery' -import {mergeOptions, omitNullableParameters} from '../utils' +import {mergeOptions, omitNullableParameters, pickValidParams} from '../utils' import * as queryKeyHelpers from './queryKeyHelpers' type Client = ApiClients['shopperStores'] @@ -38,18 +39,19 @@ export const useSearchStores = ( type Data = DataType const {shopperStores: client} = useCommerceApi() const methodName = 'searchStores' - const requiredParameters = ['organizationId', 'siteId'] as const + const requiredParameters = ShopperStores.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)) + 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(netOptions, queryOptions, { + return useQuery({...netOptions, parameters}, queryOptions, { method, queryKey, requiredParameters @@ -75,18 +77,19 @@ export const useStores = ( type Data = DataType const {shopperStores: client} = useCommerceApi() const methodName = 'getStores' - const requiredParameters = ['organizationId', 'siteId', 'ids'] as const + const requiredParameters = ShopperStores.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)) + 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(netOptions, queryOptions, { + return useQuery({...netOptions, parameters}, queryOptions, { method, queryKey, requiredParameters diff --git a/packages/commerce-sdk-react/src/hooks/ShopperStores/queryKeyHelpers.ts b/packages/commerce-sdk-react/src/hooks/ShopperStores/queryKeyHelpers.ts index ce89943feb..1e0c985e6d 100644 --- a/packages/commerce-sdk-react/src/hooks/ShopperStores/queryKeyHelpers.ts +++ b/packages/commerce-sdk-react/src/hooks/ShopperStores/queryKeyHelpers.ts @@ -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 {ShopperStores} from 'commerce-sdk-isomorphic' +import {ShopperStores} 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 = ShopperStores<{shortCode: string}> @@ -46,15 +45,19 @@ export const searchStores: QueryKeyHelper<'searchStores'> = { '/store-search' ], queryKey: (params: Params<'searchStores'>) => { - const paramKeys = [...paramKeysMap['searchStores'], ...getCustomKeys(params)] - return [...searchStores.path(params), pick(params, paramKeys)] + return [ + ...searchStores.path(params), + pickValidParams(params, ShopperStores.paramKeys.searchStores) + ] } } export const getStores: QueryKeyHelper<'getStores'> = { path: (params) => ['/commerce-sdk-react', '/organizations/', params.organizationId, '/stores'], queryKey: (params: Params<'getStores'>) => { - const paramKeys = [...paramKeysMap['getStores'], ...getCustomKeys(params)] - return [...getStores.path(params), pick(params, paramKeys)] + return [ + ...getStores.path(params), + pickValidParams(params, ShopperStores.paramKeys.getStores) + ] } } diff --git a/packages/commerce-sdk-react/src/hooks/utils.test.ts b/packages/commerce-sdk-react/src/hooks/utils.test.ts index f1eedcfc7f..72de593c6c 100644 --- a/packages/commerce-sdk-react/src/hooks/utils.test.ts +++ b/packages/commerce-sdk-react/src/hooks/utils.test.ts @@ -6,7 +6,7 @@ */ import {ShopperBaskets} from 'commerce-sdk-isomorphic' -import {mergeOptions, getCustomKeys} from './utils' +import {mergeOptions, getCustomKeys, pickValidParams} from './utils' describe('Hook utils', () => { test('mergeOptions merges body, header, and options', () => { @@ -45,6 +45,25 @@ describe('Hook utils', () => { } }) }) + + test('pickValidParams', () => { + const parameters = { + basketId: '', + organizationId: '', + siteId: '', + locale: '', + hello: 1, + c_foo: 2 + } + const paramKeys = ['organizationId', 'basketId', 'siteId', 'locale'] as const + const result = pickValidParams(parameters, paramKeys) + + // @ts-expect-error: testing invalid property + expect(result.hello).toBeUndefined() + + expect(result.basketId).toBeDefined() + expect(result.c_foo).toBeDefined() + }) }) describe('getCustomKey', function () { diff --git a/packages/commerce-sdk-react/src/hooks/utils.ts b/packages/commerce-sdk-react/src/hooks/utils.ts index d85ff1fcb9..36faaf6911 100644 --- a/packages/commerce-sdk-react/src/hooks/utils.ts +++ b/packages/commerce-sdk-react/src/hooks/utils.ts @@ -101,6 +101,16 @@ export const pick = ( return picked } +/** Get a subset of the given parameters that has the given keys and may contain a custom parameter. */ +export const pickValidParams = ( + parameters: T, + endpointParamKeys: readonly K[] +): Pick & Record<`c_${string}`, any> => { + const customKeys = getCustomKeys(parameters) + const keys = [...endpointParamKeys, ...customKeys] + return pick(parameters, keys) +} + /** Removes keys with `null` or `undefined` values from the given object. */ export const omitNullable = (obj: T): NullToOptional => { // Assertion is not true, yet, but we make it so! @@ -136,5 +146,5 @@ export const getCustomKeys = (obj: T) => { if (typeof obj !== 'object' || obj === null) { throw new Error('Invalid input. Expecting an object as an input.') } - return Object.keys(obj).filter((key: string): key is `c_${string}` => key.startsWith('c_')) + return Object.keys(obj).filter((key): key is keyof T & `c_${string}` => key.startsWith('c_')) }