I am looking at how I can import modules in NestJs runtime based on some logic in the service.
What I am trying to achieve is to build a "plugin" system which uses whichever modules are installed to extend a base service for example, if I have an FxModule which job is to return exchange rates
I might like to "extend" this module with different providers, e.g. XE or Oanda for different client applications based on which one is installed. So something like:
FxService
-> XE (if module installed)
-> Oanda (if module installed)
I have the function which checks if the modules are installed but I am struggling to get my FxService to import and use the module in runtime.
I am looking to do something like:
let fxModule: any
if (utils.Modules.isInstalled('@something/xe')) {
fxModule = require('@something/xe')
fxModule.register()
const result = await fxModule.someService.someFunctionEgGetRate()
}
This module will be pulled in via NPM, so it's just how I can do this "in the NestJS way" otherwise, I guess I move to Vanilla Typescript and drop the NestJs dependancy.