Skip to content

feat(pathRewrite): add 'res' and 'options' to custom pathRewrite function#1247

Merged
chimurai merged 2 commits into
masterfrom
pathRewrite-support-res-and-option
May 24, 2026
Merged

feat(pathRewrite): add 'res' and 'options' to custom pathRewrite function#1247
chimurai merged 2 commits into
masterfrom
pathRewrite-support-res-and-option

Conversation

@chimurai

@chimurai chimurai commented May 24, 2026

Copy link
Copy Markdown
Owner

closes #854

Summary by CodeRabbit

  • New Features

    • Custom pathRewrite callbacks now receive response and options context (in addition to path and request), allowing rewrites to depend on response state or middleware options.
  • Documentation

    • Updated pathRewrite docs and examples (sync/async) to show full function signatures and usage.
    • Clarified that the response argument is undefined for WebSocket upgrade flows.

Review Change Stack

@coderabbitai

coderabbitai Bot commented May 24, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 7808c077-acbb-422b-8757-a308ffbd3d3e

📥 Commits

Reviewing files that changed from the base of the PR and between b5e175f and 026897b.

📒 Files selected for processing (1)
  • src/types.ts

📝 Walkthrough

Walkthrough

The PR extends pathRewrite to pass (path, req, res, options) to rewrite callbacks, updates TypeScript types to include a TRes generic, re-parameters createPathRewriter, wires the rewriter through the middleware, adds tests verifying res/options are passed, and updates docs/changelog.

Changes

pathRewrite Signature Enhancement

Layer / File(s) Summary
Type contract and signature
src/types.ts
PathRewriteConfig adds TRes generic; rewrite functions may accept (path, req, res?, options?) and return `string
Path rewriter module
src/path-rewriter.ts
createPathRewriter now accepts PathRewriteConfig<TReq, TRes> and imports ServerResponse; validator signatures updated to the new generic shape.
Middleware integration
src/http-proxy-middleware.ts
Middleware stores a pathRewriter typed for TReq,TRes, constructs it with both generics, and applyPathRewrite now accepts/invokes the rewriter with (req.url, req, res, options).
Test coverage
test/unit/path-rewriter.spec.ts, test/e2e/path-rewriter.spec.ts, test/types.spec.ts
Unit test asserts custom rewriter is called with (path, req, res, options); E2E test captures res and options inside pathRewrite; type tests updated for the new signature and res usage.
User documentation
CHANGELOG.md, README.md, recipes/pathRewrite.md
Changelog, README, and recipe examples updated to show the extended pathRewrite(path, req, res, options) signature and note that res is undefined during WebSocket upgrade flows.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🐰 A rabbit hops through rewrite nights,
Passing res and options in soft lights.
Locals whisper where paths once bent,
Async hops await your intent.
Cheers to clearer hops and tiny delights!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% 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 clearly and concisely summarizes the main change: adding 'res' and 'options' parameters to the custom pathRewrite function, which aligns with the core objective of the PR.
Linked Issues check ✅ Passed The PR fully addresses issue #854 by passing the response object and options into the custom pathRewrite function, enabling access to res.locals as requested in the feature request.
Out of Scope Changes check ✅ Passed All changes are directly related to the stated objective of adding res and options parameters to pathRewrite; no out-of-scope modifications 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 pathRewrite-support-res-and-option

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@1247

commit: 026897b

@coveralls

coveralls commented May 24, 2026

Copy link
Copy Markdown

Coverage Status

coverage: 94.656%. remained the same — pathRewrite-support-res-and-option into master

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/types.ts`:
- Around line 76-82: The type for the async pathRewrite callback currently
forces Promise<string>, preventing valid no-op rewrites; update the callback
return type(s) so async handlers can return Promise<string | undefined> (and
ensure the synchronous variant also allows string | undefined) — change
occurrences of Promise<string> to Promise<string | undefined> for the
pathRewrite function type (and string to string | undefined where applicable) so
typings match runtime behavior that treats non-string/undefined as “no rewrite”.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 626fac3a-b051-4726-9fbe-bffb35dfe498

📥 Commits

Reviewing files that changed from the base of the PR and between 3ab96d8 and b5e175f.

📒 Files selected for processing (9)
  • CHANGELOG.md
  • README.md
  • recipes/pathRewrite.md
  • src/http-proxy-middleware.ts
  • src/path-rewriter.ts
  • src/types.ts
  • test/e2e/path-rewriter.spec.ts
  • test/types.spec.ts
  • test/unit/path-rewriter.spec.ts

Comment thread src/types.ts Outdated
@chimurai chimurai added this to the v4.1 milestone May 24, 2026
@chimurai chimurai merged commit 39a47ab into master May 24, 2026
26 checks passed
@chimurai chimurai deleted the pathRewrite-support-res-and-option branch May 24, 2026 18:44
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.

feat: response object access in pathRewrite (for locals)

2 participants