
Security News
Feross on TBPN: How North Korea Hijacked Axios
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.
@typemon/di
Advanced tools
Dependency Injection for TypeScript
The development experience with OOP is fantastic. But there is a dark side. One of them is dependency management. Most solves the problem by injecting dependencies through the IoC Container. We have tried and tried many libraries that have implemented functions but could not be satisfied. So we made our own style. Although the function is simple, I hope it helps. I got a lot of inspiration from the dependency injection structure of the Angular :heart:.
This library uses
reflect-metadata.
$ npm install @typemon/di
import {
/* Injector classes */
Injector, AsyncInjector,
ResolutionStrategy,
/* Decorators */
Injectable, Inject, Optional, Self, SkipSelf,
/* Injection token class */
InjectionToken,
/* Providers */
Provider, SyncProvider, AsyncProvider,
ValueProvider,
FactoryProvider,
AsyncFactoryProvider,
ClassProvider,
ConstructorProvider,
ExistingProvider,
/* error classes */
EmptyStringIdentifierError,
ProviderNotFoundError
} from '@typemon/di';
The following options must be set to
true.
experimentalDecoratorsemitDecoratorMetadataclass dependencies that the injector manages must use the Injectable decorator.injection tokens and decorators as needed.const MONSTER_NAME_TOKEN: InjectionToken<string> = new InjectionToken('Monster.Name');
const MONSTER_LEVEL_TOKEN: InjectionToken<number> = new InjectionToken('Monster.Level');
@Injectable()
class Monster {
public constructor(
@Inject(MONSTER_NAME_TOKEN) public readonly name: string,
@Inject(MONSTER_LEVEL_TOKEN) @Optional() public readonly level: number = 1
) { }
public eat(): void {
console.log('Pet: Yum Yum.');
}
public sleep(): void {
console.log('Pet: zzZ zzZ.');
}
}
const ADVENTURER_NAME_TOKEN: InjectionToken<string> = new InjectionToken('Adventurer.Name');
const ADVENTURER_LEVEL_TOKEN: InjectionToken<number> = new InjectionToken('Adventurer.Level');
const ADVENTURER_ITEMS_TOKEN: InjectionToken<string> = new InjectionToken('Adventurer.Items');
@Injectable()
class Adventurer {
public constructor(
@Inject(ADVENTURER_NAME_TOKEN) public readonly name: string,
@Inject(ADVENTURER_LEVEL_TOKEN) @Optional() public readonly level: number = 1,
@Inject(ADVENTURER_ITEMS_TOKEN) public readonly items: ReadonlyArray<string>,
public readonly pet: Monster
) { }
public eat(): void {
console.log('Adventurer: Yum Yum.');
}
public sleepWithPet(): void {
this.pet.sleep();
console.log('Adventurer: zzZ zzZ.');
}
}
create static method to create the injector.resolution strategy.const injector: Injector = Injector.create([
Provider.useValue({
identifier: MONSTER_NAME_TOKEN,
value: 'Typemon'
}),
Provider.use(Monster)
]);
const asyncInjector: AsyncInjector = AsyncInjector.create(
[
Provider.useFactory({
identifier: ADVENTURER_NAME_TOKEN,
callback: (pet: Monster): string => `${pet.name}'s Master`,
dependencies: [Monster]
}),
Provider.useAsyncFactory({
identifier: ADVENTURER_LEVEL_TOKEN,
callback: (pet: Monster, name: string): Promise<number> => {
return new Promise((resolve: (level: number) => void): void => {
setTimeout((): void => resolve(pet.level * name.length), 3000);
});
},
dependencies: [Monster, ADVENTURER_NAME_TOKEN]
}),
Provider.useValue({
identifier: ADVENTURER_ITEMS_TOKEN,
value: 'sword',
multiple: true
}),
Provider.useAsyncFactory({
identifier: ADVENTURER_ITEMS_TOKEN,
callback: async (level: number): Promise<string> => level >= 10 ? 'magic-boots' : 'boots',
dependencies: [ADVENTURER_LEVEL_TOKEN],
multiple: true
}),
Provider.use(Adventurer)
],
{
parent: injector,
resolutionStrategy: ResolutionStrategy.Lazy
}
);
cached and the cached value is returned in the next request.const monster: Monster = injector.get(Monster);
const adventurer: Adventurer = await asyncInjector.get(Adventurer);
adventurer.pet === monster; // true
adventurer.eat(); // Adventurer: Yum Yum.
adventurer.sleepWithPet(); // Pet: zzZ zzZ. / Adventurer: zzZ zzZ.
console.log(monster, adventurer);
Monster { name: 'Typemon', level: 1 }
Adventurer {
name: 'Typemon\'s Master',
level: 16,
items: [ 'sword', 'magic-boots' ],
pet: Monster { name: 'Typemon', level: 1 } }
}
4.0.0
release: version 4.0.0
FAQs
Dependency Injection for TypeScript
We found that @typemon/di demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.

Security News
OpenSSF has issued a high-severity advisory warning open source developers of an active Slack-based campaign using impersonation to deliver malware.

Research
/Security News
Malicious packages published to npm, PyPI, Go Modules, crates.io, and Packagist impersonate developer tooling to fetch staged malware, steal credentials and wallets, and enable remote access.