1
0
mirror of https://github.com/vercel/commerce.git synced 2025-07-27 04:01:23 +00:00
Files
.vscode
packages
bigcommerce
commerce
commercejs
kibocommerce
local
ordercloud
reactioncommerce
api
endpoints
cart
add-item.ts
get-cart.ts
index.ts
catalog
checkout
customers
operations
utils
customer.ts
index.ts
auth
cart
common
customer
product
utils
wishlist
.env.template
README.md
codegen.json
commerce.config.json
const.ts
fetcher.ts
index.tsx
next.config.js
provider.ts
schema.d.ts
schema.graphql
types.ts
saleor
shopify
spree
swell
taskr-swc
vendure
site
.editorconfig
.gitignore
.prettierignore
.prettierrc
README.md
license.md
package.json
turbo.json
yarn.lock
commerce/packages/reactioncommerce/api/endpoints/cart/get-cart.ts

98 lines
2.7 KiB
TypeScript

import getAnonymousCartQuery from '@framework/utils/queries/get-anonymous-cart'
import accountCartByAccountIdQuery from '@framework/utils/queries/account-cart-by-account-id'
import getCartCookie from '@framework/api/utils/get-cart-cookie'
import reconcileCarts from '@framework/api/utils/reconcile-carts'
import getViewerId from '@framework/customer/get-viewer-id'
import {
REACTION_ANONYMOUS_CART_TOKEN_COOKIE,
REACTION_CART_ID_COOKIE,
REACTION_CUSTOMER_TOKEN_COOKIE,
} from '@framework/const'
import { normalizeCart } from '@framework/utils'
import type { CartEndpoint } from '.'
const getCart: CartEndpoint['handlers']['getCart'] = async ({
req,
res,
config,
}) => {
const {
cookies: {
[REACTION_ANONYMOUS_CART_TOKEN_COOKIE]: anonymousCartToken,
[REACTION_CART_ID_COOKIE]: cartId,
[REACTION_CUSTOMER_TOKEN_COOKIE]: reactionCustomerToken,
},
} = req
let normalizedCart
if (cartId && anonymousCartToken && reactionCustomerToken) {
const rawReconciledCart = await reconcileCarts({
config,
cartId,
anonymousCartToken,
reactionCustomerToken,
})
normalizedCart = normalizeCart(rawReconciledCart)
// Clear the anonymous cart token cookie and update cart ID cookie
res.setHeader('Set-Cookie', [
getCartCookie(config.anonymousCartTokenCookie),
getCartCookie(config.cartIdCookie, normalizedCart.id, 999),
])
} else if (cartId && anonymousCartToken) {
const {
data: { cart: rawAnonymousCart },
} = await config.fetch(getAnonymousCartQuery, {
variables: {
cartId,
cartToken: anonymousCartToken,
},
})
normalizedCart = normalizeCart(rawAnonymousCart)
} else if (reactionCustomerToken && !anonymousCartToken) {
const accountId = await getViewerId({
customerToken: reactionCustomerToken,
config,
})
const {
data: { cart: rawAccountCart },
} = await config.fetch(
accountCartByAccountIdQuery,
{
variables: {
accountId,
shopId: config.shopId,
},
},
{
headers: {
Authorization: `Bearer ${reactionCustomerToken}`,
},
}
)
normalizedCart = normalizeCart(rawAccountCart)
if (cartId !== normalizedCart.id) {
res.setHeader(
'Set-Cookie',
getCartCookie(config.cartIdCookie, rawAccountCart._id, 999)
)
}
} else {
// If there's no cart for now, store a dummy cart ID to keep Next Commerce happy
res.setHeader(
'Set-Cookie',
getCartCookie(config.cartIdCookie, config.dummyEmptyCartId, 999)
)
}
res.status(200).json({ data: normalizedCart ?? null })
}
export default getCart