forked from vercel/commerce
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add dynamic API endpoints * Add missing dependency * Update api handlers * Updates * Fix build errors * Update package.json * Add checkout endpoint parser & update errors * Update tsconfig.json * Update cart.ts * Update parser * Update errors.ts * Update errors.ts * Move to Edge runtime * Revert to local * Fix switchable runtimes * Make nodejs default runtime * Update pnpm-lock.yaml * Update handlers * Fix build errors * Change headers
- Loading branch information
Showing
316 changed files
with
2,471 additions
and
2,165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,41 @@ | ||
// @ts-nocheck | ||
import type { CartEndpoint } from '.' | ||
import type { BigcommerceCart } from '../../../types' | ||
|
||
import getCartCookie from '../../utils/get-cart-cookie' | ||
|
||
import { normalizeCart } from '../../../lib/normalize' | ||
import { BigcommerceApiError } from '../../utils/errors' | ||
import getCartCookie from '../../utils/get-cart-cookie' | ||
import type { BigcommerceCart } from '../../../types' | ||
import type { CartEndpoint } from '.' | ||
|
||
// Return current cart info | ||
const getCart: CartEndpoint['handlers']['getCart'] = async ({ | ||
res, | ||
body: { cartId }, | ||
config, | ||
}) => { | ||
let result: { data?: BigcommerceCart } = {} | ||
|
||
if (cartId) { | ||
try { | ||
result = await config.storeApiFetch( | ||
const result = await config.storeApiFetch<{ | ||
data?: BigcommerceCart | ||
} | null>( | ||
`/v3/carts/${cartId}?include=line_items.physical_items.options,line_items.digital_items.options` | ||
) | ||
|
||
return { | ||
data: result?.data ? normalizeCart(result.data) : null, | ||
} | ||
} catch (error) { | ||
if (error instanceof BigcommerceApiError && error.status === 404) { | ||
// Remove the cookie if it exists but the cart wasn't found | ||
res.setHeader('Set-Cookie', getCartCookie(config.cartCookie)) | ||
return { | ||
headers: { 'Set-Cookie': getCartCookie(config.cartCookie) }, | ||
} | ||
} else { | ||
throw error | ||
} | ||
} | ||
} | ||
|
||
res.status(200).json({ | ||
data: result.data ? normalizeCart(result.data) : null, | ||
}) | ||
return { | ||
data: null, | ||
} | ||
} | ||
|
||
export default getCart |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 12 additions & 20 deletions
32
packages/bigcommerce/src/api/endpoints/cart/remove-item.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,26 @@ | ||
import type { CartEndpoint } from '.' | ||
|
||
import { normalizeCart } from '../../../lib/normalize' | ||
import getCartCookie from '../../utils/get-cart-cookie' | ||
import type { CartEndpoint } from '.' | ||
|
||
const removeItem: CartEndpoint['handlers']['removeItem'] = async ({ | ||
res, | ||
body: { cartId, itemId }, | ||
config, | ||
}) => { | ||
if (!cartId || !itemId) { | ||
return res.status(400).json({ | ||
data: null, | ||
errors: [{ message: 'Invalid request' }], | ||
}) | ||
} | ||
|
||
const result = await config.storeApiFetch<{ data: any } | null>( | ||
`/v3/carts/${cartId}/items/${itemId}?include=line_items.physical_items.options`, | ||
{ method: 'DELETE' } | ||
) | ||
const data = result?.data ?? null | ||
|
||
res.setHeader( | ||
'Set-Cookie', | ||
data | ||
? // Update the cart cookie | ||
getCartCookie(config.cartCookie, cartId, config.cartCookieMaxAge) | ||
: // Remove the cart cookie if the cart was removed (empty items) | ||
getCartCookie(config.cartCookie) | ||
) | ||
res.status(200).json({ data: data && normalizeCart(data) }) | ||
return { | ||
data: result?.data ? normalizeCart(result.data) : null, | ||
headers: { | ||
'Set-Cookie': result?.data | ||
? // Update the cart cookie | ||
getCartCookie(config.cartCookie, cartId, config.cartCookieMaxAge) | ||
: // Remove the cart cookie if the cart was removed (empty items) | ||
getCartCookie(config.cartCookie), | ||
}, | ||
} | ||
} | ||
|
||
export default removeItem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.