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

injection-js

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

injection-js

Dependency Injection library for JavaScript and TypeScript

  • 2.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
552K
decreased by-17.29%
Maintainers
1
Weekly downloads
 
Created

What is injection-js?

injection-js is a lightweight dependency injection library for JavaScript and TypeScript, inspired by Angular's dependency injection system. It allows developers to manage dependencies in a clean and modular way, promoting better code organization and testability.

What are injection-js's main functionalities?

Basic Dependency Injection

This example demonstrates basic dependency injection using injection-js. The `Car` class depends on the `Engine` class, and the injector resolves and provides the dependencies automatically.

const { ReflectiveInjector } = require('injection-js');

class Engine {
  start() {
    console.log('Engine started');
  }
}

class Car {
  constructor(engine) {
    this.engine = engine;
  }
  drive() {
    this.engine.start();
    console.log('Car is driving');
  }
}

const injector = ReflectiveInjector.resolveAndCreate([Engine, Car]);
const car = injector.get(Car);
car.drive();

Injection Tokens

This example shows how to use injection tokens to inject primitive values or non-class dependencies. The `ApiService` class depends on an API URL, which is provided using an injection token.

const { ReflectiveInjector, InjectionToken } = require('injection-js');

const API_URL = new InjectionToken('apiUrl');

class ApiService {
  constructor(apiUrl) {
    this.apiUrl = apiUrl;
  }
  getData() {
    console.log(`Fetching data from ${this.apiUrl}`);
  }
}

const injector = ReflectiveInjector.resolveAndCreate([
  { provide: API_URL, useValue: 'https://api.example.com' },
  { provide: ApiService, useClass: ApiService, deps: [API_URL] }
]);

const apiService = injector.get(ApiService);
apiService.getData();

Hierarchical Injectors

This example demonstrates hierarchical injectors, where a child injector can inherit dependencies from a parent injector. The `Car` class is resolved in the child injector, while the `Engine` class is resolved in the parent injector.

const { ReflectiveInjector } = require('injection-js');

class Engine {
  start() {
    console.log('Engine started');
  }
}

class Car {
  constructor(engine) {
    this.engine = engine;
  }
  drive() {
    this.engine.start();
    console.log('Car is driving');
  }
}

const parentInjector = ReflectiveInjector.resolveAndCreate([Engine]);
const childInjector = parentInjector.resolveAndCreateChild([Car]);

const car = childInjector.get(Car);
car.drive();

Other packages similar to injection-js

Keywords

FAQs

Package last updated on 17 Nov 2017

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