-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathqueryKeyHelpers.ts
More file actions
46 lines (43 loc) · 1.68 KB
/
queryKeyHelpers.ts
File metadata and controls
46 lines (43 loc) · 1.68 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
/*
* 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 {ShopperConfigurations} 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 = ShopperConfigurations<{shortCode: string}>
type Params<T extends keyof QueryKeys> = Partial<Argument<Client[T]>['parameters']>
export type QueryKeys = {
getConfigurations: [
'/commerce-sdk-react',
'/organizations/',
string | undefined,
'/configurations',
Params<'getConfigurations'>
]
}
// 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 full query key for an endpoint. */
queryKey: (params: Params<T>) => QueryKeys[T]
}
export const getConfigurations: QueryKeyHelper<'getConfigurations'> = {
path: (params) => [
'/commerce-sdk-react',
'/organizations/',
params?.organizationId,
'/configurations'
],
queryKey: (params: Params<'getConfigurations'>) => {
return [
...getConfigurations.path(params),
pickValidParams(params || {}, ShopperConfigurations.paramKeys.getConfigurations)
]
}
}