scottie1984 / swagger-ui-express

Adds middleware to your express app to serve the Swagger UI bound to your Swagger document. This acts as living documentation for your API hosted from within your app.
MIT License
1.42k stars 225 forks source link

Is it possible to pass variables to request interceptors ? #258

Closed ypicard closed 2 years ago

ypicard commented 3 years ago

Hi,

I am trying to pass a custom request interceptor to the swagger configuration. Unfortunately, I cannot manage to pass a custom variable to this function. I get an undefined variable error.

const audience ='...' // I would like to use this
app.use(
    prefix + '/docs',
    swaggerUi.serve,
    swaggerUi.setup(openapiSpecification, {
        swaggerOptions: {
            requestInterceptor: (request: Request) => {
                if (request.url.includes('oauth/token')) {
                    const body = `${request.body}&audience=${encodeURIComponent('http://localhost:3000')}`; <---- Here, if I replace this with the audience variable above.

                    return {
                        ...request,
                        body,
                    };
                }

                return request;
            },
            oauth: {
                clientId: process.env.AUTH0_CLIENT_ID,
                clientSecret: process.env.AUTH0_CLIENT_SECRET,
            },
        },
    })
);

Is this even possible? Thank you!

danielfev commented 3 years ago

same problem.. any solution?

scottie1984 commented 2 years ago

This function is run on the client - so you will not have access to anything from the server. You could try using the using some custom js to define some variables that you could then use https://github.com/scottie1984/swagger-ui-express#custom-js. However you might run into scoping problems depending on how the requestInterceptor function it called.