This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from 'axios'; | |
import { NextApiRequest, NextApiResponse } from 'next'; | |
/** | |
* Custom API implementation for rewrites to support runtime rewrite config | |
* https://github.com/vercel/next.js/issues/21888 | |
*/ | |
export const handler = async (req: NextApiRequest, res: NextApiResponse): Promise<void> => { | |
const newPath = req.url as string; | |
if (newPath) { | |
const url = `${process.env.SITECORE_API_HOST}${newPath}`; | |
const { data, status, headers } = await axios({ | |
url, | |
responseType: 'arraybuffer', | |
}); | |
// map headers | |
Object.entries(headers).map((h) => res.setHeader(h[0], (h[1] as string) ?? '')); | |
res.status(status).send(data); | |
} else { | |
res.status(400).json({ error: 'Request URL was not provided.' }); | |
} | |
}; | |
export default handler; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment