|
| 1 | +### Discovery Service |
| 2 | + |
| 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. |
| 4 | + |
| 5 | +Before using the `DiscoveryService`, you need to import the `DiscoveryModule` in your module: |
| 6 | + |
| 7 | +```typescript |
| 8 | +@@filename(example.module) |
| 9 | +import { Module } from '@nestjs/common'; |
| 10 | +import { DiscoveryModule } from '@nestjs/core'; |
| 11 | +import { ExampleService } from './example.service'; |
| 12 | + |
| 13 | +@Module({ |
| 14 | + imports: [DiscoveryModule], |
| 15 | + providers: [ExampleService], |
| 16 | +}) |
| 17 | +export class ExampleModule {} |
| 18 | +``` |
| 19 | + |
| 20 | +Then, inject `DiscoveryService` into a provider or service: |
| 21 | + |
| 22 | +```typescript |
| 23 | +@@filename(example.service) |
| 24 | +@Injectable() |
| 25 | +export class ExampleService { |
| 26 | + constructor(private readonly discoveryService: DiscoveryService) {} |
| 27 | +} |
| 28 | +@@switch |
| 29 | +@Injectable() |
| 30 | +@Dependencies(DiscoveryService) |
| 31 | +export class ExampleService { |
| 32 | + constructor(discoveryService) { |
| 33 | + this.discoveryService = discoveryService; |
| 34 | + } |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +> info **Hint** The `DiscoveryService` class is imported from the `@nestjs/core` package. |
| 39 | +
|
| 40 | +#### Discovering Providers |
| 41 | + |
| 42 | +You can retrieve all registered providers in the application: |
| 43 | + |
| 44 | +```typescript |
| 45 | +const providers = this.discoveryService.getProviders(); |
| 46 | +console.log(providers); |
| 47 | +``` |
| 48 | + |
| 49 | +Each provider object contains information about the instance, token, and metadata. |
| 50 | + |
| 51 | +#### Discovering Controllers |
| 52 | + |
| 53 | +Retrieve all registered controllers: |
| 54 | + |
| 55 | +```typescript |
| 56 | +const controllers = this.discoveryService.getControllers(); |
| 57 | +console.log(controllers); |
| 58 | +``` |
| 59 | + |
| 60 | +#### Finding Metadata |
| 61 | + |
| 62 | +`DiscoveryService` can help find metadata attached to providers or controllers. This is useful when working with decorators that add metadata. |
| 63 | + |
| 64 | +```typescript |
| 65 | +const providers = this.discoveryService.getProviders(); |
| 66 | + |
| 67 | +for (const provider of providers) { |
| 68 | + const metadata = this.reflector.get('custom:metadataKey', provider.instance.constructor); |
| 69 | + if (metadata) { |
| 70 | + console.log(`Metadata found:`, metadata); |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +##### Example: Custom Decorator with DiscoveryService |
| 76 | + |
| 77 | +Suppose you have a custom decorator that adds metadata to a provider: |
| 78 | + |
| 79 | +```typescript |
| 80 | +import { SetMetadata } from '@nestjs/common'; |
| 81 | + |
| 82 | +export const CustomMetadata = (value: string) => SetMetadata('custom:metadataKey', value); |
| 83 | +``` |
| 84 | + |
| 85 | +And you use it in a service: |
| 86 | + |
| 87 | +```typescript |
| 88 | +import { Injectable } from '@nestjs/common'; |
| 89 | +import { CustomMetadata } from './custom-metadata.decorator'; |
| 90 | + |
| 91 | +@Injectable() |
| 92 | +@CustomMetadata('example-value') |
| 93 | +export class CustomService {} |
| 94 | +``` |
| 95 | + |
| 96 | +Now, you can use `DiscoveryService` to find all providers with this metadata: |
| 97 | + |
| 98 | +```typescript |
| 99 | +const providers = this.discoveryService.getProviders(); |
| 100 | + |
| 101 | +const filteredProviders = providers.filter((provider) => { |
| 102 | + if (!provider.instance) return null; |
| 103 | + return !!this.reflector.get(metadataPathToken, provider.instance.constructor); |
| 104 | +}); |
| 105 | + |
| 106 | +console.log('Providers with custom metadata:', filteredProviders); |
| 107 | +``` |
| 108 | + |
| 109 | +### Conclusion |
| 110 | + |
| 111 | +`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. |
0 commit comments