@navios/core 0.5.0 → 0.5.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/docs/modules.md CHANGED
@@ -113,6 +113,132 @@ Modules in Navios follow a specific lifecycle:
113
113
  4. **Guard Application**: Module-level guards are applied to all controllers
114
114
  5. **Initialization**: Module initialization hooks are called
115
115
 
116
+ ## Module Lifecycle Methods
117
+
118
+ ### `onModuleInit`
119
+
120
+ The `onModuleInit` lifecycle method is called after the module has been initialized and all its dependencies have been resolved. This is useful for performing setup tasks, initializing connections, or running startup logic.
121
+
122
+ #### Interface
123
+
124
+ ```typescript
125
+ interface NaviosModule {
126
+ onModuleInit(): void | Promise<void>
127
+ }
128
+ ```
129
+
130
+ #### Usage
131
+
132
+ To use the `onModuleInit` lifecycle method, implement the `NaviosModule` interface in your module class:
133
+
134
+ ```typescript
135
+ import { Module, NaviosModule } from '@navios/core'
136
+
137
+ @Module({
138
+ controllers: [UserController],
139
+ })
140
+ export class UserModule implements NaviosModule {
141
+ onModuleInit() {
142
+ console.log('UserModule has been initialized')
143
+ // Perform initialization logic here
144
+ }
145
+ }
146
+ ```
147
+
148
+ #### Async Initialization
149
+
150
+ The `onModuleInit` method can be asynchronous, allowing you to perform async setup tasks:
151
+
152
+ ```typescript
153
+ import { Module, NaviosModule } from '@navios/core'
154
+
155
+ @Module({
156
+ controllers: [DatabaseController],
157
+ })
158
+ export class DatabaseModule implements NaviosModule {
159
+ async onModuleInit() {
160
+ console.log('Initializing database connection...')
161
+ await this.connectToDatabase()
162
+ console.log('Database connection established')
163
+ }
164
+
165
+ private async connectToDatabase() {
166
+ // Database connection logic
167
+ return new Promise((resolve) => setTimeout(resolve, 1000))
168
+ }
169
+ }
170
+ ```
171
+
172
+ #### Common Use Cases
173
+
174
+ - **Database Connections**: Initialize database connections or verify connectivity
175
+ - **Cache Warming**: Pre-populate caches with frequently accessed data
176
+ - **External Service Setup**: Initialize connections to external APIs or services
177
+ - **Configuration Validation**: Validate required configuration settings
178
+ - **Background Tasks**: Start background processes or scheduled tasks
179
+
180
+ ```typescript
181
+ import { Module, NaviosModule } from '@navios/core'
182
+
183
+ @Module({
184
+ controllers: [CacheController],
185
+ })
186
+ export class CacheModule implements NaviosModule {
187
+ private cache = new Map<string, any>()
188
+
189
+ async onModuleInit() {
190
+ // Warm up the cache with initial data
191
+ await this.warmUpCache()
192
+
193
+ // Validate configuration
194
+ this.validateConfiguration()
195
+
196
+ console.log('CacheModule initialized successfully')
197
+ }
198
+
199
+ private async warmUpCache() {
200
+ // Pre-populate cache with frequently accessed data
201
+ this.cache.set('app:config', await this.loadAppConfig())
202
+ this.cache.set('user:defaults', await this.loadUserDefaults())
203
+ }
204
+
205
+ private validateConfiguration() {
206
+ if (!process.env.CACHE_TTL) {
207
+ throw new Error('CACHE_TTL environment variable is required')
208
+ }
209
+ }
210
+
211
+ private async loadAppConfig() {
212
+ // Load application configuration
213
+ return { theme: 'dark', language: 'en' }
214
+ }
215
+
216
+ private async loadUserDefaults() {
217
+ // Load default user settings
218
+ return { notifications: true, theme: 'auto' }
219
+ }
220
+ }
221
+ ```
222
+
223
+ #### Execution Order
224
+
225
+ The `onModuleInit` methods are called in dependency order:
226
+
227
+ 1. **Imported modules first**: All imported modules' `onModuleInit` methods are called before the current module
228
+ 2. **Current module last**: The current module's `onModuleInit` method is called after all its dependencies
229
+
230
+ ```typescript
231
+ @Module({
232
+ imports: [DatabaseModule, CacheModule], // These initialize first
233
+ })
234
+ export class AppModule implements NaviosModule {
235
+ onModuleInit() {
236
+ // This runs after DatabaseModule and CacheModule have been initialized
237
+ console.log('AppModule initialized - all dependencies are ready')
238
+ }
239
+ }
240
+ ```
241
+
116
242
  ## Module Metadata
117
243
 
118
244
  Each module decorated with `@Module()` has associated metadata that Navios uses internally:
package/docs/services.md CHANGED
@@ -260,11 +260,10 @@ export class EmailProviderFactory {
260
260
 
261
261
  @Injectable()
262
262
  export class EmailService {
263
- private emailProviderFactory = inject(EmailProviderFactory)
263
+ private emailProvider = inject(EmailProviderFactory)
264
264
 
265
265
  async sendWelcomeEmail(user: User) {
266
- const provider = this.emailProviderFactory.create()
267
- await provider.send(
266
+ await this.emailProvider.send(
268
267
  user.email,
269
268
  'Welcome!',
270
269
  `Hello ${user.name}, welcome to our platform!`,
@@ -12,12 +12,13 @@ import { ClassTypeWithInstance } from '@navios/di';
12
12
  import { ClassTypeWithInstanceAndArgument } from '@navios/di';
13
13
  import { ClassTypeWithInstanceAndOptionalArgument } from '@navios/di';
14
14
  import { ClassTypeWithOptionalArgument } from '@navios/di';
15
+ import { ClassTypeWithoutArguments } from '@navios/di';
15
16
  import { Container } from '@navios/di';
16
- import { createDeferred } from '@navios/di';
17
17
  import { createRequestContextHolder } from '@navios/di';
18
18
  import { defaultInjectors } from '@navios/di';
19
19
  import { DefaultRequestContextHolder } from '@navios/di';
20
- import { Deferred } from '@navios/di';
20
+ import { DIError } from '@navios/di';
21
+ import { DIErrorCode } from '@navios/di';
21
22
  import type { EndpointFunctionArgs } from '@navios/builder';
22
23
  import { ErrorsEnum } from '@navios/di';
23
24
  import { EventEmitter } from '@navios/di';
@@ -48,15 +49,16 @@ import { InjectionToken } from '@navios/di';
48
49
  import { InjectionTokenSchemaType } from '@navios/di';
49
50
  import { InjectionTokenType } from '@navios/di';
50
51
  import { Injectors } from '@navios/di';
52
+ import { InjectRequest } from '@navios/di';
51
53
  import { InjectState } from '@navios/di';
52
54
  import type { InspectOptions } from 'util';
53
55
  import { InstanceDestroying } from '@navios/di';
54
- import { InstanceExpired } from '@navios/di';
55
56
  import { InstanceNotFound } from '@navios/di';
56
57
  import { IsUnion } from '@navios/di';
57
58
  import { Join } from '@navios/di';
58
59
  import { OnServiceDestroy } from '@navios/di';
59
60
  import { OnServiceInit } from '@navios/di';
61
+ import { optional } from '@navios/di';
60
62
  import { OptionalInjectionTokenSchemaType } from '@navios/di';
61
63
  import type { OutgoingHttpHeaders } from 'http';
62
64
  import { PopUnion } from '@navios/di';
@@ -258,6 +260,8 @@ export { ClassTypeWithInstanceAndOptionalArgument }
258
260
 
259
261
  export { ClassTypeWithOptionalArgument }
260
262
 
263
+ export { ClassTypeWithoutArguments }
264
+
261
265
  declare const clc: {
262
266
  bold: (text: string) => string;
263
267
  green: (text: string) => string;
@@ -546,15 +550,15 @@ export { ControllerOptions }
546
550
  export { ControllerOptions as ControllerOptions_alias_1 }
547
551
  export { ControllerOptions as ControllerOptions_alias_2 }
548
552
 
549
- export { createDeferred }
550
-
551
553
  export { createRequestContextHolder }
552
554
 
553
555
  export { defaultInjectors }
554
556
 
555
557
  export { DefaultRequestContextHolder }
556
558
 
557
- export { Deferred }
559
+ export { DIError }
560
+
561
+ export { DIErrorCode }
558
562
 
559
563
  declare function Endpoint<Method extends HttpMethod = HttpMethod, Url extends string = string, QuerySchema = undefined, ResponseSchema extends ZodType = ZodType, RequestSchema = ZodType>(endpoint: {
560
564
  config: BaseEndpointConfig<Method, Url, QuerySchema, ResponseSchema, RequestSchema>;
@@ -804,12 +808,12 @@ export { InjectionTokenType }
804
808
 
805
809
  export { Injectors }
806
810
 
811
+ export { InjectRequest }
812
+
807
813
  export { InjectState }
808
814
 
809
815
  export { InstanceDestroying }
810
816
 
811
- export { InstanceExpired }
812
-
813
817
  export { InstanceNotFound }
814
818
 
815
819
  declare class InternalServerErrorException extends HttpException {
@@ -1097,7 +1101,7 @@ export { MultipartParams as MultipartParams_alias_2 }
1097
1101
 
1098
1102
  declare type MultipartResult<EndpointDeclaration extends {
1099
1103
  config: BaseEndpointConfig<any, any, any, any, any>;
1100
- }> = EndpointDeclaration['config']['responseSchema'] extends ZodDiscriminatedUnion<any, infer Options> ? Promise<z.input<Options[number]>> : Promise<z.input<EndpointDeclaration['config']['responseSchema']>>;
1104
+ }> = EndpointDeclaration['config']['responseSchema'] extends ZodDiscriminatedUnion<infer Options> ? Promise<z.input<Options[number]>> : Promise<z.input<EndpointDeclaration['config']['responseSchema']>>;
1101
1105
  export { MultipartResult }
1102
1106
  export { MultipartResult as MultipartResult_alias_1 }
1103
1107
  export { MultipartResult as MultipartResult_alias_2 }
@@ -1193,6 +1197,8 @@ export { OnServiceDestroy }
1193
1197
 
1194
1198
  export { OnServiceInit }
1195
1199
 
1200
+ export { optional }
1201
+
1196
1202
  export { OptionalInjectionTokenSchemaType }
1197
1203
 
1198
1204
  declare type OriginType = string | boolean | RegExp;
@@ -12,12 +12,13 @@ import { ClassTypeWithInstance } from '@navios/di';
12
12
  import { ClassTypeWithInstanceAndArgument } from '@navios/di';
13
13
  import { ClassTypeWithInstanceAndOptionalArgument } from '@navios/di';
14
14
  import { ClassTypeWithOptionalArgument } from '@navios/di';
15
+ import { ClassTypeWithoutArguments } from '@navios/di';
15
16
  import { Container } from '@navios/di';
16
- import { createDeferred } from '@navios/di';
17
17
  import { createRequestContextHolder } from '@navios/di';
18
18
  import { defaultInjectors } from '@navios/di';
19
19
  import { DefaultRequestContextHolder } from '@navios/di';
20
- import { Deferred } from '@navios/di';
20
+ import { DIError } from '@navios/di';
21
+ import { DIErrorCode } from '@navios/di';
21
22
  import type { EndpointFunctionArgs } from '@navios/builder';
22
23
  import { ErrorsEnum } from '@navios/di';
23
24
  import { EventEmitter } from '@navios/di';
@@ -48,15 +49,16 @@ import { InjectionToken } from '@navios/di';
48
49
  import { InjectionTokenSchemaType } from '@navios/di';
49
50
  import { InjectionTokenType } from '@navios/di';
50
51
  import { Injectors } from '@navios/di';
52
+ import { InjectRequest } from '@navios/di';
51
53
  import { InjectState } from '@navios/di';
52
54
  import type { InspectOptions } from 'util';
53
55
  import { InstanceDestroying } from '@navios/di';
54
- import { InstanceExpired } from '@navios/di';
55
56
  import { InstanceNotFound } from '@navios/di';
56
57
  import { IsUnion } from '@navios/di';
57
58
  import { Join } from '@navios/di';
58
59
  import { OnServiceDestroy } from '@navios/di';
59
60
  import { OnServiceInit } from '@navios/di';
61
+ import { optional } from '@navios/di';
60
62
  import { OptionalInjectionTokenSchemaType } from '@navios/di';
61
63
  import type { OutgoingHttpHeaders } from 'http';
62
64
  import { PopUnion } from '@navios/di';
@@ -258,6 +260,8 @@ export { ClassTypeWithInstanceAndOptionalArgument }
258
260
 
259
261
  export { ClassTypeWithOptionalArgument }
260
262
 
263
+ export { ClassTypeWithoutArguments }
264
+
261
265
  declare const clc: {
262
266
  bold: (text: string) => string;
263
267
  green: (text: string) => string;
@@ -546,15 +550,15 @@ export { ControllerOptions }
546
550
  export { ControllerOptions as ControllerOptions_alias_1 }
547
551
  export { ControllerOptions as ControllerOptions_alias_2 }
548
552
 
549
- export { createDeferred }
550
-
551
553
  export { createRequestContextHolder }
552
554
 
553
555
  export { defaultInjectors }
554
556
 
555
557
  export { DefaultRequestContextHolder }
556
558
 
557
- export { Deferred }
559
+ export { DIError }
560
+
561
+ export { DIErrorCode }
558
562
 
559
563
  declare function Endpoint<Method extends HttpMethod = HttpMethod, Url extends string = string, QuerySchema = undefined, ResponseSchema extends ZodType = ZodType, RequestSchema = ZodType>(endpoint: {
560
564
  config: BaseEndpointConfig<Method, Url, QuerySchema, ResponseSchema, RequestSchema>;
@@ -804,12 +808,12 @@ export { InjectionTokenType }
804
808
 
805
809
  export { Injectors }
806
810
 
811
+ export { InjectRequest }
812
+
807
813
  export { InjectState }
808
814
 
809
815
  export { InstanceDestroying }
810
816
 
811
- export { InstanceExpired }
812
-
813
817
  export { InstanceNotFound }
814
818
 
815
819
  declare class InternalServerErrorException extends HttpException {
@@ -1097,7 +1101,7 @@ export { MultipartParams as MultipartParams_alias_2 }
1097
1101
 
1098
1102
  declare type MultipartResult<EndpointDeclaration extends {
1099
1103
  config: BaseEndpointConfig<any, any, any, any, any>;
1100
- }> = EndpointDeclaration['config']['responseSchema'] extends ZodDiscriminatedUnion<any, infer Options> ? Promise<z.input<Options[number]>> : Promise<z.input<EndpointDeclaration['config']['responseSchema']>>;
1104
+ }> = EndpointDeclaration['config']['responseSchema'] extends ZodDiscriminatedUnion<infer Options> ? Promise<z.input<Options[number]>> : Promise<z.input<EndpointDeclaration['config']['responseSchema']>>;
1101
1105
  export { MultipartResult }
1102
1106
  export { MultipartResult as MultipartResult_alias_1 }
1103
1107
  export { MultipartResult as MultipartResult_alias_2 }
@@ -1193,6 +1197,8 @@ export { OnServiceDestroy }
1193
1197
 
1194
1198
  export { OnServiceInit }
1195
1199
 
1200
+ export { optional }
1201
+
1196
1202
  export { OptionalInjectionTokenSchemaType }
1197
1203
 
1198
1204
  declare type OriginType = string | boolean | RegExp;
package/lib/index.d.mts CHANGED
@@ -4,13 +4,14 @@ export { Injectable } from './_tsup-dts-rollup.mjs';
4
4
  export { InjectableOptions } from './_tsup-dts-rollup.mjs';
5
5
  export { InjectableScope } from './_tsup-dts-rollup.mjs';
6
6
  export { InjectableType } from './_tsup-dts-rollup.mjs';
7
- export { ErrorsEnum } from './_tsup-dts-rollup.mjs';
8
7
  export { FactoryNotFound } from './_tsup-dts-rollup.mjs';
9
8
  export { FactoryTokenNotResolved } from './_tsup-dts-rollup.mjs';
10
9
  export { InstanceDestroying } from './_tsup-dts-rollup.mjs';
11
- export { InstanceExpired } from './_tsup-dts-rollup.mjs';
12
10
  export { InstanceNotFound } from './_tsup-dts-rollup.mjs';
13
11
  export { UnknownError } from './_tsup-dts-rollup.mjs';
12
+ export { ErrorsEnum } from './_tsup-dts-rollup.mjs';
13
+ export { DIErrorCode } from './_tsup-dts-rollup.mjs';
14
+ export { DIError } from './_tsup-dts-rollup.mjs';
14
15
  export { Factorable } from './_tsup-dts-rollup.mjs';
15
16
  export { FactorableWithArgs } from './_tsup-dts-rollup.mjs';
16
17
  export { OnServiceInit } from './_tsup-dts-rollup.mjs';
@@ -18,14 +19,13 @@ export { OnServiceDestroy } from './_tsup-dts-rollup.mjs';
18
19
  export { getInjectors } from './_tsup-dts-rollup.mjs';
19
20
  export { Injectors } from './_tsup-dts-rollup.mjs';
20
21
  export { getInjectableToken } from './_tsup-dts-rollup.mjs';
21
- export { createDeferred } from './_tsup-dts-rollup.mjs';
22
- export { Deferred } from './_tsup-dts-rollup.mjs';
23
22
  export { Join } from './_tsup-dts-rollup.mjs';
24
23
  export { UnionToIntersection } from './_tsup-dts-rollup.mjs';
25
24
  export { UnionToOvlds } from './_tsup-dts-rollup.mjs';
26
25
  export { PopUnion } from './_tsup-dts-rollup.mjs';
27
26
  export { IsUnion } from './_tsup-dts-rollup.mjs';
28
27
  export { UnionToArray } from './_tsup-dts-rollup.mjs';
28
+ export { InjectRequest } from './_tsup-dts-rollup.mjs';
29
29
  export { InjectState } from './_tsup-dts-rollup.mjs';
30
30
  export { BaseInstanceHolderManager } from './_tsup-dts-rollup.mjs';
31
31
  export { EventsConfig } from './_tsup-dts-rollup.mjs';
@@ -36,6 +36,7 @@ export { EventEmitterInterface } from './_tsup-dts-rollup.mjs';
36
36
  export { EventEmitter } from './_tsup-dts-rollup.mjs';
37
37
  export { FactoryContext } from './_tsup-dts-rollup.mjs';
38
38
  export { ClassType } from './_tsup-dts-rollup.mjs';
39
+ export { ClassTypeWithoutArguments } from './_tsup-dts-rollup.mjs';
39
40
  export { ClassTypeWithArgument } from './_tsup-dts-rollup.mjs';
40
41
  export { ClassTypeWithOptionalArgument } from './_tsup-dts-rollup.mjs';
41
42
  export { ClassTypeWithInstance } from './_tsup-dts-rollup.mjs';
@@ -52,6 +53,7 @@ export { InjectionTokenType } from './_tsup-dts-rollup.mjs';
52
53
  export { defaultInjectors } from './_tsup-dts-rollup.mjs';
53
54
  export { asyncInject } from './_tsup-dts-rollup.mjs';
54
55
  export { inject } from './_tsup-dts-rollup.mjs';
56
+ export { optional } from './_tsup-dts-rollup.mjs';
55
57
  export { wrapSyncInit } from './_tsup-dts-rollup.mjs';
56
58
  export { provideFactoryContext } from './_tsup-dts-rollup.mjs';
57
59
  export { FactoryRecord } from './_tsup-dts-rollup.mjs';
package/lib/index.d.ts CHANGED
@@ -4,13 +4,14 @@ export { Injectable } from './_tsup-dts-rollup.js';
4
4
  export { InjectableOptions } from './_tsup-dts-rollup.js';
5
5
  export { InjectableScope } from './_tsup-dts-rollup.js';
6
6
  export { InjectableType } from './_tsup-dts-rollup.js';
7
- export { ErrorsEnum } from './_tsup-dts-rollup.js';
8
7
  export { FactoryNotFound } from './_tsup-dts-rollup.js';
9
8
  export { FactoryTokenNotResolved } from './_tsup-dts-rollup.js';
10
9
  export { InstanceDestroying } from './_tsup-dts-rollup.js';
11
- export { InstanceExpired } from './_tsup-dts-rollup.js';
12
10
  export { InstanceNotFound } from './_tsup-dts-rollup.js';
13
11
  export { UnknownError } from './_tsup-dts-rollup.js';
12
+ export { ErrorsEnum } from './_tsup-dts-rollup.js';
13
+ export { DIErrorCode } from './_tsup-dts-rollup.js';
14
+ export { DIError } from './_tsup-dts-rollup.js';
14
15
  export { Factorable } from './_tsup-dts-rollup.js';
15
16
  export { FactorableWithArgs } from './_tsup-dts-rollup.js';
16
17
  export { OnServiceInit } from './_tsup-dts-rollup.js';
@@ -18,14 +19,13 @@ export { OnServiceDestroy } from './_tsup-dts-rollup.js';
18
19
  export { getInjectors } from './_tsup-dts-rollup.js';
19
20
  export { Injectors } from './_tsup-dts-rollup.js';
20
21
  export { getInjectableToken } from './_tsup-dts-rollup.js';
21
- export { createDeferred } from './_tsup-dts-rollup.js';
22
- export { Deferred } from './_tsup-dts-rollup.js';
23
22
  export { Join } from './_tsup-dts-rollup.js';
24
23
  export { UnionToIntersection } from './_tsup-dts-rollup.js';
25
24
  export { UnionToOvlds } from './_tsup-dts-rollup.js';
26
25
  export { PopUnion } from './_tsup-dts-rollup.js';
27
26
  export { IsUnion } from './_tsup-dts-rollup.js';
28
27
  export { UnionToArray } from './_tsup-dts-rollup.js';
28
+ export { InjectRequest } from './_tsup-dts-rollup.js';
29
29
  export { InjectState } from './_tsup-dts-rollup.js';
30
30
  export { BaseInstanceHolderManager } from './_tsup-dts-rollup.js';
31
31
  export { EventsConfig } from './_tsup-dts-rollup.js';
@@ -36,6 +36,7 @@ export { EventEmitterInterface } from './_tsup-dts-rollup.js';
36
36
  export { EventEmitter } from './_tsup-dts-rollup.js';
37
37
  export { FactoryContext } from './_tsup-dts-rollup.js';
38
38
  export { ClassType } from './_tsup-dts-rollup.js';
39
+ export { ClassTypeWithoutArguments } from './_tsup-dts-rollup.js';
39
40
  export { ClassTypeWithArgument } from './_tsup-dts-rollup.js';
40
41
  export { ClassTypeWithOptionalArgument } from './_tsup-dts-rollup.js';
41
42
  export { ClassTypeWithInstance } from './_tsup-dts-rollup.js';
@@ -52,6 +53,7 @@ export { InjectionTokenType } from './_tsup-dts-rollup.js';
52
53
  export { defaultInjectors } from './_tsup-dts-rollup.js';
53
54
  export { asyncInject } from './_tsup-dts-rollup.js';
54
55
  export { inject } from './_tsup-dts-rollup.js';
56
+ export { optional } from './_tsup-dts-rollup.js';
55
57
  export { wrapSyncInit } from './_tsup-dts-rollup.js';
56
58
  export { provideFactoryContext } from './_tsup-dts-rollup.js';
57
59
  export { FactoryRecord } from './_tsup-dts-rollup.js';