REPOSITORY - https://github.com/sahilrajput03/learn-nestjs/
global interceptors in nestjs:
Interceptors | NestJs: Click here |
Source: Click here
In file app.module.ts
, you can add a suitable provider like that:
USAGE:
Interceptors can be nested in this way I guess:
interceptors in nestjs:
request
object: Click here-stackoverflowhttp 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:
Using multiple gateways create different socket servers?
tldr; NO. They use same server instance if they have same ports. Source: Click here
Adding service as dependency injection to the controller. So this simply means that nestjs will automatically instantiate the service class for us.
Using params:
Passing route path (as string) to controller decorator.
nest generate service users
:
nest generate controller users
:
nest generate module users
:
app.module.ts
:
These decorators optionally can have string as first argument to define the path of the api handling of each of them.
Thats where the nestjs app starts:
How three layer architecture works for backends? Source (quite extensive article {not at all for begineers}).
We can create new sections in the swagger docs by assigning tags to a controller like that:
and it’ll reflect like: