From 3bd4feb0ed3a888ad5b5458c46bb63cd224e9855 Mon Sep 17 00:00:00 2001 From: Evert Prants Date: Tue, 20 Sep 2022 17:58:49 +0300 Subject: [PATCH] tweak proxy --- pages/api/[...path].ts | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/pages/api/[...path].ts b/pages/api/[...path].ts index e505aab..d513f2b 100644 --- a/pages/api/[...path].ts +++ b/pages/api/[...path].ts @@ -7,31 +7,24 @@ import { COOKIE_KEYS } from '../../lib/constants'; const inProd = process.env.NODE_ENV === 'production'; const handler = (req: NextApiRequest, res: NextApiResponse) => { - return new Promise((resolve, reject) => { - // removes the api prefix from url - // req.url = req.url!.replace(/^\/api/, ''); - + return new Promise((resolve, reject) => { const cookies = new Cookies(req, res, { keys: COOKIE_KEYS, secure: inProd, }); const authorization = cookies.get('authorization', { signed: true }); - // don't forwards the cookies to the target server + // don't forward 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); + proxy.web(req, res, undefined, (err) => { + if (err) return reject(err); + resolve(); + }); }); };