Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
dependency-injection-cat
Advanced tools
![npm](https://img.shields.io/npm/v/dependency-injection-cat?style=flat) Dependency Injection Cat is a TypeScript-only library which allows you to implement the Dependency Inversion pattern with Dependency Injection <!-- toc --> - [Installation](#in
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
//If you're using ttypescript
compiler: 'ttypescript',
//If you don't use ttypescript, you should pass transformer
getCustomTransformers: program => ({
before: [
dependencyInjectionCatTransformer(program),
],
}),
},
},
],
},
};
before: [
dependencyInjectionCatTransformer(program, {
diConfigPattern: '**/config/**/*.diconfig.ts'
})
]
Check out ttypescript's README for more information
tsconfig.json
{
"compilerOptions": {
"plugins": [
{
"transform": "dependency-injection-cat/transformer"
}
]
}
}
With custom options
{
"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();
Beans support 2 kinds of syntax
First
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);
}
}
Second
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
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
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.