Skip to content

feat(router): add 'res' and 'options' to router function#1244

Merged
chimurai merged 1 commit into
masterfrom
extend-router
May 24, 2026
Merged

feat(router): add 'res' and 'options' to router function#1244
chimurai merged 1 commit into
masterfrom
extend-router

Conversation

@chimurai

@chimurai chimurai commented May 24, 2026

Copy link
Copy Markdown
Owner

resolves: #662

Summary by CodeRabbit

  • New Features
    • Router functions now receive the HTTP response object and configuration options as parameters, enabling more sophisticated routing decisions based on response context and state.

Review Change Stack

@coderabbitai

coderabbitai Bot commented May 24, 2026

Copy link
Copy Markdown

Caution

Review failed

Pull request was closed or merged during review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: a0f0f3e7-b9d9-4a0a-9df8-c76670942694

📥 Commits

Reviewing files that changed from the base of the PR and between bb3bf76 and 2aec481.

📒 Files selected for processing (7)
  • CHANGELOG.md
  • src/http-proxy-middleware.ts
  • src/router.ts
  • src/types.ts
  • test/e2e/router.spec.ts
  • test/types.spec.ts
  • test/unit/router.spec.ts

📝 Walkthrough

Walkthrough

The PR extends the router function signature to receive the HTTP response object and full options parameter alongside the request, enabling router implementations to access response state (e.g., res.locals) and modify proxy configuration (e.g., headers) during target selection in a single pass.

Changes

Router Enhancement: res and options Parameters

Layer / File(s) Summary
Router API Contract Update
src/types.ts
Public Options.router interface signature changed from (req: TReq) to (req: TReq, res: TRes | undefined, options: Options<TReq, TRes>) with documentation noting res is undefined during WebSocket upgrades.
Router Implementation
src/router.ts
getTarget function signature updated to accept res parameter and forward both res and full config to router functions, while preserving plain-object router table behavior.
Middleware Request Preparation Threading
src/http-proxy-middleware.ts
HTTP proxy and WebSocket paths both thread res through prepareProxyRequest and applyRouter: HTTP passes the response object, WebSocket explicitly passes undefined.
Test Updates: Unit and Type Coverage
test/unit/router.spec.ts, test/types.spec.ts
Unit tests updated to import ServerResponse, instantiate mockRes, and thread it through getTarget calls; type tests verify router function accepts (req, res, options) signature.
E2E Test and Documentation
test/e2e/router.spec.ts, CHANGELOG.md
E2E test demonstrates accessing res.locals in router to inject custom headers; CHANGELOG documents the new res and options parameters.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • chimurai/http-proxy-middleware#1197: Modifies the router type/target-selection logic in src/router.ts including getTarget and config.router typing, though that PR focuses on stricter generic typings rather than extending the router callback contract.

Poem

🐰 A router hops with res in sight,
Options flow through, headers burn bright,
No double query, one pass does the trick,
Express locals and middleware, click!
The tunnel grows smarter, hop, skip, and bound!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: extending the router function to receive 'res' and 'options' parameters, which is the primary modification across multiple files.
Linked Issues check ✅ Passed The PR fully implements the first two requested capabilities from issue #662: router now receives 'res' for accessing res.locals and 'options' for modifying headers before proxying.
Out of Scope Changes check ✅ Passed All changes are directly related to the router enhancement objective. No unrelated modifications to other functionality, dependencies, or unrelated code areas were introduced.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch extend-router

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@pkg-pr-new

pkg-pr-new Bot commented May 24, 2026

Copy link
Copy Markdown
npm i https://pkg.pr.new/http-proxy-middleware@1244

commit: 2aec481

@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 94.656% (-0.01%) from 94.67% — extend-router into master

@chimurai chimurai merged commit 9774cbf into master May 24, 2026
25 of 26 checks passed
@chimurai chimurai deleted the extend-router branch May 24, 2026 13:03
@chimurai chimurai added this to the v4.1 milestone May 24, 2026
@chimurai chimurai linked an issue May 31, 2026 that may be closed by this pull request
@chimurai chimurai linked an issue Jun 16, 2026 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide possibility to influence proxy options during req processing feat: include options with router function Attaching a refreshed certificate

2 participants