FoalTS / foal

Full-featured Node.js framework, with no complexity. 🚀 Simple and easy to use, TypeScript-based and well-documented.
https://foalts.org/
MIT License
1.88k stars 137 forks source link

ApiDefineSchema hook executed before service boot method #948

Closed kingdun3284 closed 3 years ago

kingdun3284 commented 3 years ago

Version of FoalTS: 2.4.0

Try to access service inside ApiDefineSchema Decorator and found that the service haven't been initialized even with

serviceManager.set(Service, service, { boot: true }) 

in index.ts, thus, need to manually call boot function before createApp.

LoicPoullain commented 3 years ago

Yes, the boot and init methods are called after the controllers and routes are resolved. This is during this step that the OpenAPI document is created, thus the init methods have not been called yet. This is something that won't change in the future because it would require a lot of work to change the internal architecture and probably very complex code to make it work.

The only thing that I can suggest in your case is, as you mentionned, to boot the service before calling createApp.

I would recommend to use services.boot(Service) instead of service.boot to avoid the method to be executed a second time by createApp.

const services = new ServiceManager();
serviceManager.set(Service, service);
await serviceManager.boot(Service);

const app = await createApp(AppController, { serviceManager: services });