Search code examples
swaggernestjs

Global Headers for all Controllers (nestJs swagger)


Is there a way to globally add required headers to all endpoints / controllers in NestJS?

There is a controller bound decorator @ApiHeader. Is there a way to apply this to all endpoints?


Solution

  • Shortest way I have found is to do the following:

    export function Headers() {
      return applyDecorators(
        ApiHeader({
          name: 'header1',
          description: "description"
        }),
        ApiHeader({
          name: 'header2',
          description: "description"
        }),
        ApiHeader({
          name: 'header3',
          description: "description"
        })
      );
    }
    
    @Headers()
    @Controller('some-controller')
    export class ContactsController {}