#How to decide whether a specific feature needs to be created as a module or just being as a service
45 messages · Page 1 of 1 (latest)
This might help you: https://dev.to/smolinari/nestjs-and-project-structure-what-to-do-1223
Note the kind of decision making that is done when creating modules (or not).
Thanks. This article was grt. But can you let me know about this difference betw using auth module and guard? Wat were the functionalities to be used by either?? Why do we need 2 stuff that almost sounds like same?
@fringe nest - You should consider a Module ( in this case for Authentication) a container for the things needed for the authentication process/ feature like one or more services, guards, if you are using Passport, then strategies, the controllers for login, logout and registration, etc. etc. A guard is nothing like a module, so I'm not certain how you got the impression.
I see some example like :
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
import { Observable } from 'rxjs';
@Injectable()
export class AuthGuard implements CanActivate {
canActivate(
context: ExecutionContext,
): boolean | Promise<boolean> | Observable<boolean> {
const request = context.switchToHttp().getRequest();
return true;
}
}
here this single thing itself is implemented like a guard. Its not being implemented like a module...
can you pls elaborate on the approach used here and if it deviates from your explanation of guards/modules being the almost the same??
Where did I say guards and modules are almost the same? I even wrote above, a guard is nothing like a module. What you have above as code is a guard. A module looks completely different. Rtfm….
oh ok.. maybe I misread it. wat I meant to ask was if auth is implemented like a module, then wats the necessity for a auth guard?
these docs kindof mentions it in much complex terms. do you have any simpler explanation?
I did explain it. Reread [my reply](#1161286207624454314 message) above.
You just mentioned like :
" A guard is nothing like a module, so I'm not certain how you got the impression. "
I wrote:
You should consider a Module ( in this case for Authentication) a container for the things needed for the authentication process/ feature like one or more services, guards, if you are using Passport, then strategies, the controllers for login, logout and registration, etc. etc.
this does not explain wats that canactivate n if it implements canactivate does it mean its authorizing that something can be activated?
now suppose this guards return a observable. but observables can stream data as well
right?
what if it emits multiple data values at certain intervals?
or maybe if the type is bool, its forced to be single
Im kindof moderately new to ts as well
A guard always has to return a boolean, but either as boolean, a promise or an observable.
ya tats fine, but I meant as if observables can async emit stream data, I jus wasnt sure
observables are async. I'm not sure what your question is in the end though.
observables r async. but r kindof generator fns
so r capable to return multiple times
kindof streams data every now n then
Still, what does this have to do with the difference between a module and a guard?
tats wat I got slightly confused as Im quite new to this nest n ts.. I mean if auth has services to authenticate then maybe this guard'll utilize those same services?
It is possible to inject a service into a guard, if you need its logic to make the canActivate decision. Sure.
I arrived to almost the same conclusion about project structure when I was creating the developing guidelines form my team. It feels nice to get some validation from another fellow dev.
@weak fiber Also another thing, any ideas on wat this thing do??
<Abc>Module.forRootAsync({<options>})
as like mostly many modules like :
ThrottlerModule.forRootAsync({})
wats the purpose of such imports generically, as in wat happens when calling forrootasync?
Why would you need that typecast in the first place? What module is doing that?
@velvet fog its not a typecast
jus a generic placeholder if its any <abc/xyz> module
throttler was jus an example mentioned here for tat forRootAsync usecase
as I see lot many readily available modules have this method. I jus want to understand in-depth as to wat does that specific call do internally
.forRootAsync and forRoot are a convention in Nest. Sometimes it's forRoot, sometimes it's register(). The idea being, you need to give the module some setup information for it to be able to do its job. For a database module it might be a connection object. For the throttler module it's config for the kind of throttling it should do.
I'm sort of getting tired of answering these very basic questions that you can find answers to in the Nest docs. I'd very much like you to come back with questions that show you've at least attempted to learn and understand something... anything. It's not our job to teach you anything, as that would take forever, as this thread is ending up. Please do some learning on your own. Thanks.
I already got that. fine. but, jus wanted to knw if they did anything much more or wat .. no probs..
I've never seen a module have a generic in front. That's usually a typecast. The ClientsModule from @nestjs/microservices has a generic register to modify the options, but that's the form of ClientsModule.register<RmqOptions>()
@velvet fog He was just using the arrows <> to signify a placeholder. Had nothing to do with TypeScript. 🙂
Ahhh, I see. I got confused as the <whatever> is a valid typecast syntax, though generally no longer preferred as the as whatever is the better approach
Hi guys. Any ideas on what does a reflector do?
import { Reflector } from "@nestjs/core";
For further questions, that aren't related to the initial question of the post, please make a new post. It helps other members see conversations happening and increasing the chance of finding related posts
As for Reflector, it's a small convenience wrapper around the Reflect API to help us get and set metadata. In a recent version, we also added a way for the Reflector class to create decorators to help with type safe retrieval of the metadata from that decorator