Skip to content

feat(proxy): support onResponse callback#368

Merged
pi0 merged 2 commits intoh3js:mainfrom
enkot:feat/proxy-onresponse-callback
Jun 20, 2023
Merged

feat(proxy): support onResponse callback#368
pi0 merged 2 commits intoh3js:mainfrom
enkot:feat/proxy-onresponse-callback

Conversation

@enkot
Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

enkot commented Apr 24, 2023

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

Comment thread src/utils/proxy.ts Outdated
}

if (opts.onResponse) {
await opts.onResponse(event);
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown

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
Copy Markdown
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
Copy Markdown

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

@pi0 pi0 merged commit 5a3d0e8 into h3js:main Jun 20, 2023
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