Skip to content

Instantly share code, notes, and snippets.

@trnktms
Last active July 6, 2022 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trnktms/81a3ceb3465cddde0dfcfb2f56d784a0 to your computer and use it in GitHub Desktop.
Save trnktms/81a3ceb3465cddde0dfcfb2f56d784a0 to your computer and use it in GitHub Desktop.
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