@navios/core 0.1.15 → 0.2.0

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.
Files changed (64) hide show
  1. package/dist/_tsup-dts-rollup.d.mts +98 -422
  2. package/dist/_tsup-dts-rollup.d.ts +98 -422
  3. package/dist/index.d.mts +54 -44
  4. package/dist/index.d.ts +54 -44
  5. package/dist/index.js +111 -1023
  6. package/dist/index.mjs +113 -982
  7. package/package.json +3 -2
  8. package/src/adapters/endpoint-adapter.service.mts +3 -6
  9. package/src/adapters/handler-adapter.interface.mts +1 -1
  10. package/src/adapters/multipart-adapter.service.mts +2 -1
  11. package/src/adapters/stream-adapter.service.mts +3 -7
  12. package/src/attribute.factory.mts +1 -1
  13. package/src/config/config.provider.mts +8 -10
  14. package/src/decorators/controller.decorator.mts +3 -6
  15. package/src/decorators/module.decorator.mts +3 -6
  16. package/src/decorators/use-guards.decorator.mts +3 -2
  17. package/src/index.mts +1 -1
  18. package/src/logger/console-logger.service.mts +3 -2
  19. package/src/logger/logger.factory.mts +4 -5
  20. package/src/logger/logger.service.mts +2 -1
  21. package/src/metadata/controller.metadata.mts +3 -2
  22. package/src/metadata/handler.metadata.mts +1 -4
  23. package/src/metadata/index.mts +0 -1
  24. package/src/metadata/module.metadata.mts +3 -2
  25. package/src/navios.application.mts +9 -8
  26. package/src/navios.factory.mts +4 -2
  27. package/src/services/controller-adapter.service.mts +13 -12
  28. package/src/services/guard-runner.service.mts +4 -6
  29. package/src/services/module-loader.service.mts +4 -2
  30. package/src/tokens/application.token.mts +1 -1
  31. package/src/tokens/execution-context.token.mts +2 -1
  32. package/src/tokens/reply.token.mts +1 -1
  33. package/src/tokens/request.token.mts +1 -1
  34. package/src/metadata/injectable.metadata.mts +0 -11
  35. package/src/service-locator/__tests__/injectable.spec.mts +0 -171
  36. package/src/service-locator/__tests__/injection-token.spec.mts +0 -129
  37. package/src/service-locator/decorators/get-injectable-token.mts +0 -19
  38. package/src/service-locator/decorators/index.mts +0 -2
  39. package/src/service-locator/decorators/injectable.decorator.mts +0 -113
  40. package/src/service-locator/enums/index.mts +0 -1
  41. package/src/service-locator/enums/injectable-scope.enum.mts +0 -10
  42. package/src/service-locator/errors/errors.enum.mts +0 -8
  43. package/src/service-locator/errors/factory-not-found.mts +0 -8
  44. package/src/service-locator/errors/factory-token-not-resolved.mts +0 -10
  45. package/src/service-locator/errors/index.mts +0 -7
  46. package/src/service-locator/errors/instance-destroying.mts +0 -8
  47. package/src/service-locator/errors/instance-expired.mts +0 -8
  48. package/src/service-locator/errors/instance-not-found.mts +0 -8
  49. package/src/service-locator/errors/unknown-error.mts +0 -15
  50. package/src/service-locator/event-emitter.mts +0 -107
  51. package/src/service-locator/index.mts +0 -15
  52. package/src/service-locator/inject.mts +0 -42
  53. package/src/service-locator/injection-token.mts +0 -92
  54. package/src/service-locator/injector.mts +0 -18
  55. package/src/service-locator/interfaces/factory.interface.mts +0 -11
  56. package/src/service-locator/override.mts +0 -22
  57. package/src/service-locator/proxy-service-locator.mts +0 -99
  58. package/src/service-locator/resolve-service.mts +0 -46
  59. package/src/service-locator/service-locator-abstract-factory-context.mts +0 -23
  60. package/src/service-locator/service-locator-event-bus.mts +0 -96
  61. package/src/service-locator/service-locator-instance-holder.mts +0 -63
  62. package/src/service-locator/service-locator-manager.mts +0 -89
  63. package/src/service-locator/service-locator.mts +0 -535
  64. package/src/service-locator/sync-injector.mts +0 -66
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@navios/core",
3
- "version": "0.1.15",
3
+ "version": "0.2.0",
4
4
  "author": {
5
5
  "name": "Oleksandr Hanzha",
6
6
  "email": "alex@granted.name"
@@ -11,7 +11,7 @@
11
11
  "url": "https://github.com/Arilas/navios.git"
12
12
  },
13
13
  "license": "MIT",
14
- "typings": "./dist/index.d.ts",
14
+ "typings": "./dist/src/index.d.mts",
15
15
  "main": "./dist/index.js",
16
16
  "module": "./dist/index.mjs",
17
17
  "peerDependencies": {
@@ -40,6 +40,7 @@
40
40
  "dependencies": {
41
41
  "@fastify/cors": "^11.0.1",
42
42
  "@fastify/multipart": "^9.0.3",
43
+ "@navios/di": "^0.1.2",
43
44
  "fastify": "^5.3.2",
44
45
  "fastify-type-provider-zod": "^4.0.2"
45
46
  }
@@ -1,15 +1,12 @@
1
1
  import type { BaseEndpointConfig } from '@navios/common'
2
+ import type { ClassType } from '@navios/di'
2
3
  import type { FastifyReply, FastifyRequest } from 'fastify'
3
4
 
5
+ import { inject, Injectable, InjectionToken } from '@navios/di'
6
+
4
7
  import type { HandlerMetadata } from '../metadata/index.mjs'
5
- import type { ClassType } from '../service-locator/index.mjs'
6
8
  import type { ExecutionContext } from '../services/index.mjs'
7
9
 
8
- import {
9
- inject,
10
- Injectable,
11
- InjectionToken,
12
- } from '../service-locator/index.mjs'
13
10
  import { StreamAdapterService } from './stream-adapter.service.mjs'
14
11
 
15
12
  export const EndpointAdapterToken =
@@ -1,7 +1,7 @@
1
+ import type { ClassType } from '@navios/di'
1
2
  import type { FastifyReply, FastifyRequest } from 'fastify'
2
3
 
3
4
  import type { HandlerMetadata } from '../metadata/index.mjs'
4
- import type { ClassType } from '../service-locator/index.mjs'
5
5
  import type { ExecutionContext } from '../services/index.mjs'
6
6
 
7
7
  export interface HandlerAdapterInterface {
@@ -3,11 +3,12 @@ import type { BaseEndpointConfig } from '@navios/common'
3
3
  import type { FastifyRequest } from 'fastify'
4
4
  import type { AnyZodObject, ZodRawShape } from 'zod'
5
5
 
6
+ import { Injectable, InjectionToken } from '@navios/di'
7
+
6
8
  import { ZodArray, ZodObject, ZodOptional } from 'zod'
7
9
 
8
10
  import type { HandlerMetadata } from '../metadata/index.mjs'
9
11
 
10
- import { Injectable, InjectionToken } from '../service-locator/index.mjs'
11
12
  import { EndpointAdapterService } from './endpoint-adapter.service.mjs'
12
13
 
13
14
  export const MultipartAdapterToken =
@@ -1,17 +1,13 @@
1
1
  import type { BaseStreamConfig } from '@navios/common'
2
+ import type { ClassType } from '@navios/di'
2
3
  import type { FastifyReply, FastifyRequest } from 'fastify'
3
4
 
5
+ import { inject, Injectable, InjectionToken } from '@navios/di'
6
+
4
7
  import type { HandlerMetadata } from '../metadata/index.mjs'
5
- import type { ClassType } from '../service-locator/index.mjs'
6
8
  import type { ExecutionContext } from '../services/index.mjs'
7
9
  import type { HandlerAdapterInterface } from './handler-adapter.interface.mjs'
8
10
 
9
- import {
10
- inject,
11
- Injectable,
12
- InjectionToken,
13
- } from '../service-locator/index.mjs'
14
-
15
11
  export const StreamAdapterToken = InjectionToken.create<StreamAdapterService>(
16
12
  Symbol.for('StreamAdapterService'),
17
13
  )
@@ -1,3 +1,4 @@
1
+ import type { ClassType } from '@navios/di'
1
2
  import type { z, ZodType } from 'zod'
2
3
 
3
4
  import type {
@@ -5,7 +6,6 @@ import type {
5
6
  HandlerMetadata,
6
7
  ModuleMetadata,
7
8
  } from './metadata/index.mjs'
8
- import type { ClassType } from './service-locator/index.mjs'
9
9
 
10
10
  import {
11
11
  getControllerMetadata,
@@ -1,14 +1,15 @@
1
- import { z } from 'zod'
2
-
3
- import type { ConfigService } from './config-service.interface.mjs'
4
-
5
- import { Logger } from '../logger/index.mjs'
6
1
  import {
7
2
  Injectable,
8
3
  InjectableType,
9
4
  InjectionToken,
10
5
  syncInject,
11
- } from '../service-locator/index.mjs'
6
+ } from '@navios/di'
7
+
8
+ import { z } from 'zod'
9
+
10
+ import type { ConfigService } from './config-service.interface.mjs'
11
+
12
+ import { Logger } from '../logger/index.mjs'
12
13
  import { ConfigServiceInstance } from './config.service.mjs'
13
14
 
14
15
  export const ConfigProviderOptions = z.object({
@@ -52,8 +53,5 @@ export class ConfigProviderFactory {
52
53
  export function provideConfig<ConfigMap extends Record<string, unknown>>(
53
54
  options: z.input<typeof ConfigProviderOptions>,
54
55
  ) {
55
- return InjectionToken.bound(ConfigProvider, options) as InjectionToken<
56
- ConfigServiceInstance<ConfigMap>,
57
- undefined
58
- >
56
+ return InjectionToken.bound(ConfigProvider, options)
59
57
  }
@@ -1,11 +1,8 @@
1
- import type { ClassType } from '../service-locator/index.mjs'
1
+ import type { ClassType } from '@navios/di'
2
+
3
+ import { Injectable, InjectableScope, InjectionToken } from '@navios/di'
2
4
 
3
5
  import { getControllerMetadata } from '../metadata/index.mjs'
4
- import {
5
- Injectable,
6
- InjectableScope,
7
- InjectionToken,
8
- } from '../service-locator/index.mjs'
9
6
 
10
7
  export interface ControllerOptions {
11
8
  guards?: ClassType[] | Set<ClassType>
@@ -1,11 +1,8 @@
1
- import type { ClassType } from '../service-locator/index.mjs'
1
+ import type { ClassType } from '@navios/di'
2
+
3
+ import { Injectable, InjectableScope, InjectionToken } from '@navios/di'
2
4
 
3
5
  import { getModuleMetadata } from '../metadata/index.mjs'
4
- import {
5
- Injectable,
6
- InjectableScope,
7
- InjectionToken,
8
- } from '../service-locator/index.mjs'
9
6
 
10
7
  export interface ModuleOptions {
11
8
  controllers?: ClassType[] | Set<ClassType>
@@ -1,9 +1,10 @@
1
- import type { CanActivate } from '../interfaces/index.mjs'
2
1
  import type {
3
2
  ClassType,
4
3
  ClassTypeWithInstance,
5
4
  InjectionToken,
6
- } from '../service-locator/index.mjs'
5
+ } from '@navios/di'
6
+
7
+ import type { CanActivate } from '../interfaces/index.mjs'
7
8
 
8
9
  import {
9
10
  getControllerMetadata,
package/src/index.mts CHANGED
@@ -1,3 +1,4 @@
1
+ export * from '@navios/di'
1
2
  export * from './adapters/index.mjs'
2
3
  export * from './config/index.mjs'
3
4
  export * from './decorators/index.mjs'
@@ -5,7 +6,6 @@ export * from './exceptions/index.mjs'
5
6
  export * from './interfaces/index.mjs'
6
7
  export * from './logger/index.mjs'
7
8
  export * from './metadata/index.mjs'
8
- export * from './service-locator/index.mjs'
9
9
  export * from './services/index.mjs'
10
10
  export * from './tokens/index.mjs'
11
11
  export * from './attribute.factory.mjs'
@@ -2,10 +2,11 @@ import type { InspectOptions } from 'util'
2
2
 
3
3
  import { inspect } from 'util'
4
4
 
5
+ import { getGlobalServiceLocator, Injectable } from '@navios/di'
6
+
5
7
  import type { LogLevel } from './log-levels.mjs'
6
8
  import type { LoggerService } from './logger-service.interface.mjs'
7
9
 
8
- import { getServiceLocator, Injectable } from '../service-locator/index.mjs'
9
10
  import { Request } from '../tokens/index.mjs'
10
11
  import {
11
12
  clc,
@@ -178,7 +179,7 @@ export class ConsoleLogger implements LoggerService {
178
179
  this.originalContext = context
179
180
  }
180
181
  if (opts?.requestId) {
181
- const locator = getServiceLocator()
182
+ const locator = getGlobalServiceLocator()
182
183
  locator
183
184
  .getEventBus()
184
185
  .on(locator.getInstanceIdentifier(Request, undefined), 'create', () => {
@@ -1,10 +1,9 @@
1
+ import { Injectable, InjectableType, InjectionToken } from '@navios/di'
2
+
1
3
  import { z } from 'zod'
2
4
 
3
- import {
4
- Injectable,
5
- InjectableType,
6
- InjectionToken,
7
- } from '../service-locator/index.mjs'
5
+ import type { LoggerService } from './logger-service.interface.mjs'
6
+
8
7
  import { LoggerInstance } from './logger.service.mjs'
9
8
 
10
9
  export const LoggerInjectionToken = 'LoggerInjectionToken'
@@ -1,7 +1,8 @@
1
+ import { Injectable } from '@navios/di'
2
+
1
3
  import type { LogLevel } from './log-levels.mjs'
2
4
  import type { LoggerService } from './logger-service.interface.mjs'
3
5
 
4
- import { Injectable } from '../service-locator/index.mjs'
5
6
  import { ConsoleLogger } from './console-logger.service.mjs'
6
7
  import { isLogLevelEnabled, isObject } from './utils/index.mjs'
7
8
 
@@ -1,9 +1,10 @@
1
- import type { CanActivate } from '../interfaces/index.mjs'
2
1
  import type {
3
2
  ClassType,
4
3
  ClassTypeWithInstance,
5
4
  InjectionToken,
6
- } from '../service-locator/index.mjs'
5
+ } from '@navios/di'
6
+
7
+ import type { CanActivate } from '../interfaces/index.mjs'
7
8
  import type { HandlerMetadata } from './handler.metadata.mjs'
8
9
 
9
10
  import { getAllEndpointMetadata } from './handler.metadata.mjs'
@@ -1,12 +1,9 @@
1
1
  import type { HttpMethod } from '@navios/common'
2
+ import type { ClassTypeWithInstance, InjectionToken } from '@navios/di'
2
3
  import type { HttpHeader } from 'fastify/types/utils.js'
3
4
 
4
5
  import type { HandlerAdapterInterface } from '../adapters/index.mjs'
5
6
  import type { CanActivate } from '../interfaces/index.mjs'
6
- import type {
7
- ClassTypeWithInstance,
8
- InjectionToken,
9
- } from '../service-locator/index.mjs'
10
7
 
11
8
  export const EndpointMetadataKey = Symbol('EndpointMetadataKey')
12
9
 
@@ -1,4 +1,3 @@
1
1
  export * from './controller.metadata.mjs'
2
2
  export * from './handler.metadata.mjs'
3
- export * from './injectable.metadata.mjs'
4
3
  export * from './module.metadata.mjs'
@@ -1,9 +1,10 @@
1
- import type { CanActivate } from '../index.mjs'
2
1
  import type {
3
2
  ClassType,
4
3
  ClassTypeWithInstance,
5
4
  InjectionToken,
6
- } from '../service-locator/index.mjs'
5
+ } from '@navios/di'
6
+
7
+ import type { CanActivate } from '../index.mjs'
7
8
 
8
9
  export const ModuleMetadataKey = Symbol('ControllerMetadataKey')
9
10
 
@@ -1,11 +1,19 @@
1
1
  import type { FastifyCorsOptions } from '@fastify/cors'
2
2
  import type { FastifyMultipartOptions } from '@fastify/multipart'
3
+ import type { ClassTypeWithInstance } from '@navios/di'
3
4
  import type {
4
5
  FastifyInstance,
5
6
  FastifyListenOptions,
6
7
  FastifyServerOptions,
7
8
  } from 'fastify'
8
9
 
10
+ import {
11
+ getGlobalServiceLocator,
12
+ inject,
13
+ Injectable,
14
+ syncInject,
15
+ } from '@navios/di'
16
+
9
17
  import cors from '@fastify/cors'
10
18
  import multipart from '@fastify/multipart'
11
19
  import { fastify } from 'fastify'
@@ -16,16 +24,9 @@ import {
16
24
 
17
25
  import type { NaviosModule } from './interfaces/index.mjs'
18
26
  import type { LoggerService, LogLevel } from './logger/index.mjs'
19
- import type { ClassTypeWithInstance } from './service-locator/index.mjs'
20
27
 
21
28
  import { HttpException } from './exceptions/index.mjs'
22
29
  import { Logger, PinoWrapper } from './logger/index.mjs'
23
- import {
24
- getServiceLocator,
25
- inject,
26
- Injectable,
27
- syncInject,
28
- } from './service-locator/index.mjs'
29
30
  import {
30
31
  ControllerAdapterService,
31
32
  ModuleLoaderService,
@@ -75,7 +76,7 @@ export class NaviosApplication {
75
76
  await this.moduleLoader.loadModules(this.appModule)
76
77
  this.server = await this.getFastifyInstance(this.options)
77
78
  this.configureFastifyInstance(this.server)
78
- getServiceLocator().registerInstance(Application, this.server)
79
+ getGlobalServiceLocator().storeInstance(this.server, Application)
79
80
  // Add schema validator and serializer
80
81
  this.server.setValidatorCompiler(validatorCompiler)
81
82
  this.server.setSerializerCompiler(serializerCompiler)
@@ -1,13 +1,15 @@
1
+ import type { ClassTypeWithInstance } from '@navios/di'
2
+
3
+ import { inject } from '@navios/di'
4
+
1
5
  import type { NaviosModule } from './interfaces/index.mjs'
2
6
  import type {
3
7
  NaviosApplicationContextOptions,
4
8
  NaviosApplicationOptions,
5
9
  } from './navios.application.mjs'
6
- import type { ClassTypeWithInstance } from './service-locator/index.mjs'
7
10
 
8
11
  import { isNil, LoggerInstance } from './logger/index.mjs'
9
12
  import { NaviosApplication } from './navios.application.mjs'
10
- import { inject } from './service-locator/index.mjs'
11
13
 
12
14
  export class NaviosFactory {
13
15
  static async create(
@@ -1,19 +1,20 @@
1
+ import type { ClassType } from '@navios/di'
1
2
  import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify'
2
3
  import type { ZodTypeProvider } from 'fastify-type-provider-zod'
3
4
 
4
- import type { HandlerAdapterInterface } from '../adapters/index.mjs'
5
- import type { ModuleMetadata } from '../metadata/index.mjs'
6
- import type { ClassType } from '../service-locator/index.mjs'
7
-
8
- import { Logger } from '../logger/index.mjs'
9
- import { extractControllerMetadata } from '../metadata/index.mjs'
10
5
  import {
11
- getServiceLocator,
6
+ getGlobalServiceLocator,
12
7
  inject,
13
8
  Injectable,
14
9
  InjectionToken,
15
10
  syncInject,
16
- } from '../service-locator/index.mjs'
11
+ } from '@navios/di'
12
+
13
+ import type { HandlerAdapterInterface } from '../adapters/index.mjs'
14
+ import type { ModuleMetadata } from '../metadata/index.mjs'
15
+
16
+ import { Logger } from '../logger/index.mjs'
17
+ import { extractControllerMetadata } from '../metadata/index.mjs'
17
18
  import { ExecutionContextToken, Reply, Request } from '../tokens/index.mjs'
18
19
  import { ExecutionContext } from './execution-context.mjs'
19
20
  import { GuardRunnerService } from './guard-runner.service.mjs'
@@ -100,11 +101,11 @@ export class ControllerAdapterService {
100
101
  executionContext: ExecutionContext,
101
102
  handler: (request: FastifyRequest, reply: FastifyReply) => Promise<void>,
102
103
  ) {
103
- const locator = getServiceLocator()
104
+ const locator = getGlobalServiceLocator()
104
105
  return async (request: FastifyRequest, reply: FastifyReply) => {
105
- locator.registerInstance(Request, request)
106
- locator.registerInstance(Reply, reply)
107
- locator.registerInstance(ExecutionContextToken, executionContext)
106
+ locator.storeInstance(request, Request)
107
+ locator.storeInstance(reply, Reply)
108
+ locator.storeInstance(executionContext, ExecutionContextToken)
108
109
  executionContext.provideRequest(request)
109
110
  executionContext.provideReply(reply)
110
111
  try {
@@ -1,13 +1,11 @@
1
+ import type { ClassTypeWithInstance } from '@navios/di'
2
+
3
+ import { inject, Injectable, InjectionToken } from '@navios/di'
4
+
1
5
  import type { CanActivate } from '../interfaces/index.mjs'
2
- import type { ClassTypeWithInstance } from '../service-locator/index.mjs'
3
6
  import type { ExecutionContext } from './execution-context.mjs'
4
7
 
5
8
  import { HttpException } from '../exceptions/index.mjs'
6
- import {
7
- inject,
8
- Injectable,
9
- InjectionToken,
10
- } from '../service-locator/index.mjs'
11
9
 
12
10
  @Injectable()
13
11
  export class GuardRunnerService {
@@ -1,10 +1,12 @@
1
+ import type { ClassTypeWithInstance } from '@navios/di'
2
+
3
+ import { inject, Injectable, syncInject } from '@navios/di'
4
+
1
5
  import type { NaviosModule } from '../interfaces/index.mjs'
2
6
  import type { ModuleMetadata } from '../metadata/index.mjs'
3
- import type { ClassTypeWithInstance } from '../service-locator/index.mjs'
4
7
 
5
8
  import { Logger } from '../logger/index.mjs'
6
9
  import { extractModuleMetadata } from '../metadata/index.mjs'
7
- import { inject, Injectable, syncInject } from '../service-locator/index.mjs'
8
10
 
9
11
  @Injectable()
10
12
  export class ModuleLoaderService {
@@ -1,6 +1,6 @@
1
1
  import type { FastifyInstance } from 'fastify'
2
2
 
3
- import { InjectionToken } from '../service-locator/index.mjs'
3
+ import { InjectionToken } from '@navios/di'
4
4
 
5
5
  const ApplicationInjectionToken = 'ApplicationInjectionToken'
6
6
 
@@ -1,4 +1,5 @@
1
- import { InjectionToken } from '../service-locator/index.mjs'
1
+ import { InjectionToken } from '@navios/di'
2
+
2
3
  import { ExecutionContext } from '../services/index.mjs'
3
4
 
4
5
  export const ExecutionContextInjectionToken = 'ExecutionContextInjectionToken'
@@ -1,6 +1,6 @@
1
1
  import type { FastifyReply } from 'fastify'
2
2
 
3
- import { InjectionToken } from '../service-locator/index.mjs'
3
+ import { InjectionToken } from '@navios/di'
4
4
 
5
5
  const ReplyInjectionToken = 'ReplyInjectionToken'
6
6
 
@@ -1,6 +1,6 @@
1
1
  import type { FastifyRequest } from 'fastify'
2
2
 
3
- import { InjectionToken } from '../service-locator/index.mjs'
3
+ import { InjectionToken } from '@navios/di'
4
4
 
5
5
  const RequestInjectionToken = 'RequestInjectionToken'
6
6
 
@@ -1,11 +0,0 @@
1
- import type {
2
- InjectableScope,
3
- InjectableType,
4
- InjectionToken,
5
- } from '../index.mjs'
6
-
7
- export interface InjectableMetadata<Instance = any, Schema = any> {
8
- type: InjectableType
9
- scope: InjectableScope
10
- token: InjectionToken<Instance, Schema>
11
- }
@@ -1,171 +0,0 @@
1
- import { describe, expect, it } from 'vitest'
2
- import { z } from 'zod'
3
-
4
- import {
5
- getInjectableToken,
6
- Injectable,
7
- InjectableType,
8
- } from '../decorators/index.mjs'
9
- import { InjectableScope } from '../enums/index.mjs'
10
- import { syncInject } from '../index.mjs'
11
- import { inject } from '../inject.mjs'
12
- import { InjectionToken } from '../injection-token.mjs'
13
- import { getServiceLocator } from '../injector.mjs'
14
-
15
- describe('Injectable decorator', () => {
16
- it('should work with class', async () => {
17
- @Injectable()
18
- class Test {}
19
-
20
- const value = await inject(Test)
21
- expect(value).toBeInstanceOf(Test)
22
- })
23
-
24
- it('should work with inner inject', async () => {
25
- @Injectable()
26
- class Test {
27
- makeFoo() {
28
- return 'foo'
29
- }
30
- }
31
-
32
- @Injectable()
33
- class Test2 {
34
- fooMaker = inject(Test)
35
-
36
- async makeFoo() {
37
- const fooMaker = await this.fooMaker
38
- return fooMaker.makeFoo()
39
- }
40
- }
41
-
42
- const value = await inject(Test2)
43
- expect(value).toBeInstanceOf(Test2)
44
- const result = await value.makeFoo()
45
- expect(result).toBe('foo')
46
- })
47
-
48
- it('should work with factory', async () => {
49
- @Injectable({ type: InjectableType.Factory })
50
- class Test {
51
- create() {
52
- return 'foo'
53
- }
54
- }
55
-
56
- const value = await inject(Test)
57
- expect(value).toBe('foo')
58
- })
59
- it('should work with request scope', async () => {
60
- @Injectable({
61
- scope: InjectableScope.Instance,
62
- type: InjectableType.Factory,
63
- })
64
- class Test {
65
- create() {
66
- return Date.now()
67
- }
68
- }
69
-
70
- const value = await inject(Test)
71
- await new Promise((resolve) => setTimeout(resolve, 10))
72
- const value2 = await inject(Test)
73
- expect(value).not.toBe(value2)
74
- })
75
-
76
- it('should work with injection token', async () => {
77
- const token = InjectionToken.create('Test')
78
-
79
- @Injectable({ token })
80
- class Test {}
81
-
82
- const value = await inject(token)
83
- expect(value).toBeInstanceOf(Test)
84
- })
85
-
86
- it('should work with injection token and schema', async () => {
87
- class TestFoo {
88
- constructor(public readonly foo: string) {}
89
- }
90
- const token = InjectionToken.create(
91
- TestFoo,
92
- z.object({
93
- foo: z.string(),
94
- }),
95
- )
96
-
97
- @Injectable({ token, type: InjectableType.Factory })
98
- class Test {
99
- create(ctx: any, args: { foo: string }) {
100
- return new TestFoo(args.foo)
101
- }
102
- }
103
-
104
- const value = await inject(token, { foo: 'bar' })
105
- const differentValue = await inject(token, { foo: 'baz' })
106
- const sameValue = await inject(token, { foo: 'bar' })
107
- expect(value).toBeInstanceOf(TestFoo)
108
- expect(value.foo).toBe('bar')
109
- expect(differentValue).toBeInstanceOf(TestFoo)
110
- expect(differentValue.foo).toBe('baz')
111
- expect(value).not.toBe(differentValue)
112
- expect(value).toBe(sameValue)
113
- })
114
- it('should work with invalidation', async () => {
115
- @Injectable()
116
- class Test {
117
- value = Date.now()
118
- }
119
-
120
- @Injectable()
121
- class Test2 {
122
- test = inject(Test)
123
-
124
- async makeFoo() {
125
- const test = await this.test
126
- return test.value
127
- }
128
- }
129
- const identifier = getServiceLocator().getInstanceIdentifier(
130
- getInjectableToken(Test),
131
- undefined,
132
- )
133
- const inst1 = await inject(Test2)
134
- expect(inst1).toBeInstanceOf(Test2)
135
- const result1 = await inst1.makeFoo()
136
- const inst2 = await inject(Test2)
137
- expect(inst1).toBe(inst2)
138
- const result2 = await inst2.makeFoo()
139
- await getServiceLocator().invalidate(identifier)
140
- await new Promise((resolve) => setTimeout(resolve, 10))
141
- const inst3 = await inject(Test2)
142
- expect(inst1).not.toBe(inst3)
143
- const result3 = await inst3.makeFoo()
144
- expect(result1).not.toBe(result3)
145
- expect(result2).not.toBe(result3)
146
- expect(result1).toBe(result2)
147
- })
148
-
149
- it('should work with syncInject', async () => {
150
- @Injectable()
151
- class Test {
152
- value = Date.now()
153
- }
154
-
155
- @Injectable()
156
- class Test2 {
157
- test = syncInject(Test)
158
-
159
- makeFoo() {
160
- return this.test.value
161
- }
162
- }
163
- const inst1 = await inject(Test2)
164
- expect(inst1).toBeInstanceOf(Test2)
165
- const result1 = inst1.makeFoo()
166
- const inst2 = await inject(Test2)
167
- expect(inst1).toBe(inst2)
168
- const result2 = await inst2.makeFoo()
169
- expect(result1).toBe(result2)
170
- })
171
- })