Instead of adding the headers in $axios.onRequest(), maybe all you need to do is add the headers as part of the proxy config, like this:
proxy: {
"/api/graphql": {
...
headers: {
"cf_access_client_id": process.env.CF_ACCESS_CLIENT_ID,
"cf_access_client_secret": process.env.CF_ACCESS_CLIENT_SECRET,
},
}
}
None of this proxy config is seen in the client browser as the proxy logic is performed server-side. I use this approach myself to proxy to several different backend APIs and it works well (NuxtJS v2.15). The Nuxt Proxy module leans on http-proxy-middleware, and you can see it's available options here https://github.com/chimurai/http-proxy-middleware#options.
Something else you might find handy is how to adjust the request in a more "dynamic" way. For instance, if you had to add a secret code as a GET param to the request, or add an auth token that can only be determined at runtime (e.g. via a cookie), you could do something like the following:
proxy: {
"/api/graphql": {
...
onProxyReq: (proxyReq, req, res) => {
const code = process.env.API_CODE;
if (code) {
proxyReq.path += `${proxyReq.path.indexOf('?') !== -1 ? '&' : '?'}code=${encodeURIComponent(code)}`;
}
// add auth token??
const authToken = ...;
proxyReq.setHeader('Authorisation', `Bearer ${authToken}`);
},
},
}