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

feat(proxy): support onResponse callback #368

Merged
merged 2 commits into from
Jun 20, 2023

Conversation

enkot
Copy link
Contributor

@enkot enkot commented Mar 29, 2023

Resolves #354

This PR adds onResponse callback to sendProxy and proxyRequest utilities.
It accepts proxy event as argument which can be used to modify the response headers, cookies etc.:

return proxyRequest(event, target, {
  onResponse(_event) {
    setHeader(_event, 'foo', 'bar')
  }
})

It can also be async:

return proxyRequest(event, target, {
  async onResponse(_event) {
    // some async stuff
  }
})

@enkot
Copy link
Contributor Author

enkot commented Apr 24, 2023

@pi0 Can we merge this one? It doesn't make any breaking change.

@@ -105,6 +106,10 @@ export async function sendProxy(
event.node.res.setHeader(key, value);
}

if (opts.onResponse) {
await opts.onResponse(event);
Copy link
Member

Choose a reason for hiding this comment

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

It would be also nice to pass over the actual response as second argument

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea, done!

@codecov
Copy link

codecov bot commented Apr 29, 2023

Codecov Report

Merging #368 (e0dfee4) into main (03243ba) will increase coverage by 0.05%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #368      +/-   ##
==========================================
+ Coverage   74.80%   74.85%   +0.05%     
==========================================
  Files          26       26              
  Lines        2409     2414       +5     
  Branches      383      385       +2     
==========================================
+ Hits         1802     1807       +5     
  Misses        607      607              
Impacted Files Coverage Δ
src/utils/proxy.ts 82.81% <100.00%> (+0.45%) ⬆️

@enkot
Copy link
Contributor Author

enkot commented May 16, 2023

Hi @pi0,
It would be great to merge this one. Or some more work should be done here?

@pavloniym
Copy link

Waiting for merge! This feature could potentially help to fix unjs/nitro#1237 and #376

@pi0 pi0 merged commit 5a3d0e8 into unjs:main Jun 20, 2023
3 checks passed
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.

Add onResponse callback to sendProxy and proxyRequest
3 participants