Skip to content

Instantly share code, notes, and snippets.

@yordis
Created May 8, 2024 22:03
Show Gist options
  • Save yordis/f5ab0295cf58b58a49519f247819fe31 to your computer and use it in GitHub Desktop.
Save yordis/f5ab0295cf58b58a49519f247819fe31 to your computer and use it in GitHub Desktop.
// https://twitter.com/alchemist_ubi/status/1786275138532790439
function proxy(proxy: string, req: Request): Request {
const url = new URL(proxy);
const originUrl = new URL(req.url);
const header = new Headers(req.headers);
header.set('X-Forwarded-For', 'some-ip');
url.pathname = originUrl.pathname;
// Do other stuff mapping the `req` to the proxy req ....
// doesnot matter for the example
return new Request(url, {
...req,
headers: req.headers,
});
}
fetch(proxy('https://example.com', new Request('/api/data')));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment