mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 23:46:58 +00:00
rename checkoutCreate to createCart and remove Shopify checkout API
Signed-off-by: Loan Laux <loan@outgrow.io>
This commit is contained in:
parent
f52978e1a3
commit
3496b2a155
@ -1,7 +1,7 @@
|
||||
import type { CartHandlers } from '..'
|
||||
import {
|
||||
addCartItemsMutation,
|
||||
checkoutCreateMutation,
|
||||
createCartMutation,
|
||||
} from '@framework/utils/mutations'
|
||||
import getCartCookie from '@framework/api/utils/get-cart-cookie'
|
||||
import {
|
||||
@ -41,7 +41,7 @@ const addItem: CartHandlers['addItem'] = async ({
|
||||
if (!item.quantity) item.quantity = 1
|
||||
|
||||
if (cartId === config.dummyEmptyCartId) {
|
||||
const createdCart = await config.fetch(checkoutCreateMutation, {
|
||||
const createdCart = await config.fetch(createCartMutation, {
|
||||
variables: {
|
||||
input: {
|
||||
shopId: config.shopId,
|
||||
|
@ -1,50 +1 @@
|
||||
import isAllowedMethod from '../utils/is-allowed-method'
|
||||
import createApiHandler, {
|
||||
ReactionCommerceApiHandler,
|
||||
} from '../utils/create-api-handler'
|
||||
|
||||
import {
|
||||
REACTION_ANONYMOUS_CART_TOKEN_COOKIE,
|
||||
SHOPIFY_CHECKOUT_URL_COOKIE,
|
||||
REACTION_CUSTOMER_TOKEN_COOKIE,
|
||||
} from '../../const'
|
||||
|
||||
import { getConfig } from '..'
|
||||
import associateCustomerWithCheckoutMutation from '../../utils/mutations/associate-customer-with-checkout'
|
||||
|
||||
const METHODS = ['GET']
|
||||
|
||||
const checkoutApi: ReactionCommerceApiHandler<any> = async (
|
||||
req,
|
||||
res,
|
||||
config
|
||||
) => {
|
||||
if (!isAllowedMethod(req, res, METHODS)) return
|
||||
|
||||
config = getConfig()
|
||||
|
||||
const { cookies } = req
|
||||
const checkoutUrl = cookies[SHOPIFY_CHECKOUT_URL_COOKIE]
|
||||
const customerCookie = cookies[REACTION_CUSTOMER_TOKEN_COOKIE]
|
||||
|
||||
if (customerCookie) {
|
||||
try {
|
||||
await config.fetch(associateCustomerWithCheckoutMutation, {
|
||||
variables: {
|
||||
checkoutId: cookies[REACTION_ANONYMOUS_CART_TOKEN_COOKIE],
|
||||
customerAccessToken: cookies[REACTION_CUSTOMER_TOKEN_COOKIE],
|
||||
},
|
||||
})
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
}
|
||||
}
|
||||
|
||||
if (checkoutUrl) {
|
||||
res.redirect(checkoutUrl)
|
||||
} else {
|
||||
res.redirect('/cart')
|
||||
}
|
||||
}
|
||||
|
||||
export default createApiHandler(checkoutApi, {}, {})
|
||||
export default function () {}
|
||||
|
@ -3,13 +3,12 @@ import {
|
||||
SHOPIFY_CHECKOUT_URL_COOKIE,
|
||||
REACTION_COOKIE_EXPIRE,
|
||||
} from '../../const'
|
||||
|
||||
import checkoutCreateMutation from '../../utils/mutations/checkout-create'
|
||||
import createCartMutation from '../../utils/mutations/create-cart'
|
||||
import Cookies from 'js-cookie'
|
||||
|
||||
export const checkoutCreate = async (fetch: any) => {
|
||||
export const createCart = async (fetch: any) => {
|
||||
const data = await fetch({
|
||||
query: checkoutCreateMutation,
|
||||
query: createCartMutation,
|
||||
variables: {
|
||||
input: {
|
||||
shopId,
|
||||
@ -25,10 +24,9 @@ export const checkoutCreate = async (fetch: any) => {
|
||||
expires: REACTION_COOKIE_EXPIRE,
|
||||
}
|
||||
Cookies.set(REACTION_ANONYMOUS_CART_TOKEN_COOKIE, checkoutId, options)
|
||||
Cookies.set(SHOPIFY_CHECKOUT_URL_COOKIE, checkout?.webUrl, options)
|
||||
}
|
||||
|
||||
return checkout
|
||||
}
|
||||
|
||||
export default checkoutCreate
|
||||
export default createCart
|
@ -1,2 +1,2 @@
|
||||
export { default as checkoutToCart } from './checkout-to-cart'
|
||||
export { default as checkoutCreate } from './checkout-create'
|
||||
export { default as checkoutCreate } from './create-cart'
|
||||
|
@ -5,8 +5,6 @@ export const REACTION_CART_ID_COOKIE = 'reaction_cartId'
|
||||
|
||||
export const REACTION_EMPTY_DUMMY_CART_ID = 'DUMMY_EMPTY_CART_ID'
|
||||
|
||||
export const SHOPIFY_CHECKOUT_URL_COOKIE = 'shopify_checkoutUrl'
|
||||
|
||||
export const REACTION_CUSTOMER_TOKEN_COOKIE = 'reaction_customerToken'
|
||||
|
||||
export const STORE_DOMAIN = process.env.NEXT_PUBLIC_SHOPIFY_STORE_DOMAIN
|
||||
|
@ -4,7 +4,7 @@ import {
|
||||
minOrderQuantityFailureDetailsFragment,
|
||||
} from '@framework/utils/queries/get-checkout-query'
|
||||
|
||||
const checkoutCreateMutation = /* GraphQL */ `
|
||||
const createCartMutation = /* GraphQL */ `
|
||||
mutation createCartMutation($input: CreateCartInput!) {
|
||||
createCart(input: $input) {
|
||||
cart {
|
||||
@ -21,4 +21,4 @@ const checkoutCreateMutation = /* GraphQL */ `
|
||||
}
|
||||
}
|
||||
`
|
||||
export default checkoutCreateMutation
|
||||
export default createCartMutation
|
@ -1,6 +1,6 @@
|
||||
export { default as addCartItemsMutation } from './add-cart-items'
|
||||
export { default as createUserMutation } from './create-user'
|
||||
export { default as checkoutCreateMutation } from './checkout-create'
|
||||
export { default as createCartMutation } from './create-cart'
|
||||
export { default as authenticateMutation } from './authenticate'
|
||||
export { default as logoutMutation } from './logout'
|
||||
export { default as removeCartItemsMutation } from './remove-cart-items'
|
||||
|
Loading…
x
Reference in New Issue
Block a user