|
| 1 | +/** |
| 2 | + * Proxy configuration to hit a local orcid-web backend at http://localhost:8080/orcid-web |
| 3 | + * - Rewrites cookie domains to localhost |
| 4 | + * - Strips `secure` so cookies work over http |
| 5 | + * - Proxies only API calls (not the Angular app shell) |
| 6 | + */ |
| 7 | + |
| 8 | +function rewriteCookies(proxyRes) { |
| 9 | + const cookies = proxyRes.headers['set-cookie'] |
| 10 | + if (!cookies) return |
| 11 | + |
| 12 | + const rewrite = (cookie) => { |
| 13 | + let rewritten = cookie |
| 14 | + // Force cookies to localhost domain |
| 15 | + rewritten = rewritten.replace(/Domain=[^;]+/gi, 'Domain=localhost') |
| 16 | + // Normalize path to root |
| 17 | + rewritten = rewritten.replace(/Path=\/orcid-web/gi, 'Path=/') |
| 18 | + // Remove secure for http local dev |
| 19 | + rewritten = rewritten.replace(/;\s*secure/gi, '') |
| 20 | + return rewritten.trim() |
| 21 | + } |
| 22 | + |
| 23 | + proxyRes.headers['set-cookie'] = Array.isArray(cookies) |
| 24 | + ? cookies.map(rewrite) |
| 25 | + : rewrite(cookies) |
| 26 | +} |
| 27 | + |
| 28 | +function responseOverridesGeneric() { |
| 29 | + return (proxyRes, req, res) => { |
| 30 | + rewriteCookies(proxyRes) |
| 31 | + if (proxyRes.statusCode >= 300 && proxyRes.statusCode < 400) { |
| 32 | + const location = proxyRes.headers['location'] |
| 33 | + if (typeof location === 'string') { |
| 34 | + proxyRes.headers['location'] = location.replace( |
| 35 | + 'http://localhost:8080/orcid-web', |
| 36 | + 'http://localhost:4200' |
| 37 | + ) |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +const shouldProxyRootApi = (pathname, req) => { |
| 44 | + const accept = req.headers.accept || '' |
| 45 | + |
| 46 | + // Do NOT proxy SPA navigations or dev-server internals |
| 47 | + if (accept.includes('text/html')) return false |
| 48 | + if ( |
| 49 | + pathname.startsWith('/assets') || |
| 50 | + pathname.startsWith('/favicon.ico') || |
| 51 | + pathname.startsWith('/ng-cli-ws') || |
| 52 | + pathname.startsWith('/sockjs-node') || |
| 53 | + pathname === '/' || |
| 54 | + pathname === '/index.html' |
| 55 | + ) { |
| 56 | + return false |
| 57 | + } |
| 58 | + |
| 59 | + // Everything else at root (your API calls without a prefix) → proxy |
| 60 | + return true |
| 61 | +} |
| 62 | + |
| 63 | +export default [ |
| 64 | + { |
| 65 | + // Proxy root-level API calls to local orcid-web |
| 66 | + context: shouldProxyRootApi, |
| 67 | + target: 'http://localhost:8080/orcid-web', |
| 68 | + secure: false, |
| 69 | + changeOrigin: true, |
| 70 | + logLevel: 'debug', |
| 71 | + cookieDomainRewrite: 'localhost', |
| 72 | + onProxyRes: responseOverridesGeneric(), |
| 73 | + pathRewrite: { '^/': '/' }, |
| 74 | + }, |
| 75 | +] |
| 76 | + |
0 commit comments