navi-di 1.0.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/container/container.d.ts +92 -2
- package/dist/container/container.js +108 -3
- package/dist/container/registry.d.ts +40 -0
- package/dist/container/registry.js +40 -0
- package/dist/decorators/inject.d.ts +11 -0
- package/dist/decorators/inject.js +11 -0
- package/dist/decorators/service.d.ts +11 -0
- package/dist/decorators/service.js +1 -1
- package/dist/errors/circular-dependency-error.d.ts +8 -0
- package/dist/errors/circular-dependency-error.js +8 -0
- package/dist/errors/container-duplicated-error.d.ts +8 -0
- package/dist/errors/container-duplicated-error.js +8 -0
- package/dist/errors/default-container-id-error.d.ts +5 -0
- package/dist/errors/default-container-id-error.js +6 -1
- package/dist/errors/service-not-found-error.d.ts +8 -0
- package/dist/errors/service-not-found-error.js +8 -0
- package/dist/tokens/token.d.ts +22 -0
- package/dist/tokens/token.js +22 -0
- package/dist/types/constructable.d.ts +6 -0
- package/dist/types/container.d.ts +29 -0
- package/dist/types/container.js +3 -0
- package/dist/types/injection.d.ts +12 -0
- package/dist/types/injection.js +3 -0
- package/dist/types/service.d.ts +15 -0
- package/package.json +1 -1
|
@@ -1,14 +1,104 @@
|
|
|
1
1
|
import type { ContainerIdentifier, Metadata, ServiceIdentifier } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Resolves services and stores container-local bindings.
|
|
4
|
+
*
|
|
5
|
+
* Containers let you resolve registered services, override values for the
|
|
6
|
+
* current context, and isolate container-scoped services by container.
|
|
7
|
+
*/
|
|
2
8
|
export declare class Container {
|
|
9
|
+
/**
|
|
10
|
+
* The identifier of this container.
|
|
11
|
+
*
|
|
12
|
+
* The default container uses `'default'`, and named containers use the
|
|
13
|
+
* identifier they were created with.
|
|
14
|
+
*/
|
|
3
15
|
readonly id: ContainerIdentifier;
|
|
4
16
|
private metadataMap;
|
|
17
|
+
private bindingMap;
|
|
5
18
|
private resolving;
|
|
6
19
|
private resolvingPath;
|
|
7
20
|
constructor(id: ContainerIdentifier);
|
|
21
|
+
/**
|
|
22
|
+
* Returns the default container or a named container.
|
|
23
|
+
*
|
|
24
|
+
* Calling this method with the same identifier always returns the same
|
|
25
|
+
* container instance.
|
|
26
|
+
*
|
|
27
|
+
* @param id The container identifier. Omit this to use the default container.
|
|
28
|
+
* @returns The matching container instance.
|
|
29
|
+
*/
|
|
8
30
|
static of(id?: ContainerIdentifier): Container;
|
|
9
|
-
|
|
31
|
+
/**
|
|
32
|
+
* Registers service metadata in this container.
|
|
33
|
+
*
|
|
34
|
+
* This is a low-level API for manual registration when you are not using
|
|
35
|
+
* `@Service()`. Registering a `singleton` on a named container stores it in
|
|
36
|
+
* the default container so it can be shared across containers.
|
|
37
|
+
*
|
|
38
|
+
* @param metadata The service metadata to register.
|
|
39
|
+
* @returns The current container.
|
|
40
|
+
*/
|
|
41
|
+
register<T>(metadata: Metadata<T>): this;
|
|
42
|
+
/**
|
|
43
|
+
* Returns whether this container has a local registration for an identifier.
|
|
44
|
+
*
|
|
45
|
+
* This only checks registrations stored on the current container and does
|
|
46
|
+
* not indicate whether the identifier can be resolved through fallback.
|
|
47
|
+
*
|
|
48
|
+
* @param id The service identifier to check.
|
|
49
|
+
* @returns `true` when the current container has a local registration.
|
|
50
|
+
*/
|
|
10
51
|
has(id: ServiceIdentifier): boolean;
|
|
11
|
-
|
|
52
|
+
/**
|
|
53
|
+
* Binds a concrete value to this container.
|
|
54
|
+
*
|
|
55
|
+
* Bound values are returned as-is when the same identifier is requested from
|
|
56
|
+
* this container.
|
|
57
|
+
*
|
|
58
|
+
* @param id The service identifier to bind.
|
|
59
|
+
* @param value The value to return for that identifier.
|
|
60
|
+
* @returns The current container.
|
|
61
|
+
*/
|
|
62
|
+
set<T>(id: ServiceIdentifier<T>, value: T): this;
|
|
63
|
+
/**
|
|
64
|
+
* Removes a local binding or registration from this container.
|
|
65
|
+
*
|
|
66
|
+
* @param id The service identifier to remove.
|
|
67
|
+
* @returns The current container.
|
|
68
|
+
*/
|
|
69
|
+
remove(id: ServiceIdentifier): this;
|
|
70
|
+
/**
|
|
71
|
+
* Resets services stored in this container.
|
|
72
|
+
*
|
|
73
|
+
* Use `'value'` to clear cached instances while keeping registrations, or
|
|
74
|
+
* `'service'` to remove local registrations and bindings entirely.
|
|
75
|
+
*
|
|
76
|
+
* @param strategy The reset strategy to apply.
|
|
77
|
+
* @returns The current container.
|
|
78
|
+
*/
|
|
79
|
+
reset(strategy?: 'value' | 'service'): this;
|
|
80
|
+
/**
|
|
81
|
+
* Resolves a service from this container.
|
|
82
|
+
*
|
|
83
|
+
* If the current container does not have a local registration, named
|
|
84
|
+
* containers can continue resolution through the default container according
|
|
85
|
+
* to the service scope. Services are instantiated with `new Class()` and do
|
|
86
|
+
* not support constructor arguments.
|
|
87
|
+
*
|
|
88
|
+
* @param id The service identifier to resolve.
|
|
89
|
+
* @returns The resolved service instance or bound value.
|
|
90
|
+
* @throws {ServiceNotFoundError} If no service is registered for the identifier.
|
|
91
|
+
* @throws {CircularDependencyError} If the dependency graph contains a cycle.
|
|
92
|
+
*/
|
|
12
93
|
get<T>(id: ServiceIdentifier<T>): T;
|
|
13
94
|
private isDefault;
|
|
95
|
+
/**
|
|
96
|
+
* Resets a container by its identifier.
|
|
97
|
+
*
|
|
98
|
+
* @param containerId The container to reset.
|
|
99
|
+
* @param options Reset options.
|
|
100
|
+
*/
|
|
101
|
+
static reset(containerId: ContainerIdentifier, options?: {
|
|
102
|
+
strategy?: 'value' | 'service';
|
|
103
|
+
}): void;
|
|
14
104
|
}
|
|
@@ -1,14 +1,36 @@
|
|
|
1
1
|
import { CircularDependencyError, ServiceNotFoundError } from '../errors';
|
|
2
2
|
import { EMPTY_VALUE } from '../types';
|
|
3
3
|
import { ContainerRegistry } from './registry';
|
|
4
|
+
/**
|
|
5
|
+
* Resolves services and stores container-local bindings.
|
|
6
|
+
*
|
|
7
|
+
* Containers let you resolve registered services, override values for the
|
|
8
|
+
* current context, and isolate container-scoped services by container.
|
|
9
|
+
*/
|
|
4
10
|
export class Container {
|
|
11
|
+
/**
|
|
12
|
+
* The identifier of this container.
|
|
13
|
+
*
|
|
14
|
+
* The default container uses `'default'`, and named containers use the
|
|
15
|
+
* identifier they were created with.
|
|
16
|
+
*/
|
|
5
17
|
id;
|
|
6
18
|
metadataMap = new Map();
|
|
19
|
+
bindingMap = new Map();
|
|
7
20
|
resolving = new Set();
|
|
8
21
|
resolvingPath = [];
|
|
9
22
|
constructor(id) {
|
|
10
23
|
this.id = id;
|
|
11
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* Returns the default container or a named container.
|
|
27
|
+
*
|
|
28
|
+
* Calling this method with the same identifier always returns the same
|
|
29
|
+
* container instance.
|
|
30
|
+
*
|
|
31
|
+
* @param id The container identifier. Omit this to use the default container.
|
|
32
|
+
* @returns The matching container instance.
|
|
33
|
+
*/
|
|
12
34
|
static of(id = 'default') {
|
|
13
35
|
if (id === 'default') {
|
|
14
36
|
return ContainerRegistry.defaultContainer;
|
|
@@ -20,11 +42,21 @@ export class Container {
|
|
|
20
42
|
ContainerRegistry.registerContainer(container);
|
|
21
43
|
return container;
|
|
22
44
|
}
|
|
23
|
-
|
|
45
|
+
/**
|
|
46
|
+
* Registers service metadata in this container.
|
|
47
|
+
*
|
|
48
|
+
* This is a low-level API for manual registration when you are not using
|
|
49
|
+
* `@Service()`. Registering a `singleton` on a named container stores it in
|
|
50
|
+
* the default container so it can be shared across containers.
|
|
51
|
+
*
|
|
52
|
+
* @param metadata The service metadata to register.
|
|
53
|
+
* @returns The current container.
|
|
54
|
+
*/
|
|
55
|
+
register(metadata) {
|
|
24
56
|
if (metadata.scope === 'singleton' && !this.isDefault()) {
|
|
25
|
-
ContainerRegistry.defaultContainer.
|
|
57
|
+
ContainerRegistry.defaultContainer.register(metadata);
|
|
26
58
|
this.metadataMap.delete(metadata.id);
|
|
27
|
-
return;
|
|
59
|
+
return this;
|
|
28
60
|
}
|
|
29
61
|
const newMetadata = {
|
|
30
62
|
...metadata,
|
|
@@ -36,21 +68,84 @@ export class Container {
|
|
|
36
68
|
else {
|
|
37
69
|
this.metadataMap.set(newMetadata.id, newMetadata);
|
|
38
70
|
}
|
|
71
|
+
return this;
|
|
39
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* Returns whether this container has a local registration for an identifier.
|
|
75
|
+
*
|
|
76
|
+
* This only checks registrations stored on the current container and does
|
|
77
|
+
* not indicate whether the identifier can be resolved through fallback.
|
|
78
|
+
*
|
|
79
|
+
* @param id The service identifier to check.
|
|
80
|
+
* @returns `true` when the current container has a local registration.
|
|
81
|
+
*/
|
|
40
82
|
has(id) {
|
|
41
83
|
return this.metadataMap.has(id);
|
|
42
84
|
}
|
|
85
|
+
/**
|
|
86
|
+
* Binds a concrete value to this container.
|
|
87
|
+
*
|
|
88
|
+
* Bound values are returned as-is when the same identifier is requested from
|
|
89
|
+
* this container.
|
|
90
|
+
*
|
|
91
|
+
* @param id The service identifier to bind.
|
|
92
|
+
* @param value The value to return for that identifier.
|
|
93
|
+
* @returns The current container.
|
|
94
|
+
*/
|
|
95
|
+
set(id, value) {
|
|
96
|
+
this.bindingMap.set(id, value);
|
|
97
|
+
return this;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Removes a local binding or registration from this container.
|
|
101
|
+
*
|
|
102
|
+
* @param id The service identifier to remove.
|
|
103
|
+
* @returns The current container.
|
|
104
|
+
*/
|
|
105
|
+
remove(id) {
|
|
106
|
+
this.bindingMap.delete(id);
|
|
107
|
+
this.metadataMap.delete(id);
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Resets services stored in this container.
|
|
112
|
+
*
|
|
113
|
+
* Use `'value'` to clear cached instances while keeping registrations, or
|
|
114
|
+
* `'service'` to remove local registrations and bindings entirely.
|
|
115
|
+
*
|
|
116
|
+
* @param strategy The reset strategy to apply.
|
|
117
|
+
* @returns The current container.
|
|
118
|
+
*/
|
|
43
119
|
reset(strategy = 'value') {
|
|
44
120
|
if (strategy === 'value') {
|
|
45
121
|
this.metadataMap.forEach((metadata) => {
|
|
46
122
|
metadata.value = EMPTY_VALUE;
|
|
47
123
|
});
|
|
124
|
+
return this;
|
|
48
125
|
}
|
|
49
126
|
else {
|
|
127
|
+
this.bindingMap.clear();
|
|
50
128
|
this.metadataMap.clear();
|
|
129
|
+
return this;
|
|
51
130
|
}
|
|
52
131
|
}
|
|
132
|
+
/**
|
|
133
|
+
* Resolves a service from this container.
|
|
134
|
+
*
|
|
135
|
+
* If the current container does not have a local registration, named
|
|
136
|
+
* containers can continue resolution through the default container according
|
|
137
|
+
* to the service scope. Services are instantiated with `new Class()` and do
|
|
138
|
+
* not support constructor arguments.
|
|
139
|
+
*
|
|
140
|
+
* @param id The service identifier to resolve.
|
|
141
|
+
* @returns The resolved service instance or bound value.
|
|
142
|
+
* @throws {ServiceNotFoundError} If no service is registered for the identifier.
|
|
143
|
+
* @throws {CircularDependencyError} If the dependency graph contains a cycle.
|
|
144
|
+
*/
|
|
53
145
|
get(id) {
|
|
146
|
+
if (this.bindingMap.has(id)) {
|
|
147
|
+
return this.bindingMap.get(id);
|
|
148
|
+
}
|
|
54
149
|
let metadata = this.metadataMap.get(id);
|
|
55
150
|
if (!metadata && !this.isDefault()) {
|
|
56
151
|
const defaultMetadata = ContainerRegistry.defaultContainer.metadataMap.get(id);
|
|
@@ -105,4 +200,14 @@ export class Container {
|
|
|
105
200
|
isDefault() {
|
|
106
201
|
return this === ContainerRegistry.defaultContainer;
|
|
107
202
|
}
|
|
203
|
+
/**
|
|
204
|
+
* Resets a container by its identifier.
|
|
205
|
+
*
|
|
206
|
+
* @param containerId The container to reset.
|
|
207
|
+
* @param options Reset options.
|
|
208
|
+
*/
|
|
209
|
+
static reset(containerId, options) {
|
|
210
|
+
const container = ContainerRegistry.getContainer(containerId);
|
|
211
|
+
container?.reset(options?.strategy);
|
|
212
|
+
}
|
|
108
213
|
}
|
|
@@ -1,11 +1,51 @@
|
|
|
1
1
|
import type { ContainerIdentifier } from '../types';
|
|
2
2
|
import { Container } from './container';
|
|
3
|
+
/**
|
|
4
|
+
* Stores the default container and all named containers.
|
|
5
|
+
*
|
|
6
|
+
* This registry is the shared source of truth behind `Container.of()` and
|
|
7
|
+
* decorator-based service registration.
|
|
8
|
+
*
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
3
11
|
export declare class ContainerRegistry {
|
|
4
12
|
private static defaultContainerInstance?;
|
|
5
13
|
private static readonly containerMap;
|
|
14
|
+
/**
|
|
15
|
+
* Returns the canonical default container instance.
|
|
16
|
+
*
|
|
17
|
+
* The default container is created lazily the first time it is requested.
|
|
18
|
+
*/
|
|
6
19
|
static get defaultContainer(): Container;
|
|
20
|
+
/**
|
|
21
|
+
* Registers a named container in the shared registry.
|
|
22
|
+
*
|
|
23
|
+
* @param container The container to register.
|
|
24
|
+
* @throws {DefaultContainerIdError} If the container uses the reserved `default` id.
|
|
25
|
+
* @throws {ContainerDuplicatedError} If another container already uses the same id.
|
|
26
|
+
*/
|
|
7
27
|
static registerContainer(container: Container): void;
|
|
28
|
+
/**
|
|
29
|
+
* Returns the default container or a named container by identifier.
|
|
30
|
+
*
|
|
31
|
+
* @param id The container identifier to resolve.
|
|
32
|
+
* @returns The default container for `'default'`, or the matching named container if one exists.
|
|
33
|
+
*/
|
|
8
34
|
static getContainer(id: ContainerIdentifier): Container | undefined;
|
|
35
|
+
/**
|
|
36
|
+
* Returns whether a container exists for the given identifier.
|
|
37
|
+
*
|
|
38
|
+
* The reserved `default` identifier is always treated as present.
|
|
39
|
+
*
|
|
40
|
+
* @param id The container identifier to check.
|
|
41
|
+
* @returns `true` when the container exists in the registry.
|
|
42
|
+
*/
|
|
9
43
|
static hasContainer(id: ContainerIdentifier): boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Removes a named container from the registry.
|
|
46
|
+
*
|
|
47
|
+
* @param id The named container identifier to remove.
|
|
48
|
+
* @throws {DefaultContainerIdError} If the reserved `default` id is used.
|
|
49
|
+
*/
|
|
10
50
|
static removeContainer(id: ContainerIdentifier): void;
|
|
11
51
|
}
|
|
@@ -1,12 +1,32 @@
|
|
|
1
1
|
import { ContainerDuplicatedError, DefaultContainerIdError } from '../errors';
|
|
2
2
|
import { Container } from './container';
|
|
3
|
+
/**
|
|
4
|
+
* Stores the default container and all named containers.
|
|
5
|
+
*
|
|
6
|
+
* This registry is the shared source of truth behind `Container.of()` and
|
|
7
|
+
* decorator-based service registration.
|
|
8
|
+
*
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
3
11
|
export class ContainerRegistry {
|
|
4
12
|
static defaultContainerInstance;
|
|
5
13
|
static containerMap = new Map();
|
|
14
|
+
/**
|
|
15
|
+
* Returns the canonical default container instance.
|
|
16
|
+
*
|
|
17
|
+
* The default container is created lazily the first time it is requested.
|
|
18
|
+
*/
|
|
6
19
|
static get defaultContainer() {
|
|
7
20
|
this.defaultContainerInstance ??= new Container('default');
|
|
8
21
|
return this.defaultContainerInstance;
|
|
9
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Registers a named container in the shared registry.
|
|
25
|
+
*
|
|
26
|
+
* @param container The container to register.
|
|
27
|
+
* @throws {DefaultContainerIdError} If the container uses the reserved `default` id.
|
|
28
|
+
* @throws {ContainerDuplicatedError} If another container already uses the same id.
|
|
29
|
+
*/
|
|
10
30
|
static registerContainer(container) {
|
|
11
31
|
if (container.id === 'default') {
|
|
12
32
|
throw new DefaultContainerIdError();
|
|
@@ -16,18 +36,38 @@ export class ContainerRegistry {
|
|
|
16
36
|
}
|
|
17
37
|
this.containerMap.set(container.id, container);
|
|
18
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Returns the default container or a named container by identifier.
|
|
41
|
+
*
|
|
42
|
+
* @param id The container identifier to resolve.
|
|
43
|
+
* @returns The default container for `'default'`, or the matching named container if one exists.
|
|
44
|
+
*/
|
|
19
45
|
static getContainer(id) {
|
|
20
46
|
if (id === 'default') {
|
|
21
47
|
return this.defaultContainer;
|
|
22
48
|
}
|
|
23
49
|
return this.containerMap.get(id);
|
|
24
50
|
}
|
|
51
|
+
/**
|
|
52
|
+
* Returns whether a container exists for the given identifier.
|
|
53
|
+
*
|
|
54
|
+
* The reserved `default` identifier is always treated as present.
|
|
55
|
+
*
|
|
56
|
+
* @param id The container identifier to check.
|
|
57
|
+
* @returns `true` when the container exists in the registry.
|
|
58
|
+
*/
|
|
25
59
|
static hasContainer(id) {
|
|
26
60
|
if (id === 'default') {
|
|
27
61
|
return true;
|
|
28
62
|
}
|
|
29
63
|
return this.containerMap.has(id);
|
|
30
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* Removes a named container from the registry.
|
|
67
|
+
*
|
|
68
|
+
* @param id The named container identifier to remove.
|
|
69
|
+
* @throws {DefaultContainerIdError} If the reserved `default` id is used.
|
|
70
|
+
*/
|
|
31
71
|
static removeContainer(id) {
|
|
32
72
|
if (id === 'default') {
|
|
33
73
|
throw new DefaultContainerIdError();
|
|
@@ -1,2 +1,13 @@
|
|
|
1
1
|
import { type ServiceIdentifier } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Injects a dependency into a class field when the service is resolved.
|
|
4
|
+
*
|
|
5
|
+
* Use this field decorator on service properties that should be resolved from
|
|
6
|
+
* the container that creates the service instance.
|
|
7
|
+
*
|
|
8
|
+
* The dependency is assigned after the instance is created, so it is not
|
|
9
|
+
* available in constructors or field initializers.
|
|
10
|
+
*
|
|
11
|
+
* @param dependency The service identifier to inject into the decorated field.
|
|
12
|
+
*/
|
|
2
13
|
export declare function Inject<T>(dependency: ServiceIdentifier<T>): (_: undefined, context: ClassFieldDecoratorContext) => void;
|
|
@@ -1,4 +1,15 @@
|
|
|
1
1
|
import { INJECTION_KEY } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Injects a dependency into a class field when the service is resolved.
|
|
4
|
+
*
|
|
5
|
+
* Use this field decorator on service properties that should be resolved from
|
|
6
|
+
* the container that creates the service instance.
|
|
7
|
+
*
|
|
8
|
+
* The dependency is assigned after the instance is created, so it is not
|
|
9
|
+
* available in constructors or field initializers.
|
|
10
|
+
*
|
|
11
|
+
* @param dependency The service identifier to inject into the decorated field.
|
|
12
|
+
*/
|
|
2
13
|
export function Inject(dependency) {
|
|
3
14
|
return function (_, context) {
|
|
4
15
|
const injections = (context.metadata[INJECTION_KEY] ??= []);
|
|
@@ -1,4 +1,15 @@
|
|
|
1
1
|
import type { ServiceIdentifier, ServiceOption } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Registers a class as a service that can be resolved from a container.
|
|
4
|
+
*
|
|
5
|
+
* By default, the decorated class is registered in the default container,
|
|
6
|
+
* uses its own class as the service identifier, and uses the `container`
|
|
7
|
+
* scope.
|
|
8
|
+
*
|
|
9
|
+
* Use this decorator with a custom identifier or `scope` option when you need
|
|
10
|
+
* to change how the service is registered or reused. When you provide a
|
|
11
|
+
* custom identifier, resolve the service by that identifier.
|
|
12
|
+
*/
|
|
2
13
|
export declare function Service(): Function;
|
|
3
14
|
export declare function Service(id: ServiceIdentifier): Function;
|
|
4
15
|
export declare function Service(options?: ServiceOption): Function;
|
|
@@ -13,7 +13,7 @@ export function Service(idOrOptions) {
|
|
|
13
13
|
return function (target, context) {
|
|
14
14
|
const options = normalizeArguments(idOrOptions);
|
|
15
15
|
const injections = (context.metadata[INJECTION_KEY] ?? []);
|
|
16
|
-
ContainerRegistry.defaultContainer.
|
|
16
|
+
ContainerRegistry.defaultContainer.register({
|
|
17
17
|
id: options?.id ?? target,
|
|
18
18
|
Class: target,
|
|
19
19
|
name: context.name,
|
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import type { ServiceIdentifier } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Thrown when service resolution detects a circular dependency path.
|
|
4
|
+
*
|
|
5
|
+
* @internal
|
|
6
|
+
*/
|
|
2
7
|
export declare class CircularDependencyError extends Error {
|
|
3
8
|
name: string;
|
|
9
|
+
/**
|
|
10
|
+
* @param path The dependency chain that produced the circular reference.
|
|
11
|
+
*/
|
|
4
12
|
constructor(path: ServiceIdentifier[]);
|
|
5
13
|
}
|
|
@@ -1,5 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thrown when service resolution detects a circular dependency path.
|
|
3
|
+
*
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
1
6
|
export class CircularDependencyError extends Error {
|
|
2
7
|
name = 'CircularDependencyError';
|
|
8
|
+
/**
|
|
9
|
+
* @param path The dependency chain that produced the circular reference.
|
|
10
|
+
*/
|
|
3
11
|
constructor(path) {
|
|
4
12
|
super(`Circular dependency detected: ${path.map(String).join(' -> ')}`);
|
|
5
13
|
}
|
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import type { ContainerIdentifier } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Thrown when a named container is registered with an identifier that already exists.
|
|
4
|
+
*
|
|
5
|
+
* @internal
|
|
6
|
+
*/
|
|
2
7
|
export declare class ContainerDuplicatedError extends Error {
|
|
3
8
|
name: string;
|
|
9
|
+
/**
|
|
10
|
+
* @param id The duplicated container identifier.
|
|
11
|
+
*/
|
|
4
12
|
constructor(id: ContainerIdentifier);
|
|
5
13
|
}
|
|
@@ -1,5 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thrown when a named container is registered with an identifier that already exists.
|
|
3
|
+
*
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
1
6
|
export class ContainerDuplicatedError extends Error {
|
|
2
7
|
name = 'ContainerDuplicatedError';
|
|
8
|
+
/**
|
|
9
|
+
* @param id The duplicated container identifier.
|
|
10
|
+
*/
|
|
3
11
|
constructor(id) {
|
|
4
12
|
super(`Cannot register container with same ID(${String(id)})`);
|
|
5
13
|
}
|
|
@@ -1,6 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thrown when code attempts to use the reserved `default` identifier for a named container operation.
|
|
3
|
+
*
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
1
6
|
export class DefaultContainerIdError extends Error {
|
|
2
7
|
name = 'DefaultContainerIdError';
|
|
3
8
|
constructor() {
|
|
4
|
-
super(`
|
|
9
|
+
super('The `default` identifier is reserved for the default container.');
|
|
5
10
|
}
|
|
6
11
|
}
|
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import type { ServiceIdentifier } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Thrown when a service cannot be resolved for the requested identifier.
|
|
4
|
+
*
|
|
5
|
+
* @internal
|
|
6
|
+
*/
|
|
2
7
|
export declare class ServiceNotFoundError extends Error {
|
|
3
8
|
name: string;
|
|
9
|
+
/**
|
|
10
|
+
* @param id The service identifier that could not be resolved.
|
|
11
|
+
*/
|
|
4
12
|
constructor(id: ServiceIdentifier);
|
|
5
13
|
}
|
|
@@ -1,5 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thrown when a service cannot be resolved for the requested identifier.
|
|
3
|
+
*
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
1
6
|
export class ServiceNotFoundError extends Error {
|
|
2
7
|
name = 'ServiceNotFoundError';
|
|
8
|
+
/**
|
|
9
|
+
* @param id The service identifier that could not be resolved.
|
|
10
|
+
*/
|
|
3
11
|
constructor(id) {
|
|
4
12
|
super(`Service not found: ${String(id)}`);
|
|
5
13
|
}
|
package/dist/tokens/token.d.ts
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a typed service identifier.
|
|
3
|
+
*
|
|
4
|
+
* Tokens are useful when a dependency should be resolved by an explicit key
|
|
5
|
+
* instead of by its class constructor.
|
|
6
|
+
*/
|
|
1
7
|
export declare class Token<T> {
|
|
8
|
+
/**
|
|
9
|
+
* A human-readable name for this token.
|
|
10
|
+
*
|
|
11
|
+
* This value is useful for debugging and display, but token resolution is
|
|
12
|
+
* based on the token instance itself.
|
|
13
|
+
*/
|
|
2
14
|
name?: string;
|
|
15
|
+
/**
|
|
16
|
+
* Creates a token with an optional display name.
|
|
17
|
+
*
|
|
18
|
+
* @param name A human-readable name used for debugging and display.
|
|
19
|
+
*/
|
|
3
20
|
constructor(name?: string);
|
|
21
|
+
/**
|
|
22
|
+
* Returns a readable string representation of this token.
|
|
23
|
+
*
|
|
24
|
+
* @returns A display string for this token.
|
|
25
|
+
*/
|
|
4
26
|
toString(): string;
|
|
5
27
|
}
|
package/dist/tokens/token.js
CHANGED
|
@@ -1,9 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a typed service identifier.
|
|
3
|
+
*
|
|
4
|
+
* Tokens are useful when a dependency should be resolved by an explicit key
|
|
5
|
+
* instead of by its class constructor.
|
|
6
|
+
*/
|
|
1
7
|
// oxlint-disable-next-line no-unused-vars
|
|
2
8
|
export class Token {
|
|
9
|
+
/**
|
|
10
|
+
* A human-readable name for this token.
|
|
11
|
+
*
|
|
12
|
+
* This value is useful for debugging and display, but token resolution is
|
|
13
|
+
* based on the token instance itself.
|
|
14
|
+
*/
|
|
3
15
|
name;
|
|
16
|
+
/**
|
|
17
|
+
* Creates a token with an optional display name.
|
|
18
|
+
*
|
|
19
|
+
* @param name A human-readable name used for debugging and display.
|
|
20
|
+
*/
|
|
4
21
|
constructor(name) {
|
|
5
22
|
this.name = name;
|
|
6
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Returns a readable string representation of this token.
|
|
26
|
+
*
|
|
27
|
+
* @returns A display string for this token.
|
|
28
|
+
*/
|
|
7
29
|
toString() {
|
|
8
30
|
return this.name ? `Token(${this.name})` : `Token()`;
|
|
9
31
|
}
|
|
@@ -1,2 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A class constructor that can be instantiated with `new`.
|
|
3
|
+
*/
|
|
1
4
|
export type Constructable<T = object, Args extends unknown[] = never[]> = new (...args: Args) => T;
|
|
5
|
+
/**
|
|
6
|
+
* An abstract class constructor used for typing class-based service identifiers.
|
|
7
|
+
*/
|
|
2
8
|
export type AbstractConstructable<T = object, Args extends unknown[] = never[]> = abstract new (...args: Args) => T;
|
|
@@ -1,13 +1,42 @@
|
|
|
1
1
|
import type { Constructable } from './constructable';
|
|
2
2
|
import type { InjectionMetadata } from './injection';
|
|
3
3
|
import type { ServiceIdentifier, ServiceScope } from './service';
|
|
4
|
+
/**
|
|
5
|
+
* A container identifier used to select the default container or a named container.
|
|
6
|
+
*/
|
|
4
7
|
export type ContainerIdentifier = string | symbol;
|
|
8
|
+
/**
|
|
9
|
+
* Sentinel value used internally to mark services that have not been instantiated yet.
|
|
10
|
+
*/
|
|
5
11
|
export declare const EMPTY_VALUE: unique symbol;
|
|
12
|
+
/**
|
|
13
|
+
* Service registration metadata stored by a container.
|
|
14
|
+
*
|
|
15
|
+
* This is primarily used by low-level registration APIs and internal container logic.
|
|
16
|
+
*/
|
|
6
17
|
export interface Metadata<T = unknown> {
|
|
18
|
+
/**
|
|
19
|
+
* The identifier used to resolve the service.
|
|
20
|
+
*/
|
|
7
21
|
id: ServiceIdentifier;
|
|
22
|
+
/**
|
|
23
|
+
* The class instantiated for this service.
|
|
24
|
+
*/
|
|
8
25
|
Class: Constructable<T>;
|
|
26
|
+
/**
|
|
27
|
+
* The original class or member name, when available.
|
|
28
|
+
*/
|
|
9
29
|
name?: string | symbol;
|
|
30
|
+
/**
|
|
31
|
+
* Property injection definitions collected for the service.
|
|
32
|
+
*/
|
|
10
33
|
injections: InjectionMetadata[];
|
|
34
|
+
/**
|
|
35
|
+
* The lifetime used when resolving the service.
|
|
36
|
+
*/
|
|
11
37
|
scope: ServiceScope;
|
|
38
|
+
/**
|
|
39
|
+
* The cached service instance, or `EMPTY_VALUE` when no instance is cached.
|
|
40
|
+
*/
|
|
12
41
|
value: T | typeof EMPTY_VALUE;
|
|
13
42
|
}
|
package/dist/types/container.js
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
import type { ServiceIdentifier } from './service';
|
|
2
|
+
/**
|
|
3
|
+
* Property injection metadata collected for a service.
|
|
4
|
+
*/
|
|
2
5
|
export interface InjectionMetadata {
|
|
6
|
+
/**
|
|
7
|
+
* The identifier of the dependency to inject.
|
|
8
|
+
*/
|
|
3
9
|
id: ServiceIdentifier;
|
|
10
|
+
/**
|
|
11
|
+
* The class field that receives the resolved dependency.
|
|
12
|
+
*/
|
|
4
13
|
name: string | symbol;
|
|
5
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Metadata key used internally to store property injection definitions.
|
|
17
|
+
*/
|
|
6
18
|
export declare const INJECTION_KEY: unique symbol;
|
package/dist/types/injection.js
CHANGED
package/dist/types/service.d.ts
CHANGED
|
@@ -1,8 +1,23 @@
|
|
|
1
1
|
import type { Token } from '../tokens';
|
|
2
2
|
import type { AbstractConstructable, Constructable } from './constructable';
|
|
3
|
+
/**
|
|
4
|
+
* An identifier that can be used to register or resolve a service.
|
|
5
|
+
*/
|
|
3
6
|
export type ServiceIdentifier<T = unknown, Args extends unknown[] = never[]> = Constructable<T, Args> | AbstractConstructable<T, Args> | CallableFunction | string | Token<T>;
|
|
7
|
+
/**
|
|
8
|
+
* The lifetime used when a service is resolved from a container.
|
|
9
|
+
*/
|
|
4
10
|
export type ServiceScope = 'singleton' | 'container' | 'transient';
|
|
11
|
+
/**
|
|
12
|
+
* Options for registering a service with `@Service()`.
|
|
13
|
+
*/
|
|
5
14
|
export interface ServiceOption {
|
|
15
|
+
/**
|
|
16
|
+
* A custom identifier used to resolve the service.
|
|
17
|
+
*/
|
|
6
18
|
id?: ServiceIdentifier;
|
|
19
|
+
/**
|
|
20
|
+
* The lifetime used when the service is resolved.
|
|
21
|
+
*/
|
|
7
22
|
scope?: ServiceScope;
|
|
8
23
|
}
|