1.3.1-alpha • Published 3 months ago

@sunacchi/simple-di v1.3.1-alpha

Weekly downloads
-
License
ISC
Repository
github
Last release
3 months ago

Simple-di

Yet another Simple dependency injection module for small, personal JS project.

npm install @sunacchi/simple-di

export { inject, injectable, singleton, MetadataScope, IContainer, getContainer } from "@sunacchi/simple-di";

Using @decorator

@singleton()
class Test {
	@inject("config")
	prop: number;
	constructor(
		@inject("injected") public param: Record<string, unknown>
	) {
		this.prop = 5;
	}
};
@injectable()
class Test {
	@inject("config")
	prop: number;
	constructor(
		@inject("injected") public param: Record<string, unknown>
	) {
		this.prop = 5;
	}
};

using Container class

export interface IContainer {
    /**
     * Register a new references in the container
     *
     * @param target
     * @param scope
     *
     * @returns {boolean} done or not
     */
    register<IN>(target: (new (...args: any[]) => IN), scope?: MetadataScope): boolean;
    register(name: string, value: any, scope?: MetadataScope): boolean;
    /**
     * Return an instances or create a new one
     *
     * @param target
     *
     * @returns {IN|OUT} instance
     */
    get<OUT>(target: string): OUT;
    get<IN>(target: IN | (new (...args: any[]) => IN)): IN;
    /**
     * Create a new instance of target and get the dependence params from the container using the dependencies list, if any.
     * This function only create new instances but not save the instances in the container.
     *
     * @param {new (...args: any[]) => IN} target
     * @param {Array<string>} dependencies
     *
     * @returns {IN} target instance
     */
	factory<IN>(callback: (container: IContainer) => IN): IN;
    factory<IN>(target: new (...args: any[]) => IN, dependencies?: Array<string>): IN;
    /**
     * Wipe out one or all references from the container.
     *
     * @param {string} key
     */
    clean(key?: string): void;
}
1.3.1-alpha

3 months ago

1.3.0-alpha

3 months ago

1.2.7

7 months ago

1.2.6

7 months ago

1.2.5

7 months ago

1.2.4

7 months ago

1.2.3

7 months ago

1.2.2

1 year ago

1.2.1

1 year ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago