{"id":16244573,"url":"https://github.com/endykaufman/nestjs-custom-injector","last_synced_at":"2025-03-19T18:33:13.439Z","repository":{"id":43682886,"uuid":"462148794","full_name":"EndyKaufman/nestjs-custom-injector","owner":"EndyKaufman","description":"Custom injecting logic for NestJS with support multi providing","archived":false,"fork":false,"pushed_at":"2022-09-14T13:39:34.000Z","size":1450,"stargazers_count":10,"open_issues_count":4,"forks_count":0,"subscribers_count":2,"default_branch":"develop","last_synced_at":"2024-04-29T10:47:21.795Z","etag":null,"topics":["custom","decorators","global","inject","injector","multi","nestjs","providers"],"latest_commit_sha":null,"homepage":"http://nestjs-custom-injector.site15.ru/api","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/EndyKaufman.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["EndyKaufman"],"patreon":"EndyKaufman","ko_fi":"endykaufman","liberapay":"EndyKaufman","issuehunt":"endykaufman"}},"created_at":"2022-02-22T05:15:47.000Z","updated_at":"2024-03-05T10:54:46.000Z","dependencies_parsed_at":"2023-01-18T07:30:35.985Z","dependency_job_id":null,"html_url":"https://github.com/EndyKaufman/nestjs-custom-injector","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EndyKaufman%2Fnestjs-custom-injector","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EndyKaufman%2Fnestjs-custom-injector/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EndyKaufman%2Fnestjs-custom-injector/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EndyKaufman%2Fnestjs-custom-injector/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/EndyKaufman","download_url":"https://codeload.github.com/EndyKaufman/nestjs-custom-injector/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221729926,"owners_count":16871143,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["custom","decorators","global","inject","injector","multi","nestjs","providers"],"created_at":"2024-10-10T14:19:43.309Z","updated_at":"2024-10-27T20:32:39.313Z","avatar_url":"https://github.com/EndyKaufman.png","language":"TypeScript","funding_links":["https://github.com/sponsors/EndyKaufman","https://patreon.com/EndyKaufman","https://ko-fi.com/endykaufman","https://liberapay.com/EndyKaufman","https://issuehunt.io/r/endykaufman"],"categories":[],"sub_categories":[],"readme":"# nestjs-custom-injector\n\n[![npm version](https://badge.fury.io/js/nestjs-custom-injector.svg)](https://badge.fury.io/js/nestjs-custom-injector)\n[![monthly downloads](https://badgen.net/npm/dm/nestjs-custom-injector)](https://www.npmjs.com/package/nestjs-custom-injector)\n\n## Installation\n\n```bash\nnpm i --save nestjs-custom-injector\n```\n\n## Links\n\nhttps://nestjs-custom-injector.site15.ru/api - Demo application with nestjs-custom-injector.\nhttps://github.com/EndyKaufman/nestjs-custom-injector-example - Example generated with nest cli for \"Usage\" sections in readme.\n\n## Usage\n\nCreate common interface with token in **animal-provider.interface.ts**\n\n```typescript\nexport const ANIMAL_PROVIDER = 'ANIMAL_PROVIDER';\n\nexport interface AnimalProviderInteface {\n  type: string;\n  say(): string;\n}\n```\n\nCreate first type of logic for cats in **animal-cats.service.ts**\n\n```typescript\nimport { Injectable } from '@nestjs/common';\nimport { AnimalProviderInteface } from './animal-provider.interface';\n\n@Injectable()\nexport class AnimalCatsService implements AnimalProviderInteface {\n  type = 'cat';\n  say(): string {\n    return 'meow';\n  }\n}\n```\n\nCreate second type of logic for dogs in **animal-dogs.service.ts**\n\n```typescript\nimport { Injectable } from '@nestjs/common';\nimport { AnimalProviderInteface } from './animal-provider.interface';\n\n@Injectable()\nexport class AnimalDogsService implements AnimalProviderInteface {\n  type = 'dog';\n  say(): string {\n    return 'woof';\n  }\n}\n```\n\nCreate controller **animals.controller.ts**\n\n```typescript\nimport { Controller, Get, Query } from '@nestjs/common';\nimport { CustomInject } from 'nestjs-custom-injector';\nimport {\n  AnimalProviderInteface,\n  ANIMAL_PROVIDER,\n} from './animal-provider.interface';\n\n@Controller('animals')\nexport class AnimalsController {\n  @CustomInject(ANIMAL_PROVIDER, { multi: true })\n  private animalProviders!: AnimalProviderInteface[];\n\n  @Get('animal-types')\n  animalTypes() {\n    return this.animalProviders.map((animalProvider) =\u003e animalProvider.type);\n  }\n\n  @Get('what-says-animals')\n  whatSaysAnimals() {\n    return this.animalProviders.map(\n      (animal) =\u003e `${animal.type} say ${animal.say()}`\n    );\n  }\n\n  @Get('who-say')\n  whoSay(@Query('voice') voice: string) {\n    const animal = this.animalProviders.find(\n      (animal) =\u003e animal.say() === voice\n    );\n    if (!animal) {\n      return { error: `I don't know who say ${voice}` };\n    }\n    return `${animal.type} say ${animal.say()}`;\n  }\n}\n```\n\nAppend all logic to main app module **app.module.ts**\n\n```typescript\nimport { Module } from '@nestjs/common';\nimport { CustomInjectorModule } from 'nestjs-custom-injector';\nimport { AnimalCatsService } from './animal-cats.service';\nimport { AnimalDogsService } from './animal-dogs.service';\nimport { AnimalsController } from './animals.controller';\n\n@Module({\n  ...\n  imports: [\n    ...\n    CustomInjectorModule.forRoot(),\n    CustomInjectorModule.forFeature({\n      providers: [{ provide: ANIMAL_PROVIDER, useClass: AnimalCatsService }],\n    }),\n    CustomInjectorModule.forFeature({\n      providers: [\n        { provide: ANIMAL_PROVIDER, useValue: new AnimalDogsService() },\n      ],\n    }),\n    ...\n  ],\n  controllers: [\n    ...\n    AnimalsController\n    ...\n  ]\n  ...\n})\nexport class AppModule {}\n```\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fendykaufman%2Fnestjs-custom-injector","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fendykaufman%2Fnestjs-custom-injector","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fendykaufman%2Fnestjs-custom-injector/lists"}