Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

njct

unlight314MIT10.0.0TypeScript support: included

Inject a dependency

injector, inject, dependency, dependency-injection, service-locator

readme

inject

Inject a dependency (service locator pattern).

USAGE

// app.ts
import { inject } from 'njct';
const fs = inject('fs', () => require('fs'));
fs.readFileSync('data.json');

// app.spec.ts
import { injector } from 'njct';
injector.mock('fs', () => ({
  readFileSync: () => 'result of call of fs.readFileSync()',
}));
class Car {
  static count = 0;
  constructor() {
    Car.count++;
  }
}
let vehicle = inject(Car);
vehicle = inject(Car);
expect(vehicle).toBeA(Car);
expect(Car.count).toEqual(1);

changelog

10.0.0 (2022-06-03)

⚠ BREAKING CHANGES

  • Replace mock by provide

Code Refactoring

  • Replace mock by provide (cc91323)

9.0.0 (2022-06-01)

⚠ BREAKING CHANGES

  • Single overloaded function

Bug Fixes

Code Refactoring

  • Single overloaded function (fb03714)

8.1.0 (2021-07-16)

Features

  • Allow inject without default factory (cf20857)

8.0.0 (2019-04-22)

Features

  • Separated injection to types (fbe3f94)

BREAKING CHANGES

  • Signature changed

7.1.3 (2018-12-25)

Bug Fixes

  • Error: Cannot find module 'njct' (4f15b41)

7.1.2 (2018-12-18)

Bug Fixes

  • Do not use dist as root (4388c1e)

7.1.1 (2018-12-18)

Bug Fixes

7.1.0 (2018-12-18)

Features

  • Alias injector.provide (6d809b6)
  • Use ng-packagr for build (1e33c9f)
  • Use once 1-liner function (0dc4fea)

1.1.0 (2018-12-02)

Features

  • Inject transient class instance (singleton) (4922425)