Skip to content

MetadataScanner

reference
1 min readUpdated

Kind: Class

Source: packages/core/metadata-scanner.ts

Part of: Core

MetadataScanner inspects a class prototype and its inheritance chain to discover callable method names. It is used by the framework to scan providers, controllers, and other instances for methods that may carry metadata or require registration.

Methods

MethodSignatureReturns
scanFromPrototype`scanFromPrototype(instance: T, prototype: objectnull, callback: (name: string) => R)`
getAllFilteredMethodNamesgetAllFilteredMethodNames(prototype: object)IterableIterator<string>
getAllMethodNames`getAllMethodNames(prototype: objectnull)`

Where it refuses work

  • MetadataScanner stops the work with an early return when !prototype, in 2 places.
  • MetadataScanner stops the work with an early return when this.cachedScannedPrototypes.has(prototype).

Diagram

mermaid
graph LR
  A[Class Instance] --> B[Prototype]
  B --> C[MetadataScanner]
  C --> D[getAllFilteredMethodNames]
  D --> E[Inherited Method Names]
  E --> F[scanFromPrototype Callback]
  F --> G[Collected Results]

Usage

ts
import { MetadataScanner } from '@nestjs/core/metadata-scanner';

const ROUTE_METADATA = 'custom:route';

class UserController {
  @Reflect.metadata(ROUTE_METADATA, '/users')
  findAll() {
    return [];
  }

  findOne() {
    return {};
  }
}

const controller = new UserController();
const scanner = new MetadataScanner();

const routes = scanner.scanFromPrototype(
  controller,
  Object.getPrototypeOf(controller),
  (methodName) => {
    const handler = controller[methodName];
    const path = Reflect.getMetadata(ROUTE_METADATA, handler);

    return path ? { methodName, path } : undefined;
  },
).filter(Boolean);

console.log(routes);
// [{ methodName: 'findAll', path: '/users' }]

AI Coding Instructions

  • Pass the instance and its prototype to scanFromPrototype() so inherited methods can be discovered correctly.
  • Use the scan callback to read method-level metadata and transform discovered methods into framework-specific definitions.
  • Do not rely on getters or setters being returned; the scanner is intended for callable prototype methods.
  • Prefer scanFromPrototype() when processing methods, and use getAllMethodNames() only when raw method names are needed.
  • Avoid scanning the same prototype repeatedly in custom integrations; the scanner caches discovered prototype method names.

Relationships

  • IMPORTS → Injectable
  • IMPORTS → isConstructor
  • IMPORTS → isFunction
  • IMPORTS → isNil

Used by

7 references from 7 files. Each is a place in this repository where the symbol is actually used — go read one rather than trusting an example.

Imported by (7)

  • WebhooksExplorerintegration/discovery/src/webhooks.explorer.ts:5
  • ClientPropertiespackages/microservices/listener-metadata-explorer.ts:16
  • ListenersControllerpackages/microservices/listeners-controller.ts:45
  • Testpackages/testing/test.ts:8
  • TestingModuleOptionspackages/testing/testing-module.builder.ts:29
  • MessageMappingPropertiespackages/websockets/gateway-metadata-explorer.ts:15
  • WebSocketsControllerpackages/websockets/web-sockets-controller.ts:29

Was this page helpful?

Download as PDF
MetadataScanner — NestJS head-to-head