Skip to content

Commit

Permalink
test(types): contextual types
Browse files Browse the repository at this point in the history
  • Loading branch information
chimurai committed Oct 16, 2023
1 parent a703969 commit b18eb77
Showing 1 changed file with 50 additions and 50 deletions.
100 changes: 50 additions & 50 deletions test/types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<express.Request, express.Response>({
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 {
Expand Down

0 comments on commit b18eb77

Please sign in to comment.