Skip to content

Instantly share code, notes, and snippets.

@trnktms
Last active Jul 6, 2022
Embed
What would you like to do?
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