@navios/core 0.1.3 → 0.1.4

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.
@@ -6,8 +6,17 @@ import type { ServiceLocatorAbstractFactoryContext } from './service-locator-abs
6
6
  import type { ServiceLocatorInstanceHolder } from './service-locator-instance-holder.mjs'
7
7
 
8
8
  import { InjectableScope } from './enums/index.mjs'
9
- import { ErrorsEnum, FactoryNotFound, UnknownError } from './errors/index.mjs'
10
- import { getInjectableToken } from './index.mjs'
9
+ import {
10
+ ErrorsEnum,
11
+ FactoryNotFound,
12
+ FactoryTokenNotResolved,
13
+ UnknownError,
14
+ } from './errors/index.mjs'
15
+ import {
16
+ BoundInjectionToken,
17
+ FactoryInjectionToken,
18
+ getInjectableToken,
19
+ } from './index.mjs'
11
20
  import { InjectionToken } from './injection-token.mjs'
12
21
  import { ServiceLocatorEventBus } from './service-locator-event-bus.mjs'
13
22
  import {
@@ -86,7 +95,7 @@ export class ServiceLocator {
86
95
  }
87
96
  }
88
97
 
89
- public getInstanceIdentifier<
98
+ private resolveTokenArgs<
90
99
  Instance,
91
100
  Schema extends AnyZodObject | ZodOptional<AnyZodObject> | undefined,
92
101
  >(
@@ -96,18 +105,57 @@ export class ServiceLocator {
96
105
  : Schema extends ZodOptional<AnyZodObject>
97
106
  ? z.input<Schema> | undefined
98
107
  : undefined,
99
- ): string {
100
- const validatedArgs = token.schema
101
- ? token.schema.safeParse(args)
102
- : undefined
108
+ ):
109
+ | [
110
+ undefined,
111
+ Schema extends AnyZodObject
112
+ ? z.input<Schema>
113
+ : Schema extends ZodOptional<AnyZodObject>
114
+ ? z.input<Schema> | undefined
115
+ : undefined,
116
+ ]
117
+ | [FactoryTokenNotResolved | UnknownError] {
118
+ let realArgs = args
119
+ if (token instanceof BoundInjectionToken) {
120
+ realArgs = token.value
121
+ } else if (token instanceof FactoryInjectionToken) {
122
+ if (token.resolved) {
123
+ realArgs = token.value
124
+ } else {
125
+ return [new FactoryTokenNotResolved(token.name)]
126
+ }
127
+ }
128
+ if (!token.schema) {
129
+ return [undefined, realArgs]
130
+ }
131
+ const validatedArgs = token.schema?.safeParse(realArgs)
103
132
  if (validatedArgs && !validatedArgs.success) {
104
133
  this.logger?.error(
105
134
  `[ServiceLocator]#getInstance(): Error validating args for ${token.name.toString()}`,
106
135
  validatedArgs.error,
107
136
  )
108
- throw new UnknownError(validatedArgs.error)
137
+ return [new UnknownError(validatedArgs.error)]
138
+ }
139
+ // @ts-expect-error We return correct type
140
+ return [undefined, validatedArgs?.data]
141
+ }
142
+
143
+ public getInstanceIdentifier<
144
+ Instance,
145
+ Schema extends AnyZodObject | ZodOptional<AnyZodObject> | undefined,
146
+ >(
147
+ token: InjectionToken<Instance, Schema>,
148
+ args: Schema extends AnyZodObject
149
+ ? z.input<Schema>
150
+ : Schema extends ZodOptional<AnyZodObject>
151
+ ? z.input<Schema> | undefined
152
+ : undefined,
153
+ ): string {
154
+ const [err, realArgs] = this.resolveTokenArgs(token, args)
155
+ if (err) {
156
+ throw err
109
157
  }
110
- return this.makeInstanceName(token, validatedArgs)
158
+ return this.makeInstanceName(token, realArgs)
111
159
  }
112
160
 
113
161
  public async getInstance<
@@ -121,17 +169,17 @@ export class ServiceLocator {
121
169
  ? z.input<Schema> | undefined
122
170
  : undefined,
123
171
  ): Promise<[undefined, Instance] | [UnknownError | FactoryNotFound]> {
124
- const validatedArgs = token.schema
125
- ? token.schema.safeParse(args)
126
- : undefined
127
- if (validatedArgs && !validatedArgs.success) {
128
- this.logger?.error(
129
- `[ServiceLocator]#getInstance(): Error validating args for ${token.name.toString()}`,
130
- validatedArgs.error,
131
- )
132
- return [new UnknownError(validatedArgs.error)]
172
+ const [err, realArgs] = this.resolveTokenArgs(token, args)
173
+ if (err instanceof UnknownError) {
174
+ throw err
175
+ } else if (
176
+ err instanceof FactoryTokenNotResolved &&
177
+ token instanceof FactoryInjectionToken
178
+ ) {
179
+ await token.resolve()
180
+ return this.getInstance(token, args)
133
181
  }
134
- const instanceName = this.makeInstanceName(token, validatedArgs)
182
+ const instanceName = this.makeInstanceName(token, realArgs)
135
183
  const [error, holder] = this.manager.get(instanceName)
136
184
  if (!error) {
137
185
  if (holder.status === ServiceLocatorInstanceHolderStatus.Creating) {
@@ -167,7 +215,8 @@ export class ServiceLocator {
167
215
  default:
168
216
  return [error]
169
217
  }
170
- return this.createInstance(instanceName, token, args)
218
+ // @ts-expect-error TS2322 It's validated
219
+ return this.createInstance(instanceName, token, realArgs)
171
220
  }
172
221
 
173
222
  public async getOrThrowInstance<
@@ -213,13 +262,22 @@ export class ServiceLocator {
213
262
  this.logger?.log(
214
263
  `[ServiceLocator]#createInstance() Creating instance for ${instanceName}`,
215
264
  )
265
+ let realToken =
266
+ token instanceof BoundInjectionToken ||
267
+ token instanceof FactoryInjectionToken
268
+ ? token.token
269
+ : token
216
270
  if (
217
- this.abstractFactories.has(token) ||
218
- this.instanceFactories.has(token)
271
+ this.abstractFactories.has(realToken) ||
272
+ this.instanceFactories.has(realToken)
219
273
  ) {
220
- return this.createInstanceFromAbstractFactory(instanceName, token, args)
274
+ return this.createInstanceFromAbstractFactory(
275
+ instanceName,
276
+ realToken,
277
+ args,
278
+ )
221
279
  } else {
222
- return [new FactoryNotFound(token.name.toString())]
280
+ return [new FactoryNotFound(realToken.name.toString())]
223
281
  }
224
282
  }
225
283
 
@@ -276,7 +334,7 @@ export class ServiceLocator {
276
334
  )
277
335
  })
278
336
  }
279
- if (holder.ttl === 0) {
337
+ if (holder.ttl === 0 || !shouldStore) {
280
338
  // One time instance
281
339
  await this.invalidate(instanceName)
282
340
  }
@@ -371,17 +429,11 @@ export class ServiceLocator {
371
429
  ? z.input<Schema> | undefined
372
430
  : undefined,
373
431
  ): Instance | null {
374
- const validatedArgs = token.schema
375
- ? token.schema.safeParse(args)
376
- : undefined
377
- if (validatedArgs && !validatedArgs.success) {
378
- this.logger?.error(
379
- `[ServiceLocator]#getInstance(): Error validating args for ${token.name.toString()}`,
380
- validatedArgs.error,
381
- )
382
- throw new UnknownError(validatedArgs.error)
432
+ const [err, realArgs] = this.resolveTokenArgs(token, args)
433
+ if (err) {
434
+ return null
383
435
  }
384
- const instanceName = this.makeInstanceName(token, validatedArgs)
436
+ const instanceName = this.makeInstanceName(token, realArgs)
385
437
  const [error, holder] = this.manager.get(instanceName)
386
438
  if (error) {
387
439
  return null
@@ -24,14 +24,6 @@ export function syncInject<
24
24
  Token extends InjectionToken<T>,
25
25
  S extends AnyZodObject | unknown = Token['schema'],
26
26
  >(token: Token, args?: S extends AnyZodObject ? z.input<S> : never): T {
27
- if (token.schema) {
28
- const parsed = token.schema.safeParse(args)
29
- if (!parsed.success) {
30
- throw new Error(
31
- `[ServiceLocator] Invalid arguments for ${token.name.toString()}: ${parsed.error}`,
32
- )
33
- }
34
- }
35
27
  let realToken: InjectionToken<T, S> = token
36
28
  if (!(token instanceof InjectionToken)) {
37
29
  realToken = getInjectableToken(token) as InjectionToken<T, S>