Comparing version 4.0.0-rc-30 to 4.0.0-rc-31
@@ -1,5 +0,5 @@ | ||
import { DiMetadata, ImplementationType, SomeDependency } from './types.js'; | ||
export declare function saveConstructorMetadata<TClass>(constructor: ImplementationType<TClass>, ...dependencies: SomeDependency[]): DiMetadata; | ||
import { AbstractConstructor, Constructor, DiMetadata, ImplementationType, SomeDependency } from './types.js'; | ||
export declare function saveConstructorMetadata<T>(constructor: Constructor<T> | AbstractConstructor<T>, ...dependencies: SomeDependency[]): DiMetadata; | ||
export declare function findOrCreateMetadata<TClass>(constructor: ImplementationType<TClass>): DiMetadata; | ||
export declare function findMetadata<TClass>(constructor: ImplementationType<TClass>): DiMetadata; | ||
export declare function createConstructorMetadata<TClass>(constructor: ImplementationType<TClass>): DiMetadata; |
@@ -10,8 +10,10 @@ import { cheapDiSymbol } from './cheapDiSymbol.js'; | ||
interface DiMetadata { | ||
/** if class was registered as singleton with container.registerImplementation(...).asSingleton)*/ | ||
singleton?: boolean; | ||
/** constructor dependencies */ | ||
dependencies?: SomeDependency[]; | ||
/** reference on a constructor that was patched (helps in inheritance cases) */ | ||
modifiedClass?: unknown; | ||
/** parameters are specified with @inject decorator explicitly */ | ||
injectDependencies?: SomeDependency[]; | ||
/** reference on a constructor that was patched (helps in inheritance cases) */ | ||
modifiedClass?: unknown; | ||
/** parameters are passed to container.registerImplementation(...).inject(parameter1, parameter2) */ | ||
@@ -18,0 +20,0 @@ injected?: unknown[]; |
{ | ||
"name": "cheap-di", | ||
"description": "TypeScript dependency injection like Autofac in .Net", | ||
"version": "4.0.0-rc-30", | ||
"version": "4.0.0-rc-31", | ||
"scripts": { | ||
@@ -12,3 +12,3 @@ "compile": "tsc --build tsconfig.cjs.json ./tsconfig.esm.json ./tsconfig.types.json", | ||
"dependencies": { | ||
"typescript": "^5.2.2" | ||
"typescript": "^5.3.3" | ||
}, | ||
@@ -15,0 +15,0 @@ "devDependencies": { |
@@ -12,4 +12,13 @@ # cheap-di | ||
The recommended way of using this package is using it with code transformers like `cheap-di-ts-transform`. Because in this way you will get the truly dependency injection: | ||
The recommended way of using this package is using it with code transformers like `cheap-di-ts-transform`. Because in this way you will get the true dependency injection: | ||
> Note: | ||
> You may wonder, why we use abstract classes? | ||
> - 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. | ||
```ts | ||
@@ -36,6 +45,6 @@ abstract class Logger { | ||
/** | ||
* With cheap-di-ts-transform here will be added information about Service dependencies. | ||
* With cheap-di-ts-transform here will be added metadata about Service dependencies. | ||
* */ | ||
// somewhere in you application initialization | ||
// somewhere in your application initialization | ||
import { container } from 'cheap-di'; | ||
@@ -46,3 +55,3 @@ | ||
// somewhere in inside your code | ||
// somewhere inside your code | ||
// or you may use some middleware to do this, to get rid of Service Locator antipattern | ||
@@ -55,3 +64,3 @@ import { container } from 'cheap-di'; | ||
But if you can't use transformers you still may use cheap-di with decorators: | ||
But if you can't use transformers you still may use cheap-di with `@inject` decorator (it supports stage 2 and stage 3 TypeScript syntax): | ||
@@ -71,3 +80,4 @@ ```ts | ||
// non-classes-arguments specified as "unknown" | ||
// non-classes-arguments specified as "unknown" (or any other string) | ||
// we use `dependencies.filter((dependency) => typeof dependency !== 'string'))` code to filter non-clases dependencies | ||
@inject('unknown', SessionAccessor) | ||
@@ -140,3 +150,3 @@ class ConsoleLogger implements Logger { | ||
class Some { | ||
class Foo { | ||
constructor(private name: string) {} | ||
@@ -146,3 +156,3 @@ } | ||
container | ||
.registerImplementation(Service) | ||
.registerImplementation(Foo) | ||
.inject('some name'); | ||
@@ -155,6 +165,6 @@ ``` | ||
class Some {} | ||
class Foo {} | ||
container | ||
.registerImplementation(Service) | ||
.registerImplementation(Foo) | ||
.asSingleton(); | ||
@@ -195,3 +205,3 @@ ``` | ||
If you want to register some instance as interface | ||
If you want to register some instance as interface. Result is similar to singleton registration except the fact you have to instantiate class by your self | ||
@@ -201,2 +211,18 @@ ```ts | ||
class Database { | ||
get() { | ||
return Promise.resolve('name1'); | ||
} | ||
} | ||
container | ||
.registerInstance(new Database()) | ||
.as(Database); | ||
``` | ||
And you may use any object value as instance | ||
```ts | ||
import { container } from 'cheap-di'; | ||
abstract class Database { | ||
@@ -212,8 +238,9 @@ abstract get(): Promise<string>; | ||
container.registerInstance(db).as(Database); | ||
container | ||
.registerInstance(db) | ||
.as(Database); | ||
``` | ||
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 in `cheap-di/src/ContainerImpl.test.ts` | ||
[Changelog](../../CHANGELOG.md) | ||
[Changelog](./CHANGELOG.md) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
109225
50
958
236
0
Updatedtypescript@^5.3.3