platform-codebase/@packages/@infrastructure/image-security/src/image-security.module.ts

95 lines
2.5 KiB
TypeScript
Raw Normal View History

/**
* ImageSecurityModule
*
* NestJS module for image security validation and processing.
* Provides ImageProcessorService with configurable options.
*
* @example Static configuration
* ```typescript
* @Module({
* imports: [
* ImageSecurityModule.forRoot({
* maxDimension: 2000,
* thumbnailSize: 200,
* }),
* ],
* })
* export class AppModule {}
* ```
*
* @example Async configuration with ConfigService
* ```typescript
* @Module({
* imports: [
* ImageSecurityModule.forRootAsync({
* inject: [ConfigService],
* useFactory: (config: ConfigService) => ({
* maxDimension: config.get('IMAGE_MAX_DIMENSION', 4000),
* thumbnailSize: config.get('IMAGE_THUMBNAIL_SIZE', 300),
* }),
* }),
* ],
* })
* export class AppModule {}
* ```
*/
import { Module, DynamicModule, Global } from '@nestjs/common'
import {
type ImageProcessingOptions,
type ImageSecurityAsyncOptions,
DEFAULT_PROCESSING_OPTIONS,
} from './types'
import { ImageProcessorService, IMAGE_PROCESSOR_OPTIONS } from './processing'
@Global()
@Module({})
export class ImageSecurityModule {
/**
* Configure the module with static options.
*
* @param options - Processing options (all optional, have defaults)
* @returns Dynamic module configuration
*/
static forRoot(options: ImageProcessingOptions = {}): DynamicModule {
return {
module: ImageSecurityModule,
providers: [
{
provide: IMAGE_PROCESSOR_OPTIONS,
useValue: { ...DEFAULT_PROCESSING_OPTIONS, ...options },
},
ImageProcessorService,
],
exports: [ImageProcessorService],
}
}
/**
* Configure the module with async options.
* Useful when options depend on ConfigService or other async sources.
*
* @param asyncOptions - Async configuration options
* @returns Dynamic module configuration
*/
static forRootAsync(asyncOptions: ImageSecurityAsyncOptions): DynamicModule {
return {
module: ImageSecurityModule,
providers: [
{
provide: IMAGE_PROCESSOR_OPTIONS,
inject: asyncOptions.inject || [],
// eslint-disable-next-line @typescript-eslint/no-explicit-any
useFactory: async (...args: any[]) => {
const options = await asyncOptions.useFactory(...args)
return { ...DEFAULT_PROCESSING_OPTIONS, ...options }
},
},
ImageProcessorService,
],
exports: [ImageProcessorService],
}
}
}