|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, Salesforce, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * SPDX-License-Identifier: BSD-3-Clause |
| 5 | + * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | +import {UseQueryResult} from '@tanstack/react-query' |
| 8 | +import {ShopperConfigurations} from 'commerce-sdk-isomorphic' |
| 9 | +import {ApiClients, ApiQueryOptions, Argument, DataType, NullableParameters} from '../types' |
| 10 | +import {useQuery} from '../useQuery' |
| 11 | +import {mergeOptions, omitNullableParameters, pickValidParams} from '../utils' |
| 12 | +import * as queryKeyHelpers from './queryKeyHelpers' |
| 13 | +import {CLIENT_KEYS} from '../../constant' |
| 14 | +import useCommerceApi from '../useCommerceApi' |
| 15 | + |
| 16 | +const CLIENT_KEY = CLIENT_KEYS.SHOPPER_CONFIGURATIONS |
| 17 | +type Client = NonNullable<ApiClients[typeof CLIENT_KEY]> |
| 18 | + |
| 19 | +/** |
| 20 | + * Gets configuration information that encompasses toggles, preferences, and configuration that allow the application to be reactive to changes performed by the merchant, admin, or support engineer. |
| 21 | + * |
| 22 | + * @group ShopperConfigurations |
| 23 | + * @category Query |
| 24 | + * @parameter apiOptions - Options to pass through to `commerce-sdk-isomorphic`, with `null` accepted for unset API parameters. |
| 25 | + * @parameter queryOptions - TanStack Query query options, with `enabled` by default set to check that all required API parameters have been set. |
| 26 | + * @returns A TanStack Query query hook with data from the Shopper Configurations `getConfigurations` endpoint. |
| 27 | + * @see {@link https://developer.salesforce.com/docs/commerce/commerce-api/references/shopper-configurations?meta=getConfigurations| Salesforce Developer Center} for more information about the API endpoint. |
| 28 | + * @see {@link https://salesforcecommercecloud.github.io/commerce-sdk-isomorphic/classes/shopperconfigurations.shopperconfigurations-1.html#getconfigurations | `commerce-sdk-isomorphic` documentation} for more information on the parameters and returned data type. |
| 29 | + * @see {@link https://tanstack.com/query/latest/docs/react/reference/useQuery | TanStack Query `useQuery` reference} for more information about the return value. |
| 30 | + */ |
| 31 | +export const useConfigurations = ( |
| 32 | + apiOptions: NullableParameters<Argument<Client['getConfigurations']>>, |
| 33 | + queryOptions: ApiQueryOptions<Client['getConfigurations']> = {} |
| 34 | +): UseQueryResult<DataType<Client['getConfigurations']>> => { |
| 35 | + type Options = Argument<Client['getConfigurations']> |
| 36 | + type Data = DataType<Client['getConfigurations']> |
| 37 | + const client = useCommerceApi(CLIENT_KEY) |
| 38 | + const methodName = 'getConfigurations' |
| 39 | + const requiredParameters = ShopperConfigurations.paramKeys[`${methodName}Required`] |
| 40 | + |
| 41 | + // Parameters can be set in `apiOptions` or `client.clientConfig` |
| 42 | + // we must merge them in order to generate the correct query key. |
| 43 | + const netOptions = omitNullableParameters(mergeOptions(client, apiOptions || {})) |
| 44 | + const parameters = pickValidParams( |
| 45 | + netOptions.parameters, |
| 46 | + ShopperConfigurations.paramKeys[methodName] |
| 47 | + ) |
| 48 | + const queryKey = queryKeyHelpers[methodName].queryKey(netOptions.parameters) |
| 49 | + // We don't use `netOptions` here because we manipulate the options in `useQuery`. |
| 50 | + const method = async (options: Options) => await client[methodName](options) |
| 51 | + |
| 52 | + queryOptions.meta = { |
| 53 | + displayName: 'useConfigurations', |
| 54 | + ...queryOptions.meta |
| 55 | + } |
| 56 | + |
| 57 | + return useQuery<Client, Options, Data>({...netOptions, parameters}, queryOptions, { |
| 58 | + method, |
| 59 | + queryKey, |
| 60 | + requiredParameters |
| 61 | + }) |
| 62 | +} |
0 commit comments