nuxt-alt / proxy

An alternative module to @nuxtjs/proxy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Read env variables inside configure function during building step

lucasctd opened this issue Β· comments

Hello buddy,

First of all, thx for this great package.

Would it be possible to make the configure function execute during the build step?

//nuxt.config.ts

proxy: {
        proxies: {
            '/api/resources': {
                target: process.env.API_URL,
                rewrite: (path) => path.replace(/^\/api\/resources/, ''),
                changeOrigin: true,
                configure: (proxy, options) => {
                    options.headers = {
                        'authorization': 'Bearer ' + process.env.API_KEY,
                        'accept': 'application/json'
                    }
                },
            }
        }
    }

//.output/server/chunks/nitro/node-server.mjs

"configure": "(proxy, options) => {\n          options.headers = {\n            'authorization': 'Bearer ' + process.env.API_KEY,\n            'accept': 'application/json' };\n\n        }" } };

it writes process.env.API_KEY to the function instead of writing its actual value from the building process xD

As a workaround I am exporting this env variable to linux shell prior yarn start, but that would be great if it could just write the value itself so I wouldn't need to do it only for this variable.

commented

This is a limitation with serialization. Functions can't be used when serializing so it's converted to a string and then converted back into a function.

If I find out a better solution for this case I gonna post it here. Thx

commented

@lucasctd Do you think you can run version 2.3.0-beta.1

3 files will be created in a proxy folder in the srcDir corresponding to the functions that can't be serialized properly. As an example if you were to use the rewrite.ts file it's contents would look like this:

export default {
    testFunction: (path) => path.replace(/^\/api/, '')
}

and then in the nuxt proxy config you would use the name of the function as a string

proxy: {
    fetch: true,
    proxies: {
        '/oauth': {
            target: process.env.BROWSER_BASE_URL,
            rewrite: 'testFunction'
        },
    }
},

can you try this with your configure to see if it works for you?

@teranode

  • is it possible to access nuxt-alt/auth user token in the proxy configure?
  • How to globally add user token to API request headers?
        target: process.env.NUXT_API_BASE_URL,
        changeOrigin: true,
        rewrite: (path: string) => {
          return path.replace(/^\/my-proxy/, '')
        },
        configure: (proxy, options) => {
          options.headers = {
            // authorization: `Bearer auth.user.token `,πŸ‘ˆπŸ‘ˆπŸ‘ˆ,
          }
        },
      },```
commented

@rashidpathiyil I can pass the nitro event context through a param for the configure function and you can utilize it, but im not certain if it will be available at the time of the request.

@rashidpathiyil I can pass the nitro event context through a param for the configure function and you can utilize it, but im not certain if it will be available at the time of the request.

Do you have any suggestion to globally add user token to All API request headers when using nuxt-alt/proxy and nuxt-alt/auth?