Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(types): contextual types #946

Merged
merged 1 commit into from
Oct 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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