# mganchas/ts-inject architecture

> Architecture diagram of the GitHub repository mganchas/ts-inject, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mganchas/ts-inject
- Repository: https://github.com/mganchas/ts-inject

## Overview

##### Project Type and Purpose
- Type: A lightweight, type-safe Dependency Injection (DI) library for TypeScript.
- Purpose: Provide a container, registration API, and injection mechanisms (class-based and function-based) to manage dependencies in TS apps.

##### File Structure
Top-level:
- src/
– container.ts: Core DI container (register, resolve)
– autowire.ts: Class-based injection helpers
– autowireFn.ts: Function-based injection helpers
– index.ts: Public exports
– types.ts: Core interfaces/types
- __tests__/…: Unit tests for container behavior
- examples/…: Sample consumer code showing how to use ts-inject with classes, functions, configs
- config files: tsconfig.json, eslint, jest.config.js, etc.

##### Main Components
A. Core Library
1. DI Container (container.ts)
– Responsibilities: register providers (classes/factories/values), resolve tokens, lifecycle (singleton/transient)
2. Class Injector (autowire.ts)
– Uses metadata to inject constructor parameters or properties
3. Function Injector (autowireFn.ts)
– Wraps factory functions with argument injection
4. Public API (index.ts)
– Exposes container creation, decorators/hooks
5. Type Definitions (types.ts)
– Defines interfaces: Token, Provider, Scope, etc.
B. Consumers (Examples)
– Class example: UserService → autowire decorators
– Function example: logMessage, sendEmail → autowireFn usage
– Config example: keys.ts
C. Tests
– container.test.ts ensures register/resolve works

##### Relationships and Data Flow
- Consumer Code → calls Container.register() (binding tokens to providers)
- Consumer Code → calls Container.resolve() or uses @Inject/@Injectable (autowire)
- DI Container → looks up Provider map → instantiates or returns existing instance
- autowire/autowireFn → act as glue between consumer classes/functions and the container
- types.ts → underpins all compile-time safety, shown as a “type system” layer

##### Architectural Patterns and Principles
- Inversion of Control (IoC) via a central container
- Factory pattern: providers can be class constructors or factory functions
- Decorator/metadata: class-based injection hooks into TS metadata
- Functional composition: autowireFn wraps pure functions

## Diagram (Mermaid)

```mermaid
graph TD
    %% Core Library Modules
    PublicAPI["Public API\n(src/index.ts)"]:::core
    ClassInjector["Class Injector\n(src/autowire.ts)"]:::core
    FunctionInjector["Function Injector\n(src/autowireFn.ts)"]:::core
    DIContainer["DI Container\n(src/container.ts)"]:::core
    TypesDef["Type Definitions\n(src/types.ts)"]:::types

    %% Examples / Consumers
    subgraph "Examples" 
        direction TB
        UserService["UserService\n(examples/classes/UserService.ts)"]:::consumer
        ClassClient["Class Client\n(examples/classes/client.ts)"]:::consumer
        LogMessage["logMessage()\n(examples/functions/logMessage.ts)"]:::consumer
        SendEmail["sendEmail()\n(examples/functions/sendEmail.ts)"]:::consumer
        FunctionClient["Function Client\n(examples/functions/client.ts)"]:::consumer
        ConfigKeys["Config Keys\n(examples/config/keys.ts)"]:::consumer
        AbstractionsTypes["Abstractions Types\n(examples/abstractions/types.ts)"]:::consumer
    end

    %% Tests
    Tests["Container Tests\n(__tests__/container.test.ts)"]:::test

    %% Relationships
    PublicAPI -->|"exports createContainer(), Inject(), Injectable()"| DIContainer
    PublicAPI -->|"exports metadata hooks"| ClassInjector
    PublicAPI -->|"exports metadata hooks"| FunctionInjector

    ClassInjector -.->|"uses resolve(), metadata lookup"| DIContainer
    FunctionInjector -.->|"wraps factories, uses resolve()"| DIContainer

    UserService -->|"@Injectable() & @Inject()"| ClassInjector
    ClassClient -->|"autowire class instantiation"| ClassInjector

    LogMessage -->|"autowireFn() wrap"| FunctionInjector
    SendEmail -->|"autowireFn() wrap"| FunctionInjector
    FunctionClient -->|"autowireFn() usage"| FunctionInjector

    ConfigKeys -->|"register(token, value)"| DIContainer
    AbstractionsTypes -->|"type tokens definitions"| DIContainer

    Tests -->|"register/resolve tests"| DIContainer

    %% Type dependencies
    TypesDef -.-> PublicAPI
    TypesDef -.-> ClassInjector
    TypesDef -.-> FunctionInjector
    TypesDef -.-> DIContainer

    %% Click Events
    click DIContainer "https://github.com/mganchas/ts-inject/blob/main/src/container.ts"
    click ClassInjector "https://github.com/mganchas/ts-inject/blob/main/src/autowire.ts"
    click FunctionInjector "https://github.com/mganchas/ts-inject/blob/main/src/autowireFn.ts"
    click PublicAPI "https://github.com/mganchas/ts-inject/blob/main/src/index.ts"
    click TypesDef "https://github.com/mganchas/ts-inject/blob/main/src/types.ts"
    click UserService "https://github.com/mganchas/ts-inject/blob/main/examples/classes/UserService.ts"
    click ClassClient "https://github.com/mganchas/ts-inject/blob/main/examples/classes/client.ts"
    click LogMessage "https://github.com/mganchas/ts-inject/blob/main/examples/functions/logMessage.ts"
    click SendEmail "https://github.com/mganchas/ts-inject/blob/main/examples/functions/sendEmail.ts"
    click FunctionClient "https://github.com/mganchas/ts-inject/blob/main/examples/functions/client.ts"
    click ConfigKeys "https://github.com/mganchas/ts-inject/blob/main/examples/config/keys.ts"
    click AbstractionsTypes "https://github.com/mganchas/ts-inject/blob/main/examples/abstractions/types.ts"
    click Tests "https://github.com/mganchas/ts-inject/blob/main/__tests__/container.test.ts"

    %% Styles
    classDef core fill:#D0E7FF,stroke:#0047AB,stroke-width:2px
    classDef consumer fill:#E2F7E1,stroke:#2E7D32,stroke-width:2px,stroke-dasharray: 5 3
    classDef test fill:#FFE4C4,stroke:#FF8C00,stroke-width:2px,stroke-dasharray: 5 3
    classDef types fill:#F0F0F0,stroke:#666666,stroke-width:2px,stroke-dasharray: 2 2
```
