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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@struktos/adapter-nestjs

v0.1.0

Published

NestJS adapter for Struktos.js - Enterprise-grade context propagation, authentication, and logging for NestJS applications

Readme

@struktos/adapter-nestjs

NestJS adapter for Struktos.js - Enterprise-grade context propagation, authentication, and logging for NestJS applications

npm version License: MIT

🎯 What is this?

@struktos/adapter-nestjs brings the power of Struktos.js to NestJS applications, providing seamless integration with NestJS's dependency injection, decorators, and module system.

Key Features:

  • Context Propagation - Automatic context propagation via StruktosCoreModule
  • Custom Decorators - @Ctx(), @TraceId(), @CurrentUser() for easy context access
  • Auth Guards - StruktosAuthGuard integrates with @struktos/auth
  • Role-Based Auth - @Roles() decorator for role-based authorization
  • Claims-Based Auth - @RequireClaim() for fine-grained permissions
  • Interceptors - Request logging and response enrichment
  • Full TypeScript - Complete type safety with NestJS

📦 Installation

npm install @struktos/adapter-nestjs @struktos/core @nestjs/common @nestjs/core reflect-metadata

# Optional - for authentication
npm install @struktos/auth

# Optional - for structured logging
npm install @struktos/logger

🚀 Quick Start

1. Import Module

import { Module } from '@nestjs/common';
import { StruktosCoreModule } from '@struktos/adapter-nestjs';

@Module({
  imports: [
    StruktosCoreModule.forRoot({
      generateTraceId: () => `trace-${Date.now()}`,
      enableCancellation: true,
    }),
  ],
})
export class AppModule {}

2. Use Context in Controllers

import { Controller, Get, UseGuards } from '@nestjs/common';
import { 
  Ctx, 
  TraceId, 
  CurrentUser, 
  Roles, 
  StruktosAuthGuard 
} from '@struktos/adapter-nestjs';

@Controller('users')
export class UsersController {
  @Get()
  findAll(@TraceId() traceId: string) {
    console.log(`TraceID: ${traceId}`);
    return this.usersService.findAll();
  }

  @Get('profile')
  @UseGuards(StruktosAuthGuard)
  getProfile(@CurrentUser() user: User) {
    return user;
  }

  @Get('admin')
  @UseGuards(StruktosAuthGuard)
  @Roles('Admin')
  adminOnly() {
    return { message: 'Admin content' };
  }
}

📖 API Reference

Modules

StruktosCoreModule

Main module that provides context propagation and all Struktos features.

// Synchronous configuration
StruktosCoreModule.forRoot({
  generateTraceId: () => uuid.v4(),
  enableCancellation: true,
  extractUser: (req) => req.user,
  onContextCreated: (ctx, req) => console.log('Context created'),
  onContextDestroyed: (ctx, req) => console.log('Context destroyed'),
  isGlobal: true, // default
});

// Async configuration
StruktosCoreModule.forRootAsync({
  imports: [ConfigModule],
  useFactory: (config: ConfigService) => ({
    generateTraceId: () => config.get('TRACE_PREFIX') + uuid.v4(),
  }),
  inject: [ConfigService],
});

StruktosAuthModule

Authentication-only module for when you don't need full context propagation.

StruktosAuthModule.forRoot({
  authService: myAuthService,
  useGlobalGuard: true,
});

StruktosLoggingModule

Logging-only module with interceptors.

StruktosLoggingModule.forRoot({
  logger: myLogger,
  useGlobalInterceptor: true,
});

Decorators

Context Decorators

// Get full context
@Get()
handler(@Ctx() ctx: RequestContext<NestJSContextData>) {
  console.log(ctx.get('traceId'));
}

// Get specific context value
@Get()
handler(@Ctx('userId') userId: string) {
  console.log(userId);
}

// Shorthand decorators
@Get()
handler(
  @TraceId() traceId: string,
  @UserId() userId: string,
  @CurrentUser() user: User,
  @CurrentUser('email') email: string,
  @RequestTimestamp() timestamp: number,
  @IsCancelled() isCancelled: () => boolean,
) {}

Auth Decorators

// Role-based authorization
@Roles('Admin')
@Roles('Admin', 'Moderator')  // OR logic

// Claims-based authorization
@RequireClaim('permission', 'write:documents')
@RequireClaim('feature', 'beta-access')

// Multiple claims (AND logic)
@RequireClaims([
  { type: 'permission', value: 'read' },
  { type: 'department', value: 'engineering' }
])

// Mark route as public
@Public()

// Combined decorator
@Auth({
  roles: ['Admin'],
  claim: { type: 'permission', value: 'admin:write' }
})

// Shorthand
@AdminOnly()
@ModeratorOrAdmin()

Guards

// Unified auth + authorization guard
@UseGuards(StruktosAuthGuard)
@Roles('Admin')
handler() {}

// Role-only guard (assumes user already authenticated)
@UseGuards(StruktosRolesGuard)
@Roles('Admin')
handler() {}

// Claims-only guard
@UseGuards(StruktosClaimsGuard)
@RequireClaim('permission', 'write')
handler() {}

Interceptors

// Add trace ID and timing to responses
@UseInterceptors(StruktosContextInterceptor)

// Request/response logging
@UseInterceptors(StruktosLoggingInterceptor)

// Simple timing header only
@UseInterceptors(StruktosTimingInterceptor)

// Global registration
@Module({
  providers: [
    {
      provide: APP_INTERCEPTOR,
      useClass: StruktosContextInterceptor,
    },
  ],
})

🔐 Authentication Integration

With @struktos/auth

import { AuthService, InMemoryAuthStore } from '@struktos/auth';
import { 
  StruktosCoreModule, 
  StruktosAuthModule,
  AUTH_SERVICE 
} from '@struktos/adapter-nestjs';

const authStore = new InMemoryAuthStore();
const authService = new AuthService(authStore, {
  jwtSecret: 'your-secret',
});

@Module({
  imports: [
    StruktosCoreModule.forRoot(),
    StruktosAuthModule.forRoot({
      authService,
      useGlobalGuard: true, // Apply to all routes
    }),
  ],
})
export class AppModule {}

Public Routes with Global Guard

@Controller('auth')
export class AuthController {
  @Public()  // Bypasses global auth guard
  @Post('login')
  login() {}

  @Public()
  @Post('register')
  register() {}
}

📊 Context Access Anywhere

Context is automatically available in services, repositories, and any async operation:

import { Injectable } from '@nestjs/common';
import { RequestContext } from '@struktos/core';
import { NestJSContextData } from '@struktos/adapter-nestjs';

@Injectable()
export class UsersService {
  async findAll() {
    const ctx = RequestContext.current<NestJSContextData>();
    const traceId = ctx?.get('traceId');
    const userId = ctx?.get('userId');
    
    console.log(`[${traceId}] User ${userId} fetching all users`);
    
    return this.userRepository.find();
  }
}

🧪 Testing

import { Test } from '@nestjs/testing';
import { StruktosCoreModule, AUTH_SERVICE } from '@struktos/adapter-nestjs';

describe('UsersController', () => {
  let controller: UsersController;

  beforeEach(async () => {
    const module = await Test.createTestingModule({
      imports: [StruktosCoreModule.forRoot()],
      controllers: [UsersController],
      providers: [
        UsersService,
        {
          provide: AUTH_SERVICE,
          useValue: {
            validateToken: jest.fn().mockResolvedValue({
              id: 'test-user',
              username: 'test',
              roles: ['Admin'],
            }),
          },
        },
      ],
    }).compile();

    controller = module.get<UsersController>(UsersController);
  });

  it('should return users', async () => {
    const result = await controller.findAll();
    expect(result).toBeDefined();
  });
});

🏗️ Architecture

HTTP Request
    ↓
[StruktosMiddleware] - Initialize RequestContext
    ↓
[StruktosAuthGuard] - Authenticate & Authorize
    ↓
[StruktosContextInterceptor] - Pre-processing
    ↓
[Controller] - @Ctx(), @TraceId(), @CurrentUser()
    ↓
[Service] - RequestContext.current()
    ↓
[Repository] - RequestContext.current()
    ↓
[Response] - X-Trace-Id, X-Response-Time headers

🤝 Related Packages

📄 License

MIT © Struktos.js Team

🔗 Links


Built with ❤️ for enterprise NestJS development