@websdr/nestjs-microservice
v0.5.3
Published
This is a NestJS microservice for WebSDR
Downloads
24
Maintainers
Readme
@websdr/nestjs-microservice
Reusable NestJS building blocks for the WebSDR backend.
This package is published as an npm module and is intended to be embedded into your NestJS app (import modules, reuse guards/services), not run as a standalone server by itself.
What’s inside
- Auth (
/auth):AuthModule,AuthService,JwtAuthGuard, DTOs and interfaces. - Users (
/users):UsersModuleandUsersService. - Common (
/common): a small logging helper module (LoggingModule,createContextLogger) andLoggerLevelService/parseLogLevels.
Install
npm install @websdr/nestjs-microservicepnpm add @websdr/nestjs-microserviceyarn add @websdr/nestjs-microserviceImporting
This package is published as ESM (see type: module).
Import from the root:
import { AuthModule, UsersModule, JwtAuthGuard } from '@websdr/nestjs-microservice';Or use subpath exports:
import { AuthModule, JwtAuthGuard } from '@websdr/nestjs-microservice/auth';
import { UsersModule } from '@websdr/nestjs-microservice/users';
import { LoggingModule, LOGGER } from '@websdr/nestjs-microservice/common';Usage
1) Import modules in your Nest app
import { Module } from '@nestjs/common';
import { AuthModule, UsersModule } from '@websdr/nestjs-microservice';
@Module({
imports: [UsersModule, AuthModule],
})
export class AppModule {}2) Protect controllers with JwtAuthGuard
JwtAuthGuard validates a JWT (Passport strategy) and also checks token revocation via AuthService.isRevoked().
It looks for the token in either:
req.cookies.jwt, orAuthorization: Bearer <token>
import { Controller, Get, UseGuards } from '@nestjs/common';
import { JwtAuthGuard } from '@websdr/nestjs-microservice/auth';
@Controller('private')
export class PrivateController {
@UseGuards(JwtAuthGuard)
@Get()
getPrivateData() {
return { ok: true };
}
}Accessing the authenticated user (req.user) with proper typing:
import { Controller, Get, Req, UseGuards } from '@nestjs/common';
import type { AuthRequest } from '@websdr/nestjs-microservice/auth';
import { JwtAuthGuard } from '@websdr/nestjs-microservice/auth';
@Controller('me')
export class MeController {
@UseGuards(JwtAuthGuard)
@Get()
getMe(@Req() req: AuthRequest) {
return req.user;
}
}3) Provide a logger instance via LoggingModule
LoggingModule.forRoot() registers a logger instance under the LOGGER token.
createContextLogger() wraps a base logger and adds Nest-like context.
import { Module, Logger } from '@nestjs/common';
import { LoggingModule } from '@websdr/nestjs-microservice/common';
@Module({
imports: [LoggingModule.forRoot(new Logger())],
})
export class AppModule {}Injecting the base logger and creating a context-aware wrapper:
import { Inject, Injectable } from '@nestjs/common';
import type { LoggerService } from '@nestjs/common';
import { LOGGER, createContextLogger } from '@websdr/nestjs-microservice/common';
@Injectable()
export class DeviceService {
private readonly logger: LoggerService;
constructor(@Inject(LOGGER) base: any) {
this.logger = createContextLogger(base, DeviceService.name);
}
open() {
this.logger.log('opening device');
}
}4) Configure log levels
parseLogLevels() parses strings like "debug,warn,error", as well as "all"/"on" and "off"/"false".
import { Logger } from '@nestjs/common';
import { parseLogLevels } from '@websdr/nestjs-microservice/common';
Logger.overrideLogger(parseLogLevels(process.env.LOG_LEVELS));If you prefer a service that can update levels at runtime:
import { NestFactory } from '@nestjs/core';
import { LoggerLevelService } from '@websdr/nestjs-microservice/common';
import { AppModule } from './app.module';
const app = await NestFactory.create(AppModule);
app.get(LoggerLevelService).setLevelsFromString(process.env.LOG_LEVELS);
await app.listen(3000);Public API (summary)
@websdr/nestjs-microservice/auth:AuthModuleJwtAuthGuard- DTOs:
LoginDto - Types:
AuthUser,AuthRequest
@websdr/nestjs-microservice/users:UsersModule
@websdr/nestjs-microservice/common:LoggingModule,LOGGER,createContextLoggerLoggerLevelService,parseLogLevels,LoggerLevels
Compatibility notes
- NestJS: built against NestJS v11.
- TypeScript: ships
*.d.tstypings. - ESM: package is ESM. If your app is CommonJS, use a bundler/transpiler setup that supports ESM dependencies.
Development
From the repository root:
npm installFrom this package folder:
Build
npm run buildTest
npm testSource links
This package publishes dist/ to npm. Source is available in the GitHub repository:
- Entry point: https://github.com/wavelet-lab/websdr/blob/main/packages/nestjs-microservice/src/index.ts
- Auth exports: https://github.com/wavelet-lab/websdr/blob/main/packages/nestjs-microservice/src/auth/index.ts
- Common exports: https://github.com/wavelet-lab/websdr/blob/main/packages/nestjs-microservice/src/common/index.ts
- Users exports: https://github.com/wavelet-lab/websdr/blob/main/packages/nestjs-microservice/src/users/index.ts
Package folder (GitHub): https://github.com/wavelet-lab/websdr/tree/main/packages/nestjs-microservice
License
WebSDR is MIT licensed
