@betsys-nestjs/headers
v4.0.0
Published
This library is responsible for enforcing mandatory headers in the request.
Downloads
8
Maintainers
Keywords
Readme
@betsys-nestjs/headers
This is a simple library helps you to enforce mandatory headers in requests. Also you can provide custom validators and
conditional headers.
Works for Express
adapter.
Dependencies
| Package | Version | | --------------- | ------- | | @nestjs/common | ^10.0.0 | | @nestjs/core | ^10.0.0 | | class-validator | ^0.13.2 |
Usage
When building the module of the application you need to
initialize the library using forRoot
method that accepts PlatformType
as an argument.
PlatformType
can be 'express'
.
import {HeadersModule} from "@betsys-nestjs/headers";
@Module({
imports: [
HeadersModule.forRoot('express'),
],
controllers: [
Test1Controller,
Test2Controller,
],
})
export class AppModule {
}
As soon as you initialize module using forRoot
method, you can use forFeature
method to
create a ruleset for headers. This config under the hood dynamically configures NestJS middleware
. Method accepts 4
parameters:
This config is defined this way:
interface HeadersConfig {
requiredHeaders: Array<HeaderWithValidation>;
forRoutes: Array<string | Type | RouteInfo>;
exclude?: Array<string | RouteInfo>;
conditionalHeaders?: ConditionalHeadersConfig[];
}
requiredHeaders
- You can define headers, that are required, it is an array with this interface:
interface HeaderWithValidation {
headerName: string;
validator: Type<unknown>;
}
where:
headerName
is name of a header (key) and validator
is Dto class that uses decorators from class-validator
to
validate given header
forRoutes
(optional) - You can define array of routes using explicit string, regular expressions, Controller references orRouteInfo
object that is defined this way:
If forRoutes
is not added, middleware is set globally.
interface RouteInfo {
path: string;
method: RequestMethod;
version?: VersionValue;
}
exclude
(optional) - You can define array with routes that should be excluded using either string orRouteInfo
conditionalHeaders
(optional) - This way you can require array of any headers dependent on other headers based on custom condition. This interface is defined like this:
interface ConditionalHeadersConfig {
ifHeader: string;
matchesCondition: (headerValue: string) => boolean;
thenRequireHeaders: Array<HeaderWithValidation>;
}
where:
ifHeader
- define header name that needs to fulfil condition inmatchesCondition
.matchesCondition
- condition for the header value defined inifHeader
.thenRequireHeaders
- here you define headers that are further required whenmatchesCondition
returnstrue
.
import {HeadersModule} from "@betsys-nestjs/headers";
@Module({
imports: [
HeadersModule.forFeature(
{
requiredHeaders: [
{headerName: 'A', validator: AHeaderDto},
{
headerName: 'B',
validator: BHeaderDto,
},
],
forRoutes: ['*'],
exclude: [TEST_2_PREFIX],
conditionalHeaders: [
{
ifHeader: 'C',
matchesCondition: (headerValue: string) => headerValue === 'MNAU',
thenRequireHeaders: [{headerName: 'D', validator: DHeaderDto}],
},
],
}
),
],
controllers: [
Test1Controller,
Test2Controller,
],
})
export class FeatureModule {
}
You can set the headers globally (i.e. you do not add forRoutes
) or
for particular controllers as shown in the example above.