0.0.3-test • Published 7 months ago

netlabs-njs-common v0.0.3-test

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

Initialize your project and install netlabs-njs-common

mkdir <your-project-name> && cd <your-project-name>
nest new ./
pnpm add netlabs-njs-common
pnpm start --watch

Install netlabs-njs-common

pnpm add netlabs-njs-common

1. Use config module for app and database configuration

pnpm add @nestjs/config
// src/app.module.ts
import { AppConfigModule, AppConfigService } from 'netlabs-njs-common';
import { AppService } from './app.service';
import { AppController } from './app.controller';
import { Module } from '@nestjs/common';

@Module({
  imports: [AppConfigModule],
  controllers: [AppController],
  providers: [AppService, AppConfigService],
})
export class AppModule {}
// src/app.controller.ts
import { Controller, Get } from '@nestjs/common';
import { AppConfigService } from 'netlabs-njs-common';

@Controller()
export class AppController {
  constructor(private readonly appConfigService: AppConfigService) {}

  @Get('app-config')
  getAppConfig(): any {
    return this.appConfigService.appConfig;
  }

  @Get('database-config')
  getDatabaseConfig(): any {
    return this.appConfigService.databaseConfig;
  }
}

2. Use response interceptor for response formatting

// path/to/your/controller.tsx
import { ResponseInterceptor } from 'netlabs-njs-common';

@UseInterceptors(ResponseInterceptor)
@Controller()
export class YourController {
  @Get('your-endpoint')
  yourEndpoint(): any {
    return {
      data: 'Your data',
      metadata: { timestamp: new Date().toISOString() },
    };
  }
}

3. Use health module for health check

pnpm add @nestjs/terminus @nestjs/axios prom-client @nestjs/swagger
// src/utils/health/health.module.ts
import { Module } from '@nestjs/common';
import { HealthModule } from './health.module';

@Module({
  imports: [HealthModule],
})
export class AppModule {}