-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathqueryKeyHelpers.ts
More file actions
50 lines (46 loc) · 2.02 KB
/
queryKeyHelpers.ts
File metadata and controls
50 lines (46 loc) · 2.02 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
/*
* Copyright (c) 2025, 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 {ShopperPayments} from 'commerce-sdk-isomorphic'
import {Argument, ExcludeTail} from '../types'
import {pickValidParams, omitNullable} from '../utils'
// We must use a client with no parameters in order to have required/optional match the API spec
type Client = ShopperPayments<{shortCode: string}>
type Params<T extends keyof QueryKeys> = Partial<Argument<Client[T]>['parameters']>
export type QueryKeys = {
getPaymentConfiguration: [
'/commerce-sdk-react',
'/organizations/',
string | undefined,
'/payments/',
Params<'getPaymentConfiguration'>
]
}
// This is defined here, rather than `types.ts`, because it relies on `Client` and `QueryKeys`,
// and making those generic would add too much complexity.
type QueryKeyHelper<T extends keyof QueryKeys> = {
/** Generates the path component of the query key for an endpoint. */
path: (params: Params<T>) => ExcludeTail<QueryKeys[T]>
/** Generates the complete query key for an endpoint. */
queryKey: (params: Params<T>) => QueryKeys[T]
}
export const getPaymentConfiguration: QueryKeyHelper<'getPaymentConfiguration'> = {
path: (params) => [
'/commerce-sdk-react',
'/organizations/',
params?.organizationId,
'/payments/'
],
queryKey: (params: Params<'getPaymentConfiguration'>) => {
return [
...getPaymentConfiguration.path(params),
// pickValidParams returns the filtered parameters, TypeScript sees that zoneId
// could be string | null, and complains because null isn't allowed in the query key.
// omitNullable removes null values from the parameters (zoneId is optional but NOT nullable)
omitNullable(pickValidParams(params || {}, ShopperPayments.paramKeys.getPaymentConfiguration))
]
}
}