-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathquery.ts
More file actions
243 lines (226 loc) · 14.2 KB
/
query.ts
File metadata and controls
243 lines (226 loc) · 14.2 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* 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 {useQuery} from '../useQuery'
import {mergeOptions, omitNullableParameters, pickValidParams} from '../utils'
import * as queryKeyHelpers from './queryKeyHelpers'
import {ShopperBasketsV2} from 'commerce-sdk-isomorphic'
import {CLIENT_KEYS} from '../../constant'
import useCommerceApi from '../useCommerceApi'
const CLIENT_KEY = CLIENT_KEYS.SHOPPER_BASKETS_V2
type Client = NonNullable<ApiClients[typeof CLIENT_KEY]>
/**
* Gets a basket.
* @group ShopperBaskets
* @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 Baskets `getBasket` endpoint.
* @see {@link https://developer.salesforce.com/docs/commerce/commerce-api/references/shopper-baskets?meta=getBasket| Salesforce Developer Center} for more information about the API endpoint.
* @see {@link https://salesforcecommercecloud.github.io/commerce-sdk-isomorphic/classes/shopperbaskets.shopperbaskets-1.html#getbasket | `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 useBasket = (
apiOptions: NullableParameters<Argument<Client['getBasket']>>,
queryOptions: ApiQueryOptions<Client['getBasket']> = {}
): UseQueryResult<DataType<Client['getBasket']>> => {
type Options = Argument<Client['getBasket']>
type Data = DataType<Client['getBasket']>
const client = useCommerceApi(CLIENT_KEY)
const methodName = 'getBasket'
const requiredParameters = ShopperBasketsV2.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 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)
queryOptions.meta = {
displayName: 'useBasket',
...queryOptions.meta
}
// 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, parameters}, queryOptions, {
method,
queryKey,
requiredParameters
})
}
/**
* Gets applicable payment methods for an existing basket considering the open payment amount only.
* @group ShopperBaskets
* @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 Baskets `getPaymentMethodsForBasket` endpoint.
* @see {@link https://developer.salesforce.com/docs/commerce/commerce-api/references/shopper-baskets?meta=getPaymentMethodsForBasket| Salesforce Developer Center} for more information about the API endpoint.
* @see {@link https://salesforcecommercecloud.github.io/commerce-sdk-isomorphic/classes/shopperbaskets.shopperbaskets-1.html#getpaymentmethodsforbasket | `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 usePaymentMethodsForBasket = (
apiOptions: NullableParameters<Argument<Client['getPaymentMethodsForBasket']>>,
queryOptions: ApiQueryOptions<Client['getPaymentMethodsForBasket']> = {}
): UseQueryResult<DataType<Client['getPaymentMethodsForBasket']>> => {
type Options = Argument<Client['getPaymentMethodsForBasket']>
type Data = DataType<Client['getPaymentMethodsForBasket']>
const client = useCommerceApi(CLIENT_KEY)
const methodName = 'getPaymentMethodsForBasket'
const requiredParameters = ShopperBasketsV2.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 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)
queryOptions.meta = {
displayName: 'usePaymentMethodsForBasket',
...queryOptions.meta
}
// 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, parameters}, queryOptions, {
method,
queryKey,
requiredParameters
})
}
/**
* Gets applicable price books for an existing basket.
* @group ShopperBaskets
* @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 Baskets `getPriceBooksForBasket` endpoint.
* @see {@link https://developer.salesforce.com/docs/commerce/commerce-api/references/shopper-baskets?meta=getPriceBooksForBasket| Salesforce Developer Center} for more information about the API endpoint.
* @see {@link https://salesforcecommercecloud.github.io/commerce-sdk-isomorphic/classes/shopperbaskets.shopperbaskets-1.html#getpricebooksforbasket | `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 usePriceBooksForBasket = (
apiOptions: NullableParameters<Argument<Client['getPriceBooksForBasket']>>,
queryOptions: ApiQueryOptions<Client['getPriceBooksForBasket']> = {}
): UseQueryResult<DataType<Client['getPriceBooksForBasket']>> => {
type Options = Argument<Client['getPriceBooksForBasket']>
type Data = DataType<Client['getPriceBooksForBasket']>
const client = useCommerceApi(CLIENT_KEY)
const methodName = 'getPriceBooksForBasket'
const requiredParameters = ShopperBasketsV2.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 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)
queryOptions.meta = {
displayName: 'usePriceBooksForBasket',
...queryOptions.meta
}
// 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, parameters}, queryOptions, {
method,
queryKey,
requiredParameters
})
}
/**
* Gets the applicable shipping methods for a certain shipment of a basket.
* @group ShopperBaskets
* @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 Baskets `getShippingMethodsForShipment` endpoint.
* @see {@link https://developer.salesforce.com/docs/commerce/commerce-api/references/shopper-baskets?meta=getShippingMethodsForShipment| Salesforce Developer Center} for more information about the API endpoint.
* @see {@link https://salesforcecommercecloud.github.io/commerce-sdk-isomorphic/classes/shopperbaskets.shopperbaskets-1.html#getshippingmethodsforshipment | `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 useShippingMethodsForShipment = (
apiOptions: NullableParameters<Argument<Client['getShippingMethodsForShipment']>>,
queryOptions: ApiQueryOptions<Client['getShippingMethodsForShipment']> = {}
): UseQueryResult<DataType<Client['getShippingMethodsForShipment']>> => {
type Options = Argument<Client['getShippingMethodsForShipment']>
type Data = DataType<Client['getShippingMethodsForShipment']>
const client = useCommerceApi(CLIENT_KEY)
const methodName = 'getShippingMethodsForShipment'
const requiredParameters = ShopperBasketsV2.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 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)
queryOptions.meta = {
displayName: 'useShippingMethodsForShipment',
...queryOptions.meta
}
// 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, parameters}, queryOptions, {
method,
queryKey,
requiredParameters
})
}
/**
* This method gives you the external taxation data set by the PUT taxes API. This endpoint can be called only if external taxation mode was used for basket creation. See POST /baskets for more information.
* @group ShopperBaskets
* @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 Baskets `getTaxesFromBasket` endpoint.
* @see {@link https://developer.salesforce.com/docs/commerce/commerce-api/references/shopper-baskets?meta=getTaxesFromBasket| Salesforce Developer Center} for more information about the API endpoint.
* @see {@link https://salesforcecommercecloud.github.io/commerce-sdk-isomorphic/classes/shopperbaskets.shopperbaskets-1.html#gettaxesfrombasket | `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 useTaxesFromBasket = (
apiOptions: NullableParameters<Argument<Client['getTaxesFromBasket']>>,
queryOptions: ApiQueryOptions<Client['getTaxesFromBasket']> = {}
): UseQueryResult<DataType<Client['getTaxesFromBasket']>> => {
type Options = Argument<Client['getTaxesFromBasket']>
type Data = DataType<Client['getTaxesFromBasket']>
const client = useCommerceApi(CLIENT_KEY)
const methodName = 'getTaxesFromBasket'
const requiredParameters = ShopperBasketsV2.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 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)
queryOptions.meta = {
displayName: 'useTaxesFromBasket',
...queryOptions.meta
}
// 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, parameters}, queryOptions, {
method,
queryKey,
requiredParameters
})
}