mirror of
https://github.com/vercel/commerce.git
synced 2025-06-30 18:31:21 +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({
|
||||
config: cfg,
|
||||
variables = { first: 250 },
|
||||
}: {
|
||||
query?: string
|
||||
variables?: ProductVariables
|
||||
@ -26,7 +27,7 @@ export default function getAllProductsOperation({
|
||||
const config = commerce.getConfig(cfg)
|
||||
const { results } = await config.fetch('products', 'list', [
|
||||
{
|
||||
limit: 250,
|
||||
limit: variables.first,
|
||||
},
|
||||
])
|
||||
const products = results.map((product: SwellProduct) =>
|
||||
|
@ -9,7 +9,7 @@ export const checkoutCreate = async (fetch: any) => {
|
||||
})
|
||||
|
||||
if (!cart) {
|
||||
const cart = await fetch({
|
||||
await fetch({
|
||||
query: 'cart',
|
||||
method: 'setItems',
|
||||
variables: [[]],
|
||||
|
Loading…
x
Reference in New Issue
Block a user