-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
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 #9904 from tugascript/fix-cache-module-async-register
fix(common): Fix CacheModule registerAsync
- Loading branch information
Showing
13 changed files
with
235 additions
and
1 deletion.
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,39 @@ | ||
import { CACHE_MANAGER, INestApplication } from '@nestjs/common'; | ||
import { Test } from '@nestjs/testing'; | ||
import * as request from 'supertest'; | ||
import { AsyncRegisterExtraModule } from '../src/async-register-extra-providers/async-register-extra.module'; | ||
import { Cache } from 'cache-manager'; | ||
import { assert } from 'chai'; | ||
|
||
describe('Async Register Extra Providers', () => { | ||
let server; | ||
let app: INestApplication; | ||
let cacheManager: Cache; | ||
|
||
before(async () => { | ||
const module = await Test.createTestingModule({ | ||
imports: [AsyncRegisterExtraModule], | ||
}).compile(); | ||
|
||
cacheManager = module.get<Cache>(CACHE_MANAGER); | ||
app = module.createNestApplication(); | ||
server = app.getHttpServer(); | ||
await app.init(); | ||
}); | ||
|
||
it('should be defined', async () => { | ||
assert.isDefined(cacheManager); | ||
}); | ||
|
||
it(`should return empty`, async () => { | ||
return request(server).get('/').expect(200, 'Not found'); | ||
}); | ||
|
||
it(`should return data`, async () => { | ||
return request(server).get('/').expect(200, 'value'); | ||
}); | ||
|
||
after(async () => { | ||
await app.close(); | ||
}); | ||
}); |
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,39 @@ | ||
import { CACHE_MANAGER, INestApplication } from '@nestjs/common'; | ||
import { Test } from '@nestjs/testing'; | ||
import * as request from 'supertest'; | ||
import { AsyncRegisterModule } from '../src/async-register/async-register.module'; | ||
import { Cache } from 'cache-manager'; | ||
import { assert } from 'chai'; | ||
|
||
describe('Async Register', () => { | ||
let server; | ||
let app: INestApplication; | ||
let cacheManager: Cache; | ||
|
||
before(async () => { | ||
const module = await Test.createTestingModule({ | ||
imports: [AsyncRegisterModule], | ||
}).compile(); | ||
|
||
cacheManager = module.get<Cache>(CACHE_MANAGER); | ||
app = module.createNestApplication(); | ||
server = app.getHttpServer(); | ||
await app.init(); | ||
}); | ||
|
||
it('should be defined', async () => { | ||
assert.isDefined(cacheManager); | ||
}); | ||
|
||
it(`should return empty`, async () => { | ||
return request(server).get('/').expect(200, 'Not found'); | ||
}); | ||
|
||
it(`should return data`, async () => { | ||
return request(server).get('/').expect(200, 'value'); | ||
}); | ||
|
||
after(async () => { | ||
await app.close(); | ||
}); | ||
}); |
16 changes: 16 additions & 0 deletions
16
integration/cache/src/async-register-extra-providers/async-register-extra.controller.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,16 @@ | ||
import { Controller, Get, Inject, CACHE_MANAGER } from '@nestjs/common'; | ||
import { Cache } from 'cache-manager'; | ||
|
||
@Controller() | ||
export class AsyncRegisterExtraController { | ||
constructor(@Inject(CACHE_MANAGER) private cacheManager: Cache) {} | ||
|
||
@Get() | ||
async getFromStore(): Promise<string> { | ||
const value: string | undefined = await this.cacheManager.get('key'); | ||
if (!value) { | ||
await this.cacheManager.set('key', 'value'); | ||
} | ||
return value ?? 'Not found'; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
integration/cache/src/async-register-extra-providers/async-register-extra.module.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,16 @@ | ||
import { CacheModule, Module } from '@nestjs/common'; | ||
import { AsyncRegisterExtraController } from './async-register-extra.controller'; | ||
import { CacheConfig } from './config/cache.config'; | ||
import { ConfigModule } from './config/config.module'; | ||
|
||
@Module({ | ||
imports: [ | ||
CacheModule.registerAsync({ | ||
extraProviders: [ConfigModule], | ||
isGlobal: true, | ||
useClass: CacheConfig, | ||
}), | ||
], | ||
controllers: [AsyncRegisterExtraController], | ||
}) | ||
export class AsyncRegisterExtraModule {} |
17 changes: 17 additions & 0 deletions
17
integration/cache/src/async-register-extra-providers/config/cache.config.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 { | ||
CacheModuleOptions, | ||
CacheOptionsFactory, | ||
Injectable, | ||
} from '@nestjs/common'; | ||
import { ConfigService } from './config.service'; | ||
|
||
@Injectable() | ||
export class CacheConfig implements CacheOptionsFactory { | ||
constructor(private readonly configService: ConfigService) {} | ||
|
||
createCacheOptions(): CacheModuleOptions { | ||
const ttl = this.configService.getTtl(); | ||
|
||
return { ttl }; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
integration/cache/src/async-register-extra-providers/config/config.module.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,8 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ConfigService } from './config.service'; | ||
|
||
@Module({ | ||
providers: [ConfigService], | ||
exports: [ConfigService], | ||
}) | ||
export class ConfigModule {} |
8 changes: 8 additions & 0 deletions
8
integration/cache/src/async-register-extra-providers/config/config.service.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,8 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class ConfigService { | ||
public getTtl(): number { | ||
return 10; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
integration/cache/src/async-register-extra-providers/tsconfig.json
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", | ||
] | ||
} |
16 changes: 16 additions & 0 deletions
16
integration/cache/src/async-register/async-register.controller.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,16 @@ | ||
import { Controller, Get, Inject, CACHE_MANAGER } from '@nestjs/common'; | ||
import { Cache } from 'cache-manager'; | ||
|
||
@Controller() | ||
export class AsyncRegisterController { | ||
constructor(@Inject(CACHE_MANAGER) private cacheManager: Cache) {} | ||
|
||
@Get() | ||
async getFromStore(): Promise<string> { | ||
const value: string | undefined = await this.cacheManager.get('key'); | ||
if (!value) { | ||
await this.cacheManager.set('key', 'value'); | ||
} | ||
return value ?? 'Not found'; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
integration/cache/src/async-register/async-register.module.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,14 @@ | ||
import { CacheModule, Module } from '@nestjs/common'; | ||
import { AsyncRegisterController } from './async-register.controller'; | ||
import { CacheConfig } from './config/cache.config'; | ||
|
||
@Module({ | ||
imports: [ | ||
CacheModule.registerAsync({ | ||
isGlobal: true, | ||
useClass: CacheConfig, | ||
}), | ||
], | ||
controllers: [AsyncRegisterController], | ||
}) | ||
export class AsyncRegisterModule {} |
14 changes: 14 additions & 0 deletions
14
integration/cache/src/async-register/config/cache.config.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,14 @@ | ||
import { | ||
CacheModuleOptions, | ||
CacheOptionsFactory, | ||
Injectable, | ||
} from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class CacheConfig implements CacheOptionsFactory { | ||
createCacheOptions(): CacheModuleOptions { | ||
const ttl = 10; | ||
|
||
return { 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,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