Shared NestJS utilities (modules, guards, interceptors, decorators, DTOs, and entities) for all @bts-soft packages.
npm install @bts-soft/common
ConfigModule.
i18n for REST and GraphQL.
BaseEntity and BaseResponse for entities and DTOs.
ts
// Interceptors
export * from "./interceptors/generalResponse.interceptor";
export * from "./interceptors/sqlInjection.interceptor";
export * from "./interceptors/main.interceptor";
// Bases
export * from "./bases/BaseResponse";
export * from "./bases/BaseEntity";
// DTOs
export * from "./dtos/currentUser.dto";
export * from "./dtos/pagintion";
// Modules
export * from "./config/config.module";
export * from "./throttler/throttling.module";
export * from "./translation/translation.module";
export * from "./graphql/graphql.module";
// Production
export * from "./production/displayConsoles";
`
---
Key Components
$3
#### SqlInjectionInterceptor
Blocks SQL injection attempts in both REST and GraphQL by sanitizing all request data.
#### GeneralResponseInterceptor
Standardizes all API responses (REST and GraphQL) into a unified JSON structure.
#### setupInterceptors
Registers the interceptors globally:
`ts
setupInterceptors(app);
`
---
$3
#### BaseEntity
Extends TypeORM’s BaseEntity to include:
- ULID-based IDs
- createdAt and updatedAt timestamps
- Lifecycle logging
#### BaseResponse
Defines a standard structure for API responses across both REST and GraphQL.
---
$3
#### PaginationInfo
Provides pagination metadata: totalPages, currentPage, and totalItems.
#### CurrentUserDto
Represents the minimal data structure for authenticated users (id and email).
---
$3
Loads environment variables dynamically based on the current NODE_ENV.
`ts
envFilePath: .env.${process.env.NODE_ENV || 'development'}
`
Features:
- Global configuration availability
- Cached environment variables for performance
- Automatic .env file selection
---
$3
Implements rate-limiting strategies to prevent API abuse using:
- Short: 3 requests per second
- Medium: 20 requests per 10 seconds
- Long: 100 requests per minute
Example:
`ts
@Throttle('short')
@Get('login')
login() { return 'Limited to 3 requests per second'; }
`
---
$3
Enables i18n (multi-language) support across REST and GraphQL using JSON locale files.
- Detects language from x-lang or Accept-Language headers
- Default language fallback: English (en)
- Watches locale files for real-time updates
Example translation structure:
`
src/common/translation/locales/
├── en.json
└── ar.json
`
---
$3
Provides a full GraphQL setup with:
- Apollo driver integration
- Schema auto-generation
- Global error formatting
- Subscriptions support
- Playground and context configuration
It also includes a GraphQL exception filter to ensure consistent error responses.
---
$3
#### disableConsoleInProduction
Disables all console methods (log, error, warn, info, debug) when running in production mode.
Usage:
`ts
import { disableConsoleInProduction } from '@bts-soft/common';
disableConsoleInProduction();
`
---
Installation
`bash
npm install @bts-soft/common
`
---
Example Integration
$3
`ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { setupInterceptors } from '@bts-soft/common';
import { disableConsoleInProduction } from '@bts-soft/common';
async function bootstrap() {
disableConsoleInProduction();
const app = await NestFactory.create(AppModule);
setupInterceptors(app);
await app.listen(3000);
}
bootstrap();
`
$3
`ts
import { Module } from '@nestjs/common';
import {
ConfigModule,
ThrottlerModule,
TranslationModule,
GraphqlModule,
} from '@bts-soft/common';
@Module({
imports: [
ConfigModule,
ThrottlerModule,
TranslationModule,
GraphqlModule,
],
})
export class AppModule {}
`
---
Folder Structure Example
`
src/
├── bases/
│ ├── BaseEntity.ts
│ └── BaseResponse.ts
├── interceptors/
│ ├── generalResponse.interceptor.ts
│ ├── sqlInjection.interceptor.ts
│ └── main.interceptor.ts
├── dtos/
│ ├── currentUser.dto.ts
│ └── pagination.ts
├── config/
│ └── config.module.ts
├── throttler/
│ └── throttling.module.ts
├── translation/
│ ├── locales/
│ └── translation.module.ts
├── graphql/
│ ├── graphql.module.ts
│ └── errorHandling.filter.ts
└── production/
└── displayConsoles.ts
``