inversify-typesafe-spring-like
    Preparing search index...

    inversify-typesafe-spring-like

    inversify-typesafe-spring-like

    Add-On Library for inversify-typesafe to make it more like Spring.
    See Demo

    CI Test Coverage npm version Bundle Size MIT license

    import { ApplicationContext, BeanConfig, returnAutowired } from "inversify-typesafe-spring-like";

    interface Article {
    id: number;
    title: string;
    content: string;
    }

    interface ArticleOutgoingPort {
    getById(id: number): Promise<Article>
    }

    class ArticleRepository implements ArticleOutgoingPort {
    getById(id: number): Promise<Article> {
    return Promise.resolve({
    id: id,
    title: `title #${id}`,
    content: `content #${id}`,
    })
    }
    }

    interface GetArticleUseCase {
    execute(id: number): Promise<Article>
    }

    const { Autowired } = returnAutowired<Beans>();

    class ArticleQueryService implements GetArticleUseCase {
    constructor(
    @Autowired("ArticleOutgoingPort") // compile error if a parameter of @Autowired is not a key of Beans.
    private readonly articleOutgoingPort: ArticleOutgoingPort,
    ) { }
    execute(id: number): Promise<Article> {
    return this.articleOutgoingPort.getById(id);
    }
    }

    type Beans = {
    GetArticleUseCase: GetArticleUseCase; // interface (class is also possible)
    ArticleOutgoingPort: ArticleOutgoingPort; // interface (class is also possible)
    }

    const beanConfig: BeanConfig<Beans> = {
    // compile error if ArticleQueryService is not compatible with GetArticleUseCase.
    GetArticleUseCase: (bind) => bind().to(ArticleQueryService),
    // compile error if ArticleRepository is not compatible with ArticleOutgoingPort.
    ArticleOutgoingPort: (bind) => bind().to(ArticleRepository),
    }

    const applicationContext = ApplicationContext(beanConfig);

    const getArticleUseCase = applicationContext.get("GetArticleUseCase")

    getArticleUseCase.execute(1).then(console.log)

    This library extends inversify-typesafe to provide a development experience similar to Spring Framework.

    It sets the default container scope to Singleton and exports standard inversify-typesafe utilities with Spring-like naming conventions, making it easier for developers familiar with Spring to adopt Inversify in TypeScript projects.

    Via npm

    npm install inversify-typesafe-spring-like
    

    Via yarn

    yarn add inversify-typesafe-spring-like
    

    Via pnpm

    pnpm add inversify-typesafe-spring-like
    

    Try it out on StackBlitz.

    https://inversify-typesafe-spring-like.myeongjae.kim/modules.html

    The API is designed to mirror Spring's terminology:

    1. createTypesafeContext() $\rightarrow$ ApplicationContext()
      • Note: defaultScope is set to Singleton by default.
    2. returnTypesafeInject() $\rightarrow$ returnAutowired()
    3. TypesafeServiceConfig<T> $\rightarrow$ BeanConfig<T>

    For complete usage documentation and advanced features, please refer to the inversify-typesafe documentation.

    MIT © Myeongjae Kim