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

cheap-di

Package Overview
Dependencies
Maintainers
1
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.0.1 to 4.1.0

73

cjs/ContainerImpl.js

@@ -12,2 +12,3 @@ "use strict";

dependencies;
enrichCallbacks;
constructor() {

@@ -17,21 +18,15 @@ this.singletons = new Map();

this.dependencies = new Map();
this.enrichCallbacks = new Map();
}
/** register implementation class */
registerImplementation(implementationType) {
const inject = (...injectionParams) => {
const metadata = (0, metadata_js_1.findOrCreateMetadata)(implementationType);
metadata.injected = injectionParams;
};
registerImplementation = (implementationType) => {
this.dependencies.set(implementationType, implementationType);
return {
/** as super class */
let asType = undefined;
const registeredImplementation = {
as: (type) => {
this.dependencies.delete(implementationType);
this.dependencies.set(type, implementationType);
return {
/** add parameters that will be passed to the class constructor */
inject,
};
asType = type;
return registeredImplementation;
},
/** as singleton (optionally super class) */
asSingleton: (type) => {

@@ -41,2 +36,3 @@ if (type) {

this.dependencies.set(type, implementationType);
asType = type;
}

@@ -48,13 +44,24 @@ if (!(0, isSingleton_js_1.isSingleton)(implementationType)) {

}
return {
/** add parameters that will be passed to the class constructor */
inject,
};
return registeredImplementation;
},
/** add parameters that will be passed to the class constructor */
inject,
inject: (...injectionParams) => {
const metadata = (0, metadata_js_1.findOrCreateMetadata)(implementationType);
metadata.injected = injectionParams;
return registeredImplementation;
},
enrich: (enrichCallback) => {
if (asType) {
this.enrichCallbacks.delete(implementationType);
this.enrichCallbacks.set(asType, enrichCallback);
}
else {
this.enrichCallbacks.set(implementationType, enrichCallback);
}
return registeredImplementation;
},
};
}
return registeredImplementation;
};
/** register any object as its constructor */
registerInstance(instance) {
registerInstance = (instance) => {
const constructor = instance.constructor;

@@ -64,4 +71,4 @@ if (constructor) {

}
return {
/** or register the object as any class */
let asType = undefined;
const registeredInstance = {
as: (type) => {

@@ -72,5 +79,18 @@ if (constructor) {

this.instances.set(type, instance);
asType = type;
return registeredInstance;
},
enrich: (enrichCallback) => {
if (asType) {
this.enrichCallbacks.delete(instance);
this.enrichCallbacks.set(asType, enrichCallback);
}
else {
this.enrichCallbacks.set(instance, enrichCallback);
}
return registeredInstance;
},
};
}
return registeredInstance;
};
/** instantiate (or get instance for singleton) by class */

@@ -80,3 +100,8 @@ resolve(type, ...args) {

try {
return this.internalResolve(type, trace, ...args);
const resolvedInstance = this.internalResolve(type, trace, ...args);
const enrichCallback = this.enrichCallbacks.get(type);
if (typeof enrichCallback === 'function') {
return enrichCallback(resolvedInstance);
}
return resolvedInstance;
}

@@ -83,0 +108,0 @@ catch (error) {

{
"type": "commonjs",
"version": "4.0.1"
"version": "4.1.0"
}

@@ -9,2 +9,3 @@ import { CircularDependencyError } from './CircularDependencyError.js';

dependencies;
enrichCallbacks;
constructor() {

@@ -14,21 +15,15 @@ this.singletons = new Map();

this.dependencies = new Map();
this.enrichCallbacks = new Map();
}
/** register implementation class */
registerImplementation(implementationType) {
const inject = (...injectionParams) => {
const metadata = findOrCreateMetadata(implementationType);
metadata.injected = injectionParams;
};
registerImplementation = (implementationType) => {
this.dependencies.set(implementationType, implementationType);
return {
/** as super class */
let asType = undefined;
const registeredImplementation = {
as: (type) => {
this.dependencies.delete(implementationType);
this.dependencies.set(type, implementationType);
return {
/** add parameters that will be passed to the class constructor */
inject,
};
asType = type;
return registeredImplementation;
},
/** as singleton (optionally super class) */
asSingleton: (type) => {

@@ -38,2 +33,3 @@ if (type) {

this.dependencies.set(type, implementationType);
asType = type;
}

@@ -45,13 +41,24 @@ if (!isSingleton(implementationType)) {

}
return {
/** add parameters that will be passed to the class constructor */
inject,
};
return registeredImplementation;
},
/** add parameters that will be passed to the class constructor */
inject,
inject: (...injectionParams) => {
const metadata = findOrCreateMetadata(implementationType);
metadata.injected = injectionParams;
return registeredImplementation;
},
enrich: (enrichCallback) => {
if (asType) {
this.enrichCallbacks.delete(implementationType);
this.enrichCallbacks.set(asType, enrichCallback);
}
else {
this.enrichCallbacks.set(implementationType, enrichCallback);
}
return registeredImplementation;
},
};
}
return registeredImplementation;
};
/** register any object as its constructor */
registerInstance(instance) {
registerInstance = (instance) => {
const constructor = instance.constructor;

@@ -61,4 +68,4 @@ if (constructor) {

}
return {
/** or register the object as any class */
let asType = undefined;
const registeredInstance = {
as: (type) => {

@@ -69,5 +76,18 @@ if (constructor) {

this.instances.set(type, instance);
asType = type;
return registeredInstance;
},
enrich: (enrichCallback) => {
if (asType) {
this.enrichCallbacks.delete(instance);
this.enrichCallbacks.set(asType, enrichCallback);
}
else {
this.enrichCallbacks.set(instance, enrichCallback);
}
return registeredInstance;
},
};
}
return registeredInstance;
};
/** instantiate (or get instance for singleton) by class */

@@ -77,3 +97,8 @@ resolve(type, ...args) {

try {
return this.internalResolve(type, trace, ...args);
const resolvedInstance = this.internalResolve(type, trace, ...args);
const enrichCallback = this.enrichCallbacks.get(type);
if (typeof enrichCallback === 'function') {
return enrichCallback(resolvedInstance);
}
return resolvedInstance;
}

@@ -80,0 +105,0 @@ catch (error) {

{
"type": "module",
"version": "4.0.1"
"version": "4.1.0"
}
{
"name": "cheap-di",
"version": "4.1.0",
"description": "TypeScript dependency injection like Autofac in .Net",
"version": "4.0.1",
"type": "module",
"repository": {

@@ -18,4 +19,2 @@ "type": "git",

],
"main": "./cjs/index.js",
"types": "./types/index.d.ts",
"files": [

@@ -29,3 +28,5 @@ "cjs/**/*",

],
"module": "./esm/index.js"
"main": "./cjs/index.js",
"module": "./esm/index.js",
"types": "./types/index.d.ts"
}

@@ -269,2 +269,25 @@ # cheap-di

You may wrap resolved instance with Proxy or do something else.
For example if you want to log called methods of Api.
```ts
import { container } from 'cheap-di';
class Api {
async load() {
// ...
}
}
container
.registerImplementation(Api)
.enrich((api) => {
return new Proxy(api, {
get(instance, propertyName) {
console.log('Request to', propertyName.toString());
return instance[propertyName as keyof typeof instance];
},
});
});
```
#### <a name="register-instance"></a> registerInstance

@@ -271,0 +294,0 @@

@@ -6,23 +6,8 @@ import { AbstractConstructor, Constructor, Container, IHaveDependencies, IHaveInstances, IHaveSingletons, ImplementationType, RegistrationType } from './types.js';

dependencies: Map<RegistrationType<any>, ImplementationType<any> | object>;
enrichCallbacks: Map<RegistrationType<any> | object, (instance: any) => any>;
constructor();
/** register implementation class */
registerImplementation<TInstance>(implementationType: Constructor<TInstance>): {
/** as super class */
as: <TBase extends Partial<TInstance>>(type: RegistrationType<TBase>) => {
/** add parameters that will be passed to the class constructor */
inject: (...injectionParams: any[]) => void;
};
/** as singleton (optionally super class) */
asSingleton: <TBase_1 extends Partial<TInstance>>(type?: RegistrationType<TBase_1> | undefined) => {
/** add parameters that will be passed to the class constructor */
inject: (...injectionParams: any[]) => void;
};
/** add parameters that will be passed to the class constructor */
inject: (...injectionParams: any[]) => void;
};
registerImplementation: Container['registerImplementation'];
/** register any object as its constructor */
registerInstance<TInstance extends object>(instance: TInstance): {
/** or register the object as any class */
as: <TBase extends Partial<TInstance>>(type: RegistrationType<TBase>) => void;
};
registerInstance: Container['registerInstance'];
/** instantiate (or get instance for singleton) by class */

@@ -29,0 +14,0 @@ resolve<TInstance>(type: Constructor<TInstance> | AbstractConstructor<TInstance>, ...args: any[]): TInstance | undefined;

@@ -22,19 +22,27 @@ import { cheapDiSymbol } from './cheapDiSymbol.js';

type RegistrationType<TInstance> = Constructor<TInstance> | AbstractConstructor<TInstance>;
interface WithInjectionParams {
type AsBase<TClass, ReturnType> = <TBase extends Partial<TClass>>(type: RegistrationType<TBase>) => ReturnType;
type AsSingleton<TClass, ReturnType> = <TBase extends Partial<TClass>>(type?: RegistrationType<TBase>) => ReturnType;
type Enrich<TInstance, ReturnType> = <EnrichedInstance extends TInstance>(enrichCallback: (instance: TInstance) => EnrichedInstance) => ReturnType;
type Inject<ReturnType> = (...injectionParams: any[]) => ReturnType;
interface RegisteredImplementation<TClass> {
/** as super class */
as: AsBase<TClass, this>;
/** as singleton (optionally super class) */
asSingleton: AsSingleton<TClass, this>;
/** enrich instance when it will be resolved, example, if you want to wrap instance with Proxy */
enrich: Enrich<TClass, this>;
/** add parameters that will be passed to the class constructor */
inject: (...injectionParams: any[]) => void;
inject: Inject<this>;
}
interface RegisteredInstance<TInstance> {
/** or register the object as any class */
as: AsBase<TInstance, this>;
/** enrich instance when it will be resolved, example, if you want to wrap instance with Proxy */
enrich: Enrich<TInstance, this>;
}
interface DependencyRegistrator<RegisterTypeExtension = object, RegisterInstanceExtension = object> {
/** register implementation class */
registerImplementation: <TClass>(implementationType: Constructor<TClass>) => {
/** as super class */
as: <TBase extends Partial<TClass>>(type: RegistrationType<TBase>) => WithInjectionParams;
/** as singleton (optionally super class) */
asSingleton: <TBase extends Partial<TClass>>(type?: RegistrationType<TBase>) => WithInjectionParams;
} & WithInjectionParams & RegisterTypeExtension;
registerImplementation: <TClass>(implementationType: Constructor<TClass>) => RegisteredImplementation<TClass> & RegisterTypeExtension;
/** register any object as it constructor */
registerInstance: <TInstance extends object>(instance: TInstance) => {
/** or register the object as any class */
as: <TBase extends Partial<TInstance>>(type: RegistrationType<TBase>) => void;
} & RegisterInstanceExtension;
registerInstance: <TInstance extends object>(instance: TInstance) => RegisteredInstance<TInstance> & RegisterInstanceExtension;
}

@@ -59,2 +67,2 @@ type Resolver = <TInstance>(type: Constructor<TInstance> | AbstractConstructor<TInstance>, ...args: any[]) => TInstance | undefined;

}
export type { AbstractConstructor, Constructor, Container, Dependency, DependencyRegistrator, DependencyResolver, DiMetadata, IHaveDependencies, IHaveInstances, IHaveSingletons, ImplementationType, RegistrationType, SomeDependency, WithInjectionParams, };
export type { AbstractConstructor, Constructor, Container, Dependency, DependencyRegistrator, DependencyResolver, DiMetadata, IHaveDependencies, IHaveInstances, IHaveSingletons, ImplementationType, RegistrationType, SomeDependency, RegisteredImplementation, RegisteredInstance, AsBase, AsSingleton, Enrich, Inject, };

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

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