Socket
Book a DemoInstallSign in
Socket

@betsys-nestjs/headers

Package Overview
Dependencies
Maintainers
9
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@betsys-nestjs/headers

This library is responsible for enforcing mandatory headers in the request.

4.0.0
latest
npmnpm
Version published
Weekly downloads
2
Maintainers
9
Weekly downloads
 
Created
Source

@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.

FAQs

Package last updated on 13 Nov 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.