-
Notifications
You must be signed in to change notification settings - Fork 122
/
query.ts
62 lines (58 loc) · 3.46 KB
/
query.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* 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
*/
import {UseQueryResult} from '@tanstack/react-query'
import {ApiClients, ApiQueryOptions, Argument, DataType, NullableParameters} from '../types'
import useCommerceApi from '../useCommerceApi'
import {useQuery} from '../useQuery'
import {mergeOptions, omitNullableParameters} from '../utils'
import * as queryKeyHelpers from './queryKeyHelpers'
type Client = ApiClients['shopperGiftCertificates']
/**
* Action to retrieve an existing gift certificate.
* @group ShopperGiftCertificates
* @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 Gift Certificates `getGiftCertificate` endpoint.
* @see {@link https://developer.salesforce.com/docs/commerce/commerce-api/references/shopper-gift-certificates?meta=getGiftCertificate| Salesforce Developer Center} for more information about the API endpoint.
* @see {@link https://salesforcecommercecloud.github.io/commerce-sdk-isomorphic/classes/shoppergiftcertificates.shoppergiftcertificates-1.html#getgiftcertificate | `commerce-sdk-isomorphic` documentation} for more information on the parameters and returned data type.
* @see {@link https://tanstack.com/query/latest/docs/react/reference/useQuery | TanStack Query `useQuery` reference} for more information about the return value.
*/
export const useGiftCertificate = (
apiOptions: NullableParameters<Argument<Client['getGiftCertificate']>>,
queryOptions: ApiQueryOptions<Client['getGiftCertificate']> = {}
): UseQueryResult<DataType<Client['getGiftCertificate']>> => {
type Options = Argument<Client['getGiftCertificate']>
type Data = DataType<Client['getGiftCertificate']>
const {shopperGiftCertificates: client} = useCommerceApi()
const methodName = 'getGiftCertificate'
const requiredParameters = ['organizationId', 'siteId'] as const
// 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 queryKey = queryKeyHelpers[methodName].queryKey(netOptions.parameters)
// We don't use `netOptions` here because we manipulate the options in `useQuery`.
const method = async (options: Options) => await client[methodName](options)
// For some reason, if we don't explicitly set these generic parameters, the inferred type for
// `Data` sometimes, but not always, includes `Response`, which is incorrect. I don't know why.
return useQuery<Client, Options, Data>(
netOptions,
{
// !!! This is a violation of our design goal of minimal logic in the indivudal endpoint
// endpoint hooks. This is because this method is a post method, rather than GET,
// and its body contains secrets. Setting cacheTime to 0 avoids exposing the secrets in
// the shared cache.
cacheTime: 0,
...queryOptions
},
{
method,
queryKey,
requiredParameters
}
)
}