{"id":15527316,"url":"https://github.com/s3pweb/nestjs-common","last_synced_at":"2025-03-05T13:30:43.496Z","repository":{"id":64834779,"uuid":"577361364","full_name":"s3pweb/nestjs-common","owner":"s3pweb","description":"A collection of common utilities for NestJs","archived":true,"fork":false,"pushed_at":"2024-07-22T12:06:01.000Z","size":2219,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-01T05:02:42.150Z","etag":null,"topics":["nestjs","nestjs-boilerplate"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/s3pweb.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-12-12T15:11:05.000Z","updated_at":"2025-01-28T10:18:42.000Z","dependencies_parsed_at":"2023-11-28T10:27:32.599Z","dependency_job_id":"aa44f2dd-22e7-48fc-8e28-58e7c0696a88","html_url":"https://github.com/s3pweb/nestjs-common","commit_stats":{"total_commits":44,"total_committers":3,"mean_commits":"14.666666666666666","dds":0.06818181818181823,"last_synced_commit":"4892e86d26fcf004aff31fb36ba9bc9031dbc17a"},"previous_names":["s3pweb/nestjs-ip-whitelist"],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s3pweb%2Fnestjs-common","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s3pweb%2Fnestjs-common/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s3pweb%2Fnestjs-common/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s3pweb%2Fnestjs-common/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/s3pweb","download_url":"https://codeload.github.com/s3pweb/nestjs-common/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242034901,"owners_count":20061221,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["nestjs","nestjs-boilerplate"],"created_at":"2024-10-02T11:05:28.459Z","updated_at":"2025-03-05T13:30:43.175Z","avatar_url":"https://github.com/s3pweb.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![npm (scoped)](https://img.shields.io/npm/v/@s3pweb/nestjs-common)](https://www.npmjs.com/package/@s3pweb/nestjs-common)\n\n# S3PWeb - NestJS common libs\n\nA repo containing all the common utilities for our NestJS projects:\n- A NestJS guard to authorise only the given IP(s)\n- A logging service implementing the LoggerService interface and using the [@s3pweb/s3pweb-logger](https://github.com/s3pweb/s3pweb-logger)\n- A MetricsInterface to be used in the logging service\n- Multiple middlewares\n\n## Install\n```shell\nnpm i @s3pweb/nestjs-common\n```\n\n## Usage\n\n### IP Whitelist\n\nOn a controller to protect all routes:\n```ts\n@Controller('v1/cats')\n@IpWhitelist('0.0.0.0', '::1')\n@UseGuards(IpWhitelistGuard)\nexport class CatsController {}\n```\n\nDirectly on a route (with the `UseGuards` on the controller):\n```ts\n@Post()\n@IpWhitelist('192.168.1.1')\nasync createCat() {}\n```\n\n### Logging Service\n\nTo use the `LoggingModule`, add it to the imports of the `AppModule` with its configuration in a factory:\n\n```ts\n@Module({\n  imports: [\n    ConfigModule.forRoot({ isGlobal: true }),\n    LoggingModule.forRootAsync({\n      useFactory: async (config: ConfigService) =\u003e {\n        return config.get('logger');\n      },\n      inject: [ConfigService],\n    }),\n    PromModule,\n  ],\n  controllers: [\n    AppController,\n  ],\n})\nexport class AppModule implements NestModule {}\n```\n\nThen it can be used in a service:\n\n```ts\n@Injectable()\nexport class Service {\n  private readonly log: any;\n\n  constructor(\n    logger: LoggingService,\n  ) {\n    this.log = logger.getLogger(Service.name);\n  }\n}\n```\n\nTo be able ton inject it correctly, you need a metrics module exporting a service implementing the `MetricsInterface`:\n\n```ts\n@Global()\n@Module({\n  providers: [\n    {\n      provide: METRICS_SERVICE,\n      useClass: PromService,\n    },\n  ],\n  controllers: [PromController],\n  exports: [\n    {\n      provide: METRICS_SERVICE,\n      useClass: PromService,\n    },\n  ],\n})\nexport class PromModule {\n  // -- Empty\n}\n```\n\n```ts\n@Injectable()\nexport class PromService implements MetricsInterface {}\n```\n\nTo use the PromService in a Controller or a Service, you can inject it in a constructor with:\n```ts\n@Inject(METRICS_SERVICE) private readonly metricsService: PromService,\n```\n\n### Metrics Interface\n\nThis interface enables you to implements the required functions for the `LoggingService`.\n\n### Middlewares\n\n### Cache Control\n\nThis middleware will add a new header to disable request caching (`Cache-Control`).\n\nTo set the middleware everywhere (in the bootstrap function of main.ts):\n\n```ts\nasync function bootstrap() {\n  const app = await NestFactory.create(AppModule);\n\n  // Set Cache-Control header everywhere\n  app.use(cacheControlMiddleware());\n  \n  // ...\n}\n```\n\n#### Correlation ID\n\nThis middleware will add a new header in each request and response `X-Correlation-Id`, this is an uuid V1 string (different for each request/response).\nIt was lifted from https://github.com/eropple/nestjs-correlation-id/\n\nTo set the middleware everywhere (in the bootstrap function of main.ts):\n\n```ts\nasync function bootstrap() {\n  const app = await NestFactory.create(AppModule);\n\n  // Tracking ID\n  app.use(CorrelationIdMiddleware());\n  \n  // ...\n}\n```\n\nYou can then get the uuid in a controller:\n\n```ts\n@Controller('v1/cats')\nexport class CatsController {\n  @Post()\n  async createCat(\n    @Headers(correlationId) uuid: string,\n  ) {\n    // console.log(uuid)\n  }\n}\n```\n\n#### HTTP Exceptions Logger\n\nThis middleware will log as a warning all failed HTTP request (with their uuid) to provide more details when an error occurs.\nExample: `NotFoundException: Cannot GET /cats/v2/api/`\n\nTo set the middleware everywhere (in the bootstrap function of main.ts):\n```ts\nasync function bootstrap() {\n  const app = await NestFactory.create(AppModule);\n\n  // Use our logger for all Nest logs\n  const logger = await app.resolve(LoggingService);\n  app.useLogger(logger);\n  // Add custom filter to log all http errors\n  const { httpAdapter } = app.get(HttpAdapterHost);\n  app.useGlobalFilters(new ExceptionsLoggerFilter(logger, httpAdapter));\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fs3pweb%2Fnestjs-common","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fs3pweb%2Fnestjs-common","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fs3pweb%2Fnestjs-common/lists"}