Providers
Providers in Nest are used to create services, factories, helpers, and more that can be injected into controllers and other providers using Nest’s built-in dependency injection. The @Injectable()
decorator is used to create a provider class.
The AuthenticationService
in our blog application, for example, is a provider that injects and uses the UsersService
component.
@
Injectable
()
export
class
AuthenticationService
{
constructor
(
private
readonly
userService
:UserService
)
{}
async
validateUser
(
payload
:
{
string
;
password
:string
;
})
:
Promise
<
boolean
>
{
const
user
=
await
this
.
userService
.
findOne
({
where
:
{
payload.email
}
});
return
!!
user
;
}
}
We’ll talk more about dependency injection in the the Dependency Injection chapter.