Skip to content

Latest commit

 

History

History
208 lines (120 loc) · 8.76 KB

learn-nestjs.md

File metadata and controls

208 lines (120 loc) · 8.76 KB

learn-nestjs

REPOSITORY - https://github.com/sahilrajput03/learn-nestjs/


  • HTTP module in NestJS is nothing but axios only. Browse docs here - Click here

Source of below image - Click here

image

  • Using official nodejs libraries in nestjs:
import * as path from 'node:path';
import * as url from 'node:url';
import * as fs from 'fs';
import * as os from 'os';
  • Writing tests with nestjs is awesome: image

Scripts guide from official nestjs docs

Docs: Click here

image


How do we perform data-type conversions in params and query dto's?

We can use Type decorator i.e, @Type from class-transformer.

From official docs:

image

image

Fixing circular-dependeny for nestjs - Awesome Article

NOTE: My current slasher backend project has a good docs guide in the project readme as well.

Official Guide from NestJs: Click here

Click here

Also, in slasher we can directly make user of models inside the controller's instead of using an serviceMethod of a different service. But this is only feasible if the logic we already have been using a service method for has very simple logic which can be put in controller directly without bringing a lot of complexities to the controller code.

Versioning

OFFICIAL DOCS: Click here

RouterModule for adding a prefix to all paths of Controllers of a Module

OFFICIAL DOCS: Router module: Click here

Note: In below way the module's controllers are accessible at the prefixed path only (no longer accessible without the prefixed path).

image

Above image from source, we need to learn that adding DashboardModule to the list of imports is necessary and also adding DashboardModule to the module property inside the RouterModule.register's array.

global interceptors in nestjs:

Interceptors | NestJs: Click here

Source: Click here

In file app.module.ts, you can add a suitable provider like that:

image

USAGE:

image

Interceptors can be nested in this way I guess:

image

interceptors in nestjs:

image

http requests, and jest mocking:

    it.only('cool testing', async () => {
      jest.spyOn(httpService, 'get')
      .mockImplementation(() => of({ data: 100, status: 202, statusText: '', headers: {}, config: {} }))
      .mockImplementationOnce(() => of({
        data: 101,
        status: 202,
        statusText: '',
        headers: {},
        config: {},
      }))
      .mockImplementationOnce(() => of({
        data: 102,
        status: 202,
        statusText: '',
        headers: {},
        config: {},
      }));

      const { data: data1 } = await lastValueFrom(httpService.get('/'));
      console.log({ data1 }); // 101

      const { data: data2 } = await lastValueFrom(httpService.get('/'));
      console.log({ data2 }); // 102

      const { data: data3 } = await lastValueFrom(httpService.get('/'));
      console.log({ data3 }); // 100

      const { data: data4 } = await lastValueFrom(httpService.get('/'));
      console.log({ data4 }); // 100
      expect(1).toBe(1);
    });

Other Cool things:

  • Thats how you can use a model in specification file (test file):

image

  • Using multiple gateways create different socket servers?

    tldr; NO. They use same server instance if they have same ports. Source: Click here

    image

  • Adding service as dependency injection to the controller. So this simply means that nestjs will automatically instantiate the service class for us.

image

  • Using params:

    image

  • Passing route path (as string) to controller decorator.

    image

  • nest generate service users:

    image

  • nest generate controller users:

    image

  • nest generate module users:

    image

  • app.module.ts:

    image

  • These decorators optionally can have string as first argument to define the path of the api handling of each of them.

    image

    image

  • Thats where the nestjs app starts:

    image

  • How three layer architecture works for backends? Source (quite extensive article {not at all for begineers}).

    image

Swagger Usage

  • We can create new sections in the swagger docs by assigning tags to a controller like that:

    image

    and it'll reflect like:

    image