RPC Config
Utilitário para configuração de rotas e cache em microserviços. Utilido especificamente para projetos nclabs
Instalação
npm i @nclabs/rpc-config
Uso
Configuração
...
SERVICE_NAME=<service_name>
REDIS_HOST=<host>
REDIS_PORT=<port>
REDIS_PASSWORD=<password>
REDIS_DB=0
REDIS_CACHE_DB=0
...
Modulo
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { RpcConfigModule } from '@nclabs/rpc-config';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { CacherInterceptor } from '@nclabs/rpc-config';
@Module({
imports: [
...
RpcConfigModule.forRoot({
cache: {
prefix: process.env.CACHE_PREFIX,
ttl: parseInt(process.env.CACHE_TTL, 10),
logCacheHit: process.env.CACHE_HIT_LOG === 'true',
},
}),
...
],
controllers: [AppController],
providers: [
AppService,
{
provide: APP_INTERCEPTOR,
useClass: CacherInterceptor,
},
],
})
export class AppModule {}
Controller
import { Action, Event, Rest } from '@nclabs/rpc-config';
@Controller()
export class AppController {
...
@Rest({
rest: {
methods: ['GET', 'POST'],
path: '/user-credential/:id',
},
cache: {
keys: ['#headers.authorization', "#params.username"],
ttl: 1200,
},
})
userCredential(@Payload() params: { username: string }): Observable<UserCredential> {
return this.appService.getUserCredential();
}
...
@Action({
name: 'get-user-credential',
cache: {
keys: ['#params.apiKey'],
},
})
getTokenFromKey(@Payload() params: { apiKey: string }): Obserable<UserCredential> {
console.log('params', params);
return this.appService.getUserCredential();
}
...
@Event({
name: 'user-changed',
})
eventTeste(@Payload() params: UserPayload ): void {
return;
}
...
}