-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathqueryKeyHelpers.ts
More file actions
156 lines (149 loc) · 4.77 KB
/
queryKeyHelpers.ts
File metadata and controls
156 lines (149 loc) · 4.77 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
/*
* 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 {ShopperBasketsV2} 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 = ShopperBasketsV2<{shortCode: string}>
type Params<T extends keyof QueryKeys> = Partial<Argument<Client[T]>['parameters']>
export type QueryKeys = {
getBasket: [
'/commerce-sdk-react',
'/organizations/',
string | undefined,
'/baskets/v2/',
string | undefined,
Params<'getBasket'>
]
getPaymentMethodsForBasket: [
'/commerce-sdk-react',
'/organizations/',
string | undefined,
'/baskets/v2/',
string | undefined,
'/payment-methods',
Params<'getPaymentMethodsForBasket'>
]
getPriceBooksForBasket: [
'/commerce-sdk-react',
'/organizations/',
string | undefined,
'/baskets/v2/',
string | undefined,
'/price-books',
Params<'getPriceBooksForBasket'>
]
getShippingMethodsForShipment: [
'/commerce-sdk-react',
'/organizations/',
string | undefined,
'/baskets/v2/',
string | undefined,
'/shipments/',
string | undefined,
'/shipping-methods',
Params<'getShippingMethodsForShipment'>
]
getTaxesFromBasket: [
'/commerce-sdk-react',
'/organizations/',
string | undefined,
'/baskets/v2/',
string | undefined,
'/taxes',
Params<'getTaxesFromBasket'>
]
}
// 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 getBasket: QueryKeyHelper<'getBasket'> = {
path: (params) => [
'/commerce-sdk-react',
'/organizations/',
params?.organizationId,
'/baskets/v2/',
params?.basketId
],
queryKey: (params: Params<'getBasket'>) => {
return [
...getBasket.path(params),
pickValidParams(params || {}, ShopperBasketsV2.paramKeys.getBasket)
]
}
}
export const getPaymentMethodsForBasket: QueryKeyHelper<'getPaymentMethodsForBasket'> = {
path: (params) => [
'/commerce-sdk-react',
'/organizations/',
params?.organizationId,
'/baskets/v2/',
params?.basketId,
'/payment-methods'
],
queryKey: (params: Params<'getPaymentMethodsForBasket'>) => {
return [
...getPaymentMethodsForBasket.path(params),
pickValidParams(params || {}, ShopperBasketsV2.paramKeys.getPaymentMethodsForBasket)
]
}
}
export const getPriceBooksForBasket: QueryKeyHelper<'getPriceBooksForBasket'> = {
path: (params) => [
'/commerce-sdk-react',
'/organizations/',
params?.organizationId,
'/baskets/v2/',
params?.basketId,
'/price-books'
],
queryKey: (params: Params<'getPriceBooksForBasket'>) => {
return [
...getPriceBooksForBasket.path(params),
pickValidParams(params || {}, ShopperBasketsV2.paramKeys.getPriceBooksForBasket)
]
}
}
export const getShippingMethodsForShipment: QueryKeyHelper<'getShippingMethodsForShipment'> = {
path: (params) => [
'/commerce-sdk-react',
'/organizations/',
params?.organizationId,
'/baskets/v2/',
params?.basketId,
'/shipments/',
params?.shipmentId,
'/shipping-methods'
],
queryKey: (params: Params<'getShippingMethodsForShipment'>) => {
return [
...getShippingMethodsForShipment.path(params),
pickValidParams(params || {}, ShopperBasketsV2.paramKeys.getShippingMethodsForShipment)
]
}
}
export const getTaxesFromBasket: QueryKeyHelper<'getTaxesFromBasket'> = {
path: (params) => [
'/commerce-sdk-react',
'/organizations/',
params?.organizationId,
'/baskets/v2/',
params?.basketId,
'/taxes'
],
queryKey: (params: Params<'getTaxesFromBasket'>) => {
return [
...getTaxesFromBasket.path(params),
pickValidParams(params || {}, ShopperBasketsV2.paramKeys.getTaxesFromBasket)
]
}
}