Skip to content

Commit f7676bd

Browse files
author
Umed Khudoiberdiev
committed
added Container.has method
1 parent 5fb5ffc commit f7676bd

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 0.6.1
4+
5+
* added `Container.has` method
6+
37
## 0.6.0
48

59
* added multiple containers support

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typedi",
3-
"version": "0.6.0",
3+
"version": "0.6.1",
44
"description": "Dependency injection for TypeScript",
55
"license": "MIT",
66
"readmeFilename": "README.md",

src/Container.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,32 @@ export class Container {
4949
return container;
5050
}
5151

52+
/**
53+
* Checks if the service with given name or type is registered service container.
54+
* Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
55+
*/
56+
static has<T>(type: ObjectType<T>): boolean;
57+
58+
/**
59+
* Checks if the service with given name or type is registered service container.
60+
* Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
61+
*/
62+
static has<T>(id: string): boolean;
63+
64+
/**
65+
* Checks if the service with given name or type is registered service container.
66+
* Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
67+
*/
68+
static has<T>(id: Token<T>): boolean;
69+
70+
/**
71+
* Checks if the service with given name or type is registered service container.
72+
* Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
73+
*/
74+
static has<T>(identifier: ServiceIdentifier): boolean {
75+
return this.globalInstance.has(identifier as any);
76+
}
77+
5278
/**
5379
* Retrieves the service with given name or type from the service container.
5480
* Optionally, parameters can be passed in case if instance is initialized in the container for the first time.

src/ContainerInstance.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,32 @@ export class ContainerInstance {
4242
// Public Methods
4343
// -------------------------------------------------------------------------
4444

45+
/**
46+
* Checks if the service with given name or type is registered service container.
47+
* Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
48+
*/
49+
has<T>(type: ObjectType<T>): boolean;
50+
51+
/**
52+
* Checks if the service with given name or type is registered service container.
53+
* Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
54+
*/
55+
has<T>(id: string): boolean;
56+
57+
/**
58+
* Checks if the service with given name or type is registered service container.
59+
* Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
60+
*/
61+
has<T>(id: Token<T>): boolean;
62+
63+
/**
64+
* Checks if the service with given name or type is registered service container.
65+
* Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
66+
*/
67+
has<T>(identifier: ServiceIdentifier): boolean {
68+
return !!this.findService(identifier);
69+
}
70+
4571
/**
4672
* Retrieves the service with given name or type from the service container.
4773
* Optionally, parameters can be passed in case if instance is initialized in the container for the first time.

0 commit comments

Comments
 (0)