Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@decorators/di

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@decorators/di

node decorators - decorators for dependency injection

  • 3.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.8K
decreased by-12.66%
Maintainers
1
Weekly downloads
 
Created
Source

Node Decorators

Installation

npm install @decorators/di --save

Example

Fully working example can be found in example folder.

API

  • @Injectable() - Registers class as provider in the container
@Injectable()
class HttpService {}
  • @Inject(injectable: Injectable) - A parameter decorator that marks parameter as dependency.
@Injectable()
class HttpService {
  constructor(@Inject(API_URL) apiUrl: string) {}
}
  • @Optional() - A parameter decorator that marks parameter as optional dependency.
@Injectable()
class HttpService {
  constructor(@Optional() @Inject(API_URL) apiUrl: string) {}
}
  • InjectionToken - Creates a token that can be used in DI as Provider.
const API_URL = new InjectionToken('API_URL');
...
@Injectable()
class HttpService {
  constructor(@Inject(API_URL) apiUrl: string) {}
}
  • Container - Container interface
    • .setParent(container: Container) - set parent container
    • .provide(providers: Providers[]) - Registers an array of providers.
    • .get<T>(injectable: Injectable): Promise<T> - Retrieves a Promise with an instance of the injectable, throws:
      • MissingProviderError if dependency provider wasn't found
      • RecursiveProviderError in case of recursive dependency injection
const container = new Container();

container.provide([
  {
    provide: 'Message',
    async useFactory() {
      return delay('Async Provider');
    },
  },
  {
    provide: 'Hello World',
    useClass: Service,
  },
]);

Multi-support

It's possible to provide multiple things using one injection token via multi flag:

container.provide([
  {
    provide: GLOBAL_PIPE,
    useClass: ServerPipe,
    multi: true
  },
  {
    provide: GLOBAL_PIPE,
    useClass: ErrorPipe,
    multi: true
  },
]);

Keywords

FAQs

Package last updated on 09 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc