-
-
Notifications
You must be signed in to change notification settings - Fork 287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] Injection doesn't work anymore as before 8.4.0 ($beforeInit not called, no Injection via symbol / token) #2953
Comments
Hello @KBroichhausen $beforeInit in v8 is called before injector.load(), his purpose is to init something because any services. But because services aren't available, this hook cannot be listen using injectable class. Alternative: Use $onInit, $afterInit hooks instead. Or use functional API to handler
import {$on} from "@tsed/hooks";
import {configuration} from "@tsed/di";
$on("$beforeInit", () => {
const config = configuration()
const data = fs.readFileSync(config.get('path.to.file'))
config.set("resolved_data", data);
}); in import "./config/file.resolver.ts";
@Configuration({
path: {
to: {
file: process.env.CONFIG_FILE
}
}
})
export class Server {
} See you |
It's optional except for the $beforeInit. my apologize ;) |
Hi @Romakita, |
I read the DI documentation again with fresh eyes and figured it is now called |
Yes I missed that. Injectable decorator extra props that explain why you haven't error. I'll make a fix to add depreciation of provide prop and accept the provide prop in v8, to be removed in v9 |
Describe the bug
I'm currently using
"^7.84.1"
for all tsed packages and try to migrate to the newest verison8.4.0
.Basically, I create abstract classes like
IAService
, create a Token in the same file viaexport const IAServiceToken: unique symbol = Symbol("IAService");
and implement that abstract class inAService
class (usually in a different file then the abstract class definition). I use@Injectable({ provide: IAServiceToken })
decorator at theAService
and in other classes I inject via@Inject(IAServiceToken) private readonly aService: IAService;
which was working fine in before. Now, it just returns undefined.Also my
$beforeInit
hook is not called anymore.To Reproduce
The main branch is created with
but I created a repro in the following branch: https://github.com/KBroichhausen/tsed840/tree/injection
Expected behavior
I expect the code to work as before. If I understand correctly according to https://tsed.dev/introduction/migrate-from-v7.html there is a new API to use the hooks in a different way but that is optional, no?
Even it changed it should still be called https://tsed.dev/hooks-v8.png
Code snippets
No response
Repository URL example
https://github.com/KBroichhausen/tsed840/tree/injection
OS
Linux / WSL
Node version
v21.7.1
Library version
8.4.0
Additional context
Regarding
$beforeInit
: I just need two steps. In$beforeInit
I read some data from the filesystem in a service, in a different service I use$onInit
and data from the first service to combine it with some other data to eventually have the data ready for the controller requests and responses. So, I must ensure that the first service has read the data from the file system before the other service access it in$onInit
The text was updated successfully, but these errors were encountered: