Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(common): Fix CacheModule registerAsync #9904

Merged
merged 2 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 39 additions & 0 deletions integration/cache/e2e/async-register-extra-providers.ts
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();
});
});
39 changes: 39 additions & 0 deletions integration/cache/e2e/async-register.spec.ts
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();
});
});
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';
}
}
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 {}
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 };
}
}
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 {}
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 integration/cache/src/async-register-extra-providers/tsconfig.json
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 integration/cache/src/async-register/async-register.controller.ts
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 integration/cache/src/async-register/async-register.module.ts
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 integration/cache/src/async-register/config/cache.config.ts
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 };
}
}
22 changes: 22 additions & 0 deletions integration/cache/src/async-register/tsconfig.json
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",
]
}
5 changes: 4 additions & 1 deletion packages/common/cache/cache.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,13 @@ export class CacheModule extends ConfigurableModuleClass {
StoreConfig extends Record<any, any> = Record<string, any>,
>(options: CacheModuleAsyncOptions<StoreConfig>): DynamicModule {
const moduleDefinition = super.registerAsync(options);

return {
global: options.isGlobal,
...moduleDefinition,
providers: moduleDefinition.providers.concat(options.extraProviders),
providers: options.extraProviders
? moduleDefinition.providers.concat(options.extraProviders)
: moduleDefinition.providers,
};
}
}