Catalin Pinte c75b0fc001
Dynamic API routes (#836)
* Add dynamic API endpoints

* Add missing dependency

* Update api handlers

* Updates

* Fix build errors

* Update package.json

* Add checkout endpoint parser & update errors

* Update tsconfig.json

* Update cart.ts

* Update parser

* Update errors.ts

* Update errors.ts

* Move to Edge runtime

* Revert to local

* Fix switchable runtimes

* Make nodejs default runtime

* Update pnpm-lock.yaml

* Update handlers

* Fix build errors

* Change headers
2022-10-30 13:41:21 -05:00

44 lines
1.2 KiB
TypeScript

import type { CardFields } from '@vercel/commerce/types/customer/card'
import type { AddressFields } from '@vercel/commerce/types/customer/address'
import type { CheckoutEndpoint } from '.'
import sdkFetcherFunction from '../../utils/sdk-fetch'
import { normalizeTestCheckout } from '../../../utils/normalize-checkout'
const submitCheckout: CheckoutEndpoint['handlers']['submitCheckout'] = async ({
body: { item, cartId },
config: { sdkFetch },
}) => {
const sdkFetcher: typeof sdkFetcherFunction = sdkFetch
// Generate a checkout token
const { id: checkoutToken } = await sdkFetcher(
'checkout',
'generateTokenFrom',
'cart',
cartId
)
const shippingMethods = await sdkFetcher(
'checkout',
'getShippingOptions',
checkoutToken,
{
country: 'US',
}
)
const shippingMethodToUse = shippingMethods?.[0]?.id || ''
const checkoutData = normalizeTestCheckout({
paymentInfo: item?.card as CardFields,
shippingInfo: item?.address as AddressFields,
shippingOption: shippingMethodToUse,
})
// Capture the order
await sdkFetcher('checkout', 'capture', checkoutToken, checkoutData)
return { data: null }
}
export default submitCheckout