Add api checkout endpoint

This commit is contained in:
cond0r 2021-05-28 08:24:04 +03:00
parent 12f0d90e91
commit f16cb7206b
6 changed files with 64 additions and 3 deletions

View File

@ -1 +0,0 @@
export default function (_commerce: any) {}

View File

@ -0,0 +1,38 @@
import {
SHOPIFY_CHECKOUT_ID_COOKIE,
SHOPIFY_CHECKOUT_URL_COOKIE,
SHOPIFY_CUSTOMER_TOKEN_COOKIE,
} from '@framework/const'
import associateCustomerWithCheckoutMutation from '@framework/utils/mutations/associate-customer-with-checkout'
import type { CheckoutEndpoint } from '.'
const checkout: CheckoutEndpoint['handlers']['checkout'] = async ({
req,
res,
config,
}) => {
const { cookies } = req
const checkoutUrl = cookies[SHOPIFY_CHECKOUT_URL_COOKIE]
const customerCookie = cookies[SHOPIFY_CUSTOMER_TOKEN_COOKIE]
if (customerCookie) {
try {
await config.fetch(associateCustomerWithCheckoutMutation, {
variables: {
checkoutId: cookies[SHOPIFY_CHECKOUT_ID_COOKIE],
customerAccessToken: cookies[SHOPIFY_CUSTOMER_TOKEN_COOKIE],
},
})
} catch (error) {
console.error(error)
}
}
if (checkoutUrl) {
res.redirect(checkoutUrl)
} else {
res.redirect('/cart')
}
}
export default checkout

View File

@ -0,0 +1,18 @@
import { GetAPISchema, createEndpoint } from '@commerce/api'
import checkoutEndpoint from '@commerce/api/endpoints/checkout'
import type { CheckoutSchema } from '../../../types/checkout'
import type { ShopifyAPI } from '../..'
import checkout from './checkout'
export type CheckoutAPI = GetAPISchema<ShopifyAPI, CheckoutSchema>
export type CheckoutEndpoint = CheckoutAPI['endpoint']
export const handlers: CheckoutEndpoint['handlers'] = { checkout }
const checkoutApi = createEndpoint<CheckoutAPI>({
handler: checkoutEndpoint,
handlers,
})
export default checkoutApi

View File

@ -6,6 +6,12 @@ import { checkoutCreate, checkoutToCart } from '../utils'
import getCheckoutQuery from '../utils/queries/get-checkout-query'
import { GetCartHook } from '../types/cart'
import {
GetCheckoutQuery,
GetCheckoutQueryVariables,
CheckoutDetailsFragment,
} from '../schema'
export default useCommerceCart as UseCart<typeof handler>
export const handler: SWRHook<GetCartHook> = {

View File

@ -8,7 +8,7 @@ const fetcher: Fetcher = async ({
variables,
query,
}) => {
const { locale, ...vars } = variables
const { locale, ...vars } = variables ?? {}
return handleFetchResponse(
await fetch(url, {
method,

View File

@ -18,7 +18,7 @@ module.exports = withCommerceConfig({
return [
(isBC || isShopify) && {
source: '/checkout',
destination: '/api/bigcommerce/checkout',
destination: '/api/checkout',
},
// The logout is also an action so this route is not required, but it's also another way
// you can allow a logout!