UNPKG

@nestjs/core

Version:

The core of NestJS, a TypeScript framework for Node.js backends: modules, dependency injection and the request pipeline, on Express, Fastify or node:http.

46 lines (45 loc) • 1.96 kB
import { MetadataScanner } from '../../metadata-scanner.js'; import { ReplFunction } from '../repl-function.js'; import { clc, isNil, isPlainObject } from '@nestjs/common/internal'; export class MethodsReplFn extends ReplFunction { fnDefinition = { name: 'methods', description: 'Display all public methods available on a given provider or controller.', signature: '(token: ClassRef | string) => void', }; action(token) { const proto = typeof token !== 'function' ? this.getMethodsHost(this.ctx.app.get(token)) : this.getClassTokenMethodsHost(token); const methods = new MetadataScanner().getAllMethodNames(proto); this.ctx.writeToStdout('\n'); this.ctx.writeToStdout(`${clc.green('Methods')}:\n`); methods.forEach(methodName => this.ctx.writeToStdout(` ${clc.yellow('◻')} ${methodName}\n`)); this.ctx.writeToStdout('\n'); } /** * A class token may stand for a provider of another class (`useClass`), a * value (`useValue`) or what a factory returns (`useFactory`), so the * methods are read from the resolved instance. The prototype of the class is * used when there is no instance to get: a class that is not registered, or * a request-scoped or transient provider. */ getClassTokenMethodsHost(token) { let instance; try { instance = this.ctx.app.get(token); } catch { return token.prototype; } return isNil(instance) ? token.prototype : this.getMethodsHost(instance); } getMethodsHost(instance) { if (isNil(instance)) { return null; } // A plain object (e.g., a `useValue` provider) holds its methods as own // properties, while its prototype only has the `Object.prototype` built-ins. return isPlainObject(instance) ? instance : Object.getPrototypeOf(instance); } }