mirror of
https://github.com/vercel/commerce.git
synced 2025-06-19 13:41:22 +00:00
Add api checkout endpoint
This commit is contained in:
parent
12f0d90e91
commit
f16cb7206b
@ -1 +0,0 @@
|
|||||||
export default function (_commerce: any) {}
|
|
38
framework/shopify/api/endpoints/checkout/checkout.ts
Normal file
38
framework/shopify/api/endpoints/checkout/checkout.ts
Normal 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
|
18
framework/shopify/api/endpoints/checkout/index.ts
Normal file
18
framework/shopify/api/endpoints/checkout/index.ts
Normal 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
|
@ -6,6 +6,12 @@ import { checkoutCreate, checkoutToCart } from '../utils'
|
|||||||
import getCheckoutQuery from '../utils/queries/get-checkout-query'
|
import getCheckoutQuery from '../utils/queries/get-checkout-query'
|
||||||
import { GetCartHook } from '../types/cart'
|
import { GetCartHook } from '../types/cart'
|
||||||
|
|
||||||
|
import {
|
||||||
|
GetCheckoutQuery,
|
||||||
|
GetCheckoutQueryVariables,
|
||||||
|
CheckoutDetailsFragment,
|
||||||
|
} from '../schema'
|
||||||
|
|
||||||
export default useCommerceCart as UseCart<typeof handler>
|
export default useCommerceCart as UseCart<typeof handler>
|
||||||
|
|
||||||
export const handler: SWRHook<GetCartHook> = {
|
export const handler: SWRHook<GetCartHook> = {
|
||||||
|
@ -8,7 +8,7 @@ const fetcher: Fetcher = async ({
|
|||||||
variables,
|
variables,
|
||||||
query,
|
query,
|
||||||
}) => {
|
}) => {
|
||||||
const { locale, ...vars } = variables
|
const { locale, ...vars } = variables ?? {}
|
||||||
return handleFetchResponse(
|
return handleFetchResponse(
|
||||||
await fetch(url, {
|
await fetch(url, {
|
||||||
method,
|
method,
|
||||||
|
@ -18,7 +18,7 @@ module.exports = withCommerceConfig({
|
|||||||
return [
|
return [
|
||||||
(isBC || isShopify) && {
|
(isBC || isShopify) && {
|
||||||
source: '/checkout',
|
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
|
// The logout is also an action so this route is not required, but it's also another way
|
||||||
// you can allow a logout!
|
// you can allow a logout!
|
||||||
|
Loading…
x
Reference in New Issue
Block a user