jaycohb
jaycohb

Reputation: 13

Adding requestInterceptor to Swagger Nestjs

I was wondering how can I add requestInterceptor method to Swagger used with Nestjs? I can't find anywhere how this property can be added to SwaggerModule configuration. That's how I setup swagger docs:

SwaggerModule.setup('docs', app, document, options2);

Reason I need to use requestInterceptor is that I need to add custom 'Origin' header to Swagger's "Try it out" curl request. The only way to play with it is this requestInterceptor property.

Anyone met this problem before?

Cheers.

Upvotes: 1

Views: 1992

Answers (2)

Alexey Petushkov
Alexey Petushkov

Reputation: 2158

You need to set swaggerOptions object in setup parameters

Ex, if you want to enable cookies:

  SwaggerModule.setup('swagger', app, document, {
    swaggerOptions: {
      requestInterceptor: (req) => {
        req.credentials = 'include';
        return req;
      },
    },
  });

Upvotes: 1

Micael Levi
Micael Levi

Reputation: 6685

Your options2 object can receive this method, like:

SwaggerModule.setup('docs', app, document, {
  requestInterceptor: (req) => {
    req.headers['Origin'] = 'your custom value'
    return req
  }
})

try this.

Upvotes: 1

Related Questions