Skip to content

Commit

Permalink
Do not rewrite when 'from' rule does not match. Fixes #11.
Browse files Browse the repository at this point in the history
  • Loading branch information
75lb committed Jun 11, 2020
1 parent 7392748 commit a38c90d
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 135 deletions.
42 changes: 23 additions & 19 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,37 @@ function getTargetUrl (from, to, url) {
const fromParams = []
const re = pathToRegexp(from, fromParams)
const fromMatches = re.exec(url)
let toUrl = to
if (fromMatches) {
let toUrl = to

for (const [index, fromParam] of fromParams.entries()) {
if (fromMatches && fromMatches[index + 1]) {
fromParam.value = fromMatches[index + 1]
for (const [index, fromParam] of fromParams.entries()) {
if (fromMatches && fromMatches[index + 1]) {
fromParam.value = fromMatches[index + 1]
}
}
}

/* replace named params */
for (const fromParam of fromParams) {
if (typeof fromParam.name === 'string') {
if (fromParam.value) {
toUrl = toUrl.replace(new RegExp(`:${fromParam.name}`, 'g'), fromParam.value)
} else {
toUrl = url
/* replace named params */
for (const fromParam of fromParams) {
if (typeof fromParam.name === 'string') {
if (fromParam.value) {
toUrl = toUrl.replace(new RegExp(`:${fromParam.name}`, 'g'), fromParam.value)
} else {
toUrl = url
}
}
}
}

/* replace positional params */
for (const fromParam of fromParams) {
if (typeof fromParam.name !== 'string') {
toUrl = url.replace(re, toUrl)
/* replace positional params */
for (const fromParam of fromParams) {
if (typeof fromParam.name !== 'string') {
toUrl = url.replace(re, toUrl)
}
}
}

return toUrl
return toUrl
} else {
return url
}
}

function removeHopSpecificHeaders (headers) {
Expand Down
Loading

0 comments on commit a38c90d

Please sign in to comment.