-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): allows for root module configuration
- Loading branch information
Showing
10 changed files
with
161 additions
and
32 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './ogma.module'; | ||
export * from './ogma.service'; | ||
export * from './interfaces/ogma-options.interface'; |
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 @@ | ||
export interface OgmaModuleOptions { | ||
logLevel?: string; | ||
color?: boolean; | ||
stream?: { | ||
write: (message: any) => void; | ||
}; | ||
} |
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,14 @@ | ||
import { createConfigurableDynamicRootModule } from '@golevelup/nestjs-modules'; | ||
import { Module } from '@nestjs/common'; | ||
import { OgmaModuleOptions } from './interfaces/ogma-options.interface'; | ||
import { OGMA_OPTIONS } from './ogma.constants'; | ||
|
||
@Module({ | ||
exports: [OGMA_OPTIONS], | ||
}) | ||
export class OgmaCoreModule extends createConfigurableDynamicRootModule< | ||
OgmaCoreModule, | ||
OgmaModuleOptions | ||
>(OGMA_OPTIONS) { | ||
static Deferred = OgmaCoreModule.externallyConfigured(OgmaCoreModule, 500); | ||
} |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export const OGMA_INSTANCE = Symbol('OGMA_INSTANCE'); | ||
export const OGMA_CONTEXT = Symbol('OGMA_CONTEXT'); | ||
export const OGMA_OPTIONS = 'OGMA_OPTIONS'; |
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,67 @@ | ||
import { Test } from '@nestjs/testing'; | ||
import { OgmaModule } from './ogma.module'; | ||
import { OgmaService } from './ogma.service'; | ||
|
||
describe('OgmaModule', () => { | ||
let service: OgmaService; | ||
|
||
describe.each(['TEST_CONTEXT', '', undefined])( | ||
'create context then use that context', | ||
(options: string | undefined) => { | ||
beforeEach(async () => { | ||
const module = await Test.createTestingModule({ | ||
imports: [ | ||
OgmaModule.forRoot({ color: false }), | ||
OgmaModule.forFeature(options), | ||
], | ||
}).compile(); | ||
service = await module.resolve(OgmaService); | ||
}); | ||
|
||
it( | ||
'should have OgmaService defined with options ' + | ||
JSON.stringify(options), | ||
() => { | ||
expect(service).toBeDefined(); | ||
expect(service).toHaveProperty('context'); | ||
expect((service as any).context).toBe(options ?? ''); | ||
}, | ||
); | ||
}, | ||
); | ||
describe('create context then use new context', () => { | ||
it('should create multiple ogma instances', async () => { | ||
const module = await Test.createTestingModule({ | ||
imports: [ | ||
OgmaModule.forRoot({ color: true, logLevel: 'ERROR' }), | ||
OgmaModule.forFeature('NEW CONTEXT', { | ||
color: false, | ||
logLevel: 'ALL', | ||
}), | ||
], | ||
}).compile(); | ||
service = await module.resolve(OgmaService); | ||
expect((service as any).ogma.options.logLevel).toBeDefined(); | ||
expect((service as any).ogma.options.color).toBeDefined(); | ||
}); | ||
}); | ||
describe('create context with async config', () => { | ||
it('should create the async configuration', async () => { | ||
const module = await Test.createTestingModule({ | ||
imports: [ | ||
OgmaModule.forRootAsync({ | ||
useFactory: () => { | ||
return { | ||
color: false, | ||
logLevel: 'DEBUG', | ||
}; | ||
}, | ||
}), | ||
OgmaModule.forFeature('TEST CONTEXT'), | ||
], | ||
}).compile(); | ||
service = await module.resolve(OgmaService); | ||
expect((service as any).ogma.options.logLevel).toBeDefined(); | ||
}); | ||
}); | ||
}); |
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
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