nestjs

Run program on init

不想你离开。 提交于 2019-12-02 09:49:55
问题 I would create a program (script) that launches actions when it's get run, so I'm not using routes in this program I'm using NestJS framework (requirement). Actually I'm trying to write my code in main.ts file and importing a service with my methods . import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import {AppService} from './app.service' import { TreeChildren } from 'typeorm'; async function bootstrap() { const app = await NestFactory.create(AppModule);

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

牧云@^-^@ 提交于 2019-12-02 07:26:41
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. Something like below { provide: 'WebhookService', useFactory: async () => new dialogflow.WebhookClient({??,??}

Run program on init

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 05:19:12
I would create a program (script) that launches actions when it's get run, so I'm not using routes in this program I'm using NestJS framework (requirement). Actually I'm trying to write my code in main.ts file and importing a service with my methods . import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import {AppService} from './app.service' import { TreeChildren } from 'typeorm'; async function bootstrap() { const app = await NestFactory.create(AppModule); await app.listen(3000); } let appService: AppService; <- can't use appService methods this.appService.

Problem with multiple graphql resolver implementations in nestjs

时光怂恿深爱的人放手 提交于 2019-12-02 05:13:35
I just started learning NestJS and GraphQL. I started with a single resolver class UserResolver defined in UserModule . This class provides methods to read a list of users or a specific user. Methods are decorated with @Query() , a user.graphql file is provided, GraphQL is initialized in AppModule , all as described in the docs. All works well, I can get a list of users or specific user through Insomnia Tool or through Playground. I am happy! Now I have created a second module, RoleModule . I created a role.graphql file and a RoleResolver class, I basically replicated all the work done for

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

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 05:11:10
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({ imports: [ PassportModule.register({ defaultStrategy: 'jwt' }), JwtModule.registerAsync({ useFactory

How to redirect all routes to index.html (Angular) in nest.js?

和自甴很熟 提交于 2019-12-02 04:53:10
问题 I am making Angular + NestJS app, and I want to send index.html file for all routes. main.ts async function bootstrap() { const app = await NestFactory.create(AppModule); app.useStaticAssets(join(__dirname, '..', 'frontend', 'dist', 'my-app')); app.setBaseViewsDir(join(__dirname, '..', 'frontend', 'dist', 'my-app')); await app.listen(port); } app.controller.ts @Controller('*') export class AppController { @Get() @Render('index.html') root() { return {}; } } It works fine while I open

validate nested objects using class-validator in nest.js controller

喜欢而已 提交于 2019-12-02 00:39:40
I want to validate body payload using class-validator in a nest.js controller. My currency.dto.ts file is like this: import { IsNotEmpty, IsString, ValidateNested, IsNumber, IsDefined, } from 'class-validator'; class Data { @IsNotEmpty() @IsString() type: string; @IsNotEmpty() @IsNumber() id: number; } export class CurrencyDTO { @ValidateNested({ each: true }) @IsDefined() data: Data[]; } and in my nest.js controller, I use it like this. @Post() @UseGuards(new AuthTokenGuard()) @UsePipes(new ValidationPipe()) addNewCurrency(@Req() req, @Body() data: CurrencyDTO) { console.log('data', data); }

validate nested objects using class-validator in nest.js controller

ぐ巨炮叔叔 提交于 2019-12-02 00:37:36
问题 I want to validate body payload using class-validator in a nest.js controller. My currency.dto.ts file is like this: import { IsNotEmpty, IsString, ValidateNested, IsNumber, IsDefined, } from 'class-validator'; class Data { @IsNotEmpty() @IsString() type: string; @IsNotEmpty() @IsNumber() id: number; } export class CurrencyDTO { @ValidateNested({ each: true }) @IsDefined() data: Data[]; } and in my nest.js controller, I use it like this. @Post() @UseGuards(new AuthTokenGuard()) @UsePipes(new

How to redirect all routes to index.html (Angular) in nest.js?

三世轮回 提交于 2019-12-02 00:28:47
I am making Angular + NestJS app, and I want to send index.html file for all routes. main.ts async function bootstrap() { const app = await NestFactory.create(AppModule); app.useStaticAssets(join(__dirname, '..', 'frontend', 'dist', 'my-app')); app.setBaseViewsDir(join(__dirname, '..', 'frontend', 'dist', 'my-app')); await app.listen(port); } app.controller.ts @Controller('*') export class AppController { @Get() @Render('index.html') root() { return {}; } } It works fine while I open localhost:3000/ , but if I open localhost:3000/some_route the server falls with 500 internal error and says Can

nestjs / TypeOrm database transaction

扶醉桌前 提交于 2019-12-01 22:59:15
问题 Assuming we have 2 services, A and B. Service A has a function doing the following: Validate the data Call a service B function, that makes changes to the database Do some more stuff Do changes to the database Now, let's assume that one of the following, steps 3 or 4 failed. Since service B made changes in the database, those changes are still there. Is there any way of rolling the database back in this case? I though about database transactions, but I couldn't find any way to do that in nest