-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
With the new `ConditionalModule` devs can now register modules based on keys in the `process.env` or have them registered as blanks modules under the guise of a dyanmic module. This works for nested modules, multiple imports, and dynamic modules. Signed-off-by: Jay McDoniel <[email protected]>
- Loading branch information
Showing
4 changed files
with
160 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,24 @@ | ||
import { ModuleMetadata } from '@nestjs/common'; | ||
import { ConfigModule } from './config.module'; | ||
|
||
export class ConditionalModule { | ||
static async registerWhen( | ||
module: Required<ModuleMetadata>['imports'][number], | ||
condition: string | ((env: NodeJS.ProcessEnv) => boolean), | ||
) { | ||
if (typeof condition === 'string') { | ||
const key = condition; | ||
condition = env => { | ||
return env[key]?.toLowerCase() !== 'false'; | ||
}; | ||
} | ||
await ConfigModule.envVariablesLoaded; | ||
return condition(process.env) | ||
? { | ||
module: ConditionalModule, | ||
imports: [module], | ||
exports: [module], | ||
} | ||
: { module: ConditionalModule, imports: [] }; | ||
} | ||
} |
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(); | ||
}); | ||
}); |