Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it(`NATS`, () => {
return request(server)
.post('/')
.send({
transport: Transport.NATS,
options: {
url: 'nats://localhost:4224',
},
})
.expect(408);
});
beforeEach(async () => {
const module = await Test.createTestingModule({
controllers: [NatsBroadcastController],
}).compile();
app = module.createNestApplication();
server = app.getHttpAdapter().getInstance();
app.connectMicroservice({
transport: Transport.NATS,
options: {
url: 'nats://0.0.0.0:4222',
},
});
app.connectMicroservice({
transport: Transport.NATS,
options: {
url: 'nats://0.0.0.0:4222',
},
});
await app.startAllMicroservicesAsync();
await app.init();
});
beforeEach(async () => {
const module = await Test.createTestingModule({
controllers: [NatsController],
}).compile();
app = module.createNestApplication();
server = app.getHttpAdapter().getInstance();
app.connectMicroservice({
transport: Transport.NATS,
options: {
url: 'nats://0.0.0.0:4222',
},
});
await app.startAllMicroservicesAsync();
await app.init();
});
beforeEach(async () => {
const module = await Test.createTestingModule({
controllers: [NatsBroadcastController],
}).compile();
app = module.createNestApplication();
server = app.getHttpAdapter().getInstance();
app.connectMicroservice({
transport: Transport.NATS,
options: {
url: 'nats://0.0.0.0:4222',
},
});
app.connectMicroservice({
transport: Transport.NATS,
options: {
url: 'nats://0.0.0.0:4222',
},
});
await app.startAllMicroservicesAsync();
await app.init();
});
EventPattern,
MessagePattern,
NatsContext,
Payload,
RpcException,
Transport,
} from '@nestjs/microservices';
import { from, Observable, of, throwError } from 'rxjs';
import { catchError, scan } from 'rxjs/operators';
@Controller()
export class NatsController {
static IS_NOTIFIED = false;
@Client({
transport: Transport.NATS,
options: {
url: 'nats://localhost:4222',
},
})
client: ClientProxy;
@Post()
@HttpCode(200)
async call(
@Query('command') cmd,
@Body() data: number[],
): Promise> {
await this.client.connect();
return this.client.send(cmd, data);
}
async function bootstrap(): Promise {
const app = await NestFactory.create(AppModule);
const config = app.get(ConfigService);
const messagingUrl = config.getMessagingConfig().messagingUrl;
app.connectMicroservice({
options: {
servers: typeof messagingUrl === "string" ? [messagingUrl] : messagingUrl,
},
transport: Transport.NATS,
});
await app.startAllMicroservicesAsync();
await app.listen(config.getPort());
}
bootstrap();
import { Controller, Get } from '@nestjs/common';
import {
Client,
ClientProxy,
MessagePattern,
Transport,
} from '@nestjs/microservices';
import { Observable } from 'rxjs';
import { scan, take } from 'rxjs/operators';
@Controller()
export class NatsBroadcastController {
@Client({ transport: Transport.NATS })
client: ClientProxy;
@Get('broadcast')
multicats() {
return this.client.send('broadcast.test', {}).pipe(
scan((a, b) => a + b),
take(2),
);
}
@MessagePattern('broadcast.*')
replyBroadcast(): Observable {
return new Observable(observer => observer.next(1));
}
}
useFactory: (config: ConfigService): ClientProxy => {
const messagingUrl = config.getMessagingConfig().messagingUrl;
return ClientProxyFactory.create({
options: {
servers: typeof messagingUrl === "string" ? [messagingUrl] : messagingUrl,
},
transport: Transport.NATS,
});
},
};