1
0
Fork 0
Thin dependency injection for JavaScript (Guice for JS)
Go to file
renovate[bot] 73383d0c0a
update: update dependency @types/sinon-chai to v3.2.4 (#327)
Co-authored-by: Renovate Bot <bot@renovateapp.com>
2020-04-01 01:18:30 -05:00
.github update: from rollup-template 2019-09-24 06:52:11 -05:00
config lint(tests): clean up magic numbers 2019-11-28 06:58:11 -06:00
docs chore(release): 3.0.1 2020-03-07 10:13:35 -06:00
scripts feat(build): add alpine image from template 2019-11-09 23:13:49 -06:00
src fix(lint): replace some nulls with maybe type 2020-03-02 18:44:19 -06:00
test fix(lint): replace some nulls with maybe type 2020-03-02 18:44:19 -06:00
vendor/rollup-resources feat(build): rollup json/yaml (update from rollup-template) 2019-09-28 10:45:20 -05:00
.codeclimate.yml feat(build): replace tslint with eslint 2019-11-09 14:46:39 -06:00
.dockerignore feat(build): adopt rollup-template 2019-09-24 06:52:11 -05:00
.gitignore update(build): from template project 2019-11-09 23:13:29 -06:00
.gitlab-ci.yml feat(build): add sonar job 2019-11-10 09:53:46 -06:00
.mdlrc fix(build): disable line lengths in markdownlint 2019-09-27 18:03:58 -05:00
.npmignore feat(build): adopt rollup-template 2019-09-24 06:52:11 -05:00
.npmrc feat(build): adopt rollup-template 2019-09-24 06:52:11 -05:00
CHANGELOG.md chore(release): 3.0.1 2020-03-07 10:13:35 -06:00
Dockerfile.alpine feat(build): add alpine image from template 2019-11-09 23:13:49 -06:00
Dockerfile.stretch feat(build): add alpine image from template 2019-11-09 23:13:49 -06:00
LICENSE.md clean up readme, ignores 2018-04-20 18:16:17 -05:00
Makefile update(build): from template project 2019-11-09 23:13:29 -06:00
README.md update readme commits since badge 2019-11-11 07:32:11 -06:00
package.json update: update dependency @types/sinon-chai to v3.2.4 (#327) 2020-04-01 01:18:30 -05:00
renovate.json feat(build): upgrade to rollup from webpack 2019-08-25 11:39:22 -05:00
run.mk fix(build): update with additional makefile 2019-09-28 15:26:09 -05:00
tsconfig.json update with changes from apex-world 2018-04-20 18:06:58 -05:00
yarn.lock update: update dependency @types/sinon-chai to v3.2.4 (#327) 2020-04-01 01:18:30 -05:00

README.md

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

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.

import { Cache, Filesystem } from './interfaces';
import { LocalModule } from './local';
import { NetworkModule } from './network';

@Inject(Cache, Filesystem)
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));
  }
}

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 foo = await container.create(Server, {
    /* cache and filesystem are found and injected by container */
    ttl: 60,
  });
}

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