Skip to content

Commit 256e4cf

Browse files
committed
PD-3704
1 parent ee7970f commit 256e4cf

3 files changed

Lines changed: 81 additions & 0 deletions

File tree

angular.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,10 @@
299299
"proxyConfig": "src/proxy.conf.sandbox.mjs",
300300
"buildTarget": "ng-orcid:build:local-with-proxy"
301301
},
302+
"local-orcid-web": {
303+
"proxyConfig": "src/proxy.conf.orcid-web.mjs",
304+
"buildTarget": "ng-orcid:build:local-with-proxy"
305+
},
302306
"local": {
303307
"buildTarget": "ng-orcid:build:local"
304308
},

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"start-local": "npm run build-runtime-env && ng serve --disable-host-check --host 0.0.0.0",
66
"start": "npm run build-runtime-env && (ng serve --configuration=local-qa --host 0.0.0.0 & npm run start:ui-docs)",
77
"start-sandbox": "ng serve --configuration=local-sandbox --host 0.0.0.0",
8+
"start:orcid-web": "npm run build-runtime-env && ng serve --configuration=local-orcid-web --host 0.0.0.0",
89
"start:en": "ng serve --configuration=local-qa-en --disable-host-check",
910
"start:ar": "ng serve --configuration=local-qa-ar --disable-host-check",
1011
"start:fr": "ng serve --configuration=local-qa-fr --disable-host-check",

src/proxy.conf.orcid-web.mjs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)