Implement Shopify Provider

This commit is contained in:
cond0r
2021-02-04 13:23:44 +02:00
parent c06d9dae3a
commit 14c3f961b3
61 changed files with 16405 additions and 20 deletions

View 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

View 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

View File

@@ -0,0 +1,2 @@
export { default as checkoutToCart } from './checkout-to-cart'
export { default as checkoutCreate } from './checkout-create'