-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1409 from jmcdo29/feat/conditional-module
feat: create a conditional module
- Loading branch information
Showing
4 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { DynamicModule, Logger, ModuleMetadata } from '@nestjs/common'; | ||
import { ConfigModule } from './config.module'; | ||
|
||
/** | ||
* @publicApi | ||
*/ | ||
export class ConditionalModule { | ||
/** | ||
* @publicApi | ||
*/ | ||
static async registerWhen( | ||
module: Required<ModuleMetadata>['imports'][number], | ||
condition: string | ((env: NodeJS.ProcessEnv) => boolean), | ||
options?: { timeout: number }, | ||
) { | ||
let configResolved = false; | ||
const { timeout = 5000 } = options ?? {}; | ||
setTimeout(() => { | ||
if (!configResolved) { | ||
throw new Error( | ||
`Nest was not able to resolve the config variables within ${timeout} milliseconds. Bause of this, the ConditionalModule was not able to determine if ${module.toString()} should be registered or not`, | ||
); | ||
} | ||
}, timeout); | ||
const returnModule: Required< | ||
Pick<DynamicModule, 'module' | 'imports' | 'exports'> | ||
> = { module: ConditionalModule, imports: [], exports: [] }; | ||
if (typeof condition === 'string') { | ||
const key = condition; | ||
condition = env => { | ||
return env[key]?.toLowerCase() !== 'false'; | ||
}; | ||
} | ||
await ConfigModule.envVariablesLoaded; | ||
configResolved = true; | ||
const evaluation = condition(process.env); | ||
if (evaluation) { | ||
returnModule.imports.push(module); | ||
returnModule.exports.push(module); | ||
} else { | ||
Logger.debug( | ||
`${condition.toString()} evaluated to false. Skipping the registration of ${module.toString()}`, | ||
ConditionalModule.name, | ||
); | ||
} | ||
return returnModule; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
FOO="use it" | ||
FOO_FALSE="false" | ||
FOO_DYNAMIC="yes" | ||
FOO_CUSTOM="yeah!" | ||
BAR="yay" | ||
FOOBAR="do it" | ||
QUU="nested!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { Injectable, Module } from '@nestjs/common'; | ||
import { Test } from '@nestjs/testing'; | ||
import { ConfigModule, ConditionalModule } from '../../lib'; | ||
import { join } from 'path'; | ||
|
||
@Injectable() | ||
class FooService {} | ||
|
||
@Injectable() | ||
class FooDynamicService {} | ||
|
||
@Module({ | ||
providers: [FooService], | ||
exports: [FooService], | ||
}) | ||
class FooModule { | ||
static forRoot() { | ||
return { | ||
module: FooModule, | ||
providers: [FooDynamicService], | ||
exports: [FooDynamicService], | ||
}; | ||
} | ||
} | ||
|
||
@Injectable() | ||
class BarService {} | ||
|
||
@Module({ | ||
providers: [BarService], | ||
exports: [BarService], | ||
}) | ||
class BarModule {} | ||
|
||
@Module({ | ||
providers: [ | ||
{ | ||
provide: 'quu', | ||
useValue: 42, | ||
}, | ||
], | ||
exports: ['quu'], | ||
}) | ||
class QuuModule {} | ||
|
||
@Module({ | ||
imports: [ConditionalModule.registerWhen(QuuModule, 'QUU')], | ||
exports: [ConditionalModule], | ||
}) | ||
class FooBarModule {} | ||
|
||
describe('ConditionalModule', () => { | ||
it('it should work for a regular module', async () => { | ||
const modRef = await Test.createTestingModule({ | ||
imports: [ | ||
ConfigModule.forRoot({ | ||
envFilePath: join(process.cwd(), 'tests', 'e2e', '.env.conditional'), | ||
}), | ||
ConditionalModule.registerWhen(FooModule, 'FOO'), | ||
], | ||
}).compile(); | ||
expect(modRef.get(FooService, { strict: false })).toBeDefined(); | ||
await modRef.close(); | ||
}); | ||
it('should work for a dynamic module', async () => { | ||
const modRef = await Test.createTestingModule({ | ||
imports: [ | ||
ConfigModule.forRoot({ | ||
envFilePath: join(process.cwd(), 'tests', 'e2e', '.env.conditional'), | ||
}), | ||
ConditionalModule.registerWhen(FooModule.forRoot(), 'FOO_DYNAMIC'), | ||
], | ||
}).compile(); | ||
expect(modRef.get(FooDynamicService, { strict: false })).toBeDefined(); | ||
await modRef.close(); | ||
}); | ||
it('should not register when the value is false', async () => { | ||
const modRef = await Test.createTestingModule({ | ||
imports: [ | ||
ConfigModule.forRoot({ | ||
envFilePath: join(process.cwd(), 'tests', 'e2e', '.env.conditional'), | ||
}), | ||
ConditionalModule.registerWhen(FooModule, 'FOO_FALSE'), | ||
], | ||
}).compile(); | ||
expect(() => modRef.get(FooService, { strict: false })).toThrow(); | ||
await modRef.close(); | ||
}); | ||
it('should work for a custom condition', async () => { | ||
const modRef = await Test.createTestingModule({ | ||
imports: [ | ||
ConfigModule.forRoot({ | ||
envFilePath: join(process.cwd(), 'tests', 'e2e', '.env.conditional'), | ||
}), | ||
ConditionalModule.registerWhen(FooModule, env => { | ||
return env.FOO_CUSTOM === 'yeah!'; | ||
}), | ||
], | ||
}).compile(); | ||
expect(modRef.get(FooService, { strict: false })).toBeDefined(); | ||
await modRef.close(); | ||
}); | ||
it('should handle two conditional modules', async () => { | ||
const modRef = await Test.createTestingModule({ | ||
imports: [ | ||
ConfigModule.forRoot({ | ||
envFilePath: join(process.cwd(), 'tests', 'e2e', '.env.conditional'), | ||
}), | ||
ConditionalModule.registerWhen(FooModule, 'FOO'), | ||
ConditionalModule.registerWhen(BarModule, 'BAR'), | ||
], | ||
}).compile(); | ||
expect(modRef.get(FooService, { strict: false })).toBeDefined(); | ||
expect(modRef.get(BarService, { strict: false })).toBeDefined(); | ||
await modRef.close(); | ||
}); | ||
it('should handle nested conditional module', async () => { | ||
const modRef = await Test.createTestingModule({ | ||
imports: [ | ||
ConfigModule.forRoot({ | ||
envFilePath: join(process.cwd(), 'tests', 'e2e', '.env.conditional'), | ||
}), | ||
ConditionalModule.registerWhen(FooBarModule, 'FOOBAR'), | ||
], | ||
}).compile(); | ||
expect(modRef.get('quu', { strict: false })).toBeDefined(); | ||
}); | ||
}); |