npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@betsys-nestjs/headers

v4.0.0

Published

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

Downloads

17

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