nestjs

What's a valid @MessagePattern for NestJS MQTT microservice?

三世轮回 提交于 2019-12-04 13:24:13
I'm trying to setup a MQTT Microservice using NestJS according to the docs . I've started a working Mosquitto Broker using Docker and verified it's operability using various MQTT clients. Now, when I start the NestJS service it seems to be connecting correctly (mqqt.fx shows new client), yet I am unable to receive any messages in my controllers. This is my bootstrapping, just like in the docs: main.ts async function bootstrap() { const app = await NestFactory.createMicroservice(AppModule, { transport: Transport.MQTT, options: { host: 'localhost', port: 1883, protocol: 'tcp' } }); app.listen(()

Nest.js get injector instance

怎甘沉沦 提交于 2019-12-04 12:18:58
问题 I want to create an instance of a dynamically loaded class trough Nest.js dependency injection service. In Angular I would use Injector.create , what would be the equivalent in Nest.js ? 回答1: First of all you should get a ModuleRef which references current module, and then use its "get" method to get an instance. @Injectable() export class AppletService { files: FileService; constructor( private moduleRef: ModuleRef, ) { this.files = moduleRef.get(FileService); } } 来源: https://stackoverflow

NestJS: Adding verification options to AuthGuard with JWT

孤街醉人 提交于 2019-12-04 11:42:35
I am trying to make use of the AuthGuard decorator, and the passport JWT strategy, following the documentation . Everything in the documentation works great. But I now want to protect a route with a scope contained in the JWT. So here is a basic jwt payload generated by my application: { "user": { "id": "20189c4f-1183-4216-8b48-333ddb825de8", "username": "user.test@gmail.com" }, "scope": [ "manage_server" ], "iat": 1534766258, "exp": 1534771258, "iss": "15f2463d-8810-44f9-a908-801872ded159", "sub": "20189c4f-1183-4216-8b48-333ddb825de8", "jti": "078047bc-fc1f-4c35-8abe-72834f7bcc44" } Here is

Overriding providers in NestJS Jest tests

为君一笑 提交于 2019-12-04 10:55:30
I want to use an in-memory Mongo instance to mock data for testing purposes in my NestJS application. I have a database provider which connects to my production db using mongoose, which is part of my database module, which in turn gets imported into other modules. I am trying to override the database provider within my Jest tests so I can use the in-memory Mongo instance. This is the database module: import { Module } from '@nestjs/common'; import { databaseProviders } from './database.providers'; @Module({ providers: [...databaseProviders], exports: [...databaseProviders], }) export class

What is the nestjs error handling approach (business logic error vs. http error)?

北战南征 提交于 2019-12-04 07:51:11
While using NestJS to create API's I was wondering which is the best way to handle errors/exception. I have found two different approaches : Have individual services and validation pipes throw new Error() , have the controller catch them and than throw the appropriate kind of HttpException ( BadRequestException , ForbiddenException etc..) Have the controller simply call the service/validation pipe method responsible for handling that part of business logic, and throw the appropriate HttpException . There are pros and cons to both approeaches: This seems the right way, however, the service can

Nest can't resolve dependencies of the service which imports JwtService

流过昼夜 提交于 2019-12-04 05:06:55
问题 I am trying to use @nestjs/jwt. Particularly its registerAsync method (my config service loads the configuration asynchronously). I am registering JwtModule in the AuthModule , which then loads specific modules for each login/registration providers. Then I add JwtService to the providers of EmailService but it fails. The structure of the application is as follows (very schematic): app.module.ts @Module({ imports: [ AuthModule, ... ] }) export class ApplicationModule {} auth.module.ts @Module(

Error while running nestjs in production mode, cannot find module

筅森魡賤 提交于 2019-12-04 02:38:53
I have implemented a generic class as below which might be causing the problem, import { Logger } from '@nestjs/common'; import { PaginationOptionsInterface, Pagination } from './paginate'; import { Repository } from 'typeorm'; export class EntityService<T> { private repository: Repository<T>; constructor(repository) { this.repository = repository; } async getEntityWithPagination( options: PaginationOptionsInterface, ): Promise<Pagination<T>> { const [results, total] = await this.repository.findAndCount({ take: options.limit, skip: (options.page - 1) * options.limit, }); return new Pagination

Type of object received during file upload using @UploadFile

耗尽温柔 提交于 2019-12-02 17:54:25
问题 In the REST API below, what is the type of file object that is received. @Post('/:folderId/documents/:fileName') @UseInterceptors(FileInterceptor('file')) @ApiConsumes('multipart/form-data') @ApiImplicitParam({ name: 'folderId', description: ' Folder Id' }) @ApiImplicitParam({ name: 'fileName', description: ' File Name' }) @ApiImplicitFile({ name: 'file', required: true, description: 'PDF File' }) async uploadFile(@UploadedFile() file, @Param() folderId, @Param() fileName) { /** * I need to

How to create custom provider for third-party library in nestjs with access to request and response

纵饮孤独 提交于 2019-12-02 13:48:26
问题 I have a controller in nestjs as below import * as dialogflow from 'dialogflow-fulfillment'; import { Request, Response } from 'express'; @Controller('dialogflow-fulfillment') export class DialogflowFulfillmentController { @Post() async fulfill(@Req() request: Request, @Res() response: Response) { const agent = new dialogflow.WebhookClient({ request, response }); } } Now for being able to unit test this controller I want to use custom provider and provide an instance of WebhookClient.

Type of object received during file upload using @UploadFile

孤街醉人 提交于 2019-12-02 12:25:56
In the REST API below, what is the type of file object that is received. @Post('/:folderId/documents/:fileName') @UseInterceptors(FileInterceptor('file')) @ApiConsumes('multipart/form-data') @ApiImplicitParam({ name: 'folderId', description: ' Folder Id' }) @ApiImplicitParam({ name: 'fileName', description: ' File Name' }) @ApiImplicitFile({ name: 'file', required: true, description: 'PDF File' }) async uploadFile(@UploadedFile() file, @Param() folderId, @Param() fileName) { /** * I need to know the type of file object (first argument) of uploadFile */ this.folderService.uploadFile(file,