All files / src/interfaces container.interface.mts

0% Statements 0/0
0% Branches 0/0
0% Functions 0/0
0% Lines 0/0

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96                                                                                                                                                                                               
import type { z, ZodType } from 'zod/v4'
 
import type {
  BoundInjectionToken,
  ClassType,
  ClassTypeWithArgument,
  FactoryInjectionToken,
  InjectionToken,
  InjectionTokenSchemaType,
} from '../token/injection-token.mjs'
import type { Join, UnionToArray } from '../utils/types.mjs'
import type { Factorable } from './factory.interface.mjs'
 
/**
 * Interface for dependency injection containers.
 * Both Container and ScopedContainer implement this interface,
 * allowing them to be used interchangeably in factory contexts.
 */
export interface IContainer {
  /**
   * Gets an instance from the container.
   */
  // #1 Simple class
  get<T extends ClassType>(
    token: T,
  ): InstanceType<T> extends Factorable<infer R>
    ? Promise<R>
    : Promise<InstanceType<T>>
  // #1.1 Simple class with args
  get<T extends ClassTypeWithArgument<R>, R>(
    token: T,
    args: R,
  ): Promise<InstanceType<T>>
  // #2 Token with required Schema
  get<T, S extends InjectionTokenSchemaType>(
    token: InjectionToken<T, S>,
    args: z.input<S>,
  ): Promise<T>
  // #3 Token with optional Schema
  get<T, S extends InjectionTokenSchemaType, R extends boolean>(
    token: InjectionToken<T, S, R>,
  ): R extends false
    ? Promise<T>
    : S extends ZodType<infer Type>
      ? `Error: Your token requires args: ${Join<
          UnionToArray<keyof Type>,
          ', '
        >}`
      : 'Error: Your token requires args'
  // #4 Token with no Schema
  get<T>(token: InjectionToken<T, undefined>): Promise<T>
  get<T>(token: BoundInjectionToken<T, any>): Promise<T>
  get<T>(token: FactoryInjectionToken<T, any>): Promise<T>
 
  /**
   * Invalidates a service and its dependencies.
   */
  invalidate(service: unknown): Promise<void>
 
  /**
   * Checks if a service is registered in the container.
   */
  isRegistered(token: any): boolean
 
  /**
   * Adds an instance to the container.
   * Accepts class types, InjectionTokens, and BoundInjectionTokens.
   * Rejects InjectionTokens with required schemas (use BoundInjectionToken instead).
   *
   * @param token The class type, InjectionToken, or BoundInjectionToken to register the instance for
   * @param instance The instance to store
   */
  addInstance<T>(
    token: ClassType | InjectionToken<T, any> | BoundInjectionToken<T, any>,
    instance: T,
  ): void
 
  /**
   * Disposes the container and cleans up all resources.
   */
  dispose(): Promise<void>
 
  /**
   * Waits for all pending operations to complete.
   */
  ready(): Promise<void>
 
  /**
   * @internal
   * Attempts to get an instance synchronously if it already exists.
   * Returns null if the instance doesn't exist or is not ready.
   * Used internally by the inject system for synchronous property initialization.
   */
  tryGetSync<T>(token: any, args?: any): T | null
}