-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
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 #9534 from nestjs/feat/module-utils
feat(core): add configurable module builder, module utils
- Loading branch information
Showing
24 changed files
with
794 additions
and
139 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
integration/module-utils/src/integration.module-definition.ts
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,17 @@ | ||
import { ConfigurableModuleBuilder } from '@nestjs/common'; | ||
import { IntegrationModuleOptions } from './interfaces/integration-module-options.interface'; | ||
|
||
export const { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN } = | ||
new ConfigurableModuleBuilder<IntegrationModuleOptions>() | ||
.setClassMethodName('forRoot') | ||
.setFactoryMethodName('construct') | ||
.setExtras( | ||
{ | ||
isGlobal: true, | ||
}, | ||
(definition, extras) => ({ | ||
...definition, | ||
global: extras.isGlobal, | ||
}), | ||
) | ||
.build(); |
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,16 @@ | ||
import { Inject, Module } from '@nestjs/common'; | ||
import { | ||
ConfigurableModuleClass, | ||
MODULE_OPTIONS_TOKEN, | ||
} from './integration.module-definition'; | ||
import { IntegrationModuleOptions } from './interfaces/integration-module-options.interface'; | ||
|
||
@Module({}) | ||
export class IntegrationModule extends ConfigurableModuleClass { | ||
constructor( | ||
@Inject(MODULE_OPTIONS_TOKEN) | ||
public readonly options: IntegrationModuleOptions, | ||
) { | ||
super(); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
integration/module-utils/src/interfaces/integration-module-options.interface.ts
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,4 @@ | ||
export interface IntegrationModuleOptions { | ||
url: string; | ||
secure?: boolean; | ||
} |
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,47 @@ | ||
import { Test } from '@nestjs/testing'; | ||
import { expect } from 'chai'; | ||
import { IntegrationModule } from '../src/integration.module'; | ||
|
||
describe('Module utils (ConfigurableModuleBuilder)', () => { | ||
it('should auto-generate "forRoot" method', async () => { | ||
const moduleRef = await Test.createTestingModule({ | ||
imports: [ | ||
IntegrationModule.forRoot({ | ||
isGlobal: true, | ||
url: 'test_url', | ||
secure: false, | ||
}), | ||
], | ||
}).compile(); | ||
|
||
const integrationModule = moduleRef.get(IntegrationModule); | ||
|
||
expect(integrationModule.options).to.deep.equal({ | ||
url: 'test_url', | ||
secure: false, | ||
}); | ||
}); | ||
|
||
it('should auto-generate "forRootAsync" method', async () => { | ||
const moduleRef = await Test.createTestingModule({ | ||
imports: [ | ||
IntegrationModule.forRootAsync({ | ||
isGlobal: true, | ||
useFactory: () => { | ||
return { | ||
url: 'test_url', | ||
secure: false, | ||
}; | ||
}, | ||
}), | ||
], | ||
}).compile(); | ||
|
||
const integrationModule = moduleRef.get(IntegrationModule); | ||
|
||
expect(integrationModule.options).to.deep.equal({ | ||
url: 'test_url', | ||
secure: false, | ||
}); | ||
}); | ||
}); |
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,22 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"declaration": false, | ||
"noImplicitAny": false, | ||
"removeComments": true, | ||
"noLib": false, | ||
"emitDecoratorMetadata": true, | ||
"experimentalDecorators": true, | ||
"target": "es6", | ||
"sourceMap": true, | ||
"allowJs": true, | ||
"outDir": "./dist" | ||
}, | ||
"include": [ | ||
"src/**/*", | ||
"e2e/**/*" | ||
], | ||
"exclude": [ | ||
"node_modules", | ||
] | ||
} |
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,4 +1,3 @@ | ||
export const CACHE_MANAGER = 'CACHE_MANAGER'; | ||
export const CACHE_MODULE_OPTIONS = 'CACHE_MODULE_OPTIONS'; | ||
export const CACHE_KEY_METADATA = 'cache_module:cache_key'; | ||
export const CACHE_TTL_METADATA = 'cache_module:cache_ttl'; |
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,12 @@ | ||
import { ConfigurableModuleBuilder } from '../module-utils'; | ||
import { | ||
CacheModuleOptions, | ||
CacheOptionsFactory, | ||
} from './interfaces/cache-module.interface'; | ||
|
||
export const { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN } = | ||
new ConfigurableModuleBuilder<CacheModuleOptions>({ | ||
moduleName: 'Cache', | ||
}) | ||
.setFactoryMethodName('createCacheOptions' as keyof CacheOptionsFactory) | ||
.build(); |
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
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,3 +1 @@ | ||
export const AXIOS_INSTANCE_TOKEN = 'AXIOS_INSTANCE_TOKEN'; | ||
export const HTTP_MODULE_ID = 'HTTP_MODULE_ID'; | ||
export const HTTP_MODULE_OPTIONS = 'HTTP_MODULE_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,23 @@ | ||
import { Provider } from '../interfaces'; | ||
import { ConfigurableModuleBuilder } from '../module-utils'; | ||
import { HttpModuleOptions } from './interfaces'; | ||
|
||
export const { | ||
ConfigurableModuleClass, | ||
MODULE_OPTIONS_TOKEN, | ||
ASYNC_OPTIONS_TYPE, | ||
} = new ConfigurableModuleBuilder<HttpModuleOptions>({ | ||
moduleName: 'Http', | ||
alwaysTransient: true, | ||
}) | ||
.setFactoryMethodName('createHttpOptions') | ||
.setExtras<{ extraProviders?: Provider[] }>( | ||
{ | ||
extraProviders: [], | ||
}, | ||
(definition, extras) => ({ | ||
...definition, | ||
providers: definition.providers.concat(extras?.extraProviders), | ||
}), | ||
) | ||
.build(); |
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
Oops, something went wrong.