feat(core): add InversifyJS DI container with auth and content modules
This commit is contained in:
32
packages/core/src/di/container.ts
Normal file
32
packages/core/src/di/container.ts
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
import "reflect-metadata";
|
||||||
|
import { Container } from "inversify";
|
||||||
|
|
||||||
|
import { AuthModule } from "./modules/auth.module.js";
|
||||||
|
import { ContentModule } from "./modules/content.module.js";
|
||||||
|
import { DI_RETURN_TYPES, DI_SYMBOLS } from "./types.js";
|
||||||
|
|
||||||
|
const ApplicationContainer = new Container({
|
||||||
|
defaultScope: "Singleton",
|
||||||
|
});
|
||||||
|
|
||||||
|
export const initializeContainer = () => {
|
||||||
|
ApplicationContainer.load(AuthModule);
|
||||||
|
ApplicationContainer.load(ContentModule);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const destroyContainer = () => {
|
||||||
|
ApplicationContainer.unload(AuthModule);
|
||||||
|
ApplicationContainer.unload(ContentModule);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (process.env.NODE_ENV !== "test") {
|
||||||
|
initializeContainer();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getInjection<K extends keyof typeof DI_SYMBOLS>(
|
||||||
|
symbol: K
|
||||||
|
): DI_RETURN_TYPES[K] {
|
||||||
|
return ApplicationContainer.get(DI_SYMBOLS[symbol]);
|
||||||
|
}
|
||||||
|
|
||||||
|
export { ApplicationContainer };
|
||||||
16
packages/core/src/di/modules/auth.module.ts
Normal file
16
packages/core/src/di/modules/auth.module.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import { ContainerModule, interfaces } from "inversify";
|
||||||
|
|
||||||
|
import type { IUsersRepository } from "@/application/repositories/users.repository.interface.js";
|
||||||
|
import type { IAuthenticationService } from "@/application/services/auth.service.interface.js";
|
||||||
|
import { MockUsersRepository } from "@/infrastructure/repositories/mock-users.repository.js";
|
||||||
|
import { MockAuthenticationService } from "@/infrastructure/services/mock-auth.service.js";
|
||||||
|
import { DI_SYMBOLS } from "../types.js";
|
||||||
|
|
||||||
|
const initializeModule = (bind: interfaces.Bind) => {
|
||||||
|
bind<IUsersRepository>(DI_SYMBOLS.IUsersRepository).to(MockUsersRepository);
|
||||||
|
bind<IAuthenticationService>(DI_SYMBOLS.IAuthenticationService).to(
|
||||||
|
MockAuthenticationService
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const AuthModule = new ContainerModule(initializeModule);
|
||||||
13
packages/core/src/di/modules/content.module.ts
Normal file
13
packages/core/src/di/modules/content.module.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import { ContainerModule, interfaces } from "inversify";
|
||||||
|
|
||||||
|
import type { IArticlesRepository } from "@/application/repositories/articles.repository.interface.js";
|
||||||
|
import { MockArticlesRepository } from "@/infrastructure/repositories/mock-articles.repository.js";
|
||||||
|
import { DI_SYMBOLS } from "../types.js";
|
||||||
|
|
||||||
|
const initializeModule = (bind: interfaces.Bind) => {
|
||||||
|
bind<IArticlesRepository>(DI_SYMBOLS.IArticlesRepository).to(
|
||||||
|
MockArticlesRepository
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const ContentModule = new ContainerModule(initializeModule);
|
||||||
18
packages/core/src/di/types.ts
Normal file
18
packages/core/src/di/types.ts
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import type { IAuthenticationService } from "@/application/services/auth.service.interface.js";
|
||||||
|
import type { ITelemetryService } from "@/application/services/telemetry.service.interface.js";
|
||||||
|
import type { IUsersRepository } from "@/application/repositories/users.repository.interface.js";
|
||||||
|
import type { IArticlesRepository } from "@/application/repositories/articles.repository.interface.js";
|
||||||
|
|
||||||
|
export const DI_SYMBOLS = {
|
||||||
|
IAuthenticationService: Symbol.for("IAuthenticationService"),
|
||||||
|
ITelemetryService: Symbol.for("ITelemetryService"),
|
||||||
|
IUsersRepository: Symbol.for("IUsersRepository"),
|
||||||
|
IArticlesRepository: Symbol.for("IArticlesRepository"),
|
||||||
|
};
|
||||||
|
|
||||||
|
export interface DI_RETURN_TYPES {
|
||||||
|
IAuthenticationService: IAuthenticationService;
|
||||||
|
ITelemetryService: ITelemetryService;
|
||||||
|
IUsersRepository: IUsersRepository;
|
||||||
|
IArticlesRepository: IArticlesRepository;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user