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!
const html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Checkout</title>
<script src="https://checkout-sdk.bigcommerce.com/v1/loader.js"></script>
<script>
window.onload = function() {
checkoutKitLoader.load('checkout-sdk').then(function (service) {
service.embedCheckout({
containerId: 'checkout',
url: '${data.embedded_checkout_url}'
});
});
}
</script>
</head>
<body>
<div id="checkout"></div>
</body>
</html>
`
// const html = `
// <!DOCTYPE html>
// <html lang="en">
// <head>
// <meta charset="UTF-8">
// <meta name="viewport" content="width=device-width, initial-scale=1.0">
// <title>Checkout</title>
// <script src="https://checkout-sdk.bigcommerce.com/v1/loader.js"></script>
// <script>
// window.onload = function() {
// checkoutKitLoader.load('checkout-sdk').then(function (service) {
// service.embedCheckout({
// containerId: 'checkout',
// url: '${data.embedded_checkout_url}'
// });
// });
// }
// </script>
// </head>
// <body>
// <div id="checkout"></div>
// </body>
// </html>
// `
return new Response(html, {
headers: {
'Content-Type': 'text/html',
},
})
// return new Response(html, {
// headers: {
// 'Content-Type': 'text/html',
// },
// })
return { data: null }
}
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 { APIResponse } from './types'
@ -52,15 +52,17 @@ export const transformRequest = (req: NextApiRequest, path: string) => {
}
export const transformHeaders = (
headers?: Record<string, string | number | string[]>
) =>
headers
? Object.entries(headers).reduce((acc, [key, value]) => {
if (Array.isArray(value)) {
value.forEach((v) => acc.append(key, v))
} else {
acc.append(key, `${value}`)
headers?: Record<string, string | number | string[]> | Headers
) => {
if (headers instanceof Headers) {
return headers
}
const newHeaders = new Headers()
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))
if (output instanceof Response) {
return res.end(output.body)
}
const { status, errors, data, redirectTo, headers } = output
if (headers) {
if (headers instanceof Headers) {
headers.forEach((value, key) => {
res.setHeader(key, value)
})
} else {
Object.entries(headers).forEach(([key, value]) => {
res.setHeader(key, value)
})
}
}
if (output instanceof Response) {
return res.end(output.body)
}
if (redirectTo) {
return res.redirect(redirectTo)

View File

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