Hi, im having trouble trying to get the dependency injection to work if my files are in different directories
Im using the following structure for my project
usecases/
infrastructure/
entities/
web/
where web
is a next js app
infrastructure is where I defined the following classes
import { Service } from '@freshgum/typedi';
import IAuthService from "../../../usecases/common/interfaces/authService";
import type IGateway from "../interfaces/gateway";
import ApiGateway from '../gateways/gateway';
@Service({ scope: "transient" }, [ApiGateway])
export default class AuthService implements IAuthService {
private readonly gateway : IGateway;
/**
*
*/
constructor(gateway : IGateway) {
this.gateway = gateway;
}
}
import IGateway from "../interfaces/gateway";
import { Service } from "@freshgum/typedi";
@Service({ scope: "singleton" }, [])
export default class ApiGateway implements IGateway {
...
}
EDIT: I try to use this command inside a page of my next's project
if I try to use Container.get(AuthService);
I get the following error
ServiceNotFoundError: Service with "MaybeConstructable<AuthService>" identifier was not found in the container. Register it before usage via "Container.set" or the "@Service()" decorator.
its the same error if I try to register it manually
let identifier : ServiceIdentifier = AuthService;
if (!Container.has(identifier)) {
identifier = Container.set(AuthService);
}
const service = Container.get(AuthService);
these are my dependencies in package.json file in /web
"dependencies": {
"18": "^0.0.0",
"@freshgum/typedi": "^0.6.0",
"@headlessui/react": "^1.7.17",
"@heroicons/react": "^2.0.18",
"@tremor/react": "^3.6.6",
"@types/react": "18.2.20",
"@types/react-dom": "18.2.7",
"@types/react-router-dom": "^5.3.3",
"axios": "^1.5.1",
"next": "13.5.6",
"next-auth": "^4.23.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-hook-form": "^7.48.2",
"react-router-dom": "^6.17.0",
"reflect-metadata": "^0.1.13",
"sharp": "^0.32.6",
"zod": "^3.22.4"
},
"devDependencies": {
"@types/node": "^20",
"autoprefixer": "^10",
"eslint": "^8",
"eslint-config-next": "13.5.4",
"postcss": "^8",
"tailwindcss": "^3",
"ts-loader": "^9.5.0",
"typescript": "^5"
}
and this is the next.config.js
file
/** @type {import('next').NextConfig} */
const nextConfig = {
images: {
domains: ['avatars.githubusercontent.com', 'avatar.vercel.sh']
},
experimental: {
serverActions: true,
externalDir: true,
},
output: 'standalone',
}
module.exports = nextConfig
it works fine if dependencies are inside a directory, as I managed to write a test file in the infrastructure folder to try injecting them (although it only works with classes defined inside the infrastructure folder)
any advise on how can I make it work using multiple directories? thanks.