mirror of
https://github.com/vercel/commerce.git
synced 2025-07-01 02:41:22 +00:00
update checkout
This commit is contained in:
parent
c88da63a1d
commit
bb321e2237
@ -1,20 +0,0 @@
|
|||||||
import createApiHandler, { SwellApiHandler } from '../utils/create-api-handler'
|
|
||||||
|
|
||||||
import { SWELL_CHECKOUT_URL_COOKIE } from '../../const'
|
|
||||||
|
|
||||||
import { getConfig } from '..'
|
|
||||||
|
|
||||||
const checkoutApi: SwellApiHandler<any> = async (req, res, config) => {
|
|
||||||
config = getConfig()
|
|
||||||
|
|
||||||
const { cookies } = req
|
|
||||||
const checkoutUrl = cookies[SWELL_CHECKOUT_URL_COOKIE]
|
|
||||||
|
|
||||||
if (checkoutUrl) {
|
|
||||||
res.redirect(checkoutUrl)
|
|
||||||
} else {
|
|
||||||
res.redirect('/cart')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default createApiHandler(checkoutApi, {}, {})
|
|
30
framework/swell/api/endpoints/checkout/index.ts
Normal file
30
framework/swell/api/endpoints/checkout/index.ts
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import { CommerceAPI, createEndpoint, GetAPISchema } from '@commerce/api'
|
||||||
|
import { CheckoutSchema } from '@commerce/types/checkout'
|
||||||
|
import { SWELL_CHECKOUT_URL_COOKIE } from '../../../const'
|
||||||
|
import checkoutEndpoint from '@commerce/api/endpoints/checkout'
|
||||||
|
|
||||||
|
const checkout: CheckoutEndpoint['handlers']['checkout'] = async ({
|
||||||
|
req,
|
||||||
|
res,
|
||||||
|
config,
|
||||||
|
}) => {
|
||||||
|
const { cookies } = req
|
||||||
|
const checkoutUrl = cookies[SWELL_CHECKOUT_URL_COOKIE]
|
||||||
|
|
||||||
|
if (checkoutUrl) {
|
||||||
|
res.redirect(checkoutUrl)
|
||||||
|
} else {
|
||||||
|
res.redirect('/cart')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export const handlers: CheckoutEndpoint['handlers'] = { checkout }
|
||||||
|
|
||||||
|
export type CheckoutAPI = GetAPISchema<CommerceAPI, CheckoutSchema>
|
||||||
|
export type CheckoutEndpoint = CheckoutAPI['endpoint']
|
||||||
|
|
||||||
|
const checkoutApi = createEndpoint<CheckoutAPI>({
|
||||||
|
handler: checkoutEndpoint,
|
||||||
|
handlers,
|
||||||
|
})
|
||||||
|
|
||||||
|
export default checkoutApi
|
@ -17,6 +17,7 @@ export default function getAllProductsOperation({
|
|||||||
|
|
||||||
async function getAllProducts({
|
async function getAllProducts({
|
||||||
config: cfg,
|
config: cfg,
|
||||||
|
variables = { first: 250 },
|
||||||
}: {
|
}: {
|
||||||
query?: string
|
query?: string
|
||||||
variables?: ProductVariables
|
variables?: ProductVariables
|
||||||
@ -26,7 +27,7 @@ export default function getAllProductsOperation({
|
|||||||
const config = commerce.getConfig(cfg)
|
const config = commerce.getConfig(cfg)
|
||||||
const { results } = await config.fetch('products', 'list', [
|
const { results } = await config.fetch('products', 'list', [
|
||||||
{
|
{
|
||||||
limit: 250,
|
limit: variables.first,
|
||||||
},
|
},
|
||||||
])
|
])
|
||||||
const products = results.map((product: SwellProduct) =>
|
const products = results.map((product: SwellProduct) =>
|
||||||
|
@ -9,7 +9,7 @@ export const checkoutCreate = async (fetch: any) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if (!cart) {
|
if (!cart) {
|
||||||
const cart = await fetch({
|
await fetch({
|
||||||
query: 'cart',
|
query: 'cart',
|
||||||
method: 'setItems',
|
method: 'setItems',
|
||||||
variables: [[]],
|
variables: [[]],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user