Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/commerce-sdk-react/src/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const SERVER_AFFINITY_HEADER_KEY = 'sfdc_dwsid'

export const CLIENT_KEYS = {
SHOPPER_BASKETS: 'shopperBaskets',
SHOPPER_BASKETS_V2: 'shopperBasketsV2',
SHOPPER_CONFIGURATIONS: 'shopperConfigurations',
SHOPPER_CONTEXTS: 'shopperContexts',
SHOPPER_CUSTOMERS: 'shopperCustomers',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import {
ShopperBasketsV2Types,
ShopperBasketsTypes,
ShopperCustomers,
ShopperCustomersTypes
} from 'commerce-sdk-isomorphic'
Expand All @@ -30,7 +30,7 @@ import {CLIENT_KEYS} from '../../constant'
const CLIENT_KEY = CLIENT_KEYS.SHOPPER_BASKETS
type Client = NonNullable<ApiClients[typeof CLIENT_KEY]>
/** Data returned by every Shopper Baskets endpoint (except `deleteBasket`) */
type Basket = ShopperBasketsV2Types.Basket
type Basket = ShopperBasketsTypes.Basket
/** Data returned by `getCustomerBaskets` */
type CustomerBasketsResult = ShopperCustomersTypes.BasketsResult
/** Parameters that get passed around, includes client config and possible parameters from other endpoints */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {useShopperBasketsMutationHelper} from './helpers'
import {useCustomerBaskets} from '../ShopperCustomers'
import {renderWithProviders} from '../../test-utils'
import {screen, waitFor} from '@testing-library/react'
import {ShopperBasketsV2Types} from 'commerce-sdk-isomorphic'
import {ShopperBasketsTypes} from 'commerce-sdk-isomorphic'
import jwt from 'jsonwebtoken'

const basketId = '10cf6aa40edba4fcfcc6915594'
Expand Down Expand Up @@ -52,7 +52,7 @@ const MockComponent = () => {
price: 100,
quantity: 1
}
] as ShopperBasketsV2Types.ProductItem[] & Record<`c_${string}`, any>)
] as ShopperBasketsTypes.ProductItem[] & Record<`c_${string}`, any>)
.catch((e) => console.log('e', e))
}}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
import {useCustomerId, useShopperBasketsMutation} from '../index'
import {useCustomerBaskets} from '../ShopperCustomers'
import {ApiClients, Argument} from '../types'
import {ShopperBasketsV2Types} from 'commerce-sdk-isomorphic'
import {ShopperBasketsTypes} from 'commerce-sdk-isomorphic'
import {CLIENT_KEYS} from '../../constant'

const CLIENT_KEY = CLIENT_KEYS.SHOPPER_BASKETS
type Client = NonNullable<ApiClients[typeof CLIENT_KEY]>
type Basket = ShopperBasketsV2Types.Basket
type Basket = ShopperBasketsTypes.Basket

/**
* This is a helper function for Basket Mutations.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* 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 {ShopperBasketsV2} from 'commerce-sdk-isomorphic'
import {ShopperBaskets} from 'commerce-sdk-isomorphic'
import {getUnimplementedEndpoints} from '../../test-utils'
import {cacheUpdateMatrix} from './cache'
import {ShopperBasketsMutations as mutations} from './mutation'
Expand All @@ -13,7 +13,7 @@ import * as queries from './query'
describe('Shopper Baskets hooks', () => {
test('all endpoints have hooks', () => {
// unimplemented = SDK method exists, but no query hook or value in mutations enum
const unimplemented = getUnimplementedEndpoints(ShopperBasketsV2, queries, mutations)
const unimplemented = getUnimplementedEndpoints(ShopperBaskets, queries, mutations)
// If this test fails: create a new query hook, add the endpoint to the mutations enum,
// or add it to the `expected` array with a comment explaining "TODO" or "never" (and why).
expect(unimplemented).toEqual([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import {act} from '@testing-library/react'
import {ShopperBasketsV2Types, ShopperCustomersTypes} from 'commerce-sdk-isomorphic'
import {ShopperBasketsTypes, ShopperCustomersTypes} from 'commerce-sdk-isomorphic'
import nock from 'nock'
import {
assertInvalidateQuery,
Expand All @@ -32,9 +32,9 @@ jest.mock('../../auth/index.ts', () => {

const CLIENT_KEY = CLIENT_KEYS.SHOPPER_BASKETS
type Client = NonNullable<ApiClients[typeof CLIENT_KEY]>
type Basket = ShopperBasketsV2Types.Basket
type Basket = ShopperBasketsTypes.Basket
type BasketsResult = ShopperCustomersTypes.BasketsResult
type ProductItem = ShopperBasketsV2Types.ProductItem
type ProductItem = ShopperBasketsTypes.ProductItem

/** Create an options object for Shopper Baskets endpoints, with `basketId` pre-filled. */
const createOptions = <Method extends Exclude<keyof Client, 'clientConfig'>>(
Expand Down
37 changes: 11 additions & 26 deletions packages/commerce-sdk-react/src/hooks/ShopperBaskets/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {ApiClients, ApiQueryOptions, Argument, DataType, NullableParameters} fro
import {useQuery} from '../useQuery'
import {mergeOptions, omitNullableParameters, pickValidParams} from '../utils'
import * as queryKeyHelpers from './queryKeyHelpers'
import {ShopperBasketsV2} from 'commerce-sdk-isomorphic'
import {ShopperBaskets} from 'commerce-sdk-isomorphic'
import {CLIENT_KEYS} from '../../constant'
import useCommerceApi from '../useCommerceApi'

Expand All @@ -35,15 +35,12 @@ export const useBasket = (
type Data = DataType<Client['getBasket']>
const client = useCommerceApi(CLIENT_KEY)
const methodName = 'getBasket'
const requiredParameters = ShopperBasketsV2.paramKeys[`${methodName}Required`]
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))
const parameters = pickValidParams(
netOptions.parameters,
ShopperBasketsV2.paramKeys[methodName]
)
const parameters = pickValidParams(netOptions.parameters, ShopperBaskets.paramKeys[methodName])
const queryKey = queryKeyHelpers[methodName].queryKey(netOptions.parameters)
// We don't use `netOptions` here because we manipulate the options in `useQuery`.
const method = async (options: Options) => await client[methodName](options)
Expand Down Expand Up @@ -80,15 +77,12 @@ export const usePaymentMethodsForBasket = (
type Data = DataType<Client['getPaymentMethodsForBasket']>
const client = useCommerceApi(CLIENT_KEY)
const methodName = 'getPaymentMethodsForBasket'
const requiredParameters = ShopperBasketsV2.paramKeys[`${methodName}Required`]
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))
const parameters = pickValidParams(
netOptions.parameters,
ShopperBasketsV2.paramKeys[methodName]
)
const parameters = pickValidParams(netOptions.parameters, ShopperBaskets.paramKeys[methodName])
const queryKey = queryKeyHelpers[methodName].queryKey(netOptions.parameters)
// We don't use `netOptions` here because we manipulate the options in `useQuery`.
const method = async (options: Options) => await client[methodName](options)
Expand Down Expand Up @@ -125,15 +119,12 @@ export const usePriceBooksForBasket = (
type Data = DataType<Client['getPriceBooksForBasket']>
const client = useCommerceApi(CLIENT_KEY)
const methodName = 'getPriceBooksForBasket'
const requiredParameters = ShopperBasketsV2.paramKeys[`${methodName}Required`]
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))
const parameters = pickValidParams(
netOptions.parameters,
ShopperBasketsV2.paramKeys[methodName]
)
const parameters = pickValidParams(netOptions.parameters, ShopperBaskets.paramKeys[methodName])
const queryKey = queryKeyHelpers[methodName].queryKey(netOptions.parameters)
// We don't use `netOptions` here because we manipulate the options in `useQuery`.
const method = async (options: Options) => await client[methodName](options)
Expand Down Expand Up @@ -170,15 +161,12 @@ export const useShippingMethodsForShipment = (
type Data = DataType<Client['getShippingMethodsForShipment']>
const client = useCommerceApi(CLIENT_KEY)
const methodName = 'getShippingMethodsForShipment'
const requiredParameters = ShopperBasketsV2.paramKeys[`${methodName}Required`]
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))
const parameters = pickValidParams(
netOptions.parameters,
ShopperBasketsV2.paramKeys[methodName]
)
const parameters = pickValidParams(netOptions.parameters, ShopperBaskets.paramKeys[methodName])
const queryKey = queryKeyHelpers[methodName].queryKey(netOptions.parameters)
// We don't use `netOptions` here because we manipulate the options in `useQuery`.
const method = async (options: Options) => await client[methodName](options)
Expand Down Expand Up @@ -215,15 +203,12 @@ export const useTaxesFromBasket = (
type Data = DataType<Client['getTaxesFromBasket']>
const client = useCommerceApi(CLIENT_KEY)
const methodName = 'getTaxesFromBasket'
const requiredParameters = ShopperBasketsV2.paramKeys[`${methodName}Required`]
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))
const parameters = pickValidParams(
netOptions.parameters,
ShopperBasketsV2.paramKeys[methodName]
)
const parameters = pickValidParams(netOptions.parameters, ShopperBaskets.paramKeys[methodName])
const queryKey = queryKeyHelpers[methodName].queryKey(netOptions.parameters)
// We don't use `netOptions` here because we manipulate the options in `useQuery`.
const method = async (options: Options) => await client[methodName](options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
* 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 {ShopperBasketsV2} from 'commerce-sdk-isomorphic'
import {ShopperBaskets} from 'commerce-sdk-isomorphic'
import {Argument, ExcludeTail} from '../types'
import {pickValidParams} from '../utils'

// We must use a client with no parameters in order to have required/optional match the API spec
type Client = ShopperBasketsV2<{shortCode: string}>
type Client = ShopperBaskets<{shortCode: string}>
type Params<T extends keyof QueryKeys> = Partial<Argument<Client[T]>['parameters']>
export type QueryKeys = {
getBasket: [
Expand Down Expand Up @@ -80,7 +80,7 @@ export const getBasket: QueryKeyHelper<'getBasket'> = {
queryKey: (params: Params<'getBasket'>) => {
return [
...getBasket.path(params),
pickValidParams(params || {}, ShopperBasketsV2.paramKeys.getBasket)
pickValidParams(params || {}, ShopperBaskets.paramKeys.getBasket)
]
}
}
Expand All @@ -97,7 +97,7 @@ export const getPaymentMethodsForBasket: QueryKeyHelper<'getPaymentMethodsForBas
queryKey: (params: Params<'getPaymentMethodsForBasket'>) => {
return [
...getPaymentMethodsForBasket.path(params),
pickValidParams(params || {}, ShopperBasketsV2.paramKeys.getPaymentMethodsForBasket)
pickValidParams(params || {}, ShopperBaskets.paramKeys.getPaymentMethodsForBasket)
]
}
}
Expand All @@ -114,7 +114,7 @@ export const getPriceBooksForBasket: QueryKeyHelper<'getPriceBooksForBasket'> =
queryKey: (params: Params<'getPriceBooksForBasket'>) => {
return [
...getPriceBooksForBasket.path(params),
pickValidParams(params || {}, ShopperBasketsV2.paramKeys.getPriceBooksForBasket)
pickValidParams(params || {}, ShopperBaskets.paramKeys.getPriceBooksForBasket)
]
}
}
Expand All @@ -133,7 +133,7 @@ export const getShippingMethodsForShipment: QueryKeyHelper<'getShippingMethodsFo
queryKey: (params: Params<'getShippingMethodsForShipment'>) => {
return [
...getShippingMethodsForShipment.path(params),
pickValidParams(params || {}, ShopperBasketsV2.paramKeys.getShippingMethodsForShipment)
pickValidParams(params || {}, ShopperBaskets.paramKeys.getShippingMethodsForShipment)
]
}
}
Expand All @@ -150,7 +150,7 @@ export const getTaxesFromBasket: QueryKeyHelper<'getTaxesFromBasket'> = {
queryKey: (params: Params<'getTaxesFromBasket'>) => {
return [
...getTaxesFromBasket.path(params),
pickValidParams(params || {}, ShopperBasketsV2.paramKeys.getTaxesFromBasket)
pickValidParams(params || {}, ShopperBaskets.paramKeys.getTaxesFromBasket)
]
}
}
Loading
Loading