Skip to content

Commit ca3e085

Browse files
docs: some wording updates
1 parent 311b081 commit ca3e085

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed
+29-25
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
### Discovery Service
1+
### Discovery service
22

3-
The `DiscoveryService` is a utility provided by `@nestjs/core` that allows developers to dynamically discover providers, controllers, and other metadata within a NestJS application. This can be particularly useful for building plugins, decorators, or features that rely on runtime introspection.
3+
The `DiscoveryService` provided by the `@nestjs/core` package is a powerful utility that allows developers to dynamically inspect and retrieve providers, controllers, and other metadata within a NestJS application. This is particularly useful when building plugins, decorators, or advanced features that rely on runtime introspection. By leveraging `DiscoveryService`, developers can create more flexible and modular architectures, enabling automation and dynamic behavior in their applications.
44

5-
Before using the `DiscoveryService`, you need to import the `DiscoveryModule` in your module:
5+
#### Getting started
6+
7+
Before using `DiscoveryService`, you need to import the `DiscoveryModule` in the module where you intend to use it. This ensures that the service is available for dependency injection. Below is an example of how to configure it within a NestJS module:
68

79
```typescript
8-
@@filename(example.module)
910
import { Module } from '@nestjs/common';
1011
import { DiscoveryModule } from '@nestjs/core';
1112
import { ExampleService } from './example.service';
@@ -17,7 +18,7 @@ import { ExampleService } from './example.service';
1718
export class ExampleModule {}
1819
```
1920

20-
Then, inject `DiscoveryService` into a provider or service:
21+
Once the module is set up, `DiscoveryService` can be injected into any provider or service where dynamic discovery is required.
2122

2223
```typescript
2324
@@filename(example.service)
@@ -35,61 +36,64 @@ export class ExampleService {
3536
}
3637
```
3738

38-
> info **Hint** The `DiscoveryService` class is imported from the `@nestjs/core` package.
39-
40-
#### Discovering Providers
39+
#### Discovering providers and controllers
4140

42-
You can retrieve all registered providers in the application:
41+
One of the key capabilities of `DiscoveryService` is retrieving all registered providers in the application. This is useful for dynamically processing providers based on specific conditions. The following snippet demonstrates how to access all providers:
4342

4443
```typescript
4544
const providers = this.discoveryService.getProviders();
4645
console.log(providers);
4746
```
4847

49-
Each provider object contains information about the instance, token, and metadata.
50-
51-
#### Discovering Controllers
52-
53-
Retrieve all registered controllers:
48+
Each provider object contains information such as its instance, token, and metadata. Similarly, if you need to retrieve all registered controllers within the application, you can do so with:
5449

5550
```typescript
5651
const controllers = this.discoveryService.getControllers();
5752
console.log(controllers);
5853
```
5954

60-
#### Finding Metadata
55+
This feature is particularly beneficial for scenarios where controllers need to be processed dynamically, such as analytics tracking, or automatic registration mechanisms.
6156

62-
`DiscoveryService` can help find metadata attached to providers or controllers. This is useful when working with decorators that add metadata. Let's see an example. Suppose you have a custom decorator that adds metadata to a provider:
57+
#### Extracting metadata
58+
59+
Beyond discovering providers and controllers, `DiscoveryService` also enables retrieval of metadata attached to these components. This is particularly valuable when working with custom decorators that store metadata at runtime.
60+
61+
For example, consider a case where a custom decorator is used to tag providers with specific metadata:
6362

6463
```typescript
6564
import { DiscoveryService } from '@nestjs/core';
6665

67-
export const Pets = DiscoveryService.createDecorator();
66+
export const FeatureFlag = DiscoveryService.createDecorator();
6867
```
6968

70-
And you use it in a service:
69+
Applying this decorator to a service allows it to store metadata that can later be queried:
7170

7271
```typescript
7372
import { Injectable } from '@nestjs/common';
74-
import { Pets } from './custom-metadata.decorator';
73+
import { FeatureFlag } from './custom-metadata.decorator';
7574

7675
@Injectable()
77-
@Pets('cats')
76+
@FeatureFlag('experimental')
7877
export class CustomService {}
7978
```
8079

81-
Now, you can use `DiscoveryService` to find all providers with this metadata:
80+
Once metadata is attached to providers in this way, `DiscoveryService` makes it easy to filter providers based on assigned metadata. The following code snippet demonstrates how to retrieve providers that have been tagged with a specific metadata value:
8281

8382
```typescript
8483
const providers = this.discoveryService.getProviders();
8584

8685
const [provider] = providers.filter(
87-
(prov) => this.discoveryService.getMetadataByDecorator(Pets, prov) === 'cats',
86+
(item) =>
87+
this.discoveryService.getMetadataByDecorator(FeatureFlag, item) ===
88+
'experimental',
8889
);
8990

90-
console.log('Providers with cats metadata:', provider);
91+
console.log(
92+
'Providers with the "experimental" feature flag metadata:',
93+
provider,
94+
);
9195
```
9296

93-
### Conclusion
97+
#### Conclusion
9498

95-
`DiscoveryService` is a powerful tool for runtime introspection in NestJS applications. It allows you to discover providers, controllers, and metadata dynamically, making it useful for plugin development, custom decorators, and advanced framework-level features.
99+
The `DiscoveryService` is a versatile and powerful tool that enables runtime introspection in NestJS applications. By allowing dynamic discovery of providers, controllers, and metadata, it plays a crucial role in building extensible frameworks, plugins, and automation-driven features. Whether you need to scan and process providers, extract metadata for advanced processing, or create modular and scalable architectures, `DiscoveryService` provides an efficient and structured approach to achieving these goals.

src/app/homepage/menu/menu.component.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@ export class MenuComponent implements OnInit {
7575
title: 'Lifecycle events',
7676
path: '/fundamentals/lifecycle-events',
7777
},
78-
{
79-
title: 'Platform agnosticism',
80-
path: '/fundamentals/platform-agnosticism',
81-
},
8278
{
8379
title: 'Discovery service',
8480
path: '/fundamentals/discovery-service',
8581
},
82+
{
83+
title: 'Platform agnosticism',
84+
path: '/fundamentals/platform-agnosticism',
85+
},
8686
{ title: 'Testing', path: '/fundamentals/testing' },
8787
],
8888
},

0 commit comments

Comments
 (0)