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

cheap-di

Package Overview
Dependencies
Maintainers
0
Versions
71
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cheap-di - npm Package Compare versions

Comparing version 4.1.1 to 4.1.2-dev.1

CHANGELOG.md

51

package.json
{
"name": "cheap-di",
"version": "4.1.1",
"version": "4.1.2-dev.1",
"description": "TypeScript dependency injection like Autofac in .Net",
"type": "module",
"scripts": {
"compile": "tsc --build tsconfig.cjs.json ./tsconfig.esm.json ./tsconfig.types.json",
"prepare-package-json": "cross-env NODE_OPTIONS=\"--loader ts-node/esm --disable-warning ExperimentalWarning\" ts-node scripts/prepare-package-json.ts",
"build:clean": "rimraf -rf ./dist",
"build": "npm-run-all build:clean compile prepare-package-json"
},
"devDependencies": {
"@types/node": "^20",
"cross-env": "^7.0.3",
"npm-run-all": "^4",
"rimraf": "^5",
"ts-node": "^10",
"typescript": "^5"
},
"repository": {

@@ -19,16 +33,21 @@ "type": "git",

],
"main": "./cjs/index.js",
"types": "./types/index.d.ts",
"_main": "./dist/cjs/index.js",
"_module": "./dist/esm/index.js",
"_types": "./dist/types/index.d.ts",
"files": [
"cjs/**/*",
"esm/**/*",
"types/**/*",
"package.json",
"README.md",
"LICENSE"
],
"module": "./esm/index.js"
}
"_main": "./src/index.ts",
"_types": "./src/index.ts",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/types/index.d.ts",
"build-instructions": {
"name": "cheap-di",
"files": [
"cjs/**/*",
"esm/**/*",
"types/**/*",
"package.json",
"README.md",
"LICENSE"
],
"main": "./cjs/index.js",
"module": "./esm/index.js",
"types": "./types/index.d.ts"
}
}

@@ -5,8 +5,8 @@ # cheap-di

* [Installation](#installation)
* [How to use](#how-to-use)
* [Using with abstractions](#using-abstractions)
* [Registration variants](#registration-variants)
* [registerImplementation](#register-implementation)
* [registerInstance](#register-instance)
- [Installation](#installation)
- [How to use](#how-to-use)
- [Using with abstractions](#using-abstractions)
- [Registration variants](#registration-variants)
- [registerImplementation](#register-implementation)
- [registerInstance](#register-instance)

@@ -21,3 +21,6 @@ ## <a name="installation"></a> Installation

The recommended way of using this package is to use it with code transformers like <a href="https://github.com/tomas-light/cheap-di/tree/master/packages/cheap-di-ts-transform">cheap-di-ts-transform</a>. Because in this way, you will get true dependency injection:
The recommended way of using this package is to use it with code transformers
like <a href="https://github.com/tomas-light/cheap-di/tree/master/packages/cheap-di-ts-transform">
cheap-di-ts-transform</a>. Because in this way, you will get true dependency injection:
```ts

@@ -39,2 +42,3 @@ import { container } from 'cheap-di';

}
/**

@@ -52,3 +56,6 @@ * With cheap-di-ts-transform here will be added metadata about Service dependencies.

If you can't use <a href="https://github.com/tomas-light/cheap-di/tree/master/packages/cheap-di-ts-transform">cheap-di-ts-transform</a>, you have to use `@inject` decorator to define your dependencies (<i>it supports stage 2 and stage 3 TypeScript syntax</i>):
If you can't use <a href="https://github.com/tomas-light/cheap-di/tree/master/packages/cheap-di-ts-transform">
cheap-di-ts-transform</a>, you have to use `@inject` decorator to define your dependencies (<i>it supports stage 2 and
stage 3 TypeScript syntax</i>):
```ts

@@ -85,7 +92,9 @@ import { container, inject } from 'cheap-di';

> - It is syntax analog of interface.
>
>
> But why don't we use interfaces?
> - Because we need some unique "token" compatible with JavaScript, to be able to register and resolve implementations. You can't use TypeScript interface in runtime (because it doesn't exists in JavaScript), and you can use classes!
>
> Some another DI libraries use symbols to achieve that, but we don't see necessity to map somewhere symbols with implementations if we may use only classes everywhere. Less code -> less issues.
> - Because we need some unique "token" compatible with JavaScript, to be able to register and resolve implementations.
> You can't use TypeScript interface in runtime (because it doesn't exists in JavaScript), and you can use classes!
>
> Some another DI libraries use symbols to achieve that, but we don't see necessity to map somewhere symbols with
> implementations if we may use only classes everywhere. Less code -> less issues.

@@ -106,2 +115,3 @@ ```ts

```
```ts

@@ -119,2 +129,3 @@ // service.ts

```
```ts

@@ -128,2 +139,3 @@ // somewhere in your application initialization

```
```ts

@@ -139,3 +151,5 @@ // somewhere inside your code

Without <a href="https://github.com/tomas-light/cheap-di/tree/master/packages/cheap-di-ts-transform">cheap-di-ts-transform</a>, you still may use cheap-di with `@inject` decorator (it supports stage 2 and stage 3 TypeScript syntax):
Without <a href="https://github.com/tomas-light/cheap-di/tree/master/packages/cheap-di-ts-transform">
cheap-di-ts-transform</a>, you still may use cheap-di with `@inject` decorator (it supports stage 2 and stage 3
TypeScript syntax):

@@ -152,3 +166,5 @@ ```ts

}
abstract class InfoLogger extends Logger {}
abstract class ErrorLogger extends Logger {}

@@ -160,6 +176,11 @@

class ConsoleLogger implements Logger {
constructor(public prefix: string, private sessionAccessor: SessionAccessor) {}
constructor(
public prefix: string,
private sessionAccessor: SessionAccessor
) {}
debug(message: string) {
console.log(`[${this.sessionAccessor.getSession()}] ${this.prefix}: ${message}`);
console.log(
`[${this.sessionAccessor.getSession()}] ${this.prefix}: ${message}`
);
}

@@ -177,2 +198,3 @@ }

```
```ts

@@ -184,7 +206,14 @@ // somewhere

const infoPrefix = 'INFO: ';
container.registerImplementation(ConsoleLogger).as(InfoLogger).inject(infoPrefix);
container
.registerImplementation(ConsoleLogger)
.as(InfoLogger)
.inject(infoPrefix);
const errorPrefix = 'ERROR: ';
container.registerImplementation(ConsoleLogger).as(ErrorLogger).inject(errorPrefix);
container
.registerImplementation(ConsoleLogger)
.as(ErrorLogger)
.inject(errorPrefix);
```
```ts

@@ -201,2 +230,3 @@ // somewhere in inside your code

To use stage 2 decorators, you need to adjust your tsconfig.json like this:
```json

@@ -218,2 +248,3 @@ {

If you would like to specify implementation of your interface:
```ts

@@ -231,2 +262,3 @@ import { container } from 'cheap-di';

Or if you want to inject some parameters to its constructor:
```ts

@@ -245,2 +277,3 @@ import { container } from 'cheap-di';

Or if you want to have only one instance of the implementation class:
```ts

@@ -257,2 +290,3 @@ import { container } from 'cheap-di';

And singletons may also be used with interface specifications:
```ts

@@ -270,2 +304,3 @@ import { container } from 'cheap-di';

And even with argument injection:
```ts

@@ -288,4 +323,5 @@ import { container } from 'cheap-di';

You may wrap resolved instance with Proxy or do something else.
You may wrap resolved instance with Proxy or do something else.
For example if you want to log called methods of Api.
```ts

@@ -314,3 +350,4 @@ import { container } from 'cheap-di';

If you want to register some instance as an interface, the result is similar to singleton registration, except that you have to instantiate the class yourself.
If you want to register some instance as an interface, the result is similar to singleton registration, except that you
have to instantiate the class yourself.

@@ -351,4 +388,6 @@ ```ts

You can see more examples of container methods in <a href="https://github.com/tomas-light/cheap-di/blob/master/tests/jest-test/src/ContainerImpl.test.ts">ContainerImpl.test.ts</a>
You can see more examples of container methods
in <a href="https://github.com/tomas-light/cheap-di/blob/master/tests/jest-test/src/ContainerImpl.test.ts">
ContainerImpl.test.ts</a>
[Changelog](./CHANGELOG.md)

Sorry, the diff of this file is not supported yet

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