4.0.0 • Published 6 months ago

@betsys-nestjs/headers v4.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
6 months ago

@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

PackageVersion
@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 or RouteInfo 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 or RouteInfo
  • 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 in matchesCondition.
  • matchesCondition - condition for the header value defined in ifHeader.
  • thenRequireHeaders - here you define headers that are further required when matchesCondition returns true.
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.

4.0.0

6 months ago

3.0.0

7 months ago

2.1.0

8 months ago

1.0.2

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.0.3

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago