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

@wessberg/di

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wessberg/di - npm Package Compare versions

Comparing version 1.1.0 to 2.0.3

dist/cjs/construct-instance-options/i-construct-instance-options.d.ts

90

package.json
{
"name": "@wessberg/di",
"version": "1.1.0",
"description": "A Dependency-Injection container that holds services and can produce instances of them as required. It mimics reflection by parsing the app at compile-time and supporting the generic-reflection syntax.",
"version": "2.0.3",
"description": "A compile-time powered Dependency-Injection container for Typescript that holds services and can produce instances of them as required.",
"scripts": {
"changelog:generate": "conventional-changelog --outfile CHANGELOG.md --release-count 0",
"readme:badges": "node node_modules/@wessberg/ts-config/readme/badge/helper/add-badges.js",
"readme:refresh": "npm run changelog:generate && npm run readme:badges",
"commit:readme": "npm run readme:refresh && git commit -am \"Bumped version\" --no-verify || true",
"clean:dist": "rm -r -f dist",
"clean:compiled": "rm -r -f compiled",
"generate:readme": "scaffold readme",
"generate:license": "scaffold license",
"generate:contributing": "scaffold contributing",
"generate:coc": "scaffold coc",
"generate:all": "npm run generate:license & npm run generate:contributing & npm run generate:coc & npm run generate:readme",
"update": "ncu -ua && npm update && npm install",
"lint": "tsc --noEmit && tslint -c tslint.json --project tsconfig.json",
"clean:dist": "rm -rf dist",
"clean:compiled": "rm -rf compiled",
"clean": "npm run clean:dist && npm run clean:compiled",
"tsc:dist:umd": "tsc --module umd --outDir dist/umd -p tsconfig.dist.json",
"tsc:dist:es2015": "tsc --module es2015 --outDir dist/es2015 -p tsconfig.dist.json",
"tsc:test": "tsc --module umd --target es2017 --sourceMap",
"build:umd": "npm run tsc:dist:umd",
"build:es2015": "npm run tsc:dist:es2015",
"prebuild": "npm run clean",
"build": "npm run build:umd & npm run build:es2015",
"tslint": "tslint -c tslint.json -p tsconfig.json",
"validate": "npm run tslint && npm run test",
"pretest": "npm run clean:compiled && npm run tsc:test",
"test": "NODE_ENV=TEST echo \"skipping tests...\"",
"posttest": "npm run clean:compiled",
"prepublishOnly": "npm run validate && npm run build",
"precommit": "npm run tslint && exit 0",
"prepush": "npm run validate && exit 0",
"publish:major": "npm version major && npm run commit:readme && git push --no-verify && npm publish",
"publish:minor": "npm version minor && npm run commit:readme && git push --no-verify && npm publish",
"publish:patch": "npm version patch && npm run commit:readme && git push --no-verify && npm publish"
"tsc:cjs": "tsc --module commonjs --outDir dist/cjs -p tsconfig.dist.json",
"tsc:esm": "tsc --module es2015 --outDir dist/esm -p tsconfig.dist.json",
"prebuild": "npm run clean:dist",
"build": "npm run tsc:cjs & npm run tsc:esm",
"validate": "npm run lint",
"prepare": "npm run build",
"beforepublish": "NODE_ENV=production npm run lint && NODE_ENV=production npm run prepare && npm run generate:all && git commit -am \"Bumped version\" || true",
"publish:patch": "npm run beforepublish && npm version patch && git push && npm publish",
"publish:minor": "npm run beforepublish && npm version minor && git push && npm publish",
"publish:major": "npm run beforepublish && npm version major && git push && npm publish"
},

@@ -45,17 +40,16 @@ "keywords": [

"devDependencies": {
"@wessberg/ts-config": "0.0.27",
"conventional-changelog-cli": "^2.0.1",
"husky": "latest",
"tslint": "^5.10.0",
"typescript": "^2.9.2"
"@wessberg/ts-config": "^0.0.30",
"@wessberg/scaffold": "^1.0.4",
"tslint": "^5.11.0",
"typescript": "^3.1.6"
},
"dependencies": {
"tslib": "^1.9.2"
"tslib": "^1.9.3"
},
"main": "./dist/umd/index.js",
"module": "./dist/es2015/index.js",
"browser": "./dist/es2015/index.js",
"types": "./dist/es2015/index.d.ts",
"typings": "./dist/es2015/index.d.ts",
"es2015": "./dist/es2015/index.js",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"browser": "./dist/esm/index.js",
"types": "./dist/esm/index.d.ts",
"typings": "./dist/esm/index.d.ts",
"es2015": "./dist/esm/index.js",
"repository": {

@@ -74,5 +68,21 @@ "type": "git",

"engines": {
"node": ">=7.4.0"
"node": ">=9.0.0"
},
"license": "MIT"
"license": "MIT",
"files": [
"dist/**/*.*"
],
"scaffold": {
"patreonUserId": "11315442",
"logo": "https://raw.githubusercontent.com/wessberg/di/master/documentation/asset/di-logo.png",
"contributorMeta": {
"Frederik Wessberg": {
"imageUrl": "https://avatars2.githubusercontent.com/u/20454213?s=460&v=4",
"role": "Maintainer",
"twitterHandle": "FredWessberg",
"isCocEnforcer": true
}
},
"backers": []
}
}

@@ -1,54 +0,130 @@

# DI (Dependency Injector)
[![NPM version][npm-version-image]][npm-version-url]
[![License-mit][license-mit-image]][license-mit-url]
<img alt="Logo for @wessberg/di" src="https://raw.githubusercontent.com/wessberg/di/master/documentation/asset/di-logo.png" height="200"></img><br>
<a href="https://npmcharts.com/compare/@wessberg/di?minimal=true"><img alt="Downloads per month" src="https://img.shields.io/npm/dm/%40wessberg%2Fdi.svg" height="20"></img></a>
<a href="https://david-dm.org/wessberg/di"><img alt="Dependencies" src="https://img.shields.io/david/wessberg/di.svg" height="20"></img></a>
<a href="https://www.npmjs.com/package/@wessberg/di"><img alt="NPM Version" src="https://badge.fury.io/js/%40wessberg%2Fdi.svg" height="20"></img></a>
<a href="https://github.com/wessberg/di/graphs/contributors"><img alt="Contributors" src="https://img.shields.io/github/contributors/wessberg%2Fdi.svg" height="20"></img></a>
<a href="https://opensource.org/licenses/MIT"><img alt="MIT License" src="https://img.shields.io/badge/License-MIT-yellow.svg" height="20"></img></a>
<a href="https://www.patreon.com/bePatron?u=11315442"><img alt="Support on Patreon" src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" height="20"></img></a>
[license-mit-url]: https://opensource.org/licenses/MIT
# `@wessberg/di`
[license-mit-image]: https://img.shields.io/badge/License-MIT-yellow.svg
> A compile-time powered Dependency-Injection container for Typescript that holds services and can produce instances of them as required.
[npm-version-url]: https://www.npmjs.com/package/@wessberg/di
## Description
[npm-version-image]: https://badge.fury.io/js/%40wessberg%2Fdi.svg
This is a tiny library that brings Dependency-Injection to Typescript. There are several competing libraries out there, but this one is unique in the sense
that:
> A Dependency-Injection container that holds services and can produce instances of them as required. It mimics reflection by parsing the app at compile-time and supporting the generic-reflection syntax.
- It is _seriously_ small.
- It does its work on compile-time. The only runtime dependency is the `DIContainer` itself.
- It doesn't ask you to reflect metadata or to annotate your classes with decorators. _"It just works"_.
- It maps interfaces to implementations. Most popular dependency injection systems for TypeScript doesn't do this. This allows you to truly decouple an abstraction from its implementation.
- It supports the .NET generic reflection flavour: `registerSingleton<Interface, Implementation>()`. No need for anything else.
## Installation
Simply do: `npm install @wessberg/di`.
This library provides constructor-based dependency injection. This means that your classes will receive dependency-injected services as arguments to their constructors.
## Description
This library is a runtime dependency, but you need to transform your code with the [`DI Custom Transformer`](https://github.com/wessberg/di-compiler) as part of your Typescript compilation step to make the reflection work.
`DI` is truly a fresh take on dependency injection in a TypeScript/JavaScript environment:
## Install
- It does its work on compile-time. The only runtime dependency is the DIContainer itself, which is tiny.
- It doesn't ask you to reflect metadata or to annotate your classes with decorators. "It just works".
- It maps interfaces to implementations. Most popular dependency injection systems for TypeScript doesn't do this. This allows you to truly decouple an abstraction from its implementation.
- It supports the .NET generic reflection flavour: `registerSingleton<Interface, Implementation>()`. No need for anything else.
### NPM
```
$ npm install @wessberg/di
```
### Yarn
```
$ yarn add @wessberg/di
```
## Usage
This library is meant to be super straightforward, super simple to use.
The following examples hopefully shows that:
### Registering services
To register services, simply instantiate a new service container and add services to it.
Here's several examples of how you may do that:
```typescript
import {DIContainer} from "@wessberg/di";
DIContainer.registerSingleton<IFoo, Foo>();
DIContainer.registerTransient<IBar, Bar>();
DIContainer.get<IBar>(); // Retrieves a concrete instance of the IBar service.
// And so on...
// Instantiate a new container for services
const container = new DIContainer();
// Register the service as a Singleton. Whenever the 'IMyService' service is requested,
// the same instance of MyService will be injected
DIContainer.registerSingleton<IMyService, MyService>();
// Register the service as a Transient. Whenever the 'IMyService' service is requested,
// a new instance of MyService will be injected
DIContainer.registerTransient<IMyOtherService, MyOtherService>();
// Rather than mapping a class to an interface,
// here we provide a function that returns an object that implements
// the required interface
DIContainer.registerSingleton<IAppConfig>(() => myAppConfig);
// You don't have to map an interface to an implementation.
DIContainer.registerSingleton<MyAwesomeService>();
```
### Overwriting a new-expression for a custom constructor
### Retrieving instances of services
Sometimes, you may want to invoke the constructor of a service with custom arguments, rather than relying on every other non-initialized parameter of the service constructor to be dependency injected.
You can do that by passing a function that returns a new instance of the provided service as the first argument `registerSingleton` or `registerTransient`.
#### Injecting instances of services into classes
...Works completely automatically. As long as your class is constructed via
a `DIContainer`, and as long as the services it depends on are registered,
the class will receive the services as arguments to its' constructor:
```typescript
DIContainer.registerSingleton<IFoo, Foo>(() => new Foo("customArg", 123, "foo"));
class MyClass {
constructor(
private myService: IMyService,
private myOtherService: IMyOtherService,
private myAwesomeService: MyAwesomeService
) {}
}
```
### Making it work
The true power of this library in comparison to others is that all of this mapping happens on compile-time.
This is what enables you to depend on interfaces, rather than objects that live on runtime.
To make the injections work - and to support the generic reflection notation - you need to compile the source code with the [DI-Compiler](https://github.com/wessberg/DI-compiler).
#### Getting instances directly from the `DIContainer`
If you are using [Rollup](https://github.com/rollup/rollup), then use [rollup-plugin-di](https://github.com/wessberg/rollup-plugin-di) to compile your code automatically as part of your bundle.
Sure, you can do that if you want to:
## How does it work
```typescript
// Gets a concrete instance of 'IMyService'. The implementation will
// depend on what you provided when you registered the service
const service = container.get<IMyService>();
```
It uses [CodeAnalyzer](https://github.com/wessberg/CodeAnalyzer) to check all of your code and all of its dependencies recursively to track classes and the constructor arguments they take (and their order). When a `DIContainer` constructs a new instance of a service, it knows which concrete implementations match interfaces that exists in the signature of class constructors.
## Contributing
Do you want to contribute? Awesome! Please follow [these recommendations](./CONTRIBUTING.md).
## Maintainers
- <a href="https://github.com/wessberg"><img alt="Frederik Wessberg" src="https://avatars2.githubusercontent.com/u/20454213?s=460&v=4" height="11"></img></a> [Frederik Wessberg](https://github.com/wessberg): _Maintainer_
## FAQ
#### This is pure magic. How does it work?
It may look like it, but I assure you it is quite simple. [Read this answer for an explanation](https://github.com/wessberg/di-compiler#how-does-it-work-exactly).
#### Is it possible to have multiple, scoped containers?
Sure. You can instantiate as many as you want to, as long as you make sure the [Custom Transformer for DI](https://github.com/wessberg/di-compiler) get's to see the files that contain them.
## Backers 🏅
[Become a backer](https://www.patreon.com/bePatron?u=11315442) and get your name, logo, and link to your site listed here.
## License 📄
MIT © [Frederik Wessberg](https://github.com/wessberg)
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