Skip to content

Commit

Permalink
better typing and update of readme
Browse files Browse the repository at this point in the history
  • Loading branch information
leopoldhub committed Jul 15, 2023
1 parent cfc127d commit 6b6e68e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 26 deletions.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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())
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/Container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends ClassType = ClassType>(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<T>;
}

/**
Expand Down
25 changes: 12 additions & 13 deletions src/DependencyInjector.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
AutowiredDescriptor,
BeanType,
ClassType,
ContainerIdType,
RegisterBeanArgs,
Expand Down Expand Up @@ -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<T extends ClassType = ClassType>(
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<T>;
}

/**
Expand Down Expand Up @@ -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<T extends ClassType = ClassType>(
id: string | T,
descriptor: AutowiredDescriptor<InstanceType<T>>,
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<T>);
return instance as InstanceType<T>;
}
this.autoWireQueue.push(
new AutoWireQueueElement(bid, containerId, descriptor)
);
return undefined;
return undefined as InstanceType<T>;
}

/**
Expand All @@ -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<T extends ClassType>(
public wire<T extends ClassType = ClassType>(
id: string | T,
containerId: string = DEFAULT_CONTAINER_ID
) {
Expand All @@ -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<T extends ClassType>(
public waitForWire<T extends ClassType = ClassType>(
id: string | T,
containerId: string = DEFAULT_CONTAINER_ID
) {
Expand Down
5 changes: 3 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ export type ClassType = new (...args: any) => any;
export type BeanType = 'bean' | (string & NonNullable<unknown>);
export type ContainerIdType = 'default' | (string & NonNullable<unknown>);

//we trick typescript and avoid errors by setting val type to undefined
export type AutowiredDescriptor = (val: undefined) => void;
export type AutowiredDescriptor<
T extends InstanceType<ClassType> = InstanceType<ClassType>,
> = (val: T) => unknown;

export type RegisterBeanArgs = {
id?: string;
Expand Down

0 comments on commit 6b6e68e

Please sign in to comment.