import { Module } from '@nestjs/common' ; import { ConfigType } from '@nestjs/config' ; import { ModuleRef } from '@nestjs/core' ; import ocrConfig from 'src/config/ocr.config' ; import { GoogleOCRService } from './google-ocr.service' ; import { OCRService } from './ocr.service' ; @ Module ( { providers : [ { provide : OCRService , useFactory : ( moduleRef , ocrCfg ) => { if ( ! ocrCfg . enable ) { return null ; } if ( ocrCfg . driver === 'google' ) { return moduleRef . create ( GoogleOCRService ) ; } try { const module = require ( `tas-ocr-driver-${ ocrCfg . driver }` ) ; return moduleRef . create ( module . OCRService ) ; } catch ( e ) { console . error ( '====>> Failed to get OCR driver: ' + ocrCfg . driver ) ; throw e ; } } , inject : [ ModuleRef , ocrConfig . KEY ] , } , ] , exports : [ OCRService ] , } ) export class OCRModule { }