Fix build errors

This commit is contained in:
Catalin Pinte 2022-10-20 11:11:26 +03:00
parent 3fd24810f9
commit c27328f853
4 changed files with 60 additions and 51 deletions

View File

@ -64,36 +64,38 @@ const getCheckout: CheckoutEndpoint['handlers']['getCheckout'] = async ({
} }
// TODO: make the embedded checkout work too! // TODO: make the embedded checkout work too!
const html = ` // const html = `
<!DOCTYPE html> // <!DOCTYPE html>
<html lang="en"> // <html lang="en">
<head> // <head>
<meta charset="UTF-8"> // <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> // <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Checkout</title> // <title>Checkout</title>
<script src="https://checkout-sdk.bigcommerce.com/v1/loader.js"></script> // <script src="https://checkout-sdk.bigcommerce.com/v1/loader.js"></script>
<script> // <script>
window.onload = function() { // window.onload = function() {
checkoutKitLoader.load('checkout-sdk').then(function (service) { // checkoutKitLoader.load('checkout-sdk').then(function (service) {
service.embedCheckout({ // service.embedCheckout({
containerId: 'checkout', // containerId: 'checkout',
url: '${data.embedded_checkout_url}' // url: '${data.embedded_checkout_url}'
}); // });
}); // });
} // }
</script> // </script>
</head> // </head>
<body> // <body>
<div id="checkout"></div> // <div id="checkout"></div>
</body> // </body>
</html> // </html>
` // `
return new Response(html, { // return new Response(html, {
headers: { // headers: {
'Content-Type': 'text/html', // 'Content-Type': 'text/html',
}, // },
}) // })
return { data: null }
} }
export default getCheckout export default getCheckout

View File

@ -1,4 +1,4 @@
import type { NextApiRequest } from 'next' import type { NextApiRequest, NextApiResponse } from 'next'
import type { ZodSchema } from 'zod' import type { ZodSchema } from 'zod'
import type { APIResponse } from './types' import type { APIResponse } from './types'
@ -52,15 +52,17 @@ export const transformRequest = (req: NextApiRequest, path: string) => {
} }
export const transformHeaders = ( export const transformHeaders = (
headers?: Record<string, string | number | string[]> headers?: Record<string, string | number | string[]> | Headers
) => ) => {
headers if (headers instanceof Headers) {
? Object.entries(headers).reduce((acc, [key, value]) => { return headers
if (Array.isArray(value)) { }
value.forEach((v) => acc.append(key, v))
} else { const newHeaders = new Headers()
acc.append(key, `${value}`)
Object.entries(headers || {}).forEach(([key, value]) => {
newHeaders.append(key, value as string)
})
return newHeaders
} }
return acc
}, new Headers())
: new Headers()

View File

@ -45,18 +45,23 @@ export default function nodeHandler<P extends APIProvider>(
} }
const output = await handlers[path](transformRequest(req, path)) const output = await handlers[path](transformRequest(req, path))
if (output instanceof Response) {
return res.end(output.body)
}
const { status, errors, data, redirectTo, headers } = output const { status, errors, data, redirectTo, headers } = output
if (headers) { if (headers) {
if (headers instanceof Headers) {
headers.forEach((value, key) => {
res.setHeader(key, value)
})
} else {
Object.entries(headers).forEach(([key, value]) => { Object.entries(headers).forEach(([key, value]) => {
res.setHeader(key, value) res.setHeader(key, value)
}) })
} }
}
if (output instanceof Response) {
return res.end(output.body)
}
if (redirectTo) { if (redirectTo) {
return res.redirect(redirectTo) return res.redirect(redirectTo)

View File

@ -7,7 +7,7 @@ export type APIResponse<Data = any> = {
data?: Data data?: Data
errors?: ErrorData[] errors?: ErrorData[]
status?: number status?: number
headers?: Record<string, number | string | string[]> headers?: Record<string, number | string | string[]> | Headers
/** /**
* @type {string} * @type {string}
* @example redirectTo: '/cart' * @example redirectTo: '/cart'