diff --git a/README.md b/README.md index 7f5a492..929809b 100644 --- a/README.md +++ b/README.md @@ -57,13 +57,17 @@ One of the solutions is to use Dependency Injection. First, we need to declare our service (before we need it, so it's okay at startup). ```javascript -injector.registerBean('clockService', ClockService); +injector.registerBean(ClockService); +// with a custom id +injector.registerBean(ClockService, { id: 'myClockService' }); ``` And every time we need it, we can simply call it using its name. ```javascript -injector.wire('clockService').getCurrentTime(); +injector.wire(ClockService).getCurrentTime(); +// by id +injector.wire('myClockService').getCurrentTime(); ``` Now, let's say you have a service that prints the current time in the terminal. @@ -72,7 +76,7 @@ We can wire the service with the `clockService` directly in the class declaratio ```javascript class TimePrintService { - private clockService = injector.autoWire('clockService', (b) => (this.clockService = b)); + private clockService = injector.autoWire(ClockService, (b) => (this.clockService = b)); public printCurrentTime() { console.log("Current time: ", clockService.getCurrentTime()) @@ -97,12 +101,12 @@ You can simply do it this way: ```javascript if (COUNTRY === 'France') { - injector.registerBean('clockService', FranceClockService); + injector.registerBean(FranceClockService, { id: 'myClockService' }); } else { - injector.registerBean('clockService', EnglandClockService); + injector.registerBean(EnglandClockService, { id: 'myClockService' }); } -injector.wire('clockService').getCurrentTime(); +injector.wire('myClockService').getCurrentTime(); ``` ## Contributing diff --git a/package-lock.json b/package-lock.json index e0dd82e..29d821c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "wire-dependency-injection", - "version": "1.0.8", + "version": "1.0.9", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "wire-dependency-injection", - "version": "1.0.8", + "version": "1.0.9", "license": "Apache-2.0", "devDependencies": { "@types/node": "^20.4.2", diff --git a/package.json b/package.json index 9fca960..6c0ff66 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wire-dependency-injection", - "version": "1.0.8", + "version": "1.0.9", "description": "An easy to use JavaScript Dependency Injection Library", "main": "dist/index.js", "type": "module", diff --git a/src/Container.ts b/src/Container.ts index b21fc25..5825639 100644 --- a/src/Container.ts +++ b/src/Container.ts @@ -76,12 +76,12 @@ export default class Container { * @returns The bean instance. * @throws DependencyNotFoundError if the specified bean does not exist in the container. */ - public getBean(id: string | ClassType) { + public getBean(id: string | T) { const bid = parseId(id); if (!this.haveBean(bid)) { throw new DependencyNotFoundError(); } - return this.beans.find((bean) => bean.getId() === id); + return this.beans.find((bean) => bean.getId() === id) as Bean; } /** diff --git a/src/DependencyInjector.ts b/src/DependencyInjector.ts index e378749..db3671e 100644 --- a/src/DependencyInjector.ts +++ b/src/DependencyInjector.ts @@ -1,6 +1,5 @@ import { AutowiredDescriptor, - BeanType, ClassType, ContainerIdType, RegisterBeanArgs, @@ -155,15 +154,15 @@ export default class DependencyInjector { * @throws ContainerNotFoundError if the specified container does not exist. * @throws DependencyNotFoundError if the specified bean does not exist in the container. */ - public getBean( - id: string | ClassType, + public getBean( + id: string | T, containerId: string = DEFAULT_CONTAINER_ID ) { const bid = parseId(id); if (!this.haveContainer(containerId)) { throw new ContainerNotFoundError(); } - return this.getContainer(containerId)?.getBean(bid); + return this.getContainer(containerId)?.getBean(bid) as Bean; } /** @@ -241,21 +240,21 @@ export default class DependencyInjector { * @param containerId - The ID of the container to retrieve the bean from. * @returns The autowired instance, or `undefined` if the bean is not found. */ - public autoWire( - id: string | ClassType, - descriptor: AutowiredDescriptor, + public autoWire( + id: string | T, + descriptor: AutowiredDescriptor>, containerId: string = DEFAULT_CONTAINER_ID ) { const bid = parseId(id); if (this.haveBean(bid, containerId)) { const instance = this.wire(bid, containerId); - descriptor(instance as unknown as undefined); - return instance as undefined; + descriptor(instance as unknown as InstanceType); + return instance as InstanceType; } this.autoWireQueue.push( new AutoWireQueueElement(bid, containerId, descriptor) ); - return undefined; + return undefined as InstanceType; } /** @@ -267,7 +266,7 @@ export default class DependencyInjector { * @throws ContainerNotFoundError if the specified container does not exist. * @throws DependencyNotFoundError if the specified bean does not exist in the container. */ - public wire( + public wire( id: string | T, containerId: string = DEFAULT_CONTAINER_ID ) { @@ -282,10 +281,10 @@ export default class DependencyInjector { /** * Returns a Promise that resolves with the wired instance. * @param id - The ID or class of the bean to wait for AutoWire. - * @param containerId - The ID of the container to retrieve the bean from. Defaults to DEFAULT_CONTAINER_ID. + * @param containerId - The ID of the container to retrieve the bean from. * @returns A Promise that resolves with the wired instance of the bean. */ - public waitForWire( + public waitForWire( id: string | T, containerId: string = DEFAULT_CONTAINER_ID ) { diff --git a/src/types.ts b/src/types.ts index 95e7633..00c81c1 100644 --- a/src/types.ts +++ b/src/types.ts @@ -4,8 +4,9 @@ export type ClassType = new (...args: any) => any; export type BeanType = 'bean' | (string & NonNullable); export type ContainerIdType = 'default' | (string & NonNullable); -//we trick typescript and avoid errors by setting val type to undefined -export type AutowiredDescriptor = (val: undefined) => void; +export type AutowiredDescriptor< + T extends InstanceType = InstanceType, +> = (val: T) => unknown; export type RegisterBeanArgs = { id?: string;