Skip to content

refactor: lighten cart data method retrieval #473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/lib/data/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import { getRegion } from "./regions"
* @param cartId - optional - The ID of the cart to retrieve.
* @returns The cart object if found, or null if not found.
*/
export async function retrieveCart(cartId?: string) {
export async function retrieveCart(cartId?: string, fields?: string) {
const id = cartId || (await getCartId())
fields ??= "*items, *region, *items.product, *items.variant, *items.thumbnail, *items.metadata, +items.total, *promotions, +shipping_methods.name"

if (!id) {
return null
Expand All @@ -39,14 +40,13 @@ export async function retrieveCart(cartId?: string) {
.fetch<HttpTypes.StoreCartResponse>(`/store/carts/${id}`, {
method: "GET",
query: {
fields:
"*items, *region, *items.product, *items.variant, *items.thumbnail, *items.metadata, +items.total, *promotions, +shipping_methods.name",
fields
},
headers,
next,
cache: "force-cache",
})
.then(({ cart }) => cart)
.then(({ cart }: { cart: HttpTypes.StoreCart }) => cart)
.catch(() => null)
}

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

let cart = await retrieveCart()
let cart = await retrieveCart(undefined, 'id,region_id')

const headers = {
...(await getAuthHeaders()),
Expand Down Expand Up @@ -99,7 +99,7 @@ export async function updateCart(data: HttpTypes.StoreUpdateCart) {

return sdk.store.cart
.update(cartId, data, {}, headers)
.then(async ({ cart }) => {
.then(async ({ cart }: { cart: HttpTypes.StoreCart }) => {
const cartCacheTag = await getCacheTag("carts")
revalidateTag(cartCacheTag)

Expand Down