Skip to content

Commit 5b939a5

Browse files
authored
refactor: lighten cart data method retrieval (#473)
1 parent b27e173 commit 5b939a5

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/lib/data/cart.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ import { getRegion } from "./regions"
2020
* @param cartId - optional - The ID of the cart to retrieve.
2121
* @returns The cart object if found, or null if not found.
2222
*/
23-
export async function retrieveCart(cartId?: string) {
23+
export async function retrieveCart(cartId?: string, fields?: string) {
2424
const id = cartId || (await getCartId())
25+
fields ??= "*items, *region, *items.product, *items.variant, *items.thumbnail, *items.metadata, +items.total, *promotions, +shipping_methods.name"
2526

2627
if (!id) {
2728
return null
@@ -39,14 +40,13 @@ export async function retrieveCart(cartId?: string) {
3940
.fetch<HttpTypes.StoreCartResponse>(`/store/carts/${id}`, {
4041
method: "GET",
4142
query: {
42-
fields:
43-
"*items, *region, *items.product, *items.variant, *items.thumbnail, *items.metadata, +items.total, *promotions, +shipping_methods.name",
43+
fields
4444
},
4545
headers,
4646
next,
4747
cache: "force-cache",
4848
})
49-
.then(({ cart }) => cart)
49+
.then(({ cart }: { cart: HttpTypes.StoreCart }) => cart)
5050
.catch(() => null)
5151
}
5252

@@ -57,7 +57,7 @@ export async function getOrSetCart(countryCode: string) {
5757
throw new Error(`Region not found for country code: ${countryCode}`)
5858
}
5959

60-
let cart = await retrieveCart()
60+
let cart = await retrieveCart(undefined, 'id,region_id')
6161

6262
const headers = {
6363
...(await getAuthHeaders()),
@@ -99,7 +99,7 @@ export async function updateCart(data: HttpTypes.StoreUpdateCart) {
9999

100100
return sdk.store.cart
101101
.update(cartId, data, {}, headers)
102-
.then(async ({ cart }) => {
102+
.then(async ({ cart }: { cart: HttpTypes.StoreCart }) => {
103103
const cartCacheTag = await getCacheTag("carts")
104104
revalidateTag(cartCacheTag)
105105

0 commit comments

Comments
 (0)