Skip to main content

nestrs_core/
discovery.rs

1use crate::{ModuleRef, RouteInfo, RouteRegistry};
2use std::any::TypeId;
3
4/// NestJS [`DiscoveryService`](https://docs.nestjs.com/fundamentals/discovery-service) analogue:
5/// introspect registered providers and compiled HTTP routes.
6///
7/// Unlike Nest, there is no reflection over arbitrary class metadata: you get [`TypeId`](std::any::TypeId)
8/// keys, type **names** (strings), and whatever is in the global [`RouteRegistry`](crate::RouteRegistry).
9/// Construct with [`Self::new`](DiscoveryService::new)([`ModuleRef`](crate::ModuleRef)).
10///
11/// **Docs:** see the mdBook **Fundamentals** chapter in the `nestrs` repo (`docs/src/fundamentals.md`).
12pub struct DiscoveryService {
13    module: ModuleRef,
14}
15
16impl DiscoveryService {
17    pub fn new(module: ModuleRef) -> Self {
18        Self { module }
19    }
20
21    pub fn module_ref(&self) -> ModuleRef {
22        self.module.clone()
23    }
24
25    pub fn get_providers(&self) -> Vec<TypeId> {
26        self.module.registry().registered_type_ids()
27    }
28
29    pub fn get_provider_type_names(&self) -> Vec<&'static str> {
30        self.module.registry().registered_type_names()
31    }
32
33    pub fn get_routes(&self) -> Vec<RouteInfo> {
34        RouteRegistry::list()
35    }
36}