Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional Headers on Swagger Typescript

I am creating a method that uses optional headers and if you make a request directly to the API works, but the swagger creates them as required. I am using NestJS as a framework.

This is the code for the request on the controller:

  createShoppingCart(
    @Headers('authToken') authToken?: string ,
    @Headers('sessionToken') sessionToken?: string,
    @Headers('segmentToken') segmentToken?: string,
  ): Promise<GetCartInformationResponseDto> {
    return this.shoppingCartsService.createShoppingCart(
      authToken,
      sessionToken,
      segmentToken,
    );
  }

And this is how the swagger displays the Headers. enter image description here

If I add the "or undefined" option it does not even show it into the swagger:

@Headers('authToken') authToken?: string | undefined,

Does anyone knows how to solve this?

like image 880
Luis Fernando Badel Méndez Avatar asked Aug 22 '26 17:08

Luis Fernando Badel Méndez


1 Answers

to make headers optional you can use @ApiHeader() decorator in your controller's route.

@Get()
@ApiHeader({
    name: 'x-skip-cache', // your header name
    required: false, // Set required to false to make it optional
    description: 'Optional header to skip cache',
  })
 async getLocationsData(
    @Headers('x-skip-cache') skipCache: string = 'false',
  ){}
like image 67
ttt Avatar answered Aug 27 '26 01:08

ttt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!