Skip to content

Commit

Permalink
feat(proxy): support conditional options for proxy request (#1888)
Browse files Browse the repository at this point in the history
...by returning an options object from the `bypass` function.
  • Loading branch information
cisen committed Feb 5, 2021
1 parent 9c819b9 commit e81a118
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/vite/src/node/server/middlewares/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,18 @@ export function proxyMiddleware({
url.startsWith(context)
) {
const [proxy, opts] = proxies[context]
const proxyOpitons: HttpProxy.ServerOptions = {}

This comment has been minimized.

Copy link
@tbl0605

tbl0605 Feb 5, 2021

Little typo ;)


if (opts.bypass) {
const bypassResult = opts.bypass(req, res, opts)
if (typeof bypassResult === 'string') {
req.url = bypassResult
debug(`bypass: ${req.url} -> ${bypassResult}`)
return next()
} else if (typeof bypassResult === 'object') {
Object.assign(proxyOpitons, bypassResult)
debug(`bypass: ${req.url} use modified opitions: %O`, proxyOpitons)
return next()
} else if (bypassResult === false) {
debug(`bypass: ${req.url} -> 404`)
return res.end(404)
Expand All @@ -102,7 +107,7 @@ export function proxyMiddleware({
if (opts.rewrite) {
req.url = opts.rewrite(req.url!)
}
proxy.web(req, res)
proxy.web(req, res, proxyOpitons)
return
}
}
Expand Down

0 comments on commit e81a118

Please sign in to comment.