{"_id":"@asanovr/nestjs-discovery","name":"@asanovr/nestjs-discovery","dist-tags":{"latest":"5.0.0"},"versions":{"5.0.0":{"name":"@asanovr/nestjs-discovery","version":"5.0.0","description":"A Badass NestJS module for querying your app's controllers, providers and handlers","keywords":["NestJS","discovery","modules"],"author":{"name":"Jesse Carter","email":"jesse.r.carter@gmail.com"},"homepage":"https://github.com/golevelup/nestjs/blob/master/packages/discovery/README.md","license":"MIT","main":"lib/index.js","typings":"lib/index.d.ts","directories":{"lib":"lib","test":"__tests__"},"publishConfig":{"access":"public"},"repository":{"type":"git","url":"git+https://github.com/asanovr/nestjs.git"},"scripts":{"build":"tsc --build tsconfig.build.json","build:watch":"tsc --build tsconfig.build.json --watch","test":"jest"},"dependencies":{"lodash":"^4.17.21"},"peerDependencies":{"@nestjs/common":"^10.x","@nestjs/core":"^10.x"},"bugs":{"url":"https://github.com/golevelup/nestjs/issues"},"jest":{"moduleFileExtensions":["js","json","ts"],"rootDir":"src","testRegex":".spec.ts$","transform":{"^.+\\.ts$":"ts-jest"},"coverageDirectory":"../coverage","testEnvironment":"node"},"gitHead":"83abc8861cf5f8df94b8cd39964f41ce728aff6d","_id":"@asanovr/nestjs-discovery@5.0.0","_nodeVersion":"16.15.1","_npmVersion":"lerna/3.22.1/node@v16.15.1+arm64 (darwin)","dist":{"integrity":"sha512-vlDT2Aj9NA9OxEKUGx+GJE1ECLygw5RGbdkDWepcK71MEO7GPumhVFY2oB94+a6RKW1AxPy80pI/JFwcaeGhfg==","shasum":"e4961a1f084a8606278d783c095cbba73f31d9d0","tarball":"https://registry.npmjs.org/@asanovr/nestjs-discovery/-/nestjs-discovery-5.0.0.tgz","fileCount":20,"unpackedSize":41639,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIC6Sf2ACj59D3PDvr6qGDzUvFkmPTnRxQd8Lz5CBYhpEAiEAiAxXxN/eKMHbOE4j+AaqsEcmqWwfLdC50gLX3faVYJU="}]},"_npmUser":{"name":"asanovr","email":"bioforge91@gmail.com"},"maintainers":[{"name":"asanovr","email":"bioforge91@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/nestjs-discovery_5.0.0_1693545916315_0.6579430722190918"},"_hasShrinkwrap":false}},"time":{"created":"2023-09-01T05:25:16.200Z","5.0.0":"2023-09-01T05:25:16.473Z","modified":"2023-09-01T05:25:16.779Z"},"maintainers":[{"name":"asanovr","email":"bioforge91@gmail.com"}],"description":"A Badass NestJS module for querying your app's controllers, providers and handlers","homepage":"https://github.com/golevelup/nestjs/blob/master/packages/discovery/README.md","keywords":["NestJS","discovery","modules"],"repository":{"type":"git","url":"git+https://github.com/asanovr/nestjs.git"},"author":{"name":"Jesse Carter","email":"jesse.r.carter@gmail.com"},"bugs":{"url":"https://github.com/golevelup/nestjs/issues"},"license":"MIT","readme":"# @golevelup/nestjs-discovery\n\n<p align=\"center\">\n<a href=\"https://www.npmjs.com/package/@golevelup/nestjs-discovery\"><img src=\"https://img.shields.io/npm/v/@golevelup/nestjs-discovery.svg?style=flat\" alt=\"version\" /></a>\n<a href=\"https://www.npmjs.com/package/@golevelup/nestjs-discovery\"><img alt=\"downloads\" src=\"https://img.shields.io/npm/dt/@golevelup/nestjs-discovery.svg?style=flat\"></a>\n<img alt=\"license\" src=\"https://img.shields.io/npm/l/@golevelup/nestjs-discovery.svg\">\n</p>\n\n## Description\n\nThis module provides access to the `DiscoveryService` which can be used to query the various modules, providers, controllers and handlers that make up your NestJS application.\n\n## Motivation\n\nWhen building modules that extend NestJS functionality, it's common to use custom `Decorators` to attach metadata to different parts of the application. Once metdata is attached, the module will then need to be able to \"discover\" all the metadata to be able to connect it's functionality. For example, the official `@nestjs/graphql` package needs to be able to discover all the `@Mutation` and `@Resolver` decorated classes in order to properly build the GraphQL schema.\n\nNestJS provides the `MetadataScanner` class to be able to retrieve this data but doesn't expose a friendly API to be able to easily and quickly find the components in question. The `DiscoveryService` fills this gap by exposing common discovery patterns that can be used when building module extensions to NestJS\n\n## Usage\n\n### Install\n\n`npm install ---save @golevelup/nestjs-discovery`\n\nor\n\n`yarn add @golevelup/nestjs-discovery`\n\n### Import\n\nImport and add `DiscoveryModule` to the `imports` section of the module you wish to implement Discovery features in. It's common to inject it directly into consuming Module's contructor so that it can be used during the `onModuleInit` lifecycle hook at application startup.\n\n```typescript\nimport { DiscoveryModule } from '@golevelup/nestjs-discovery';\nimport { Module } from '@nestjs/common';\n\n@Module({\n  imports: [DiscoveryModule],\n})\nexport class ExampleModule implements OnModuleInit {\n  constructor(private readonly discover: DiscoveryService) {}\n\n  public async onModuleInit() {\n    // const providers = await this.discover.providersWithMetaAtKey<number>('metaKey')\n  }\n}\n```\n\n### Discover\n\nThe `DiscoveryService` exposes several different querying patterns for your app's components that are [well documented with comments](src/discovery.service.ts). This will also provide intellisense for querying in a TypeScript compatible IDE.\n\nIn the case of querying for `providers` or `controllers`, the service returns the following interfaces:\n\n```typescript\nexport interface DiscoveredModule {\n  name: string;\n  instance: {};\n  injectType?: Type<{}>;\n  dependencyType: Type<{}>;\n}\n\nexport interface DiscoveredClass extends DiscoveredModule {\n  parentModule: DiscoveredModule;\n}\n```\n\nThis gives access to the (singleton) `instance` of the matching provider or controller created by the NestJS Dependency Injection container.\n\nThe `injectType` can contain the constructor function of the provider token if it is provided as an @Injectable class. In the case of custom providers, this value will either contain the type of the factory function that created the dependency, or undefined if a value was directly provided with `useValue`.\n\nThe `dependencyType` is a shortcut to retrieve the constructor function of the actual provided dependency itself. For @Injectable providers/controllers this will simply be the decorated class but for dyanmic providers it will return the constructor function of whatever dependency was actually returned from `useValue` or `useFactory`.\n\nIt also provides the string based name for convenience. A `DiscoveredClass` contains a `parentModule` which provides the same set of information for the `@Module` class that the dependency was discovered in.\n\nWhen querying for methods on `providers` or `controllers` the following interface is returned:\n\n```typescript\nexport interface DiscoveredMethod {\n  handler: (...args: any[]) => any;\n  methodName: string;\n  parentClass: DiscoveredClass;\n}\n```\n\nThis gives access to the `handler` which is the actual class method implementation as well as the ability to navigate back up the dependency tree with the attached `parentClass`.\n\nWhen specifically querying for components in the context of looking for decorator metadata, the `...WithMetaAtKey<T>` service methods return the types above along with the metadata that was discovered.\n\n```typescript\nexport interface DiscoveredMethodWithMeta<T> {\n  discoveredMethod: DiscoveredMethod;\n  meta: T;\n}\n\nexport interface DiscoveredClassWithMeta<T> {\n  discoveredClass: DiscoveredClass;\n  meta: T;\n}\n```\n\n### Example\n\nAssuming you were using a custom decorator in your application that attached metadata at a key called `exampleKey`:\n\n```typescript\nconst ExampleDecorator = (meta: string) => SetMetadata('exampleKey', meta);\n```\n\nFind all controller methods that have been decorated with `@ExampleDecorator` and retrieve the value they set for meta:\n\n```typescript\nconst exampleMethodsMeta = await this.discover.controllerMethodsWithMetaAtKey<\n  string\n>('exampleKey');\n```\n\n## Contribute\n\nContributions welcome! Read the [contribution guidelines](../../CONTRIBUTING.md) first.\n\n## License\n\n[MIT License](../../LICENSE)\n","readmeFilename":"README.md"}