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?
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 {}
You can use DocumentBuilder.addGlobalParameters method.
Unfortunately, it's not described in the documentation, but here is my example:
SwaggerModule.setup(
'docs',
application, // your NestJS application created by NestFactory.create
SwaggerModule.createDocument(
application,
new DocumentBuilder()
.setTitle('My application')
.addGlobalParameters({
in: 'header',
required: false,
name: 'x-global-header',
schema: {
example: 'some value',
},
})
.build(),
),
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With