You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

flex-injector

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

flex-injector

A simple dependency injection library for TypeScript. Uses class decorators to inject dependencies, detects circular dependencies, and manages dependencies.

1.0.1
latest
Source
npmnpm
Version published
Weekly downloads
0
-100%
Maintainers
1
Weekly downloads
 
Created
Source

Flex-Injector

npm version Downloads

English | 한국어

Flex-Injector is a library that makes dependency injection easy. This library utilizes TypeScript's decorator feature to manage dependencies in a simple and clear way.

Using createInjector, you can create multiple injectors and inject dependencies through different containers. This is particularly useful in a monorepo environment. Each package or module can use an independent injector, making dependency management more efficient and clear.

Installation

  • Install module:
npm install flex-injector
  • Install peer dependencies:
npm install reflect-metadata
  • tsconfig.json compilerOptions :
{
  "emitDecoratorMetadata": true,
  "experimentalDecorators": true
}

Example of usage

./service
├── todo.service.ts
├── user.service.ts
└── injector.ts
//  ./service/injector.ts

import { createInjector } from 'flex-injector';

const { InjectAble, inject } = createInjector();

export { InjectAble, inject };

//  ./service/todo.service.ts

import { InjectAble } from './injector';

@InjectAble
export class TodoService {

  async getTodo(userId:string) {
    return ...;
  }
}

//  ./service/user.service.ts

import { InjectAble } from './injector';

@InjectAble
export class UserService {

  constructor(private todoService: TodoService) {}


  async getTodo(userId:string) {
    return this.todoService.getTodo(userId);
  }

  async find(userId:string) {
    return ...;
  }
}
// express server example

const userService = inject(UserService);

app.get('/todo', async (req, res) => {
  const todoList = await userService.getTodo(req.session.userId);
  res.json(todoList);
});

❌ Bad Case

//  Beware of circular reference errors. Below is a bad example where circular references occur.

const { inject, InjectAble } = createInjector();

@InjectAble
class A {
  constructor(private b: B) {}
}

@InjectAble
class B {
  constructor(private a: A) {}
}

const a = inject(A); // Throw Circular dependency detected

More examples

Keywords

typescript

FAQs

Package last updated on 03 Apr 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts