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

Package detail

noicejs

ssube534MIT5.0.0-3TypeScript support: included

extremely thin dependency injection

di, dependency-injection, dependency-injection-container, service-locator, guice

readme

noicejs

Extremely thin, async dependency injection, now with a getting started guide.

Inspired by Google's Guice library and written in Typescript.

Features

  • async dependency resolution
  • constructor and property injection
  • modular containers with inheritance
  • named dependencies using strings or unique symbols
  • typed errors
  • typescript typedefs
  • zero runtime dependencies, bundled or otherwise
  • extensive test coverage

Contents

Status

Pipeline status Lines of Code Test coverage MIT license FOSSA Status

Open bug count Open issue count Closed issue count

Renovate badge Dependency status Dev dependency status Known vulnerabilities

Maintainability score Technical debt ratio Quality issues Language grade: JavaScript Total alerts

Releases

  • 3.x versions are compatible with Node 12+
  • 4.x versions are compatible with Node 16+

github release link github release version github commits since release

npm package link npm release version Typescript definitions

Usage

Consider a Server class that needs to fetch data from the Cache and Filesystem, but doesn't know (or need to know) how those are implemented. The following example is also part of the unit tests.

import { LocalModule } from './local';
import { NetworkModule } from './network';

class Cache {
  public get(path: string, ttl: number, fallback: () => Promise<string>): Promise<string> {
    /* ... */
  }
}

class Filesystem {
  public get(path: string): Promise<string> {
    /* ... */
  }
}

/**
 * Constructors, strings, and symbols are supported. Symbols are
 * preferred, as the most unique, but names can be convenient.
 */
@Inject(Cache.name.toLowerCase(), Filesystem.name.toLowerCase())
class Server {
  protected readonly cache: Cache;
  protected readonly filesystem: Filesystem;
  protected readonly ttl: number;

  constructor(options) {
    this.cache = options.cache;
    this.filesystem = options.filesystem;
    this.ttl = defaultTo(options.ttl, 0);
  }

  get(path: string) {
    return options.cache.get(path, this.ttl, () => options.filesystem.get(path));
  }
}

const TEST_TTL = 60;

function module() {
  if (process.env['DEBUG'] === 'TRUE') {
    return new LocalModule();
  } else {
    return new NetworkModule();
  }
}

async function main() {
  const container = Container.from(module());
  await container.configure();

  const server = await container.create(Server, {
    /* cache and filesystem are found and injected by container */
    ttl: TEST_TTL,
  });

  /* server.cache.get and server.filesystem.get will be called in order */
  const result = await server.get('some/file');
}

noicejs will collect dependencies from the decorated constructor and any superclasses, find a provider for each injected dependency, and asynchronously resolve them before calling the constructor. Any extra parameters are passed on to the original constructor, along with the container and resolved dependencies.

Build

To build a bundle and run tests:

> make

yarn
yarn install v1.17.3
[1/4] Resolving packages...
success Already up-to-date.
Done in 0.20s.
/home/ssube/code/ssube/noicejs//node_modules/.bin/rollup --config /home/ssube/code/ssube/noicejs//config/rollup.js

src/index.ts, test/harness.ts, test/**/Test*.ts → out/...
...
created out/ in 3.3s
/home/ssube/code/ssube/noicejs//node_modules/.bin/api-extractor run --config /home/ssube/code/ssube/noicejs//config/api-extractor.json --local -v

api-extractor 7.3.8  - https://api-extractor.com/
...

API Extractor completed successfully
Success!

License

FOSSA Status

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

5.0.0-3 (2022-10-20)

Features

  • field: add helper to populate fields (2f61059)
  • add field decorator (4fe165b)

Bug Fixes

  • docs: describe how to use field decorator, add example (8ae83cb)
  • docs: update API signatures (f470f25)
  • field: bind fields to constructor, cover field injection (9ea00ed)
  • module: consistently use contract name type (156d257)
  • test: cover basic usage of field decorator (a344da6)
  • test: cover missing fields better (134b389)

5.0.0-2 (2022-04-26)

Bug Fixes

5.0.0-1 (2022-04-26)

⚠ BREAKING CHANGES

  • the library will now be published as loose ES modules. Library users are better informed on bundling needs, and repeated bundling introduces extra boilerplate code.

Features

  • build: switch to c8 for coverage (bb6b113)
  • error: change error cause from a method to a getter (091a71d)
  • update code and build to use ES modules (e14b9b3)

Bug Fixes

  • build: add command for dev image, fix node bin path (b172d23)
  • build: add docker TLS options, remove codecov job (1227e47)
  • build: correct GPG socket in dev container (b4b13c8)
  • build: update API extractor report for ES modules (19c2de1)
  • deps: add tslib directly, pin others (b30664c)
  • test: restore coverage, mark noop methods (6a4f35c)

5.0.0-0 (2022-01-30)

⚠ BREAKING CHANGES

  • the library will now be published as loose ES modules. Library users are better informed on bundling needs, and repeated bundling introduces extra boilerplate code.

Features

  • build: switch to c8 for coverage (bb6b113)
  • update code and build to use ES modules (e14b9b3)

Bug Fixes

  • build: add command for dev image, fix node bin path (b172d23)
  • build: add docker TLS options, remove codecov job (1227e47)
  • build: correct GPG socket in dev container (b4b13c8)
  • build: update API extractor report for ES modules (19c2de1)
  • deps: add tslib directly, pin others (b30664c)
  • test: restore coverage, mark noop methods (6a4f35c)

4.0.0 (2021-07-10)

⚠ BREAKING CHANGES

  • the 4.x release family will support Node 16+ and drop support for previous versions.

Features

  • document supported Node versions (aca25b4)

3.3.0 (2021-07-10)

Features

  • only bind module prototype if it exists (cf46c10)
  • build: update to node 12 (6f61e80)

Bug Fixes

  • build: add eslint peer dep (04af63f)
  • build: pull images from nexus (e1c12dd)
  • test: cover modules without prototypes (86281e2)

3.2.0 (2021-07-10)

Features

  • only bind module prototype if it exists (cf46c10)
  • build: update to node 12 (6f61e80)

Bug Fixes

  • build: add eslint peer dep (04af63f)
  • build: pull images from nexus (e1c12dd)
  • test: cover modules without prototypes (86281e2)

3.1.0 (2020-09-06)

Features

  • test: add readme example as test (6eb9816)

Bug Fixes

  • build: disable warnings for shadowing of private global types (a9691ac)
  • build: extern source map modules to fix requires (9d1cad0)
  • build: update eslint for typescript-eslint 4 rules (b779072)
  • config: update lint to naming-convention rule (2771a29)
  • docs: update readme with link to example test (54170e3)

3.0.1 (2020-03-07)

Bug Fixes

  • lint: replace some nulls with maybe type (da28e08)

3.0.0 (2019-11-11)

⚠ BREAKING CHANGES

  • module: this prevents the fluent bind().toConstructor() from inferring options if the constructor or factory requires a subclass of BaseOptions, since the options are resolved against the arguments to bind. Previously, AnyOptions would have been accepted, regardless of what the constructor or factory expects.

Bug Fixes

  • build: handle unplaced chunks (dca135c)
  • build: update package keywords (e7724f2)
  • docs: update pages index example (814ac8c)
  • docs: update readme example (bdc8457)
  • error: indent every line of base stack (1aadd0c)
  • module: add binding options type arg (5adcb8f)
  • module: type options in implementation (717a856)

Features

3.0.0-8 (2019-11-10)

Bug Fixes

  • docs: name links (bb88f09)
  • docs: replace readme symlink with file (14cd6c9)

Features

  • build: add alpine image from template (755ee73)
  • build: replace tslint with eslint (ec60786)

3.0.0-7 (2019-10-04)

⚠ BREAKING CHANGES

  • logger: properties of LogLevel in object literals will need to be updated from strings ('info') to enum references (LogLevel.Info). This does not impact other usages.

Bug Fixes

  • build: remove readme link (b6ebcc4)
  • build: update lockfile with mirror packages (dbfc411)
  • build: update with additional makefile (a380aed)
  • container: switch extra parameters back to any (fixes #113) (8aee04e)
  • docs: clean up preface module example (ad33250)
  • docs: inject symbol (94f8881)
  • docs: matching lists (bbf0b48)
  • docs: note map module, add ToC (20b530a)
  • docs: rewrite preface without ternary (8a11ed2)
  • docs: use readme as docs root, link back (3105f68)
  • logger: make log level an enum (fixes #108) (b30ee41)
  • return wrapped options for constructors (68094d7)
  • take extra args as readonly arrays (2ae2e0f)

Features

  • build: enable coverage thresholds (8700216)
  • build: migrate to rollup template config (804e20e)
  • build: rollup json/yaml (update from rollup-template) (89ffd03)
  • docs: add GH pages config (cc4f23d)
  • logger: add winston log adapter (#120) (78877d5)

3.0.0-6 (2019-09-27)

Bug Fixes

  • build: disable line lengths in markdownlint (38b89df)
  • docs: add preface to getting started (e71f445)
  • docs: declare classes at module scope (1565076)
  • docs: use map module in getting started (5d041e5)
  • tests: improve coverage (9752051)

Features

  • add map module for simpler setup (fixes #102) (e6e63c5)
  • docs: add basic getting started (8906c4d)
  • docs: begin adding examples (395039d)
  • docs: explain provider types and decorator (f7c272c)

3.0.0-5 (2019-09-27)

Bug Fixes

  • build: switch to custom docker images with build tools (0b163d8)
  • build: use template typedef in package (7a2b355)

Features

  • build: adopt rollup-template (3976d82)

3.0.0-4 (2019-09-15)

Bug Fixes

  • options: remove base type (8633ad4)
  • test: add test logger helper, improve test helper naming (5064dd2)
  • tests: merge old tests into new suites (49360c8)

3.0.0-3 (2019-09-15)

Bug Fixes

  • build: add tslint, fix errors (c8eb91c)
  • build: bump coverage requirements (31233de)
  • docs: readme formatting (f6da95e)
  • error: remove redundant check for undefined stack traces (10fdf66)
  • test: improve debug coverage, error docs (a616c71)
  • tests: test error cases better, clean up negated conditionals (6108ab3)

Features

  • decorator: split constructor and function wrappers (44f3531)

3.0.0-2 (2019-09-08)

Features

  • export constructor type guard (ce1cfe4)

3.0.0-1 (2019-09-08)

Bug Fixes

  • build: explicitly set publish registry, remove cache from package (80d3b9a)

3.0.0-0 (2019-09-08)

⚠ BREAKING CHANGES

  • Contract requires two type parameters and option type parameters extend BaseOptions, narrowing some types
  • bundle library with rollup

Bug Fixes

  • build: ensure deps land in test bundle (ae8b46e)
  • build: fix main bundle name (bdaa17c)
  • build: remove external webpack modules from coverage (45b8eb8)
  • build: use nexus mirror for yarn packages (a6e3045)
  • test: module mock type (598bc70)
  • ensure option types extend base options, make arguments unknown (7d6503e)
  • build: move library source and entry to main (2ed6a0d)
  • build: update tests for rollup (73d1d60)
  • docs: fix license section (ae4612f)
  • docs: replace greenkeeper badge with renovate (4a1309b)
  • docs: update usage example (e00318d)
  • logger/console: pass data parameters individually, test (9d8bcd1)
  • imports and typedef config (d3a8cf8)

Features

  • test: cover all the easy stuff (errors and loggers) (e6073d2)
  • add withContainer decorator for components and other constructors (9f05630)
  • build: add msft api-extractor and bundle typedefs (f860b4c)
  • build: upgrade to rollup from webpack (e397489)
  • docs: begin tagging exported symbols for API docs (fedf58c)
  • docs: build and add api docs (ec93f71)
  • docs: license section (a116b00)
  • test: add source-map-support during tests (7030dbc)
  • test: inner error via cause (af636c3)
  • test: rudimentary error coverage (03f0264)
  • test: test stack traces (1b24aa8)
  • upgrade from webpack to rollup (49d3571)

2.5.2 (2019-01-20)