update checkout

This commit is contained in:
Greg Hoskin 2021-06-09 09:29:58 -07:00
parent c88da63a1d
commit bb321e2237
4 changed files with 33 additions and 22 deletions

View File

@ -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, {}, {})

View 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

View File

@ -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) =>

View File

@ -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: [[]],