Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
dependency-injection-cat
Advanced tools
![npm](https://img.shields.io/npm/v/dependency-injection-cat?style=flat) # dependency-injection-cat
Dependency Injection Cat is a TypeScript-only library which allows you to implement the Dependency Inversion pattern with Dependency Injection
Yarn
yarn add dependency-injection-cat
NPM
npm install dependency-injection-cat
interface TransformerConfig {
diConfigPattern?: string; // Glob pattern, default value = '**/*.diconfig.ts'
ignorePatterns?: string[]; // Array of Glob patterns, default value = ['**/node_modules/**']
}
Dependency Injection Cat supports transpileOnly mode for faster builds! More Info
With Webpack, You can use any TypeScript-related loader that supports custom transformers, e.g. awesome-typescript-loader or ts-loader
const dependencyInjectionCatTransformer = require('dependency-injection-cat/transformer').default;
module.exports = {
// ...
module: {
rules: [
{
test: /\.ts$/,
loader: 'ts-loader', // or 'awesome-typescript-loader'
options: {
transpileOnly: true, // If set transpileOnly: true, you're loosing TypeChecking
getCustomTransformers: program => ({
before: [
dependencyInjectionCatTransformer(program)
]
})
}
}
]
}
};
before: [
dependencyInjectionCatTransformer(program, {
diConfigPattern: '**/config/**/*.diconfig.ts'
})
]
Check out ttypescript's README for more information
{
"compilerOptions": {
"plugins": [
{
"transform": "dependency-injection-cat/transformer"
}
]
}
}
{
"compilerOptions": {
"plugins": [
{
"transform": "dependency-injection-cat/transformer",
"diConfigPattern": "**/config/**/*.diconfig.ts"
}
]
}
}
// requesters.diconfig.ts
import { Bean, Qualifier } from 'dependency-injection-cat';
import { IRequester } from '../IRequester';
import { Requester } from '../Requester';
import { ILogger } from '../ILogger';
export class Requesters {
@Bean
requester(logger: ILogger): IRequester {
return new Requester(logger);
}
//or
requester = Bean<IRequester>(Requester);
}
// loggers.diconfig.ts
import { Bean } from 'dependency-injection-cat';
import { ILogger } from '../ILogger';
import { Logger } from '../Logger';
export class Loggers {
@Bean
logger(): ILogger {
return new Logger();
}
//or
@Bean
logger = Bean<ILogger>(Logger);
}
// main.ts
import { container } from 'dependency-injection-cat';
import { IRequester } from './IRequester';
const requester = container.get<IRequester>();
requester.makeRequest();
import { Bean } from 'dependency-injection-cat';
class SuperClass {
//Without any dependencies
@Bean
someMethod(): Interface {
return new ImplementationOfInterface();
}
//With Bean dependencies
@Bean
someMethod(
dependency1: InterfaceOfDependency1,
dependency2: InterfaceOfDependency2,
): Interface {
return new ImplementationOfInterface(dependency1, dependency2);
}
//With Bean configuration
@Bean({ qualifier: 'someCoolImpl', scope: 'prototype' })
someMethod(
dependency1: InterfaceOfDependency1,
dependency2: InterfaceOfDependency2,
): Interface {
return new ImplementationOfInterface(dependency1, dependency2);
}
//Beans do not support arrow-functions methods, this will throw an error
@Bean
someMethod = (): Interface => new ImplementationOfInterface();
//Beans should have complex types, this will throw an error
@Bean
someMethod(): number | string | any {
return new ImplementationOfInterface();
}
//Should not have cyclyc dependencies, this will throw an error
@Bean
someMethod(
dependency1: InterfaceOfDependency1, //Cyclic dependency
): Interface {
return new ImplementationOfInterface(dependency1);
}
@Bean
someMethod2(
dependency1: Interface, //Cyclic dependency
): InterfaceOfDependency1 {
return new ImplementationOfDependency(dependency1);
}
}
import { Bean } from 'dependency-injection-cat';
class SuperClass {
//If you don't need to pass specific dependencies in Bean, it will resolve all dependencies automatically
someBeanProperty = Bean<Interface>(ImplementationOfInterface);
//With Bean configuration
//First argument in Bean should always be implementation of interface, second is configuration object
//When using this syntax, implementation should be a class
//You should pass Bean type in generic
someBeanProperty = Bean<Interface>(ImplementationOfInterface, { qualifier: 'someCoolImpl' });
}
interface BeanConfiguration {
//By default all beans are singleton, if you will set scope 'prototype' Bean will no longer be a singleton
scope?: 'prototype' | 'singleton';
//Read about Qualifiers and their rules below
qualifier?: string;
}
In fact, Qualifier it's just a name of Bean You can use it, if you have a few different implementations of interface
import { Bean, Qualifier } from 'dependency-injection-cat';
class SuperClass {
//Correct example
@Bean
someMethod(
@Qualifier('someQualifier') dependency1: InterfaceOfDependency1,
): Interface {
return new ImplementationOfInterface();
}
}
//Wrong examples
import { Bean, Qualifier } from 'dependency-injection-cat';
const superQualifierName = 'superQualifierNameValue';
class SuperClass {
@Bean
someMethod(
@Qualifier(superQualifierName) dependency1: InterfaceOfDependency1,
): Interface {
return new ImplementationOfInterface();
}
@Bean
someMethod(
@Qualifier('') dependency1: InterfaceOfDependency1,
): Interface {
return new ImplementationOfInterface();
}
}
Container has only one method "get"
//Any TypeScript file in project
import { container } from 'dependency-injection-cat';
//Without Qualifier
const someBean = container.get<Interface>();
//With Qualifier
const someBean = container.get<Interface>('someQualifier');
This project is under the MIT License
FAQs
DI Cat is a truly clean DI-container, which allows you not to pollute your business logic with decorators from DI/IOC libraries!
The npm package dependency-injection-cat receives a total of 17 weekly downloads. As such, dependency-injection-cat popularity was classified as not popular.
We found that dependency-injection-cat 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
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.