class-injector
TypeScript icon, indicating that this package has built-in type declarations

1.6.0 • Public • Published

💉 class-injector

Crazy simple dependency injection for TypeScript.

Why?

Dependency injection should be simple - really simple. Most dependency injection libraries for typescript require some combination of excessive boilerplate, manual binding of dependencies and unnecessary type annotations. class-injector aims for maximum simplicity with no compromises. This means no boilerplate, no manual binding of dependencies and no unnecessary type annotations. You don't even need to decorate classes to automatically inject them.

Example

import { Context, Inject } from "class-injector";

class UserRepository {
  constructor(
    private dbUri: string
  ) {}

  public async getUser(id: string) {
    // --snip--
  }
}

class UserService {
  @Inject() userRepository!: UserRepository;

  public async getUser(id: string) {
    return this.userRepository.getUser(id);
  }
}

class UserController {
  @Inject() userService!: UserService;

  public async getUser(req: any) {
    const id = req.query.id;
    const user = await this.userService.getUser(id);
    return { user };
  }
}

@Context({
  provide: [
    new UserRepository('<uri>'),
  ],
})
class Application {
  @Inject() userController!: UserController;

  // --snip--
}

const app = new Application();

Requirements

It is recommended to set the following options in your tsconfig:

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
  }
}

Without these, class-injector will not work.

Testing

When testing, you may want to switch out a dependency for a mock value.

import { createContext, Inject } from "class-injector";

describe('README Testing Example #1', () => {
  it('should demonstrate mock dependencies', () => {
    const mockDependency: Dependency = {
      fn: jest.fn(),
    };

    class Dependency {
      public fn() {
        fail('Should not be called');
      }
    }

    class Application {
      @Inject() dependency!: Dependency;
    }

    const context = createContext({
      provide: [
        [Dependency, mockDependency],
        new Application(),
      ],
    });

    const app = context.get<Application>(Application)!;

    app.dependency.fn();

    expect(mockDependency.fn).toHaveBeenCalled();
  });
});

Package Sidebar

Install

npm i class-injector

Weekly Downloads

1

Version

1.6.0

License

ISC

Unpacked Size

14.7 kB

Total Files

5

Last publish

Collaborators

  • bcheidemann