From 82ad7c1bf6fecc7d4ceede7de619ded476ee2a07 Mon Sep 17 00:00:00 2001 From: chimurai <655241+chimurai@users.noreply.github.com> Date: Mon, 16 Oct 2023 23:21:31 +0200 Subject: [PATCH] test(types): contextual types (#946) --- test/types.spec.ts | 100 ++++++++++++++++++++++----------------------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/test/types.spec.ts b/test/types.spec.ts index 2f80201c..c0dcd752 100644 --- a/test/types.spec.ts +++ b/test/types.spec.ts @@ -193,56 +193,56 @@ describe('http-proxy-middleware TypeScript Types', () => { expect(app).toBeDefined(); }); - // FIXME: contextual types should work with express path middleware - // it('should get contextual types from express server', () => { - // const app = express(); - // app.use( - // '/', - // middleware({ - // router: (req) => req.params, - // pathFilter: (pathname, req) => !!req.params, - // on: { - // error(error, req, res, target) { - // req.params; - - // // https://www.typescriptlang.org/docs/handbook/2/narrowing.html - // if (res instanceof http.ServerResponse) { - // res.status(200).send('OK'); - // } - // }, - // proxyReq(proxyReq, req, res, options) { - // req.params; - // res.status(200).send('OK'); - // }, - // proxyReqWs(proxyReq, req, socket, options, head) { - // req.params; - // }, - // proxyRes(proxyRes, req, res) { - // req.params; - // res.status(200).send('OK'); - // }, - // close(proxyRes, proxySocket, proxyHead) { - // proxyRes.params; - // }, - // start(req, res, target) { - // req.params; - // res.status(200).send('OK'); - // }, - // end(req, res, proxyRes) { - // req.params; - // res.status(200).send('OK'); - // proxyRes.params; - // }, - // econnreset(error, req, res, target) { - // req.params; - // res.status(200).send('OK'); - // }, - // }, - // }) - // ); - - // expect(app).toBeDefined(); - // }); + it('should get contextual types from express server', () => { + const app = express(); + app.use( + '/', + // FIXME: contextual types should work with express path middleware (without providing explicit types) + middleware({ + router: (req) => req.params, + pathFilter: (pathname, req) => !!req.params, + on: { + error(error, req, res, target) { + req.params; + + // https://www.typescriptlang.org/docs/handbook/2/narrowing.html + if (res instanceof http.ServerResponse) { + res.status(200).send('OK'); + } + }, + proxyReq(proxyReq, req, res, options) { + req.params; + res.status(200).send('OK'); + }, + proxyReqWs(proxyReq, req, socket, options, head) { + req.params; + }, + proxyRes(proxyRes, req, res) { + req.params; + res.status(200).send('OK'); + }, + close(proxyRes, proxySocket, proxyHead) { + proxyRes.params; + }, + start(req, res, target) { + req.params; + res.status(200).send('OK'); + }, + end(req, res, proxyRes) { + req.params; + res.status(200).send('OK'); + proxyRes.params; + }, + econnreset(error, req, res, target) { + req.params; + res.status(200).send('OK'); + }, + }, + }), + ); + + expect(app).toBeDefined(); + }); it('should work with explicit generic custom req & res types', () => { interface MyRequest extends http.IncomingMessage {