mirror of
https://github.com/vercel/commerce.git
synced 2025-07-27 04:01:23 +00:00
Implement Shopify Provider
This commit is contained in:
20
framework/shopify/cart/utils/checkout-create.ts
Normal file
20
framework/shopify/cart/utils/checkout-create.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { SHOPIFY_CHECKOUT_COOKIE } from '@framework'
|
||||
import checkoutCreateMutation from '@framework/utils/mutations/checkout-create'
|
||||
import Cookies from 'js-cookie'
|
||||
|
||||
export const createCheckout = async (fetch: any) => {
|
||||
const data = await fetch({
|
||||
query: checkoutCreateMutation,
|
||||
})
|
||||
|
||||
const checkout = data?.checkoutCreate?.checkout
|
||||
const checkoutId = checkout?.id
|
||||
|
||||
if (checkoutId) {
|
||||
Cookies.set(SHOPIFY_CHECKOUT_COOKIE, checkoutId)
|
||||
}
|
||||
|
||||
return checkout
|
||||
}
|
||||
|
||||
export default createCheckout
|
57
framework/shopify/cart/utils/checkout-to-cart.ts
Normal file
57
framework/shopify/cart/utils/checkout-to-cart.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { Cart } from '@commerce/types'
|
||||
import { CommerceError, ValidationError } from '@commerce/utils/errors'
|
||||
import { Checkout, CheckoutLineItemEdge, Maybe } from '@framework/schema'
|
||||
|
||||
const checkoutToCart = (checkoutResponse?: any): Maybe<Cart> => {
|
||||
if (!checkoutResponse) {
|
||||
throw new CommerceError({
|
||||
message: 'Missing checkout details from response cart Response',
|
||||
})
|
||||
}
|
||||
|
||||
const {
|
||||
checkout,
|
||||
userErrors,
|
||||
}: { checkout?: Checkout; userErrors?: any[] } = checkoutResponse
|
||||
|
||||
if (userErrors && userErrors.length) {
|
||||
throw new ValidationError({
|
||||
message: userErrors[0].message,
|
||||
})
|
||||
}
|
||||
|
||||
if (!checkout) {
|
||||
throw new ValidationError({
|
||||
message: 'Missing checkout details from response cart Response',
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
...checkout,
|
||||
currency: { code: checkout.currencyCode },
|
||||
lineItems: checkout.lineItems?.edges.map(
|
||||
({
|
||||
node: { id, title: name, quantity, variant },
|
||||
}: CheckoutLineItemEdge) => ({
|
||||
id,
|
||||
checkoutUrl: checkout.webUrl,
|
||||
variantId: variant?.id,
|
||||
productId: id,
|
||||
name,
|
||||
quantity,
|
||||
discounts: [],
|
||||
path: '',
|
||||
variant: {
|
||||
id: variant?.id,
|
||||
image: {
|
||||
url: variant?.image?.src,
|
||||
altText: variant?.title,
|
||||
},
|
||||
price: variant?.price,
|
||||
},
|
||||
})
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
export default checkoutToCart
|
2
framework/shopify/cart/utils/index.ts
Normal file
2
framework/shopify/cart/utils/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export { default as checkoutToCart } from './checkout-to-cart'
|
||||
export { default as checkoutCreate } from './checkout-create'
|
Reference in New Issue
Block a user