(target: string, requestPath: string, search = '')
| 79 | } |
| 80 | |
| 81 | export const buildUpstreamUrl = (target: string, requestPath: string, search = '') => { |
| 82 | const targetUrl = new URL(target) |
| 83 | const normalizedTargetPath = targetUrl.pathname === '/' ? '' : targetUrl.pathname.replace(/\/$/, '') |
| 84 | const normalizedRequestPath = requestPath.startsWith('/') ? requestPath : `/${requestPath}` |
| 85 | const hasTargetPrefix = normalizedTargetPath |
| 86 | && (normalizedRequestPath === normalizedTargetPath || normalizedRequestPath.startsWith(`${normalizedTargetPath}/`)) |
| 87 | |
| 88 | targetUrl.pathname = hasTargetPrefix |
| 89 | ? normalizedRequestPath |
| 90 | : `${normalizedTargetPath}${normalizedRequestPath}` |
| 91 | targetUrl.search = search |
| 92 | |
| 93 | return targetUrl |
| 94 | } |
| 95 | |
| 96 | const createProxyRequestHeaders = ( |
| 97 | request: Request, |
no test coverage detected