This repository has been archived on 2024-06-14. You can view files and clone it, but cannot push or open issues or pull requests.
icynet-admin/pages/api/[...path].ts

40 lines
1.0 KiB
TypeScript
Raw Normal View History

2022-08-29 18:09:28 +00:00
import { proxy } from '../../server/proxy';
import Cookies from 'cookies';
import { NextApiRequest, NextApiResponse } from 'next';
import { COOKIE_KEYS } from '../../lib/constants';
const handler = (req: NextApiRequest, res: NextApiResponse) => {
return new Promise((resolve, reject) => {
// removes the api prefix from url
// req.url = req.url!.replace(/^\/api/, '');
const cookies = new Cookies(req, res, { keys: COOKIE_KEYS });
const authorization = cookies.get('authorization', { signed: true });
// don't forwards the cookies to the target server
req.headers.cookie = '';
if (authorization) {
req.headers.authorization = `Bearer ${authorization}`;
}
/**
* if an error occurs in the proxy, we will reject the promise.
* it is so important. if you don't reject the promise,
* you're facing the stalled requests issue.
*/
proxy.once('error', reject);
proxy.web(req, res);
});
};
export default handler;
export const config = {
api: {
bodyParser: false,
},
};