#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)

fringe nest
#

Is it incase if it requires routes/controllers??

weak fiber
fringe nest
weak fiber
#

@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.

fringe nest
#

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??

weak fiber
#

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….

fringe nest
#

these docs kindof mentions it in much complex terms. do you have any simpler explanation?

weak fiber
fringe nest
weak fiber
#

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.

fringe nest
#

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

weak fiber
#

A guard always has to return a boolean, but either as boolean, a promise or an observable.

fringe nest
#

ya tats fine, but I meant as if observables can async emit stream data, I jus wasnt sure

weak fiber
#

observables are async. I'm not sure what your question is in the end though.

fringe nest
#

observables r async. but r kindof generator fns

#

so r capable to return multiple times

#

kindof streams data every now n then

weak fiber
#

Still, what does this have to do with the difference between a module and a guard?

fringe nest
#

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?

weak fiber
#

It is possible to inject a service into a guard, if you need its logic to make the canActivate decision. Sure.

haughty sigil
fringe nest
#

@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?

velvet fog
#

Why would you need that typecast in the first place? What module is doing that?

fringe nest
#

@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

weak fiber
#

.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.

fringe nest
velvet fog
#

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>()

weak fiber
#

@velvet fog He was just using the arrows <> to signify a placeholder. Had nothing to do with TypeScript. 🙂

velvet fog
#

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

fringe nest
#

Hi guys. Any ideas on what does a reflector do?

import { Reflector } from "@nestjs/core";
velvet fog
#

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

fringe nest
#

oh.. by now you must've guessed that I never knew about the reflect api until now that existed in js

#

luckily I just got to knw now