@navios/di 0.1.11 → 0.1.12

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.
@@ -0,0 +1,182 @@
1
+ # @Injectable decorator
2
+
3
+ The `@Injectable` decorator is used to define a class or a factory class as an injectable service.
4
+
5
+ ## Parameters
6
+
7
+ The `@Injectable` decorator accepts the following fields in its options:
8
+
9
+ - `token`: The injection token to use for the service. If not provided, will create a new token using the class itself.
10
+ - `scope`: The lifetime of the service. Accepts `InjectableScope.Singleton` or `InjectableScope.Instance`. By default, it will be `InjectableScope.Singleton`.
11
+ - `type`: The type of the service. Accepts `InjectableType.Class` or `InjectableType.Factory`. By default, it will be `InjectableType.Class`.
12
+ - `registry`: The registry to use for the service. Uses the default registry if not provided.
13
+
14
+ ## Usage
15
+
16
+ Injectable can be used as a class decorator or a factory decorator.
17
+
18
+ Inside a class that you're decorating with `@Injectable` you can use `inject` and `syncInject` functions to inject the services.
19
+
20
+ ```ts
21
+ @Injectable()
22
+ class GreeterService {
23
+ sayHello(name: string) {
24
+ return `Hello ${name}`
25
+ }
26
+ }
27
+
28
+ @Injectable()
29
+ class UserService {
30
+ private readonly greeterService = syncInject(GreeterService)
31
+
32
+ makeGreeting(name: string) {
33
+ return this.greeterService.sayHello(name)
34
+ }
35
+ }
36
+ ```
37
+
38
+ Please note that `syncInject` can be used only with services that are created with `InjectableScope.Singleton`.
39
+
40
+ If you need to inject a service that is created with `InjectableScope.Instance`, you can use `inject` function. and it will return a Promise.
41
+
42
+ For example:
43
+
44
+ ```ts
45
+ @Injectable({ scope: InjectableScope.Instance })
46
+ class GreeterService {
47
+ private readonly createdAt = new Date()
48
+
49
+ sayHello(name: string) {
50
+ return `Hello ${name} ${this.createdAt.toISOString()}`
51
+ }
52
+ }
53
+
54
+ @Injectable()
55
+ class UserService {
56
+ private readonly greeterService = inject(GreeterService)
57
+
58
+ async makeGreeting(name: string) {
59
+ const service = await this.greeterService
60
+ return service.sayHello(name)
61
+ }
62
+ }
63
+ ```
64
+
65
+ ## Examples
66
+
67
+ ### Simple class
68
+
69
+ ```ts
70
+ @Injectable()
71
+ class GreeterService {
72
+ sayHello(name: string) {
73
+ return `Hello ${name}`
74
+ }
75
+ }
76
+
77
+ const greeterService = await inject(GreeterService)
78
+ console.log(greeterService.sayHello('John'))
79
+ ```
80
+
81
+ ### Simple Factory
82
+
83
+ ```ts
84
+ @Injectable({ type: InjectableType.Factory })
85
+ class GreeterServiceFactory {
86
+ create() {
87
+ return new GreeterService()
88
+ }
89
+ }
90
+
91
+ const greeterService = await inject(GreeterServiceFactory)
92
+ console.log(greeterService.sayHello('John'))
93
+ ```
94
+
95
+ ### Class with token
96
+
97
+ ```ts
98
+ export interface GreeterServiceInterface {
99
+ sayHello(name: string): string
100
+ }
101
+
102
+ const GreeterServiceParams = z.object({
103
+ context: z.string(),
104
+ })
105
+
106
+ export const GREETER_SERVICE = InjectionToken.create<
107
+ GreeterServiceInterface,
108
+ typeof GreeterServiceParams
109
+ >('GreeterService', GreeterServiceParams)
110
+
111
+ @Injectable({ token: GREETER_SERVICE })
112
+ class GreeterService {
113
+ constructor(private readonly config: z.infer<typeof GreeterServiceParams>) {}
114
+
115
+ sayHello(name: string) {
116
+ return `Hello ${name} ${this.config.context}`
117
+ }
118
+ }
119
+
120
+ const greeterService = await inject(GREETER_SERVICE, { context: 'World' })
121
+ console.log(greeterService.sayHello('John'))
122
+ ```
123
+
124
+ ### Factory with token
125
+
126
+ ```ts
127
+ export interface GreeterServiceInterface {
128
+ sayHello(name: string): string
129
+ }
130
+
131
+ const GreeterServiceParams = z.object({
132
+ context: z.string(),
133
+ })
134
+
135
+ export const GREETER_SERVICE = InjectionToken.create<
136
+ GreeterServiceInterface,
137
+ typeof GreeterServiceParams
138
+ >('GreeterService', GreeterServiceParams)
139
+
140
+ @Injectable({ type: InjectableType.Factory, token: GREETER_SERVICE })
141
+ class GreeterServiceFactory {
142
+ create(ctx: FactoryContext, args: z.infer<typeof GreeterServiceParams>) {
143
+ return new GreeterService(args)
144
+ }
145
+ }
146
+
147
+ const greeterService = await inject(GREETER_SERVICE, { context: 'World' })
148
+ console.log(greeterService.sayHello('John'))
149
+ ```
150
+
151
+ ## InjectableScope
152
+
153
+ The `InjectableScope` enum defines the scope of the service.
154
+
155
+ - `InjectableScope.Singleton`: The service will be created once and reused.
156
+ - `InjectableScope.Instance`: The service will be created every time it is injected.
157
+
158
+ ## InjectableType
159
+
160
+ The `InjectableType` enum defines the type of the service.
161
+
162
+ - `InjectableType.Class`: The service will be a class.
163
+ - `InjectableType.Factory`: The service will be a factory.
164
+
165
+ ## Registry
166
+
167
+ The `Registry` is the registry of the service. It is used to store the service and its dependencies.
168
+
169
+ ## FactoryContext
170
+
171
+ The `FactoryContext` is the context of the factory. It is used to add additional information to the factory.
172
+
173
+ Context API:
174
+
175
+ - `inject`: Injects the service, same as global inject, but additionally it will track the dependencies of the service.
176
+ - `on`: Adds a listener to the service locator event bus.
177
+ - `getDependencies`: Returns the dependencies of the service.
178
+ - `invalidate`: Invalidates self and all the services that depend on it.
179
+ - `addEffect`: Adds an effect to the service. Effect is a function that will be called when the service is invalidated.
180
+ - `setTtl`: Sets the ttl of the service.
181
+ - `getTtl`: Returns the ttl of the service.
182
+ - `locator`: Returns the service locator you are currently in.
@@ -0,0 +1,145 @@
1
+ # Injection Token
2
+
3
+ Injection tokens are used to identify and retrieve services from the dependency injection container. They are typically defined as constants and used to inject services into classes.
4
+
5
+ ## Creating an Injection Token
6
+
7
+ To create an injection token, you can use the `InjectionToken` class from the `@navios/di` or `@navios/core` package. Here's an example:
8
+
9
+ ```ts
10
+ import { InjectionToken } from '@navios/di'
11
+
12
+ export const GREETER_SERVICE =
13
+ InjectionToken.create<GreeterService>('GreeterService')
14
+
15
+ @Injectable({ token: GREETER_SERVICE })
16
+ class GreeterService {}
17
+ ```
18
+
19
+ ## Using an Injection Token
20
+
21
+ To use an injection token, you can inject it into a class or a function.
22
+
23
+ ```ts
24
+ import { inject } from '@navios/di'
25
+
26
+ const greeterService = await inject(GREETER_SERVICE)
27
+ ```
28
+
29
+ or
30
+
31
+ ```ts
32
+ @Injectable()
33
+ class UserService {
34
+ private readonly greeterService = inject(GREETER_SERVICE) // This will be a Promise<GreeterService>
35
+ private readonly greeterServiceSync = syncInject(GREETER_SERVICE) // This will be a GreeterService
36
+ }
37
+ ```
38
+
39
+ ## Adding a parameter to the injection token
40
+
41
+ You can add a parameter to the injection token to make it more specific.
42
+
43
+ ```ts
44
+ import { z } from 'zod'
45
+
46
+ const GreeterServiceParams = z.object({
47
+ context: z.string(),
48
+ })
49
+
50
+ export const GREETER_SERVICE = InjectionToken.create<
51
+ GreeterService,
52
+ typeof GreeterServiceParams
53
+ >('GreeterService', GreeterServiceParams)
54
+
55
+ @Injectable({ token: GREETER_SERVICE })
56
+ class GreeterService {}
57
+
58
+ const greeterService = await inject(GREETER_SERVICE, { context: 'Hello' })
59
+ ```
60
+
61
+ ## Providing a value to an injection token
62
+
63
+ You can provide a value to an injection token by using the `bound` function.
64
+
65
+ ```ts
66
+ import { inject, InjectionToken } from '@navios/di'
67
+
68
+ const helloGreeterService = InjectionToken.bound(GREETER_SERVICE, {
69
+ context: 'Hello',
70
+ })
71
+
72
+ const greeterService = await inject(helloGreeterService)
73
+ ```
74
+
75
+ ## Providing a factory to an injection token
76
+
77
+ You can provide a factory to an injection token by using the `factory` function.
78
+
79
+ It is useful, when you need to bind a value to a result of another service (e.g. a configuration).
80
+
81
+ ```ts
82
+ import { inject, InjectionToken } from '@navios/di'
83
+
84
+ @Injectable()
85
+ class GreeterConfigService {
86
+ getContext() {
87
+ return 'Hello'
88
+ }
89
+ }
90
+ const helloGreeterService = InjectionToken.factory(
91
+ GREETER_SERVICE,
92
+ async () => {
93
+ const config = await inject(GreeterConfigService)
94
+ return { context: config.getContext() }
95
+ },
96
+ )
97
+
98
+ const greeterService = await inject(helloGreeterService)
99
+ ```
100
+
101
+ ## API
102
+
103
+ ### `InjectionToken.create`
104
+
105
+ Creates a new injection token.
106
+
107
+ Generic arguments:
108
+
109
+ - `T`: The type of the value to bind to the injection token.
110
+ - `P`: Zod schema type of the parameters of the injection token.
111
+
112
+ Arguments:
113
+
114
+ - `name: string | Class | symbol`: The name of the injection token.
115
+ - `params: ZodSchema | undefined`: The parameters of the injection token.
116
+
117
+ Returns:
118
+
119
+ - `InjectionToken`: The new injection token.
120
+
121
+ ### `InjectionToken.bound`
122
+
123
+ Creates a new injection token that is bound to a value.
124
+
125
+ Arguments:
126
+
127
+ - `token: InjectionToken`: The injection token to bind.
128
+ - `value: any`: The value to bind to the injection token.
129
+
130
+ Returns:
131
+
132
+ - `BoundInjectionToken`: The new injection token.
133
+
134
+ ### `InjectionToken.factory`
135
+
136
+ Creates a new injection token that is a factory.
137
+
138
+ Arguments:
139
+
140
+ - `token: InjectionToken`: The injection token to bind.
141
+ - `factory: () => Promise<any>`: The factory to bind to the injection token.
142
+
143
+ Returns:
144
+
145
+ - `FactoryInjectionToken`: The new injection token.
package/lib/index.js CHANGED
@@ -1,63 +1,6 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/index.mts
21
- var index_exports = {};
22
- __export(index_exports, {
23
- BoundInjectionToken: () => BoundInjectionToken,
24
- ErrorsEnum: () => ErrorsEnum,
25
- EventEmitter: () => EventEmitter,
26
- FactoryInjectionToken: () => FactoryInjectionToken,
27
- FactoryNotFound: () => FactoryNotFound,
28
- FactoryTokenNotResolved: () => FactoryTokenNotResolved,
29
- Injectable: () => Injectable,
30
- InjectableScope: () => InjectableScope,
31
- InjectableTokenMeta: () => InjectableTokenMeta,
32
- InjectableType: () => InjectableType,
33
- InjectionToken: () => InjectionToken,
34
- InjectorsBase: () => InjectorsBase,
35
- InstanceDestroying: () => InstanceDestroying,
36
- InstanceExpired: () => InstanceExpired,
37
- InstanceNotFound: () => InstanceNotFound,
38
- ProxyServiceLocator: () => ProxyServiceLocator,
39
- Registry: () => Registry,
40
- ServiceLocator: () => ServiceLocator,
41
- ServiceLocatorEventBus: () => ServiceLocatorEventBus,
42
- ServiceLocatorInstanceHolderKind: () => ServiceLocatorInstanceHolderKind,
43
- ServiceLocatorInstanceHolderStatus: () => ServiceLocatorInstanceHolderStatus,
44
- ServiceLocatorManager: () => ServiceLocatorManager,
45
- UnknownError: () => UnknownError,
46
- getGlobalServiceLocator: () => getGlobalServiceLocator,
47
- getInjectableToken: () => getInjectableToken,
48
- getInjectors: () => getInjectors,
49
- globalRegistry: () => globalRegistry,
50
- inject: () => inject,
51
- makeProxyServiceLocator: () => makeProxyServiceLocator,
52
- provideServiceLocator: () => provideServiceLocator,
53
- resolveService: () => resolveService,
54
- syncInject: () => syncInject,
55
- wrapSyncInit: () => wrapSyncInit
56
- });
57
- module.exports = __toCommonJS(index_exports);
1
+ 'use strict';
58
2
 
59
3
  // src/decorators/injectable.decorator.mts
60
- var import_zod3 = require("zod");
61
4
 
62
5
  // src/enums/injectable-scope.enum.mts
63
6
  var InjectableScope = /* @__PURE__ */ ((InjectableScope3) => {
@@ -72,9 +15,6 @@ var InjectableType = /* @__PURE__ */ ((InjectableType2) => {
72
15
  InjectableType2["Factory"] = "Factory";
73
16
  return InjectableType2;
74
17
  })(InjectableType || {});
75
-
76
- // src/injection-token.mts
77
- var import_zod = require("zod");
78
18
  var InjectionToken = class _InjectionToken {
79
19
  constructor(name, schema) {
80
20
  this.name = name;
@@ -149,9 +89,6 @@ var FactoryInjectionToken = class {
149
89
  return this.token.toString();
150
90
  }
151
91
  };
152
-
153
- // src/registry.mts
154
- var import_zod2 = require("zod");
155
92
  var Registry = class {
156
93
  constructor(parent) {
157
94
  this.parent = parent;
@@ -450,9 +387,6 @@ var UnknownError = class extends Error {
450
387
  }
451
388
  };
452
389
 
453
- // src/interfaces/factory.interface.mts
454
- var import_zod4 = require("zod");
455
-
456
390
  // src/event-emitter.mts
457
391
  var EventEmitter = class {
458
392
  listeners = /* @__PURE__ */ new Map();
@@ -988,40 +922,39 @@ var inject = values.inject;
988
922
  var syncInject = values.syncInject;
989
923
  var wrapSyncInit = values.wrapSyncInit;
990
924
  var provideServiceLocator = values.provideServiceLocator;
991
- // Annotate the CommonJS export names for ESM import in node:
992
- 0 && (module.exports = {
993
- BoundInjectionToken,
994
- ErrorsEnum,
995
- EventEmitter,
996
- FactoryInjectionToken,
997
- FactoryNotFound,
998
- FactoryTokenNotResolved,
999
- Injectable,
1000
- InjectableScope,
1001
- InjectableTokenMeta,
1002
- InjectableType,
1003
- InjectionToken,
1004
- InjectorsBase,
1005
- InstanceDestroying,
1006
- InstanceExpired,
1007
- InstanceNotFound,
1008
- ProxyServiceLocator,
1009
- Registry,
1010
- ServiceLocator,
1011
- ServiceLocatorEventBus,
1012
- ServiceLocatorInstanceHolderKind,
1013
- ServiceLocatorInstanceHolderStatus,
1014
- ServiceLocatorManager,
1015
- UnknownError,
1016
- getGlobalServiceLocator,
1017
- getInjectableToken,
1018
- getInjectors,
1019
- globalRegistry,
1020
- inject,
1021
- makeProxyServiceLocator,
1022
- provideServiceLocator,
1023
- resolveService,
1024
- syncInject,
1025
- wrapSyncInit
1026
- });
925
+
926
+ exports.BoundInjectionToken = BoundInjectionToken;
927
+ exports.ErrorsEnum = ErrorsEnum;
928
+ exports.EventEmitter = EventEmitter;
929
+ exports.FactoryInjectionToken = FactoryInjectionToken;
930
+ exports.FactoryNotFound = FactoryNotFound;
931
+ exports.FactoryTokenNotResolved = FactoryTokenNotResolved;
932
+ exports.Injectable = Injectable;
933
+ exports.InjectableScope = InjectableScope;
934
+ exports.InjectableTokenMeta = InjectableTokenMeta;
935
+ exports.InjectableType = InjectableType;
936
+ exports.InjectionToken = InjectionToken;
937
+ exports.InjectorsBase = InjectorsBase;
938
+ exports.InstanceDestroying = InstanceDestroying;
939
+ exports.InstanceExpired = InstanceExpired;
940
+ exports.InstanceNotFound = InstanceNotFound;
941
+ exports.ProxyServiceLocator = ProxyServiceLocator;
942
+ exports.Registry = Registry;
943
+ exports.ServiceLocator = ServiceLocator;
944
+ exports.ServiceLocatorEventBus = ServiceLocatorEventBus;
945
+ exports.ServiceLocatorInstanceHolderKind = ServiceLocatorInstanceHolderKind;
946
+ exports.ServiceLocatorInstanceHolderStatus = ServiceLocatorInstanceHolderStatus;
947
+ exports.ServiceLocatorManager = ServiceLocatorManager;
948
+ exports.UnknownError = UnknownError;
949
+ exports.getGlobalServiceLocator = getGlobalServiceLocator;
950
+ exports.getInjectableToken = getInjectableToken;
951
+ exports.getInjectors = getInjectors;
952
+ exports.globalRegistry = globalRegistry;
953
+ exports.inject = inject;
954
+ exports.makeProxyServiceLocator = makeProxyServiceLocator;
955
+ exports.provideServiceLocator = provideServiceLocator;
956
+ exports.resolveService = resolveService;
957
+ exports.syncInject = syncInject;
958
+ exports.wrapSyncInit = wrapSyncInit;
959
+ //# sourceMappingURL=index.js.map
1027
960
  //# sourceMappingURL=index.js.map