Howto get req.user in services in Nest JS

Update March 2019 Since version v6, you can now inject the request object into a request-scoped provider: import { REQUEST } from ‘@nestjs/core’; import { Request } from ‘express’; @Injectable({ scope: Scope.REQUEST }) export class UsersService { constructor(@Inject(REQUEST) private readonly request: Request) {} } Outdated answer It’s not possible to inject the user (or request) … Read more

Access raw body of Stripe webhook in Nest.js

For anyone looking for a more elegant solution, turn off the bodyParser in main.ts. Create two middleware functions, one for rawbody and the other for json-parsed-body. json-body.middleware.ts import { Request, Response } from ‘express’; import * as bodyParser from ‘body-parser’; import { Injectable, NestMiddleware } from ‘@nestjs/common’; @Injectable() export class JsonBodyMiddleware implements NestMiddleware { use(req: … Read more

Nest can’t resolve dependencies of the ItemsService (?). Please make sure that the argument at index [0] is available in the AppModule context

In order to solve it you have to remove the ItemsController and ItemsService imports from the app.module.ts file. This was the solution because: You already import ItemsController and ItemsService in your items.module.ts file so it’s not necessary to import them again in the app.module.ts file. In your items.module.ts you have the next line: @Module({ imports: … Read more

How to exclude entity field from returned by controller JSON. NestJS + Typeorm

I’d suggest creating an interceptor that takes advantage of the class-transformer library: @Injectable() export class TransformInterceptor implements NestInterceptor { intercept( context: ExecutionContext, call$: Observable<any>, ): Observable<any> { return call$.pipe(map(data => classToPlain(data))); } } Then, simply exclude properties using @Exclude() decorator, for example: import { Exclude } from ‘class-transformer’; export class User { id: number; email: … Read more

Is it possible to add Authentication to access to NestJS’ Swagger Explorer

Securing access to your Swagger with HTTP Basic Auth using NestJS with Express First run npm i express-basic-auth then add the following to your main.{ts,js}: // add import import * as basicAuth from ‘express-basic-auth’; // … // Sometime after NestFactory add this to add HTTP Basic Auth app.use( [‘/docs’, ‘/docs-json’], basicAuth({ challenge: true, users: { … Read more

How to create nested routes with parameters using NestJS

i think you need this? import {Controller, Get, Param} from “@nestjs/common”; @Controller(‘accounts/:account’) export class TestController{ @Get(‘resource2/:someParam/whatever’) arsPW(@Param(‘account’) account, @Param(‘someParam’) someparam){ console.log(‘:account/resource2/:someParam/whatever’,account,someparam) return account+’_’+someparam+’___’; } @Get(‘resource1/:someparam’) aRSP(@Param(‘account’) account, @Param(‘someparam’) someparam){ console.log(‘:account/resource1/:someParam’,account,someparam) return account+’_’+someparam; } @Get() getget(){ console.log(‘get’); return ‘aaa’; } }

How to use Nest.js’s @Headers properly?

Headers will be sent in lower case so you need @Headers(‘my-id’) instead. Easy to debug by injecting the full headers object: import { Headers } from ‘@nestjs/common’; … @Put(“https://stackoverflow.com/”) public async put(@Headers() headers) { console.log(headers); } The headers variable will then refer to the req.headers.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)