Skip to content

Commit a39a8de

Browse files
refactor: re-sturcture project and update file naming
1 parent cc97ee1 commit a39a8de

26 files changed

+97
-91
lines changed

src/ContainerInstance.ts renamed to src/container-instance.class.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { Container } from './Container';
2-
import { MissingProvidedServiceTypeError } from './error/MissingProvidedServiceTypeError';
3-
import { ServiceNotFoundError } from './error/ServiceNotFoundError';
4-
import { Token } from './Token';
5-
import { ObjectType } from './types/ObjectType';
6-
import { ServiceIdentifier } from './types/ServiceIdentifier';
7-
import { ServiceMetadata } from './types/ServiceMetadata';
1+
import { Container } from './container.class';
2+
import { MissingProvidedServiceTypeError } from './error/missing-provided-service-type.error';
3+
import { ServiceNotFoundError } from './error/service-not-found.error';
4+
import { Token } from './token.class';
5+
import { Constructable } from './types/constructable.type';
6+
import { ServiceIdentifier } from './types/service-identifier.type';
7+
import { ServiceMetadata } from './interfaces/service-metadata.interface.';
88

99
/**
1010
* TypeDI can have multiple containers.
@@ -45,7 +45,7 @@ export class ContainerInstance {
4545
* Checks if the service with given name or type is registered service container.
4646
* Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
4747
*/
48-
has<T>(type: ObjectType<T>): boolean;
48+
has<T>(type: Constructable<T>): boolean;
4949

5050
/**
5151
* Checks if the service with given name or type is registered service container.
@@ -71,7 +71,7 @@ export class ContainerInstance {
7171
* Retrieves the service with given name or type from the service container.
7272
* Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
7373
*/
74-
get<T>(type: ObjectType<T>): T;
74+
get<T>(type: Constructable<T>): T;
7575

7676
/**
7777
* Retrieves the service with given name or type from the service container.

src/Container.ts renamed to src/container.class.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { ContainerInstance } from './ContainerInstance';
2-
import { Token } from './Token';
3-
import { Handler } from './types/Handler';
4-
import { ObjectType } from './types/ObjectType';
5-
import { ServiceIdentifier } from './types/ServiceIdentifier';
6-
import { ServiceMetadata } from './types/ServiceMetadata';
1+
import { ContainerInstance } from './container-instance.class';
2+
import { Token } from './token.class';
3+
import { Handler } from './interfaces/handler.interface';
4+
import { Constructable } from './types/constructable.type';
5+
import { ServiceIdentifier } from './types/service-identifier.type';
6+
import { ServiceMetadata } from './interfaces/service-metadata.interface.';
77

88
/**
99
* Service container.
@@ -51,7 +51,7 @@ export class Container {
5151
* Checks if the service with given name or type is registered service container.
5252
* Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
5353
*/
54-
static has<T>(type: ObjectType<T>): boolean;
54+
static has<T>(type: Constructable<T>): boolean;
5555

5656
/**
5757
* Checks if the service with given name or type is registered service container.
@@ -77,7 +77,7 @@ export class Container {
7777
* Retrieves the service with given name or type from the service container.
7878
* Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
7979
*/
80-
static get<T>(type: ObjectType<T>): T;
80+
static get<T>(type: Constructable<T>): T;
8181

8282
/**
8383
* Retrieves the service with given name or type from the service container.

src/decorators/InjectMany.ts renamed to src/decorators/inject-many.decorator.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Container } from '../Container';
2-
import { Token } from '../Token';
3-
import { CannotInjectError } from '../error/CannotInjectError';
1+
import { Container } from '../container.class';
2+
import { Token } from '../token.class';
3+
import { CannotInjectValueError } from '../error/cannot-inject-value.error';
44

55
/**
66
* Injects a service into a class property or constructor parameter.
@@ -38,7 +38,7 @@ export function InjectMany(typeOrName?: ((type?: any) => Function) | string | To
3838
identifier = typeOrName();
3939
}
4040

41-
if (identifier === Object) throw new CannotInjectError(target, propertyName);
41+
if (identifier === Object) throw new CannotInjectValueError(target, propertyName);
4242

4343
return containerInstance.getMany<any>(identifier);
4444
},

src/decorators/Inject.ts renamed to src/decorators/inject.decorator.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Container } from '../Container';
2-
import { Token } from '../Token';
3-
import { CannotInjectError } from '../error/CannotInjectError';
1+
import { Container } from '../container.class';
2+
import { Token } from '../token.class';
3+
import { CannotInjectValueError } from '../error/cannot-inject-value.error';
44

55
/**
66
* Injects a service into a class property or constructor parameter.
@@ -38,7 +38,7 @@ export function Inject(typeOrName?: ((type?: any) => Function) | string | Token<
3838
identifier = typeOrName();
3939
}
4040

41-
if (identifier === Object) throw new CannotInjectError(target, propertyName);
41+
if (identifier === Object) throw new CannotInjectValueError(target, propertyName);
4242

4343
return containerInstance.get<any>(identifier);
4444
},

src/decorators/Service.ts renamed to src/decorators/service.decorator.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Container } from '../Container';
2-
import { ContainerInstance } from '../ContainerInstance';
3-
import { Token } from '../Token';
4-
import { ServiceMetadata } from '../types/ServiceMetadata';
5-
import { ServiceOptions } from '../types/ServiceOptions';
1+
import { Container } from '../container.class';
2+
import { ContainerInstance } from '../container-instance.class';
3+
import { Token } from '../token.class';
4+
import { ServiceMetadata } from '../interfaces/service-metadata.interface.';
5+
import { ServiceOptions } from '../interfaces/service-options.interface';
66

77
export type ObjectType<T1> = { new (...args: any[]): T1 } | { service: T1 } | Token<T1>;
88

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/**
22
* Thrown when DI cannot inject value into property decorated by @Inject decorator.
33
*/
4-
export class CannotInjectError extends Error {
5-
name = 'ServiceNotFoundError';
4+
export class CannotInjectValueError extends Error {
5+
name = 'CannotInjectValueError';
66

77
constructor(target: Object, propertyName: string) {
88
super(
99
`Cannot inject value into "${target.constructor.name}.${propertyName}". ` +
1010
`Please make sure you setup reflect-metadata properly and you don't use interfaces without service tokens as injection value.`
1111
);
12-
Object.setPrototypeOf(this, CannotInjectError.prototype);
12+
Object.setPrototypeOf(this, CannotInjectValueError.prototype);
1313
}
1414
}

src/error/MissingProvidedServiceTypeError.ts renamed to src/error/missing-provided-service-type.error.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Thrown when service is registered without type.
33
*/
44
export class MissingProvidedServiceTypeError extends Error {
5-
name = 'ServiceNotFoundError';
5+
name = 'MissingProvidedServiceTypeError';
66

77
constructor(identifier: any) {
88
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions

src/error/ServiceNotFoundError.ts renamed to src/error/service-not-found.error.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { ServiceIdentifier } from '../types/ServiceIdentifier';
2-
import { Token } from '../Token';
1+
import { ServiceIdentifier } from '../types/service-identifier.type';
2+
import { Token } from '../token.class';
33

44
/**
55
* Thrown when requested service was not found.

src/index.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { Container } from './Container';
1+
import { Container } from './container.class';
22

3-
export * from './decorators/Service';
4-
export * from './decorators/Inject';
5-
export * from './decorators/InjectMany';
6-
export { Container } from './Container';
7-
export { ContainerInstance } from './ContainerInstance';
8-
export { Token } from './Token';
9-
export { Handler } from './types/Handler';
10-
export { ServiceOptions } from './types/ServiceOptions';
11-
export { ServiceIdentifier } from './types/ServiceIdentifier';
12-
export { ServiceMetadata } from './types/ServiceMetadata';
13-
export { ObjectType } from './types/ObjectType';
3+
export * from './decorators/service.decorator';
4+
export * from './decorators/inject.decorator';
5+
export * from './decorators/inject-many.decorator';
6+
export { Container } from './container.class';
7+
export { ContainerInstance } from './container-instance.class';
8+
export { Token } from './token.class';
9+
export { Handler } from './interfaces/handler.interface';
10+
export { ServiceOptions } from './interfaces/service-options.interface';
11+
export { ServiceIdentifier } from './types/service-identifier.type';
12+
export { ServiceMetadata } from './interfaces/service-metadata.interface.';
13+
export { Constructable as ObjectType } from './types/constructable.type';
1414

1515
export default Container;

src/types/Handler.ts renamed to src/interfaces/handler.interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ContainerInstance } from '../ContainerInstance';
1+
import { ContainerInstance } from '../container-instance.class';
22

33
/**
44
* Used to register special "handler" which will be executed on a service class during its initialization.

0 commit comments

Comments
 (0)